diff --git a/src/services/blog-api/api.go b/src/services/blog-api/api.go index e6c5798..2febaaa 100644 --- a/src/services/blog-api/api.go +++ b/src/services/blog-api/api.go @@ -24,8 +24,9 @@ func initBlogRouteMap(route *gin.RouterGroup) { blog := route.Group("/blog") blog.Use(middleware.Authorization([]int{1})) - blog.GET("/", handler.GetStroies) - blog.GET("/:id", handler.GetStory) + blog.POST("/", handler.CreateStory) + blog.POST("/:id", handler.UpdateStory) + } func initUserRouteMap(route *gin.RouterGroup) { diff --git a/src/services/blog-api/entities/story.go b/src/services/blog-api/entities/story.go new file mode 100644 index 0000000..8531c42 --- /dev/null +++ b/src/services/blog-api/entities/story.go @@ -0,0 +1,29 @@ +package entities + +import ( + "time" + + "go.mongodb.org/mongo-driver/bson/primitive" +) + +type Story struct { + ID primitive.ObjectID `bson:"_id,omitempty"` + CreatedDate time.Time `bson:"createddate,omitempty"` + ModifiedDate time.Time `bson:"modifieddate,omitempty"` + DisplayRate int `bson:"displayrate,omitempty"` + UserID primitive.ObjectID `bson:"userid,omitempty"` + Title string `bson:"title,omitempty"` + Content string `bson:"content,omitempty"` +} + +func NewStory(title, content string, userId primitive.ObjectID) Story { + story := Story{ + CreatedDate: time.Now().UTC(), + DisplayRate: 0, + UserID: userId, + Title: title, + Content: content, + } + + return story +} diff --git a/src/services/blog-api/handler/blogHandler.go b/src/services/blog-api/handler/blogHandler.go new file mode 100644 index 0000000..7347f35 --- /dev/null +++ b/src/services/blog-api/handler/blogHandler.go @@ -0,0 +1,50 @@ +package handler + +import ( + "blog-on-containers/entities" + "blog-on-containers/helper" + "blog-on-containers/models" + "blog-on-containers/services" + "fmt" + "net/http" + + "github.com/gin-gonic/gin" +) + +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) + errors = append(errors, models.ErrorDetail{ + ErrorType: models.ErrorTypeValidation, + ErrorMessage: fmt.Sprintf("%v", err), + }) + badRequest(context, http.StatusBadRequest, "invalid request", errors) + } + + storyService := services.NewStoryService(context) + + storyService.UpdateStory(story) + +} + +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) + errors = append(errors, models.ErrorDetail{ + ErrorType: models.ErrorTypeValidation, + ErrorMessage: fmt.Sprintf("%v", err), + }) + badRequest(context, http.StatusBadRequest, "invalid request", errors) + } + + cu := helper.GetCurrentUser(context) + + newStory := entities.NewStory(story.Title, story.Content, cu.ID) + + storyService := services.NewStoryService(context) + storyService.CreateStory(newStory) + + ok(context, http.StatusCreated, "story Added", story) +} diff --git a/src/services/blog-api/models/storyRequest.go b/src/services/blog-api/models/storyRequest.go new file mode 100644 index 0000000..4b7aaf8 --- /dev/null +++ b/src/services/blog-api/models/storyRequest.go @@ -0,0 +1,6 @@ +package models + +type StoryRequest struct { + Title string `json:"Title"` + Content string `json:"Content"` +} diff --git a/src/services/blog-api/repository/mongoDB.go b/src/services/blog-api/repository/mongoDB.go index 66c8530..1190259 100644 --- a/src/services/blog-api/repository/mongoDB.go +++ b/src/services/blog-api/repository/mongoDB.go @@ -54,3 +54,15 @@ func (repo *MongoRepository) FindOne(selector map[string]interface{}, v interfac func (repo *MongoRepository) CountDocuments(selector map[string]interface{}) (int64, error) { return repo.Collection.CountDocuments(context.Background(), selector) } + +func (repo *MongoRepository) InsertOne(v interface{}) (*mongo.InsertOneResult, error) { + return repo.Collection.InsertOne(context.Background(), v) +} + +func (repo *MongoRepository) UpdateOne(filter, update map[string]interface{}) (*mongo.UpdateResult, error) { + return repo.Collection.UpdateOne( + context.Background(), + filter, + update, + ) +} diff --git a/src/services/blog-api/services/story-service.go b/src/services/blog-api/services/story-service.go new file mode 100644 index 0000000..179b395 --- /dev/null +++ b/src/services/blog-api/services/story-service.go @@ -0,0 +1,58 @@ +package services + +import ( + "blog-on-containers/entities" + "blog-on-containers/helper" + "blog-on-containers/models" + "blog-on-containers/repository" + "time" + + "github.com/gin-gonic/gin" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/bson/primitive" +) + +var ( + repoStories *repository.MongoRepository + storyContext *gin.Context +) + +const m_COLLECTION_NAME_STORIES = "stories" + +type StoryService struct{} + +func NewStoryService(context *gin.Context) StoryService { + repoStories = repository.GetMongoRepository(m_COLLECTION_NAME_STORIES) + storyContext = context + + return StoryService{} +} + +func (*StoryService) CreateStory(story entities.Story) { + repoStories.InsertOne(story) +} + +func (*StoryService) UpdateStory(story models.StoryRequest) 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} + update := bson.M{ + "$set": bson.M{ + "title": story.Title, + "content": story.Content, + "modifieddate": time.Now().UTC(), + }, + } + + repoStories.UpdateOne(filter, update) + + return true +}