diff --git a/admin-app/post.go b/admin-app/post.go index 67bb112..cbe63f2 100644 --- a/admin-app/post.go +++ b/admin-app/post.go @@ -108,13 +108,19 @@ func deletePostHandler(database database.Database) func(*gin.Context) { return } - err = database.DeletePost(delete_post_binding.Id) + rows_affected, err := database.DeletePost(delete_post_binding.Id) if err != nil { log.Error().Msgf("failed to delete post: %v", err) c.JSON(http.StatusBadRequest, common.ErrorRes("could not delete post", err)) return } + if rows_affected != 1 { + log.Error().Msgf("no post found with id `%d`", delete_post_binding.Id) + c.JSON(http.StatusNotFound, common.MsgErrorRes("no post found")) + return + } + c.JSON(http.StatusOK, PostIdResponse{ delete_post_binding.Id, }) diff --git a/common/urchin_responses.go b/common/urchin_responses.go index e0f6fe7..c85ddf0 100644 --- a/common/urchin_responses.go +++ b/common/urchin_responses.go @@ -25,5 +25,5 @@ func ErrorRes(msg string, err error) ErrorResponse { // Supported responses for urchin shared between app and admin-app. type ErrorResponse struct { Msg string `json:"msg"` - Err string `json:"error"` + Err string `json:"error,omitempty"` } diff --git a/database/database.go b/database/database.go index 64186e5..1eed06a 100644 --- a/database/database.go +++ b/database/database.go @@ -15,7 +15,7 @@ type Database interface { GetPost(post_id int) (common.Post, error) AddPost(title string, excerpt string, content string) (int, error) ChangePost(id int, title string, excerpt string, content string) error - DeletePost(id int) error + DeletePost(id int) (int, error) } type SqlDatabase struct { @@ -127,12 +127,18 @@ func (db SqlDatabase) ChangePost(id int, title string, excerpt string, content s // / This function changes a post based on the values // / provided. Note that empty strings will mean that // / the value will not be updated. -func (db SqlDatabase) DeletePost(id int) error { - if _, err := db.Connection.Exec("DELETE FROM posts WHERE id=?;", id); err != nil { - return err +func (db SqlDatabase) DeletePost(id int) (int, error) { + var res, err = db.Connection.Exec("DELETE FROM posts where id=?", id) + if err != nil { + return 0, err } - return nil + rows_affected, err := res.RowsAffected() + if err != nil { + return 0, err + } + + return int(rows_affected), nil } func MakeSqlConnection(user string, password string, address string, port int, database string) (SqlDatabase, error) { diff --git a/tests/mocks/mocks.go b/tests/mocks/mocks.go index 80d0ce2..c56022e 100644 --- a/tests/mocks/mocks.go +++ b/tests/mocks/mocks.go @@ -27,6 +27,6 @@ func (db DatabaseMock) ChangePost(id int, title string, excerpt string, content return nil } -func (db DatabaseMock) DeletePost(id int) error { - return fmt.Errorf("not implemented") +func (db DatabaseMock) DeletePost(id int) (int, error) { + return 0, fmt.Errorf("not implemented") }