Skip to content

Commit

Permalink
🌱 Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
dumindu committed Sep 14, 2023
1 parent d07509b commit 9d2c09e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
30 changes: 19 additions & 11 deletions api/resource/book/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (a *API) List(w http.ResponseWriter, r *http.Request) {
return
}

if books == nil {
if len(books) == 0 {
fmt.Fprint(w, "[]")
return
}
Expand Down Expand Up @@ -98,7 +98,10 @@ func (a *API) Create(w http.ResponseWriter, r *http.Request) {
return
}

book, err := a.repository.Create(form.ToModel())
newBook := form.ToModel()
newBook.ID = uuid.New()

book, err := a.repository.Create(newBook)
if err != nil {
a.logger.Error().Err(err).Msg("")
e.ServerError(w, e.DataCreationFailure)
Expand Down Expand Up @@ -196,19 +199,19 @@ func (a *API) Update(w http.ResponseWriter, r *http.Request) {
return
}

bookModel := form.ToModel()
bookModel.ID = id

if err := a.repository.Update(bookModel); err != nil {
if err == gorm.ErrRecordNotFound {
w.WriteHeader(http.StatusNotFound)
return
}
book := form.ToModel()
book.ID = id

rows, err := a.repository.Update(book)
if err != nil {
a.logger.Error().Err(err).Msg("")
e.ServerError(w, e.DataUpdateFailure)
return
}
if rows == 0 {
w.WriteHeader(http.StatusNotFound)
return
}

a.logger.Info().Str("id", id.String()).Msg("book updated")
}
Expand All @@ -233,11 +236,16 @@ func (a *API) Delete(w http.ResponseWriter, r *http.Request) {
return
}

if err := a.repository.Delete(id); err != nil {
rows, err := a.repository.Delete(id)
if err != nil {
a.logger.Error().Err(err).Msg("")
e.ServerError(w, e.DataDeletionFailure)
return
}
if rows == 0 {
w.WriteHeader(http.StatusNotFound)
return
}

a.logger.Info().Str("id", id.String()).Msg("book deleted")
}
1 change: 0 additions & 1 deletion api/resource/book/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func (f *Form) ToModel() *Book {
pubDate, _ := time.Parse("2006-01-02", f.PublishedDate)

return &Book{
ID: uuid.New(),
Title: f.Title,
Author: f.Author,
PublishedDate: pubDate,
Expand Down
20 changes: 12 additions & 8 deletions api/resource/book/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ func (r *Repository) List() (Books, error) {
return nil, err
}

if len(books) == 0 {
return nil, nil
}

return books, nil
}

Expand All @@ -45,10 +41,18 @@ func (r *Repository) Read(id uuid.UUID) (*Book, error) {
return book, nil
}

func (r *Repository) Update(book *Book) error {
return r.db.Updates(book).Where("id = %s", book.ID).Error
func (r *Repository) Update(book *Book) (int64, error) {
result := r.db.Model(&Book{}).
Select("Title", "Author", "PublishedDate", "ImageURL", "Description", "UpdatedAt").
Where("id = ?", book.ID).
Updates(book)

return result.RowsAffected, result.Error
}

func (r *Repository) Delete(id uuid.UUID) error {
return r.db.Where("id = ?", id).Delete(&Book{}).Error
func (r *Repository) Delete(id uuid.UUID) (int64, error) {
result := r.db.Where("id = ?", id).Delete(&Book{})

return result.RowsAffected, result.Error

}
22 changes: 15 additions & 7 deletions api/resource/book/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func TestRepository_List(t *testing.T) {

repo := book.NewRepository(db)

rows := sqlmock.NewRows([]string{"id", "title", "author"}).
mockRows := sqlmock.NewRows([]string{"id", "title", "author"}).
AddRow(uuid.New(), "Book1", "Author1").
AddRow(uuid.New(), "Book2", "Author2")

mock.ExpectQuery("^SELECT (.+) FROM \"books\"").WillReturnRows(rows)
mock.ExpectQuery("^SELECT (.+) FROM \"books\"").WillReturnRows(mockRows)

books, err := repo.List()
testUtil.NoError(t, err)
Expand Down Expand Up @@ -60,12 +60,12 @@ func TestRepository_Read(t *testing.T) {
repo := book.NewRepository(db)

id := uuid.New()
rows := sqlmock.NewRows([]string{"id", "title", "author"}).
mockRows := sqlmock.NewRows([]string{"id", "title", "author"}).
AddRow(id, "Book1", "Author1")

mock.ExpectQuery("^SELECT (.+) FROM \"books\" WHERE (.+)").
WithArgs(id).
WillReturnRows(rows)
WillReturnRows(mockRows)

book, err := repo.Read(id)
testUtil.NoError(t, err)
Expand All @@ -81,15 +81,19 @@ func TestRepository_Update(t *testing.T) {
repo := book.NewRepository(db)

id := uuid.New()
_ = sqlmock.NewRows([]string{"id", "title", "author"}).
AddRow(id, "Book1", "Author1")

mock.ExpectBegin()
mock.ExpectExec("^UPDATE \"books\" SET").
WithArgs("Title", "Author", mockDB.AnyTime{}, id).
WithArgs("Title", "Author", mockDB.AnyTime{}, "", "", mockDB.AnyTime{}, id).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()

book := &book.Book{ID: id, Title: "Title", Author: "Author"}
err = repo.Update(book)
rows, err := repo.Update(book)
testUtil.NoError(t, err)
testUtil.Equal(t, 1, rows)
}

func TestRepository_Delete(t *testing.T) {
Expand All @@ -101,12 +105,16 @@ func TestRepository_Delete(t *testing.T) {
repo := book.NewRepository(db)

id := uuid.New()
_ = sqlmock.NewRows([]string{"id", "title", "author"}).
AddRow(id, "Book1", "Author1")

mock.ExpectBegin()
mock.ExpectExec("^UPDATE \"books\" SET \"deleted_at\"").
WithArgs(mockDB.AnyTime{}, id).
WillReturnResult(sqlmock.NewResult(1, 1))
mock.ExpectCommit()

err = repo.Delete(id)
rows, err := repo.Delete(id)
testUtil.NoError(t, err)
testUtil.Equal(t, 1, rows)
}

0 comments on commit 9d2c09e

Please sign in to comment.