Skip to content

Commit

Permalink
feat: now deletion goes to the database
Browse files Browse the repository at this point in the history
  • Loading branch information
xpmatteo committed Feb 11, 2024
1 parent 021a71f commit 003db3a
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 9 deletions.
8 changes: 8 additions & 0 deletions db/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type TodoRepository interface {
Find(todo.ItemId) (*todo.Item, bool, error)
Save(item todo.Item) (todo.ItemId, error)
FindList() (*todo.List, error)
Destroy(id todo.ItemId) error
}

type todoRepository struct {
Expand Down Expand Up @@ -125,3 +126,10 @@ values (?, ?)`
}
return newId, nil
}

//goland:noinspection SqlNoDataSourceInspection
func (t todoRepository) Destroy(id todo.ItemId) error {
destroySql := `delete from todo_items where id = ?`
_, err := t.db.Exec(destroySql, id)
return err
}
19 changes: 19 additions & 0 deletions db/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ func Test_findAll(t *testing.T) {
assert.Equal(id1, all[1].Id)
}

func Test_destroy_ok(t *testing.T) {
assert := assert.New(t)
db := initTestDb()
repo := NewTodoRepository(db)
id0, err := repo.Save(todo.Item{Title: "first", IsDone: false})
require.NoError(t, err)
id1, err := repo.Save(todo.Item{Title: "second", IsDone: true})
require.NoError(t, err)

err = repo.Destroy(id1)
require.NoError(t, err)

list, err := repo.FindList()
require.NoError(t, err)

assert.Equal(1, len(list.Items))
assert.Equal("first", list.Items[id0].Title)
}

//goland:noinspection SqlNoDataSourceInspection
func initTestDb() *sql.DB {
db, err := sql.Open("sqlite", "test.db")
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func main() {
web.Metrics("destroy",
web.Logging(
web.POSTonly(
web.DestroyHandler(templ, model)))))
web.DestroyHandler(templ, repository, repository)))))

http.Handle("/metrics", promhttp.Handler())

Expand Down
25 changes: 22 additions & 3 deletions web/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package web
import (
"github.com/xpmatteo/todomvc-golang/todo"
"html/template"
"log"
"net/http"
)

Expand All @@ -17,6 +18,10 @@ type ListFinder interface {
FindList() (*todo.List, error)
}

type Destroyer interface {
Destroy(id todo.ItemId) error
}

func IndexHandler(templ *template.Template, repo ListFinder) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" && r.URL.Path != pathActive && r.URL.Path != pathCompleted {
Expand All @@ -25,7 +30,7 @@ func IndexHandler(templ *template.Template, repo ListFinder) http.Handler {
}
model, err := repo.FindList()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
internalServerError(w, err)
return
}
print(model.Items)
Expand Down Expand Up @@ -100,7 +105,7 @@ func EditHandler(templ *template.Template, model *todo.List) http.Handler {
})
}

func DestroyHandler(templ *template.Template, model *todo.List) http.Handler {
func DestroyHandler(templ *template.Template, finder ListFinder, destroyer Destroyer) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
Expand All @@ -112,13 +117,27 @@ func DestroyHandler(templ *template.Template, model *todo.List) http.Handler {
badRequest(w, err)
return
}
model.Destroy(id)

if err := destroyer.Destroy(id); err != nil {
internalServerError(w, err)
return
}

model, err := finder.FindList()
if err != nil {
internalServerError(w, err)
return
}
vm := viewModel(model, r)
render(w, r, templ, vm)
})
}

func internalServerError(w http.ResponseWriter, err error) {
log.Printf("Finder: %s", err.Error())
http.Error(w, "Internal server error", http.StatusInternalServerError)
}

func badRequest(w http.ResponseWriter, err error) {
http.Error(w, err.Error(), http.StatusBadRequest)
}
19 changes: 14 additions & 5 deletions web/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,25 @@ func Test_editHandler_textIsEmpty(t *testing.T) {
assert.Equal(0, len(model.Items))
}

type DestroyerMock struct {
ids []todo.ItemId
}

func (d *DestroyerMock) Destroy(id todo.ItemId) error {
d.ids = append(d.ids, id)
return nil
}

func Test_destroyHandler_ok(t *testing.T) {
assert := assert.New(t)
w, r := httptest.NewRecorder(), httptest.NewRequest(http.MethodPost, "/", strings.NewReader("todoItemId=0"))
w, r := httptest.NewRecorder(), httptest.NewRequest(http.MethodPost, "/", strings.NewReader("todoItemId=123"))
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
model := todo.NewList()
model.Add("foo")
testFinder := ListFinderStub{todo.NewList()}
destroyer := &DestroyerMock{}

DestroyHandler(templ, model).ServeHTTP(w, r)
DestroyHandler(templ, testFinder, destroyer).ServeHTTP(w, r)

assert.Equal(http.StatusOK, w.Code)
assert.Equal("<p>[]</p>", w.Body.String())
assert.Equal(0, len(model.Items))
assert.Contains(destroyer.ids, todo.MustNewItemId("123"))
}

0 comments on commit 003db3a

Please sign in to comment.