Added custom error type for ID cannot be 0
This commit is contained in:
parent
d309d5b3b6
commit
2e2877156b
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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")
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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."})
|
||||
}
|
||||
|
||||
|
|
|
@ -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"})
|
||||
}
|
||||
|
||||
|
|
|
@ -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."})
|
||||
}
|
||||
|
||||
|
|
|
@ -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"})
|
||||
}
|
||||
|
||||
|
|
|
@ -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."})
|
||||
}
|
||||
|
||||
|
|
|
@ -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."})
|
||||
}
|
||||
|
||||
|
|
|
@ -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."})
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue