From f81a6ffeb0b7076031939d2a9d31edd6e5a0241d Mon Sep 17 00:00:00 2001 From: Eren Date: Sun, 13 Feb 2022 11:19:49 +0300 Subject: [PATCH] Get Stories --- src/services/blog-api/api.go | 3 +++ src/services/blog-api/handler/blogHandler.go | 14 ++++++++++---- src/services/blog-api/repository/mongoDB.go | 6 ++++++ src/services/blog-api/services/story-service.go | 13 +++++++++---- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/services/blog-api/api.go b/src/services/blog-api/api.go index 230cd28..71b1720 100644 --- a/src/services/blog-api/api.go +++ b/src/services/blog-api/api.go @@ -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) } diff --git a/src/services/blog-api/handler/blogHandler.go b/src/services/blog-api/handler/blogHandler.go index 88b6203..549e334 100644 --- a/src/services/blog-api/handler/blogHandler.go +++ b/src/services/blog-api/handler/blogHandler.go @@ -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) @@ -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) +} diff --git a/src/services/blog-api/repository/mongoDB.go b/src/services/blog-api/repository/mongoDB.go index a56dd37..581a9d1 100644 --- a/src/services/blog-api/repository/mongoDB.go +++ b/src/services/blog-api/repository/mongoDB.go @@ -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) } diff --git a/src/services/blog-api/services/story-service.go b/src/services/blog-api/services/story-service.go index 6d848e1..15a945c 100644 --- a/src/services/blog-api/services/story-service.go +++ b/src/services/blog-api/services/story-service.go @@ -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 @@ -58,7 +56,6 @@ func (*StoryService) UpdateStory(story models.StoryRequest) bool { } func (*StoryService) DeleteStory() bool { - storyId := storyContext.Param("id") id, err := primitive.ObjectIDFromHex(storyId) if err != nil { @@ -66,7 +63,6 @@ func (*StoryService) DeleteStory() bool { } cu := helper.GetCurrentUser(storyContext) - filter := bson.M{"_id": id, "userid": cu.ID} repoStories.DeleteOne(filter) @@ -74,3 +70,12 @@ func (*StoryService) DeleteStory() bool { return true } + +func (*StoryService) GetStories() ([]entities.Story, error) { + var stories []entities.Story + filter := bson.M{} + + err := repoStories.Find(filter, &stories) + + return stories, err +}