-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
158 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |