postgres and such
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/dryrun Pipeline was successful
ci/woodpecker/push/publish-tag Pipeline was successful
ci/woodpecker/push/publish-latest Pipeline was successful

This commit is contained in:
2024-06-17 23:37:43 -04:00
parent 088365c411
commit 6f7bcc9503
17 changed files with 423 additions and 36 deletions

View 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{},
}
}

View 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,
}
}