Skip to content

Commit

Permalink
feat: 完成所有接口开发
Browse files Browse the repository at this point in the history
  • Loading branch information
dokidokikoi committed Sep 16, 2023
1 parent d3fa768 commit 5c6c936
Show file tree
Hide file tree
Showing 46 changed files with 590 additions and 112 deletions.
2 changes: 1 addition & 1 deletion internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ func Run() {
e.Use(gin.Recovery())
router.InstallAll(e)

endless.ListenAndServe(":8080", e)
endless.ListenAndServe(":10080", e)
}
1 change: 1 addition & 0 deletions internal/code/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const (
const (
ErrPassword int = iota + 100101
ErrCaptcha
ErrGenCaptcha
)

// 数据库错误码 1xx2xx
Expand Down
23 changes: 12 additions & 11 deletions internal/controller/article/article.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ type Tag struct {
}

type CreateArticle struct {
Title string `json:"title"`
Summary string `json:"summary"`
Cover string `json:"cover"`
Weight int `json:"weight"`
ArticleBodyID uint `json:"article_body_id"`
Category Category `json:"category"`
Tags []Tag `json:"tags"`
Series series.Series `json:"series"`
AuthorID uint `json:"author_id"`
ArticleBody string `json:"article_body"`
Title string `json:"title"`
Summary string `json:"summary"`
Cover string `json:"cover"`
Weight int `json:"weight"`
Category Category `json:"category"`
Tags []Tag `json:"tags"`
Series series.Series `json:"series"`
AuthorID uint `json:"author_id"`
ArticleBody string `json:"article_body"`
}

type UpdateArticle struct {
Expand All @@ -48,7 +47,9 @@ type DelArticle struct {
}

type Query struct {
Keyword string `json:"keyword"`
Keyword string `form:"keyword"`
CategoryID uint `form:"category_id"`
SeriesID uint `form:"series_id"`
}

type Controller struct {
Expand Down
4 changes: 3 additions & 1 deletion internal/controller/article/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"

zaplog "github.com/dokidokikoi/go-common/log/zap"
meta "github.com/dokidokikoi/go-common/meta/option"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
Expand All @@ -19,7 +20,8 @@ func (c *Controller) Get(ctx *gin.Context) {
return
}

s, err := c.srv.Article().Get(ctx, &article.Article{ID: uint(articleID)}, nil)
option := &meta.GetOption{Preload: []string{"Category", "Tags", "Series", "ArticleBody"}}
s, err := c.srv.Article().Get(ctx, &article.Article{ID: uint(articleID)}, option)
if err != nil {
zaplog.L().Error("获取文章信息失败", zap.Error(err))
core.WriteResponse(ctx, myErrors.ApiRecordNotFound, nil)
Expand Down
28 changes: 27 additions & 1 deletion internal/controller/article/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go-blog/internal/core"
"go-blog/internal/db/model/article"
myErrors "go-blog/internal/errors"
"strconv"

zaplog "github.com/dokidokikoi/go-common/log/zap"
meta "github.com/dokidokikoi/go-common/meta/option"
Expand All @@ -28,11 +29,12 @@ func (c *Controller) List(ctx *gin.Context) {
}

listOption := pageQuery.GetListOption()
listOption.Preload = []string{"Category", "Tags", "Series"}
var (
items []*article.Article
total int64
err error
example = &article.Article{}
example = &article.Article{CategoryID: input.CategoryID, SeriesID: input.SeriesID}
)
if input.Keyword != "" {
node := &meta.WhereNode{
Expand All @@ -56,3 +58,27 @@ func (c *Controller) List(ctx *gin.Context) {

core.WriteListResponse(ctx, nil, total, items)
}

func (c *Controller) ListTagArticle(ctx *gin.Context) {
tagID, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
if err != nil {
zaplog.L().Error("参数校验失败", zap.Error(err))
core.WriteResponse(ctx, myErrors.ApiErrValidation, nil)
return
}
var pageQuery query.PageQuery
if ctx.ShouldBindQuery(&pageQuery) != nil {
zaplog.L().Error("参数校验失败")
core.WriteResponse(ctx, myErrors.ApiErrValidation, "")
return
}
listOption := pageQuery.GetListOption()
listOption.Preload = []string{"Category", "Tags", "Series"}
articles, total, err := c.srv.Article().ListTagArticle(ctx, uint(tagID), listOption)
if err != nil {
zaplog.L().Error("获取文章列表失败", zap.Error(err))
core.WriteResponse(ctx, myErrors.ApiRecordNotFound, "")
return
}
core.WriteListResponse(ctx, nil, total, articles)
}
12 changes: 10 additions & 2 deletions internal/controller/article/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ func (c *Controller) Update(ctx *gin.Context) {
})
}

err := c.srv.Site().DeleteSiteAllTags(ctx, input.ID)
err := c.srv.Article().DeleteArticleAllTags(ctx, input.ID)
if err != nil {
zaplog.L().Error("删除文章标签失败", zap.Error(err))
core.WriteResponse(ctx, myErrors.ApiErrDatabasDel, nil)
return
}

a, err := c.srv.Article().Get(ctx, &article.Article{ID: input.ID}, nil)
if err != nil {
zaplog.L().Error("文章不存在", zap.Error(err))
core.WriteResponse(ctx, myErrors.ApiRecordNotFound, nil)
return
}

err = c.srv.Article().Update(ctx, &article.Article{
ID: input.ID,
Title: input.Title,
Expand All @@ -43,6 +50,7 @@ func (c *Controller) Update(ctx *gin.Context) {
Weight: input.Weight,
Series: input.Series,
ArticleBody: article.ArticleBody{
ID: a.ArticleBodyID,
Content: input.ArticleBody,
},
Category: category.Category{
Expand All @@ -55,7 +63,7 @@ func (c *Controller) Update(ctx *gin.Context) {
}, nil)
if err != nil {
zaplog.L().Error("更新文章消息失败", zap.Error(err))
core.WriteResponse(ctx, myErrors.ApiErrValidation, nil)
core.WriteResponse(ctx, myErrors.ApiNoUpdateRows, nil)
return
}
core.WriteResponse(ctx, nil, nil)
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/category/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type DelCategory struct {
}

type Query struct {
Keyword string `json:"keyword"`
Type int8 `json:"type" binding:"required"`
Keyword string `form:"keyword"`
Type int8 `form:"type" binding:"required"`
}

type Controller struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/controller/category/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ func (c *Controller) List(ctx *gin.Context) {

core.WriteListResponse(ctx, nil, total, list)
}

func (c *Controller) ListSites(ctx *gin.Context) {

}
3 changes: 2 additions & 1 deletion internal/controller/comment/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type DelComment struct {
}

type Query struct {
Keyword string `json:"keyword"`
Keyword string `form:"keyword"`
ArticleID uint `form:"article_id"`
}

type Controller struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/comment/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *Controller) List(ctx *gin.Context) {
items []*comment.Comment
total int64
err error
example = &comment.Comment{}
example = &comment.Comment{ArticleID: input.ArticleID}
)
if input.Keyword != "" {
node := &meta.WhereNode{
Expand Down
2 changes: 1 addition & 1 deletion internal/controller/link/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type DelLink struct {
}

type Query struct {
Keyword string `json:"keyword"`
Keyword string `form:"keyword"`
}

type Controller struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/list/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ type DelItem struct {
}

type Query struct {
Keyword string `json:"keyword"`
Type int8 `json:"type"`
Keyword string `form:"keyword"`
Type int8 `form:"type"`
}

type Controller struct {
Expand Down
5 changes: 2 additions & 3 deletions internal/controller/series/series.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
type CreateSeries struct {
SeriesName string `json:"series_name" binding:"required"`
Summary string `json:"summary" binding:"required"`
Type int8 `json:"type" binding:"required"`
}

type UpdateSeries struct {
Expand All @@ -22,8 +21,8 @@ type DelSeries struct {
}

type Query struct {
Keyword string `json:"keyword"`
Type int8 `json:"type" binding:"required"`
Keyword string `form:"keyword"`
Type int8 `form:"type" binding:"required"`
}

type Controller struct {
Expand Down
4 changes: 3 additions & 1 deletion internal/controller/site/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strconv"

zaplog "github.com/dokidokikoi/go-common/log/zap"
meta "github.com/dokidokikoi/go-common/meta/option"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
Expand All @@ -19,7 +20,8 @@ func (c *Controller) Get(ctx *gin.Context) {
return
}

s, err := c.srv.Site().Get(ctx, &site.Site{ID: uint(siteID)}, nil)
option := &meta.GetOption{Preload: []string{"Category", "Tags"}}
s, err := c.srv.Site().Get(ctx, &site.Site{ID: uint(siteID)}, option)
if err != nil {
zaplog.L().Error("获取网站信息失败", zap.Error(err))
core.WriteResponse(ctx, myErrors.ApiRecordNotFound, nil)
Expand Down
28 changes: 27 additions & 1 deletion internal/controller/site/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go-blog/internal/core"
"go-blog/internal/db/model/site"
myErrors "go-blog/internal/errors"
"strconv"

zaplog "github.com/dokidokikoi/go-common/log/zap"
meta "github.com/dokidokikoi/go-common/meta/option"
Expand All @@ -28,11 +29,12 @@ func (c *Controller) List(ctx *gin.Context) {
}

listOption := pageQuery.GetListOption()
listOption.Preload = []string{"Category", "Tags"}
var (
items []*site.Site
total int64
err error
example = &site.Site{}
example = &site.Site{CategoryID: input.CategoryID}
)
if input.Keyword != "" {
node := &meta.WhereNode{
Expand All @@ -56,3 +58,27 @@ func (c *Controller) List(ctx *gin.Context) {

core.WriteListResponse(ctx, nil, total, items)
}

func (c *Controller) ListTagSite(ctx *gin.Context) {
tagID, err := strconv.ParseInt(ctx.Param("id"), 10, 64)
if err != nil {
zaplog.L().Error("参数校验失败", zap.Error(err))
core.WriteResponse(ctx, myErrors.ApiErrValidation, nil)
return
}
var pageQuery query.PageQuery
if ctx.ShouldBindQuery(&pageQuery) != nil {
zaplog.L().Error("参数校验失败")
core.WriteResponse(ctx, myErrors.ApiErrValidation, "")
return
}
listOption := pageQuery.GetListOption()
listOption.Preload = []string{"Category", "Tags"}
sites, total, err := c.srv.Site().ListTagSite(ctx, uint(tagID), listOption)
if err != nil {
zaplog.L().Error("获取网站列表失败", zap.Error(err))
core.WriteResponse(ctx, myErrors.ApiRecordNotFound, "")
return
}
core.WriteListResponse(ctx, nil, total, sites)
}
3 changes: 2 additions & 1 deletion internal/controller/site/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ type DelSite struct {
}

type Query struct {
Keyword string `json:"keyword"`
Keyword string `form:"keyword"`
CategoryID uint `form:"category_id"`
}

type Controller struct {
Expand Down
3 changes: 2 additions & 1 deletion internal/controller/site/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func (c *Controller) Update(ctx *gin.Context) {
}

err = c.srv.Site().Update(ctx, &site.Site{
ID: input.ID,
SiteName: input.SiteName,
Summary: input.Summary,
Logo: input.Logo,
Expand All @@ -50,7 +51,7 @@ func (c *Controller) Update(ctx *gin.Context) {
}, nil)
if err != nil {
zaplog.L().Error("更新网站消息失败", zap.Error(err))
core.WriteResponse(ctx, myErrors.ApiErrValidation, nil)
core.WriteResponse(ctx, myErrors.ApiNoUpdateRows, nil)
return
}
core.WriteResponse(ctx, nil, nil)
Expand Down
8 changes: 8 additions & 0 deletions internal/controller/tag/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@ func (c *Controller) List(ctx *gin.Context) {

core.WriteListResponse(ctx, nil, total, list)
}

func (c *Controller) ListArticle(ctx *gin.Context) {

}

func (c *Controller) ListSites(ctx *gin.Context) {

}
4 changes: 2 additions & 2 deletions internal/controller/tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type DelTag struct {
}

type Query struct {
Keyword string `json:"keyword"`
Type int8 `json:"type" binding:"required"`
Keyword string `form:"keyword"`
Type int8 `form:"type" binding:"required"`
}

type Controller struct {
Expand Down
Loading

0 comments on commit 5c6c936

Please sign in to comment.