Skip to content

Commit

Permalink
delete story
Browse files Browse the repository at this point in the history
  • Loading branch information
alnck committed Feb 12, 2022
1 parent fd46c35 commit 6de5502
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/services/blog-api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func initBlogRouteMap(route *gin.RouterGroup) {

blog.POST("/", handler.CreateStory)
blog.POST("/:id", handler.UpdateStory)
blog.DELETE("/:id", handler.DeleteStory)

}

Expand Down
24 changes: 16 additions & 8 deletions src/services/blog-api/handler/blogHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/gin-gonic/gin"
)

func UpdateStory(context *gin.Context) {
func CreateStory(context *gin.Context) {
var story models.StoryRequest
if err := context.ShouldBindJSON(&story); err != nil {
var errors []models.ErrorDetail = make([]models.ErrorDetail, 0, 1)
Expand All @@ -22,13 +22,17 @@ func UpdateStory(context *gin.Context) {
badRequest(context, http.StatusBadRequest, "invalid request", errors)
}

storyService := services.NewStoryService(context)
cu := helper.GetCurrentUser(context)

storyService.UpdateStory(story)
newStory := entities.NewStory(story.Title, story.Content, cu.ID)

storyService := services.NewStoryService(context)
storyService.CreateStory(newStory)

ok(context, http.StatusCreated, "story Added", story)
}

func CreateStory(context *gin.Context) {
func UpdateStory(context *gin.Context) {
var story models.StoryRequest
if err := context.ShouldBindJSON(&story); err != nil {
var errors []models.ErrorDetail = make([]models.ErrorDetail, 0, 1)
Expand All @@ -39,12 +43,16 @@ func CreateStory(context *gin.Context) {
badRequest(context, http.StatusBadRequest, "invalid request", errors)
}

cu := helper.GetCurrentUser(context)
storyService := services.NewStoryService(context)

newStory := entities.NewStory(story.Title, story.Content, cu.ID)
storyService.UpdateStory(story)

}

func DeleteStory(context *gin.Context) {

storyService := services.NewStoryService(context)
storyService.CreateStory(newStory)

ok(context, http.StatusCreated, "story Added", story)
storyService.DeleteStory()

}
4 changes: 4 additions & 0 deletions src/services/blog-api/repository/mongoDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,7 @@ func (repo *MongoRepository) UpdateOne(filter, update map[string]interface{}) (*
update,
)
}

func (repo *MongoRepository) DeleteOne(filter map[string]interface{}) (*mongo.DeleteResult, error) {
return repo.Collection.DeleteOne(context.Background(), filter)
}
18 changes: 18 additions & 0 deletions src/services/blog-api/services/story-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,21 @@ func (*StoryService) UpdateStory(story models.StoryRequest) bool {

return true
}

func (*StoryService) DeleteStory() bool {

storyId := storyContext.Param("id")
id, err := primitive.ObjectIDFromHex(storyId)
if err != nil {
return false
}

cu := helper.GetCurrentUser(storyContext)

filter := bson.M{"_id": id, "userid": cu.ID}

repoStories.DeleteOne(filter)

return true

}

0 comments on commit 6de5502

Please sign in to comment.