Skip to content

Commit

Permalink
(#77) DELETE posts requests: (#89)
Browse files Browse the repository at this point in the history
- Fixed behaviour when executing multiple `DELETE` requests for posts.

---------

Co-authored-by: matheusgomes28 <[email protected]>
  • Loading branch information
AlDu2407 and matheusgomes28 authored Jun 1, 2024
1 parent e9a57d9 commit 9681a29
Show file tree
Hide file tree
Showing 6 changed files with 292 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
tmp/

tests/system_tests/helpers/migrations
tests/system_tests/admin_app/endpoint_tests/*.png
images/


Expand Down
33 changes: 32 additions & 1 deletion admin-app/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package admin_app

import (
"encoding/json"
"fmt"
"net/http"
"strings"

"github.com/gin-gonic/gin"
"github.com/matheusgomes28/urchin/common"
Expand Down Expand Up @@ -51,6 +53,13 @@ func postPostHandler(database database.Database) func(*gin.Context) {
return
}

err = checkRequiredData(add_post_request)
if err != nil {
log.Error().Msgf("failed to add post required data is missing: %v", err)
c.JSON(http.StatusBadRequest, common.ErrorRes("missing required data", err))
return
}

id, err := database.AddPost(
add_post_request.Title,
add_post_request.Excerpt,
Expand Down Expand Up @@ -108,15 +117,37 @@ 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 == 0 {
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,
})
}
}

func checkRequiredData(addPostRequest AddPostRequest) error {
if strings.TrimSpace(addPostRequest.Title) == "" {
return fmt.Errorf("missing required data 'Title'")
}

if strings.TrimSpace(addPostRequest.Excerpt) == "" {
return fmt.Errorf("missing required data 'Excerpt'")
}

if strings.TrimSpace(addPostRequest.Content) == "" {
return fmt.Errorf("missing required data 'Content'")
}

return nil
}
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
}

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) {
Expand Down
Loading

0 comments on commit 9681a29

Please sign in to comment.