Adding persistence to your API
To add persistence to our API, we will not write all the code needed in the book, but you can check the whole code in the book repository under the chapter06 folder.
The first thing that we need to do is create the repository abstraction. We create a brand-new file named repository.go and put some code in it:
package main
import (
"database/sql"
sq "github.com/Masterminds/squirrel"
_ "github.com/mattn/go-sqlite3"
)
type Repository struct {
db *sql.DB
}
func NewRepository(database string) (*Repository, error) {
db, err := sql.Open("sqlite3", database)
if err != nil {
return nil, err
}
return &Repository{db}, nil
}
With this, we define the basic structure of our repository and how to create a repository instance. As you can see, we are encapsulating the database connection inside our repository and opening the database using the SQLite driver.
The next step is to set up the...