diff --git a/models/author_test.go b/models/author_test.go index b93edc6..4948654 100644 --- a/models/author_test.go +++ b/models/author_test.go @@ -45,6 +45,7 @@ func TestAddOrUpdateAuthor(t *testing.T) { // Pass an empty author to see if it fails _, err = AddOrUpdateAuthor(Author{}) assert.Error(t, err) + assert.True(t, IsErrAuthorCannotBeEmpty(err)) // Update the author testauthor.ID = author1.ID @@ -62,4 +63,9 @@ func TestAddOrUpdateAuthor(t *testing.T) { _, exists, err = GetAuthorByID(author1.ID) assert.NoError(t, err) assert.False(t, exists) + + // Try deleting an author with ID = 0 + err = DeleteAuthorByID(0) + assert.Error(t, err) + assert.True(t, IsErrIDCannotBeZero(err)) } diff --git a/models/authors_add_update.go b/models/authors_add_update.go index 2ccd727..3473df7 100644 --- a/models/authors_add_update.go +++ b/models/authors_add_update.go @@ -1,7 +1,5 @@ package models -import "fmt" - // AddOrUpdateAuthor adds a new author based on an author struct func AddOrUpdateAuthor(author Author) (newAuthor Author, err error) { @@ -9,7 +7,7 @@ func AddOrUpdateAuthor(author Author) (newAuthor Author, err error) { if author.ID == 0 { // Check if the author is empty, only insert it if not if author.Forename == "" && author.Lastname == "" { - return Author{}, fmt.Errorf("Author cannot be empty") + return Author{}, ErrAuthorCannotBeEmpty{} } _, err = x.Insert(&author) diff --git a/models/authors_delete.go b/models/authors_delete.go index e11edd3..a63ad2c 100644 --- a/models/authors_delete.go +++ b/models/authors_delete.go @@ -1,12 +1,10 @@ package models -import "fmt" - // DeleteAuthorByID deletes an author by its ID func DeleteAuthorByID(id int64) error { // Check if the id is 0 if id == 0 { - return fmt.Errorf("ID cannot be 0") + return ErrIDCannotBeZero{} } // Delete the author diff --git a/models/book_test.go b/models/book_test.go index ce696b7..70b8ec2 100644 --- a/models/book_test.go +++ b/models/book_test.go @@ -126,4 +126,9 @@ func TestAddOrUpdateBook(t *testing.T) { _, exists, err = GetBookByID(book1.ID) assert.NoError(t, err) assert.False(t, exists) + + // Try deleting one with ID = 0 + err = DeleteBookByID(0) + assert.Error(t, err) + assert.True(t, IsErrIDCannotBeZero(err)) } diff --git a/models/books_delete.go b/models/books_delete.go index 3a74e8c..63a9d87 100644 --- a/models/books_delete.go +++ b/models/books_delete.go @@ -1,12 +1,10 @@ package models -import "fmt" - // DeleteBookByID deletes a book by its ID func DeleteBookByID(id int64) error { // Check if the id is 0 if id == 0 { - return fmt.Errorf("ID cannot be 0") + return ErrIDCannotBeZero{} } // Delete the book diff --git a/models/error.go b/models/error.go index cd4e80c..70674f4 100644 --- a/models/error.go +++ b/models/error.go @@ -51,3 +51,31 @@ func IsErrUserDoesNotExist(err error) bool { func (err ErrUserDoesNotExist) Error() string { return fmt.Sprintf("this user does not exist [user id: %d]", err.UserID) } + +// ErrIDCannotBeZero represents a "UsernameAlreadyExists" kind of error. +type ErrIDCannotBeZero struct {} + +// IsErrIDCannotBeZero checks if an error is a ErrUsernameExists. +func IsErrIDCannotBeZero(err error) bool { + _, ok := err.(ErrIDCannotBeZero) + return ok +} + +func (err ErrIDCannotBeZero) Error() string { + return fmt.Sprintf("ID cannot be 0") +} + +// ErrAuthorCannotBeEmpty represents a "AuthorCannotBeEmpty" kind of error. +type ErrAuthorCannotBeEmpty struct {} + +// IsErrIDCannotBeZero checks if an error is a ErrUsernameExists. +func IsErrAuthorCannotBeEmpty(err error) bool { + _, ok := err.(ErrAuthorCannotBeEmpty) + return ok +} + +func (err ErrAuthorCannotBeEmpty) Error() string { + return fmt.Sprintf("author cannot be empty") +} + + diff --git a/models/item_test.go b/models/item_test.go index 20bdcb7..1a1f801 100644 --- a/models/item_test.go +++ b/models/item_test.go @@ -85,4 +85,9 @@ func TestAddOrUpdateItem(t *testing.T) { _, exists, err = GetItemByID(item1.ID) assert.NoError(t, err) assert.False(t, exists) + + // Try deleting one with ID = 0 + err = DeleteItemByID(0) + assert.Error(t, err) + assert.True(t, IsErrIDCannotBeZero(err)) } diff --git a/models/items_delete.go b/models/items_delete.go index 28c80d1..e8c4ac5 100644 --- a/models/items_delete.go +++ b/models/items_delete.go @@ -1,12 +1,10 @@ package models -import "fmt" - // DeleteItemByID deletes a item by its ID func DeleteItemByID(id int64) error { // Check if the id is 0 if id == 0 { - return fmt.Errorf("ID cannot be 0") + return ErrIDCannotBeZero{} } // Delete the item diff --git a/models/publisher_test.go b/models/publisher_test.go index eb65c1b..794fd78 100644 --- a/models/publisher_test.go +++ b/models/publisher_test.go @@ -60,4 +60,9 @@ func TestAddOrUpdatePublisher(t *testing.T) { _, exists, err = GetPublisherByID(publisher1.ID) assert.NoError(t, err) assert.False(t, exists) + + // Try deleting one with ID = 0 + err = DeletePublisherByID(0) + assert.Error(t, err) + assert.True(t, IsErrIDCannotBeZero(err)) } diff --git a/models/publishers_delete.go b/models/publishers_delete.go index b317f74..353e37b 100644 --- a/models/publishers_delete.go +++ b/models/publishers_delete.go @@ -1,12 +1,10 @@ package models -import "fmt" - // DeletePublisherByID deletes a publisher by its ID func DeletePublisherByID(id int64) error { // Check if the id is 0 if id == 0 { - return fmt.Errorf("ID cannot be 0") + return ErrIDCannotBeZero{} } // Delete the publisher diff --git a/models/user_delete.go b/models/user_delete.go index 8eb357d..8ff6fa4 100644 --- a/models/user_delete.go +++ b/models/user_delete.go @@ -1,12 +1,10 @@ package models -import "fmt" - // DeleteUserByID deletes a user by its ID func DeleteUserByID(id int64) error { // Check if the id is 0 if id == 0 { - return fmt.Errorf("ID cannot be 0") + return ErrIDCannotBeZero{} } // Delete the user diff --git a/routes/api/v1/author_delete.go b/routes/api/v1/author_delete.go index d458a0e..ee88c43 100644 --- a/routes/api/v1/author_delete.go +++ b/routes/api/v1/author_delete.go @@ -34,6 +34,9 @@ func AuthorDelete(c echo.Context) error { err = models.DeleteAuthorByID(authorID) if err != nil { + if models.IsErrIDCannotBeZero(err) { + return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"}) + } return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete author."}) } diff --git a/routes/api/v1/authors_add_update.go b/routes/api/v1/authors_add_update.go index 238e5fe..4ddc2ee 100644 --- a/routes/api/v1/authors_add_update.go +++ b/routes/api/v1/authors_add_update.go @@ -62,6 +62,9 @@ func AuthorAddOrUpdate(c echo.Context) error { newAuthor, err := models.AddOrUpdateAuthor(*datAuthor) if err != nil { + if models.IsErrAuthorCannotBeEmpty(err) { + return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"}) + } return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) } diff --git a/routes/api/v1/book_delete.go b/routes/api/v1/book_delete.go index 2621cff..6901899 100644 --- a/routes/api/v1/book_delete.go +++ b/routes/api/v1/book_delete.go @@ -34,6 +34,9 @@ func BookDelete(c echo.Context) error { err = models.DeleteBookByID(bookID) if err != nil { + if models.IsErrIDCannotBeZero(err) { + return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"}) + } return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete book."}) } diff --git a/routes/api/v1/books_add_update.go b/routes/api/v1/books_add_update.go index 7d2f9a1..01a66cd 100644 --- a/routes/api/v1/books_add_update.go +++ b/routes/api/v1/books_add_update.go @@ -62,6 +62,9 @@ func BookAddOrUpdate(c echo.Context) error { newBook, err := models.AddOrUpdateBook(*datBook) if err != nil { + if models.IsErrAuthorCannotBeEmpty(err) { + return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"}) + } return c.JSON(http.StatusInternalServerError, models.Message{"Error"}) } diff --git a/routes/api/v1/items_delete.go b/routes/api/v1/items_delete.go index be9c4df..31948ba 100644 --- a/routes/api/v1/items_delete.go +++ b/routes/api/v1/items_delete.go @@ -34,6 +34,9 @@ func ItemDelete(c echo.Context) error { err = models.DeleteItemByID(itemID) if err != nil { + if models.IsErrIDCannotBeZero(err) { + return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"}) + } return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete item."}) } diff --git a/routes/api/v1/publishers_delete.go b/routes/api/v1/publishers_delete.go index d6bf241..2c26ff4 100644 --- a/routes/api/v1/publishers_delete.go +++ b/routes/api/v1/publishers_delete.go @@ -34,6 +34,9 @@ func PublisherDelete(c echo.Context) error { err = models.DeletePublisherByID(publisherID) if err != nil { + if models.IsErrIDCannotBeZero(err) { + return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"}) + } return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete publisher."}) } diff --git a/routes/api/v1/user_delete.go b/routes/api/v1/user_delete.go index 348b8ea..0abd496 100644 --- a/routes/api/v1/user_delete.go +++ b/routes/api/v1/user_delete.go @@ -39,6 +39,9 @@ func UserDelete(c echo.Context) error { err = models.DeleteUserByID(userID) if err != nil { + if models.IsErrIDCannotBeZero(err) { + return c.JSON(http.StatusInternalServerError, models.Message{"Id cannot be 0"}) + } return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete user."}) }