From 9d2c09e1762950fb319462aac4ec5a5af860da0d Mon Sep 17 00:00:00 2001 From: Dumindu Madunuwan Date: Fri, 15 Sep 2023 02:57:53 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8C=B1=20Refactor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/resource/book/handler.go | 30 ++++++++++++++++++---------- api/resource/book/model.go | 1 - api/resource/book/repository.go | 20 +++++++++++-------- api/resource/book/repository_test.go | 22 +++++++++++++------- 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/api/resource/book/handler.go b/api/resource/book/handler.go index 9032726..41d4fc8 100644 --- a/api/resource/book/handler.go +++ b/api/resource/book/handler.go @@ -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 } @@ -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) @@ -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") } @@ -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") } diff --git a/api/resource/book/model.go b/api/resource/book/model.go index 57a6710..3b340fc 100644 --- a/api/resource/book/model.go +++ b/api/resource/book/model.go @@ -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, diff --git a/api/resource/book/repository.go b/api/resource/book/repository.go index 7f7d3e2..b993a45 100644 --- a/api/resource/book/repository.go +++ b/api/resource/book/repository.go @@ -21,10 +21,6 @@ func (r *Repository) List() (Books, error) { return nil, err } - if len(books) == 0 { - return nil, nil - } - return books, nil } @@ -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 + } diff --git a/api/resource/book/repository_test.go b/api/resource/book/repository_test.go index ee11591..d6b712f 100644 --- a/api/resource/book/repository_test.go +++ b/api/resource/book/repository_test.go @@ -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) @@ -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) @@ -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) { @@ -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) }