Skip to content

Commit

Permalink
database: update exercise 2
Browse files Browse the repository at this point in the history
Add instructions for how to implement a database abstraction. Add
a template file for the db abstraction. Explain all the methods and
some best practices.

Signed-off-by: soypete <[email protected]>
  • Loading branch information
Soypete committed Oct 5, 2023
1 parent c124e8f commit 6a3c702
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
4 changes: 2 additions & 2 deletions database/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Recording to come

## Exercise 2

Build a client and interface around your database connection.
Build a client and interface around your database connection. You can use your existing main.go file and build new database package for your abstraction, or you can use the template files found in [database/ex-2-abstraction](database/ex-2-abstraction/main.go). Make sure to create an interface, a User struct, a database client, and the methods to create, update, and query the User data. If you have any questions please put them in the chat.

Questions:
Follow-up Questions:

* what are the differences between manually creating a database object vs generating one with sqlc?

Expand Down
49 changes: 49 additions & 0 deletions database/ex-2-abstraction/database/database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package database

import (
"database/sql"
"fmt"
)

// Connection struct holds the database connection
type Connection struct {
db *sql.DB
}

// User struct holds the user data
type User struct {
//TODO: add user fields
}

type Connector interface {
QueryUser() (*User, error)
UpdateUser() error
AddUser() error
}

// Connect to database and return a Connection struct.
// This return value needs to be a pointer because we are
// using it to implement the Connector interface.
func Connect() (*Connection, error) {
fileName := "database/ex-1-connection/sqlite.db"
db, err := sql.Open("sqlite3", fileName)
if err != nil {
return nil, fmt.Errorf("cannot open sqlite connection: %w", err)
}
if err = db.Ping(); err != nil {
return nil, fmt.Errorf("cannot ping database: %w", err)
}
return &Connection{db: db}, nil
}

func (c *Connection) QueryUser() (*User, error) {
// TODO: query database
}

func (c *Connection) UpdateUser() error {
// TODO: update database
}

func (c *Connection) AddUser() error {
// TODO: add user to database
}
19 changes: 19 additions & 0 deletions database/ex-2-abstraction/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"log"

"github.com/soypete/WebServices-in-3-weeks/database/ex-2-abstraction/database"
)

func main() {

// this will enforge the interface
var db database.Connector
db, err := database.Connect()
if err != nil {
log.Fatal("Error connecting to database")
}

// call your client methods here
}

0 comments on commit 6a3c702

Please sign in to comment.