Skip to content

Commit

Permalink
Story create and update coding
Browse files Browse the repository at this point in the history
  • Loading branch information
alnck committed Feb 12, 2022
1 parent 7350fe5 commit abfba18
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/services/blog-api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 29 additions & 0 deletions src/services/blog-api/entities/story.go
Original file line number Diff line number Diff line change
@@ -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
}
50 changes: 50 additions & 0 deletions src/services/blog-api/handler/blogHandler.go
Original file line number Diff line number Diff line change
@@ -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)
}
6 changes: 6 additions & 0 deletions src/services/blog-api/models/storyRequest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package models

type StoryRequest struct {
Title string `json:"Title"`
Content string `json:"Content"`
}
12 changes: 12 additions & 0 deletions src/services/blog-api/repository/mongoDB.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
58 changes: 58 additions & 0 deletions src/services/blog-api/services/story-service.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit abfba18

Please sign in to comment.