Skip to content

Commit

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

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

blog.GET("/", handler.GetStories)

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

}
Expand Down
14 changes: 10 additions & 4 deletions src/services/blog-api/handler/blogHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ func CreateStory(context *gin.Context) {
}

cu := helper.GetCurrentUser(context)

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

storyService := services.NewStoryService(context)
Expand All @@ -44,15 +43,22 @@ func UpdateStory(context *gin.Context) {
}

storyService := services.NewStoryService(context)

storyService.UpdateStory(story)

}

func DeleteStory(context *gin.Context) {

storyService := services.NewStoryService(context)

storyService.DeleteStory()

}

func GetStories(context *gin.Context) {
storyService := services.NewStoryService(context)
stories, err := storyService.GetStories()
if err != nil {
badRequest(context, http.StatusBadRequest, "Stories Not Found", nil)
}

ok(context, http.StatusOK, "All Stories Taken", stories)
}
6 changes: 6 additions & 0 deletions src/services/blog-api/repository/mongoDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ func GetMongoRepository(collectionName string) *MongoRepository {
return &MongoRepository{Collection: mongoDBInstance.Collection(collectionName)}
}

func (repo *MongoRepository) Find(selector map[string]interface{}, v interface{}) error {
cursor, err := repo.Collection.Find(context.Background(), selector)
cursor.All(context.Background(), v)
return err
}

func (repo *MongoRepository) FindOne(selector map[string]interface{}, v interface{}) error {
return repo.Collection.FindOne(context.Background(), selector).Decode(v)
}
Expand Down
13 changes: 9 additions & 4 deletions src/services/blog-api/services/story-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ func (*StoryService) CreateStory(story entities.Story) {
}

func (*StoryService) UpdateStory(story models.StoryRequest) bool {

storyId := storyContext.Param("id")

id, err := primitive.ObjectIDFromHex(storyId)
if err != nil {
return false
Expand All @@ -58,19 +56,26 @@ func (*StoryService) UpdateStory(story models.StoryRequest) bool {
}

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

}

func (*StoryService) GetStories() ([]entities.Story, error) {
var stories []entities.Story
filter := bson.M{}

err := repoStories.Find(filter, &stories)

return stories, err
}

0 comments on commit f81a6ff

Please sign in to comment.