diff --git a/articles/routers.go b/articles/routers.go index 820ed4c..84a8ef3 100644 --- a/articles/routers.go +++ b/articles/routers.go @@ -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) @@ -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()}) -} diff --git a/articles/views.go b/articles/views.go new file mode 100644 index 0000000..cd5d589 --- /dev/null +++ b/articles/views.go @@ -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()}) +} diff --git a/users/routers.go b/users/routers.go index aa661c7..82b9401 100644 --- a/users/routers.go +++ b/users/routers.go @@ -1,12 +1,5 @@ package users -import ( - "errors" - "github.com/wangzitian0/golang-gin-starter-kit/common" - "gopkg.in/gin-gonic/gin.v1" - "net/http" -) - func UsersRegister(router *gin.RouterGroup) { router.POST("/", UsersRegistration) router.POST("/login", UsersLogin) @@ -22,110 +15,3 @@ func ProfileRegister(router *gin.RouterGroup) { router.POST("/:username/follow", ProfileFollow) router.DELETE("/:username/follow", ProfileUnfollow) } - -func ProfileRetrieve(c *gin.Context) { - username := c.Param("username") - userModel, err := FindOneUser(&UserModel{Username: username}) - if err != nil { - c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username"))) - return - } - profileSerializer := ProfileSerializer{c, userModel} - c.JSON(http.StatusOK, gin.H{"profile": profileSerializer.Response()}) -} - -func ProfileFollow(c *gin.Context) { - username := c.Param("username") - userModel, err := FindOneUser(&UserModel{Username: username}) - if err != nil { - c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username"))) - return - } - myUserModel := c.MustGet("my_user_model").(UserModel) - err = myUserModel.following(userModel) - if err != nil { - c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err)) - return - } - serializer := ProfileSerializer{c, userModel} - c.JSON(http.StatusOK, gin.H{"profile": serializer.Response()}) -} - -func ProfileUnfollow(c *gin.Context) { - username := c.Param("username") - userModel, err := FindOneUser(&UserModel{Username: username}) - if err != nil { - c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username"))) - return - } - myUserModel := c.MustGet("my_user_model").(UserModel) - - err = myUserModel.unFollowing(userModel) - if err != nil { - c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err)) - return - } - serializer := ProfileSerializer{c, userModel} - c.JSON(http.StatusOK, gin.H{"profile": serializer.Response()}) -} - -func UsersRegistration(c *gin.Context) { - userModelValidator := NewUserModelValidator() - if err := userModelValidator.Bind(c); err != nil { - c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err)) - return - } - - if err := SaveOne(&userModelValidator.userModel); err != nil { - c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err)) - return - } - c.Set("my_user_model", userModelValidator.userModel) - serializer := UserSerializer{c} - c.JSON(http.StatusCreated, gin.H{"user": serializer.Response()}) -} - -func UsersLogin(c *gin.Context) { - loginValidator := NewLoginValidator() - if err := loginValidator.Bind(c); err != nil { - c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err)) - return - } - userModel, err := FindOneUser(&UserModel{Email: loginValidator.userModel.Email}) - - if err != nil { - c.JSON(http.StatusForbidden, common.NewError("login", errors.New("Not Registered email or invalid password"))) - return - } - - if userModel.checkPassword(loginValidator.User.Password) != nil { - c.JSON(http.StatusForbidden, common.NewError("login", errors.New("Not Registered email or invalid password"))) - return - } - UpdateContextUserModel(c, userModel.ID) - serializer := UserSerializer{c} - c.JSON(http.StatusOK, gin.H{"user": serializer.Response()}) -} - -func UserRetrieve(c *gin.Context) { - serializer := UserSerializer{c} - c.JSON(http.StatusOK, gin.H{"user": serializer.Response()}) -} - -func UserUpdate(c *gin.Context) { - myUserModel := c.MustGet("my_user_model").(UserModel) - userModelValidator := NewUserModelValidatorFillWith(myUserModel) - if err := userModelValidator.Bind(c); err != nil { - c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err)) - return - } - - userModelValidator.userModel.ID = myUserModel.ID - if err := myUserModel.Update(userModelValidator.userModel); err != nil { - c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err)) - return - } - UpdateContextUserModel(c, myUserModel.ID) - serializer := UserSerializer{c} - c.JSON(http.StatusOK, gin.H{"user": serializer.Response()}) -} diff --git a/users/views.go b/users/views.go new file mode 100644 index 0000000..ccb8445 --- /dev/null +++ b/users/views.go @@ -0,0 +1,115 @@ +package users + +import ( + "errors" + "github.com/wangzitian0/golang-gin-starter-kit/common" + "gopkg.in/gin-gonic/gin.v1" + "net/http" +) + +func ProfileRetrieve(c *gin.Context) { + username := c.Param("username") + userModel, err := FindOneUser(&UserModel{Username: username}) + if err != nil { + c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username"))) + return + } + profileSerializer := ProfileSerializer{c, userModel} + c.JSON(http.StatusOK, gin.H{"profile": profileSerializer.Response()}) +} + +func ProfileFollow(c *gin.Context) { + username := c.Param("username") + userModel, err := FindOneUser(&UserModel{Username: username}) + if err != nil { + c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username"))) + return + } + myUserModel := c.MustGet("my_user_model").(UserModel) + err = myUserModel.following(userModel) + if err != nil { + c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err)) + return + } + serializer := ProfileSerializer{c, userModel} + c.JSON(http.StatusOK, gin.H{"profile": serializer.Response()}) +} + +func ProfileUnfollow(c *gin.Context) { + username := c.Param("username") + userModel, err := FindOneUser(&UserModel{Username: username}) + if err != nil { + c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username"))) + return + } + myUserModel := c.MustGet("my_user_model").(UserModel) + + err = myUserModel.unFollowing(userModel) + if err != nil { + c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err)) + return + } + serializer := ProfileSerializer{c, userModel} + c.JSON(http.StatusOK, gin.H{"profile": serializer.Response()}) +} + +func UsersRegistration(c *gin.Context) { + userModelValidator := NewUserModelValidator() + if err := userModelValidator.Bind(c); err != nil { + c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err)) + return + } + + if err := SaveOne(&userModelValidator.userModel); err != nil { + c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err)) + return + } + c.Set("my_user_model", userModelValidator.userModel) + serializer := UserSerializer{c} + c.JSON(http.StatusCreated, gin.H{"user": serializer.Response()}) +} + +func UsersLogin(c *gin.Context) { + loginValidator := NewLoginValidator() + if err := loginValidator.Bind(c); err != nil { + c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err)) + return + } + userModel, err := FindOneUser(&UserModel{Email: loginValidator.userModel.Email}) + + if err != nil { + c.JSON(http.StatusForbidden, common.NewError("login", errors.New("Not Registered email or invalid password"))) + return + } + + if userModel.checkPassword(loginValidator.User.Password) != nil { + c.JSON(http.StatusForbidden, common.NewError("login", errors.New("Not Registered email or invalid password"))) + return + } + UpdateContextUserModel(c, userModel.ID) + serializer := UserSerializer{c} + c.JSON(http.StatusOK, gin.H{"user": serializer.Response()}) +} + +func UserRetrieve(c *gin.Context) { + serializer := UserSerializer{c} + c.JSON(http.StatusOK, gin.H{"user": serializer.Response()}) +} + +func UserUpdate(c *gin.Context) { + myUserModel := c.MustGet("my_user_model").(UserModel) + userModelValidator := NewUserModelValidatorFillWith(myUserModel) + if err := userModelValidator.Bind(c); err != nil { + c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err)) + return + } + + userModelValidator.userModel.ID = myUserModel.ID + if err := myUserModel.Update(userModelValidator.userModel); err != nil { + c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err)) + return + } + UpdateContextUserModel(c, myUserModel.ID) + serializer := UserSerializer{c} + c.JSON(http.StatusOK, gin.H{"user": serializer.Response()}) +}