-
Notifications
You must be signed in to change notification settings - Fork 7
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
104 changed files
with
3,206 additions
and
2,603 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
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
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,57 @@ | ||
package article | ||
|
||
import ( | ||
"github.com/puti-projects/puti/internal/backend/api" | ||
"github.com/puti-projects/puti/internal/backend/service" | ||
"github.com/puti-projects/puti/internal/pkg/errno" | ||
"github.com/puti-projects/puti/internal/pkg/token" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// Create create article (published or draft) handler | ||
func Create(c *gin.Context) { | ||
// get token and parse | ||
t := c.Query("token") | ||
userContext, err := token.ParseToken(t) | ||
|
||
var r service.ArticleCreateRequest | ||
if err := c.Bind(&r); err != nil { | ||
api.SendResponse(c, errno.ErrBind, nil) | ||
return | ||
} | ||
|
||
// check params | ||
if err := checkCreateParam(&r); err != nil { | ||
api.SendResponse(c, err, nil) | ||
return | ||
} | ||
|
||
rsp, err := service.CreateArticle(&r, userContext.ID) | ||
if err != nil { | ||
api.SendResponse(c, errno.ErrArticleCreateFailed, nil) | ||
return | ||
} | ||
|
||
api.SendResponse(c, nil, rsp) | ||
} | ||
|
||
func checkCreateParam(r *service.ArticleCreateRequest) error { | ||
if r.Title == "" { | ||
return errno.New(errno.ErrValidation, nil).Add("Title can not be empty.") | ||
} | ||
|
||
if r.Content == "" { | ||
return errno.New(errno.ErrValidation, nil).Add("Content can not be empty.") | ||
} | ||
|
||
if r.Status == "" { | ||
return errno.New(errno.ErrValidation, nil).Add("Status can not be empty.") | ||
} | ||
|
||
if r.Status != "publish" && r.Status != "draft" { | ||
return errno.New(errno.ErrValidation, nil).Add("Status is incorrect.") | ||
} | ||
|
||
return nil | ||
} |
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,24 @@ | ||
package article | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/puti-projects/puti/internal/backend/api" | ||
"github.com/puti-projects/puti/internal/backend/service" | ||
"github.com/puti-projects/puti/internal/pkg/errno" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// Get get article handler (article info in detail) | ||
func Get(c *gin.Context) { | ||
articleID, _ := strconv.Atoi(c.Param("id")) | ||
|
||
article, err := service.GetArticleDetail(uint64(articleID)) | ||
if err != nil { | ||
api.SendResponse(c, errno.ErrArticleNotFount, nil) | ||
return | ||
} | ||
|
||
api.SendResponse(c, nil, article) | ||
} |
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,40 @@ | ||
package article | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/puti-projects/puti/internal/backend/api" | ||
"github.com/puti-projects/puti/internal/backend/service" | ||
"github.com/puti-projects/puti/internal/pkg/constvar" | ||
"github.com/puti-projects/puti/internal/pkg/errno" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// List return the article list in page | ||
func List(c *gin.Context) { | ||
var r service.ArticleListRequest | ||
if err := c.Bind(&r); err != nil { | ||
api.SendResponse(c, errno.ErrBind, nil) | ||
return | ||
} | ||
|
||
if r.Number == 0 { | ||
r.Number = constvar.DefaultLimit | ||
} | ||
|
||
infos, count, err := service.ListArticle("article", &r) | ||
if err != nil { | ||
api.SendResponse(c, err, nil) | ||
return | ||
} | ||
|
||
number := int64(r.Number) | ||
totalPage := math.Ceil(float64(count / number)) | ||
|
||
api.SendResponse(c, nil, service.ArticleListResponse{ | ||
TotalCount: count, | ||
TotalPage: uint64(totalPage), | ||
ArticleList: infos, | ||
}) | ||
} |
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,72 @@ | ||
package article | ||
|
||
import ( | ||
"strconv" | ||
|
||
"github.com/puti-projects/puti/internal/backend/api" | ||
"github.com/puti-projects/puti/internal/backend/service" | ||
"github.com/puti-projects/puti/internal/pkg/errno" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// Update update article | ||
// Delete and restore article are also in this function and it depends on the 'status' | ||
func Update(c *gin.Context) { | ||
// Get article id | ||
ID, _ := strconv.Atoi(c.Param("id")) | ||
|
||
var r service.ArticleUpdateRequest | ||
if err := c.ShouldBind(&r); err != nil { | ||
api.SendResponse(c, errno.ErrBind, nil) | ||
return | ||
} | ||
|
||
articleID := uint64(ID) | ||
|
||
// check params | ||
if err := checkUpdateParam(&r, articleID); err != nil { | ||
api.SendResponse(c, err, nil) | ||
return | ||
} | ||
|
||
if r.Status == "deleted" { | ||
if err := service.TrashPost(articleID); err != nil { | ||
api.SendResponse(c, err, nil) | ||
return | ||
} | ||
} else if r.Status == "restore" { | ||
if err := service.RestorePost(articleID); err != nil { | ||
api.SendResponse(c, err, nil) | ||
return | ||
} | ||
} else { | ||
if err := service.UpdateArticle(&r); err != nil { | ||
api.SendResponse(c, err, nil) | ||
return | ||
} | ||
} | ||
|
||
api.SendResponse(c, nil, nil) | ||
return | ||
} | ||
|
||
func checkUpdateParam(r *service.ArticleUpdateRequest, articleID uint64) error { | ||
if r.ID == 0 { | ||
return errno.New(errno.ErrValidation, nil).Add("need id.") | ||
} | ||
|
||
if r.ID != articleID { | ||
return errno.New(errno.ErrValidation, nil).Add("error id.") | ||
} | ||
|
||
if r.Status == "" { | ||
return errno.New(errno.ErrValidation, nil).Add("need status.") | ||
} | ||
|
||
if r.Status != "publish" && r.Status != "draft" && r.Status != "deleted" && r.Status != "restore" { | ||
return errno.New(errno.ErrValidation, nil).Add("error status.") | ||
} | ||
|
||
return nil | ||
} |
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
2 changes: 1 addition & 1 deletion
2
internal/backend/handler/handler.go → internal/backend/api/handler.go
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package handler | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
|
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
Oops, something went wrong.