Skip to content

Commit

Permalink
(#77) DELETE post requests:
Browse files Browse the repository at this point in the history
- Fixed behaviour when executing multiple 'DELETE' requests for posts.
  • Loading branch information
AlDu2407 committed May 30, 2024
1 parent 6a45d83 commit b8644c2
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
8 changes: 7 additions & 1 deletion admin-app/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 111 in admin-app/post.go

View check run for this annotation

Codecov / codecov/patch

admin-app/post.go#L111

Added line #L111 was not covered by tests
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
}

Check warning on line 122 in admin-app/post.go

View check run for this annotation

Codecov / codecov/patch

admin-app/post.go#L118-L122

Added lines #L118 - L122 were not covered by tests

c.JSON(http.StatusOK, PostIdResponse{
delete_post_binding.Id,
})
Expand Down
2 changes: 1 addition & 1 deletion common/urchin_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
16 changes: 11 additions & 5 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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

Check warning on line 133 in database/database.go

View check run for this annotation

Codecov / codecov/patch

database/database.go#L130-L133

Added lines #L130 - L133 were not covered by tests
}

return nil
rows_affected, err := res.RowsAffected()
if err != nil {
return 0, err
}

Check warning on line 139 in database/database.go

View check run for this annotation

Codecov / codecov/patch

database/database.go#L136-L139

Added lines #L136 - L139 were not covered by tests

return int(rows_affected), nil

Check warning on line 141 in database/database.go

View check run for this annotation

Codecov / codecov/patch

database/database.go#L141

Added line #L141 was not covered by tests
}

func MakeSqlConnection(user string, password string, address string, port int, database string) (SqlDatabase, error) {
Expand Down
4 changes: 2 additions & 2 deletions tests/mocks/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

0 comments on commit b8644c2

Please sign in to comment.