postgres and such
This commit is contained in:
61
repositories/inMemoryTodoRepository.go
Normal file
61
repositories/inMemoryTodoRepository.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gofrs/uuid/v5"
|
||||
"michaelthomson.dev/mthomson/go-todos-app/models"
|
||||
)
|
||||
|
||||
type InMemoryTodoRepository struct {
|
||||
Todos []models.Todo
|
||||
}
|
||||
|
||||
func (ts *InMemoryTodoRepository) List() (todo []models.Todo, err error) {
|
||||
return ts.Todos, nil
|
||||
}
|
||||
|
||||
func (ts *InMemoryTodoRepository) Get(id uuid.UUID) (todo models.Todo, err error) {
|
||||
index := -1
|
||||
for i, todo := range ts.Todos {
|
||||
if id == todo.Id {
|
||||
index = i
|
||||
}
|
||||
}
|
||||
|
||||
if index == -1 {
|
||||
return models.Todo{}, fmt.Errorf("Could not find todo by id %q", id)
|
||||
}
|
||||
|
||||
ts.Todos = append(ts.Todos[:index], ts.Todos[index + 1:]...)
|
||||
|
||||
return ts.Todos[index], nil
|
||||
}
|
||||
|
||||
func (ts *InMemoryTodoRepository) Add(todo models.Todo) (addedTodo models.Todo, err error) {
|
||||
ts.Todos = append(ts.Todos, todo)
|
||||
return todo, nil
|
||||
}
|
||||
|
||||
func (ts *InMemoryTodoRepository) Delete(id uuid.UUID) (err error) {
|
||||
index := -1
|
||||
for i, todo := range ts.Todos {
|
||||
if id == todo.Id {
|
||||
index = i
|
||||
}
|
||||
}
|
||||
|
||||
if index == -1 {
|
||||
return fmt.Errorf("Could not find todo by id %q", id)
|
||||
}
|
||||
|
||||
ts.Todos = append(ts.Todos[:index], ts.Todos[index + 1:]...)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewInMemoryTodoRepository() InMemoryTodoRepository {
|
||||
return InMemoryTodoRepository{
|
||||
Todos: []models.Todo{},
|
||||
}
|
||||
}
|
||||
86
repositories/postgresTodoRepository.go
Normal file
86
repositories/postgresTodoRepository.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"github.com/gofrs/uuid/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"michaelthomson.dev/mthomson/go-todos-app/models"
|
||||
)
|
||||
|
||||
type PostgresTodoRepository struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func (ts *PostgresTodoRepository) List() ([]models.Todo, error) {
|
||||
var todos []models.Todo
|
||||
rows, err := ts.db.Query(context.Background(), "SELECT id, name, done FROM todo")
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return todos, err
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var r models.Todo
|
||||
err := rows.Scan(&r.Id, &r.Name, &r.Done)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return todos, err
|
||||
}
|
||||
todos = append(todos, r)
|
||||
}
|
||||
|
||||
return todos, err
|
||||
}
|
||||
|
||||
func (ts *PostgresTodoRepository) Get(id uuid.UUID) (models.Todo, error) {
|
||||
var todo models.Todo
|
||||
err := ts.db.QueryRow(context.Background(), "SELECT id, name, done FROM todo WHERE id=$1", id).Scan(&todo.Id, &todo.Name, &todo.Done)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return todo, err
|
||||
}
|
||||
|
||||
return todo, err
|
||||
}
|
||||
|
||||
func (ts *PostgresTodoRepository) Update(id uuid.UUID, name string, done bool) error {
|
||||
_, err := ts.db.Exec(context.Background(), "UPDATE todo SET name=$1, done=$2 WHERE id=$3", name, done, id)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (ts *PostgresTodoRepository) Add(todo models.Todo) error {
|
||||
_, err := ts.db.Exec(context.Background(), "INSERT INTO todo(id, name, done) values($1, $2, $3)", todo.Id, todo.Name, todo.Done)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (ts *PostgresTodoRepository) Delete(id uuid.UUID) error {
|
||||
_, err := ts.db.Exec(context.Background(), "DELETE FROM todo where id=$1", id)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func NewPostgresTodoRepository(db *pgxpool.Pool) PostgresTodoRepository {
|
||||
return PostgresTodoRepository{
|
||||
db: db,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user