Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add views similar to django like structure #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 0 additions & 194 deletions articles/routers.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package articles

import (
"errors"
"github.com/wangzitian0/golang-gin-starter-kit/common"
"github.com/wangzitian0/golang-gin-starter-kit/users"
"gopkg.in/gin-gonic/gin.v1"
"net/http"
"strconv"
)

func ArticlesRegister(router *gin.RouterGroup) {
router.POST("/", ArticleCreate)
router.PUT("/:slug", ArticleUpdate)
Expand All @@ -28,188 +19,3 @@ func ArticlesAnonymousRegister(router *gin.RouterGroup) {
func TagsAnonymousRegister(router *gin.RouterGroup) {
router.GET("/", TagList)
}

func ArticleCreate(c *gin.Context) {
articleModelValidator := NewArticleModelValidator()
if err := articleModelValidator.Bind(c); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
return
}
//fmt.Println(articleModelValidator.articleModel.Author.UserModel)

if err := SaveOne(&articleModelValidator.articleModel); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
return
}
serializer := ArticleSerializer{c, articleModelValidator.articleModel}
c.JSON(http.StatusCreated, gin.H{"article": serializer.Response()})
}

func ArticleList(c *gin.Context) {
//condition := ArticleModel{}
tag := c.Query("tag")
author := c.Query("author")
favorited := c.Query("favorited")
limit := c.Query("limit")
offset := c.Query("offset")
articleModels, modelCount, err := FindManyArticle(tag, author, limit, offset, favorited)
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid param")))
return
}
serializer := ArticlesSerializer{c, articleModels}
c.JSON(http.StatusOK, gin.H{"articles": serializer.Response(), "articlesCount": modelCount})
}

func ArticleFeed(c *gin.Context) {
limit := c.Query("limit")
offset := c.Query("offset")
myUserModel := c.MustGet("my_user_model").(users.UserModel)
if myUserModel.ID == 0 {
c.AbortWithError(http.StatusUnauthorized, errors.New("{error : \"Require auth!\"}"))
return
}
articleUserModel := GetArticleUserModel(myUserModel)
articleModels, modelCount, err := articleUserModel.GetArticleFeed(limit, offset)
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid param")))
return
}
serializer := ArticlesSerializer{c, articleModels}
c.JSON(http.StatusOK, gin.H{"articles": serializer.Response(), "articlesCount": modelCount})
}

func ArticleRetrieve(c *gin.Context) {
slug := c.Param("slug")
if slug == "feed" {
ArticleFeed(c)
return
}
articleModel, err := FindOneArticle(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid slug")))
return
}
serializer := ArticleSerializer{c, articleModel}
c.JSON(http.StatusOK, gin.H{"article": serializer.Response()})
}

func ArticleUpdate(c *gin.Context) {
slug := c.Param("slug")
articleModel, err := FindOneArticle(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid slug")))
return
}
articleModelValidator := NewArticleModelValidatorFillWith(articleModel)
if err := articleModelValidator.Bind(c); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
return
}

articleModelValidator.articleModel.ID = articleModel.ID
if err := articleModel.Update(articleModelValidator.articleModel); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
return
}
serializer := ArticleSerializer{c, articleModel}
c.JSON(http.StatusOK, gin.H{"article": serializer.Response()})
}

func ArticleDelete(c *gin.Context) {
slug := c.Param("slug")
err := DeleteArticleModel(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid slug")))
return
}
c.JSON(http.StatusOK, gin.H{"article": "Delete success"})
}

func ArticleFavorite(c *gin.Context) {
slug := c.Param("slug")
articleModel, err := FindOneArticle(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid slug")))
return
}
myUserModel := c.MustGet("my_user_model").(users.UserModel)
err = articleModel.favoriteBy(GetArticleUserModel(myUserModel))
serializer := ArticleSerializer{c, articleModel}
c.JSON(http.StatusOK, gin.H{"article": serializer.Response()})
}

func ArticleUnfavorite(c *gin.Context) {
slug := c.Param("slug")
articleModel, err := FindOneArticle(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid slug")))
return
}
myUserModel := c.MustGet("my_user_model").(users.UserModel)
err = articleModel.unFavoriteBy(GetArticleUserModel(myUserModel))
serializer := ArticleSerializer{c, articleModel}
c.JSON(http.StatusOK, gin.H{"article": serializer.Response()})
}

func ArticleCommentCreate(c *gin.Context) {
slug := c.Param("slug")
articleModel, err := FindOneArticle(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("comment", errors.New("Invalid slug")))
return
}
commentModelValidator := NewCommentModelValidator()
if err := commentModelValidator.Bind(c); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
return
}
commentModelValidator.commentModel.Article = articleModel

if err := SaveOne(&commentModelValidator.commentModel); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
return
}
serializer := CommentSerializer{c, commentModelValidator.commentModel}
c.JSON(http.StatusCreated, gin.H{"comment": serializer.Response()})
}

func ArticleCommentDelete(c *gin.Context) {
id64, err := strconv.ParseUint(c.Param("id"), 10, 32)
id := uint(id64)
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("comment", errors.New("Invalid id")))
return
}
err = DeleteCommentModel([]uint{id})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("comment", errors.New("Invalid id")))
return
}
c.JSON(http.StatusOK, gin.H{"comment": "Delete success"})
}

func ArticleCommentList(c *gin.Context) {
slug := c.Param("slug")
articleModel, err := FindOneArticle(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("comments", errors.New("Invalid slug")))
return
}
err = articleModel.getComments()
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("comments", errors.New("Database error")))
return
}
serializer := CommentsSerializer{c, articleModel.Comments}
c.JSON(http.StatusOK, gin.H{"comments": serializer.Response()})
}
func TagList(c *gin.Context) {
tagModels, err := getAllTags()
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid param")))
return
}
serializer := TagsSerializer{c, tagModels}
c.JSON(http.StatusOK, gin.H{"tags": serializer.Response()})
}
195 changes: 195 additions & 0 deletions articles/views.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
package articles

import (
"errors"
"github.com/wangzitian0/golang-gin-starter-kit/common"
"github.com/wangzitian0/golang-gin-starter-kit/users"
"gopkg.in/gin-gonic/gin.v1"
"net/http"
"strconv"
)

func ArticleCreate(c *gin.Context) {
articleModelValidator := NewArticleModelValidator()
if err := articleModelValidator.Bind(c); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
return
}
//fmt.Println(articleModelValidator.articleModel.Author.UserModel)

if err := SaveOne(&articleModelValidator.articleModel); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
return
}
serializer := ArticleSerializer{c, articleModelValidator.articleModel}
c.JSON(http.StatusCreated, gin.H{"article": serializer.Response()})
}

func ArticleList(c *gin.Context) {
//condition := ArticleModel{}
tag := c.Query("tag")
author := c.Query("author")
favorited := c.Query("favorited")
limit := c.Query("limit")
offset := c.Query("offset")
articleModels, modelCount, err := FindManyArticle(tag, author, limit, offset, favorited)
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid param")))
return
}
serializer := ArticlesSerializer{c, articleModels}
c.JSON(http.StatusOK, gin.H{"articles": serializer.Response(), "articlesCount": modelCount})
}

func ArticleFeed(c *gin.Context) {
limit := c.Query("limit")
offset := c.Query("offset")
myUserModel := c.MustGet("my_user_model").(users.UserModel)
if myUserModel.ID == 0 {
c.AbortWithError(http.StatusUnauthorized, errors.New("{error : \"Require auth!\"}"))
return
}
articleUserModel := GetArticleUserModel(myUserModel)
articleModels, modelCount, err := articleUserModel.GetArticleFeed(limit, offset)
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid param")))
return
}
serializer := ArticlesSerializer{c, articleModels}
c.JSON(http.StatusOK, gin.H{"articles": serializer.Response(), "articlesCount": modelCount})
}

func ArticleRetrieve(c *gin.Context) {
slug := c.Param("slug")
if slug == "feed" {
ArticleFeed(c)
return
}
articleModel, err := FindOneArticle(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid slug")))
return
}
serializer := ArticleSerializer{c, articleModel}
c.JSON(http.StatusOK, gin.H{"article": serializer.Response()})
}

func ArticleUpdate(c *gin.Context) {
slug := c.Param("slug")
articleModel, err := FindOneArticle(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid slug")))
return
}
articleModelValidator := NewArticleModelValidatorFillWith(articleModel)
if err := articleModelValidator.Bind(c); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
return
}

articleModelValidator.articleModel.ID = articleModel.ID
if err := articleModel.Update(articleModelValidator.articleModel); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
return
}
serializer := ArticleSerializer{c, articleModel}
c.JSON(http.StatusOK, gin.H{"article": serializer.Response()})
}

func ArticleDelete(c *gin.Context) {
slug := c.Param("slug")
err := DeleteArticleModel(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid slug")))
return
}
c.JSON(http.StatusOK, gin.H{"article": "Delete success"})
}

func ArticleFavorite(c *gin.Context) {
slug := c.Param("slug")
articleModel, err := FindOneArticle(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid slug")))
return
}
myUserModel := c.MustGet("my_user_model").(users.UserModel)
err = articleModel.favoriteBy(GetArticleUserModel(myUserModel))
serializer := ArticleSerializer{c, articleModel}
c.JSON(http.StatusOK, gin.H{"article": serializer.Response()})
}

func ArticleUnfavorite(c *gin.Context) {
slug := c.Param("slug")
articleModel, err := FindOneArticle(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid slug")))
return
}
myUserModel := c.MustGet("my_user_model").(users.UserModel)
err = articleModel.unFavoriteBy(GetArticleUserModel(myUserModel))
serializer := ArticleSerializer{c, articleModel}
c.JSON(http.StatusOK, gin.H{"article": serializer.Response()})
}

func ArticleCommentCreate(c *gin.Context) {
slug := c.Param("slug")
articleModel, err := FindOneArticle(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("comment", errors.New("Invalid slug")))
return
}
commentModelValidator := NewCommentModelValidator()
if err := commentModelValidator.Bind(c); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
return
}
commentModelValidator.commentModel.Article = articleModel

if err := SaveOne(&commentModelValidator.commentModel); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
return
}
serializer := CommentSerializer{c, commentModelValidator.commentModel}
c.JSON(http.StatusCreated, gin.H{"comment": serializer.Response()})
}

func ArticleCommentDelete(c *gin.Context) {
id64, err := strconv.ParseUint(c.Param("id"), 10, 32)
id := uint(id64)
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("comment", errors.New("Invalid id")))
return
}
err = DeleteCommentModel([]uint{id})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("comment", errors.New("Invalid id")))
return
}
c.JSON(http.StatusOK, gin.H{"comment": "Delete success"})
}

func ArticleCommentList(c *gin.Context) {
slug := c.Param("slug")
articleModel, err := FindOneArticle(&ArticleModel{Slug: slug})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("comments", errors.New("Invalid slug")))
return
}
err = articleModel.getComments()
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("comments", errors.New("Database error")))
return
}
serializer := CommentsSerializer{c, articleModel.Comments}
c.JSON(http.StatusOK, gin.H{"comments": serializer.Response()})
}
func TagList(c *gin.Context) {
tagModels, err := getAllTags()
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("articles", errors.New("Invalid param")))
return
}
serializer := TagsSerializer{c, tagModels}
c.JSON(http.StatusOK, gin.H{"tags": serializer.Response()})
}
Loading