提交 84efd7b6 编写于 作者: 5 520MianXiangDuiXiang520

add tag api

上级 36164634
......@@ -10,8 +10,11 @@
|----|----|----|
|api/friendship/list|获取所有友链列表|[api/friendship/list](#apifriendshiplist)|
|api/friendship/add| 添加友链 |[api/friendship/add](#apifriendshipadd) |
|api/friendship/delete| 删除友链| [api/friendship/delete](#apifriendshipdelete)|
|api/tag/list| 获取所有标签 | [api/tag/list](#apitaglist)|
|api/tag/add|添加标签 | [api/tag/add](#apitagadd)|
### 接口详情
## 接口详情
### api/friendship
......@@ -74,4 +77,88 @@
"msg": "ok"
}
}
```
\ No newline at end of file
```
#### api/friendship/delete
请求
```json
{
"id": 13
}
```
响应
```json
{
"header": {
"code": 200,
"msg": "ok"
}
}
```
### api/tag
#### api/tag/list
请求
```json
{}
```
响应
```json
{
"header": {
"code": 200,
"msg": "ok"
},
"total": 3,
"tags": [
{
"id": 3,
"name": "Java",
"create_time": "2020-08-01T23:49:59+08:00",
"article_total": 10
},
{
"id": 1,
"name": "Golang",
"create_time": "2020-08-12T23:46:13+08:00",
"article_total": 10
},
{
"id": 2,
"name": "Python",
"create_time": "2020-08-12T23:46:41+08:00",
"article_total": 10
},
]
}
```
#### api/tag/add
请求
```json
{
"name": "设计模式"
}
```
响应
```json
{
"header": {
"code": 200,
"msg": "ok"
}
}
```
......@@ -21,3 +21,11 @@ func FriendAddCheck(ctx *gin.Context, req interface{}) (interface{}, error) {
}
return nil, nil
}
func FriendDeleteCheck(ctx *gin.Context, req interface{}) (interface{}, error) {
reqD := req.(*message.FriendDeleteReq)
if reqD.ID <= 0 {
return consts.ParamErrorRespHeader, errors.New("请求参数错误")
}
return nil, nil
}
package check
import (
"JuneGoBlog/src/consts"
"JuneGoBlog/src/message"
"errors"
"github.com/gin-gonic/gin"
"log"
)
func TagListCheck(ctx *gin.Context, req interface{}) (interface{}, error) {
return nil, nil
}
func TagAddCheck(ctx *gin.Context, req interface{}) (interface{}, error) {
reqA := req.(*message.TagAddReq)
if reqA.TagName == "" {
log.Printf("Check Add Tag Error! name = [%v]\n", reqA.TagName)
return consts.ParamErrorRespHeader, errors.New("")
}
return nil, nil
}
package dao
import "errors"
var (
NoRecordError = errors.New("NoRecordError")
)
package dao
import "log"
import (
"log"
)
// 查询所有的友链信息
func QueryAllFriendLink(fl *[]FriendShipLink) error {
......@@ -24,4 +27,25 @@ func AddFriendship(fs *FriendShipLink) error {
}()
err = tx.Create(&fs).Error
return err
}
func DeleteFriendshipByID(fid int) error {
tx := DB.Begin()
var err error
defer func() {
if err != nil {
tx.Rollback()
log.Printf("DeleteFriendshipByID DB.Begin() Error, ID is [%v]\n\n", err)
}
tx.Commit()
}()
deleteFri := tx.Model(&FriendShipLink{}).Where("id = ?", fid)
var count int
deleteFri.Count(&count)
if count <= 0 {
log.Printf("No This Friendship")
return NoRecordError
}
return tx.Where("id = ?", fid).Delete(&FriendShipLink{}).Error
}
\ No newline at end of file
......@@ -11,4 +11,8 @@ func TestQueryAllFriendLink(t *testing.T) {
if len(fl) <= 0 {
t.Error("NO Result Query!!")
}
}
func TestAddFriendship(t *testing.T) {
}
\ No newline at end of file
package dao
// 单个标签信息
type TagInfo struct {
Name string `form:"name"` // 标签名
CreateTime int `form:"createTime"` // 创建时间(时间戳)
ArticleAmount int `form:"articleAmount"` // 该标签下的文章数
}
// 单个文章信息
type ArticleInfo struct {
Name string `json:"name"` // 文章名
CreateTime int `json:"createTime"` // 创建时间(时间戳)
Abstract string `json:"abstract"` // 摘要
Text string `json:"text"` // 正文
ReadingAmount int `json:"readingAmount"` // 阅读量
Tags []TagInfo `json:"tags"` // 标签信息
}
import "time"
// 友链信息
type FriendShipLink struct {
ID int `json:"id" gorm:"column:id"`
SiteName string `json:"siteName" gorm:"column:siteName"` // 网站名
SiteLink string `json:"link" gorm:"column:siteLink"` // 链接
ImgLink string `json:"imgLink" gorm:"column:imgLink"` // 网站图标链接
Intro string `json:"intro" gorm:"column:intro"` // 简介
ID int `json:"id" gorm:"column:id"`
SiteName string `json:"siteName" gorm:"column:siteName"` // 网站名
SiteLink string `json:"link" gorm:"column:siteLink"` // 链接
ImgLink string `json:"imgLink" gorm:"column:imgLink"` // 网站图标链接
Intro string `json:"intro" gorm:"column:intro"` // 简介
}
func (FriendShipLink) TableName() string {
return "friendship"
}
// 文章标签
type Tag struct {
ID int `json:"id" gorm:"column:id"`
Name string `json:"name" gorm:"column:name"`
CreateTime time.Time `json:"create_time" gorm:"column:create_time"`
}
func (Tag) TableName() string {
return "tags"
}
\ No newline at end of file
package dao
import (
"log"
"time"
)
// 查询所有标签,按创建时间排序
func QueryAllTagsOrderByTime(resp *[]Tag) error {
return DB.Order("create_time").Find(&resp).Error
}
func AddTag(name string) error {
tx := DB.Begin()
if err := tx.Error; err != nil {
log.Printf("AddTag Begin Error, Name = [%v], : [%v]\n",name, err)
return err
}
var err error
defer func() {
if err != nil {
tx.Rollback()
log.Printf("AddTag Error, Callbacked... tagName = [%v]; [%v]", name, err)
}
tx.Commit()
}()
err = tx.Create(&Tag{
Name: name,
CreateTime: time.Now(),
}).Error
return err
}
package message
import (
"JuneGoBlog/src/dao"
)
type ArticleListResp struct {
Header BaseRespHeader `json:"header"` // 响应头
ArticleList []dao.ArticleInfo `json:"articleList"` // 文章列表
Total int `json:"total"` // 将返回的文章总数
}
// 请求文章列表格式
type ArticleListReq struct {
Page int `form:"page"` // 页数
PageSize int `form:"pageSize"` // 每页请求的文章数量
Tag string `form:"tag"` // 标签
}
//type ArticleListResp struct {
// Header BaseRespHeader `json:"header"` // 响应头
// ArticleList []dao.ArticleInfo `json:"articleList"` // 文章列表
// Total int `json:"total"` // 将返回的文章总数
//}
//
//// 请求文章列表格式
//type ArticleListReq struct {
// Page int `form:"page"` // 页数
// PageSize int `form:"pageSize"` // 每页请求的文章数量
// Tag string `form:"tag"` // 标签
//}
......@@ -3,20 +3,30 @@ package message
import "JuneGoBlog/src/dao"
type FriendShipListResp struct {
Header BaseRespHeader `json:"header"` // 响应头
Total int `json:"total"` // 友链总数
FriendShipList []dao.FriendShipLink `json:"friendShipList"` // 友链列表
Header BaseRespHeader `json:"header"` // 响应头
Total int `json:"total"` // 友链总数
FriendShipList []dao.FriendShipLink `json:"friendShipList"` // 友链列表
}
// 添加友链的请求头格式
type FriendAddReq struct {
SiteName string `form:"siteName"` // 网站名称(必填)
SiteLink string `form:"siteLink"` // 网站链接(必填)
ImgLink string `form:"imgLink"` // 网站图标链接
Intro string `form:"intro"` // 网站简介
SiteName string `form:"siteName" json:"siteName"` // 网站名称(必填)
SiteLink string `form:"siteLink" json:"siteLink"` // 网站链接(必填)
ImgLink string `form:"imgLink" json:"imgLink"` // 网站图标链接
Intro string `form:"intro" json:"intro"` // 网站简介
}
// 添加友链响应格式
type FriendAddResp struct {
Header BaseRespHeader `json:"header"`
}
// 删除友链请求格式
type FriendDeleteReq struct {
ID int `form:"id" json:"id"` // 要删除的友链ID
}
// 删除友链响应格式
type FriendDeleteResp struct {
Header BaseRespHeader `json:"header"`
}
\ No newline at end of file
package message
import "JuneGoBlog/src/dao"
// api/tag/list 请求格式
type TagListReq struct {}
// 标签信息
type TagInfo struct {
dao.Tag
ArticleTotal int `json:"article_total"` // 文章数
}
// api/tag/list 响应格式
type TagListResp struct {
Header BaseRespHeader `json:"header"` // 响应头
Total int `json:"total"` // 标签总数
Tags []TagInfo `json:"tags"` // 标签列表
}
type TagAddReq struct {
TagName string `form:"name" json:"name"` // 标签名
}
type TagAddResp struct {
Header BaseRespHeader `json:"header"`
}
\ No newline at end of file
package routes
import (
"JuneGoBlog/src/check"
"JuneGoBlog/src/message"
"JuneGoBlog/src/server"
"JuneGoBlog/src/utils"
"github.com/gin-gonic/gin"
)
func ArticleRegister(rg *gin.RouterGroup) {
rg.POST("/list", utils.EasyHandler(check.ListCheck, server.ListLogic, &message.ArticleListReq{}))
rg.POST("/add", utils.EasyHandler(check.ListCheck, server.ListLogic, message.ArticleListReq{}))
rg.POST("/update", utils.EasyHandler(check.ListCheck, server.ListLogic, message.ArticleListReq{}))
rg.POST("/delete", utils.EasyHandler(check.ListCheck, server.ListLogic, message.ArticleListReq{}))
rg.POST("/detail", utils.EasyHandler(check.ListCheck, server.ListLogic, message.ArticleListReq{}))
//rg.POST("/list", utils.EasyHandler(check.ListCheck, server.ListLogic, &message.ArticleListReq{}))
//rg.POST("/add", utils.EasyHandler(check.ListCheck, server.ListLogic, message.ArticleListReq{}))
//rg.POST("/update", utils.EasyHandler(check.ListCheck, server.ListLogic, message.ArticleListReq{}))
//rg.POST("/delete", utils.EasyHandler(check.ListCheck, server.ListLogic, message.ArticleListReq{}))
//rg.POST("/detail", utils.EasyHandler(check.ListCheck, server.ListLogic, message.ArticleListReq{}))
}
......@@ -9,6 +9,10 @@ import (
)
func FriendShipRoutes (rg *gin.RouterGroup) {
rg.POST("list/", utils.EasyHandler(check.FriendShipListCheck, server.FriendShipListLogic, &message.FriendAddReq{}))
rg.POST("add/", utils.EasyHandler(check.FriendAddCheck, server.FriendAddLogic, &message.FriendAddReq{}))
}
rg.POST("list/", utils.EasyHandler(check.FriendShipListCheck,
server.FriendShipListLogic, &message.FriendAddReq{}))
rg.POST("add/", utils.EasyHandler(check.FriendAddCheck,
server.FriendAddLogic, &message.FriendAddReq{}))
rg.POST("delete/", utils.EasyHandler(check.FriendShipListCheck,
server.FriendDeleteLogic, &message.FriendDeleteReq{}))
}
package routes
import (
"JuneGoBlog/src/check"
"JuneGoBlog/src/message"
"JuneGoBlog/src/server"
"JuneGoBlog/src/utils"
"github.com/gin-gonic/gin"
)
func TagRegister(rg *gin.RouterGroup) {
rg.POST("/list", func(context *gin.Context) {
context.JSON(200, "ok")
})
rg.POST("/list", utils.EasyHandler(check.TagListCheck,
server.TagListLogin, &message.TagListReq{}))
rg.POST("/add", utils.EasyHandler(check.TagAddCheck,
server.TagAddLogin, &message.TagAddReq{}))
rg.POST("/delete")
}
package server
import (
"JuneGoBlog/src/consts"
"JuneGoBlog/src/dao"
"JuneGoBlog/src/message"
"github.com/gin-gonic/gin"
)
func ListLogic(ctx *gin.Context, req interface{}) interface{} {
var resp *message.ArticleListResp
_ = ctx.ShouldBindJSON(&req)
// TODO: dao
articleList := make([]dao.ArticleInfo, 0)
articleInfo := dao.ArticleInfo{Name: "Go 内存模型",
CreateTime: 12345678, Abstract: "xxxx", Text: "Hello Go", ReadingAmount: 10,
Tags: []dao.TagInfo{}}
articleList = append(articleList, articleInfo)
resp = &message.ArticleListResp{Header: consts.SuccessRespHeader, ArticleList: articleList, Total: 1}
return resp
return nil
}
......@@ -43,3 +43,19 @@ func FriendAddLogic(ctx *gin.Context, re interface{}) interface{} {
resp.Header = consts.SuccessRespHeader
return resp
}
// 删除友链
func FriendDeleteLogic(ctx *gin.Context, req interface{}) interface{} {
reqD := req.(*message.FriendDeleteReq)
var resp message.FriendAddResp
if err := dao.DeleteFriendshipByID(reqD.ID); err != nil {
if err == dao.NoRecordError {
return consts.ParamErrorRespHeader
}
log.Printf("DELETE Friendship ERROR ! id = [%d]", reqD.ID)
return consts.SystemErrorRespHeader
}
resp.Header = consts.SuccessRespHeader
return resp
}
package server
import (
"JuneGoBlog/src/consts"
"JuneGoBlog/src/dao"
"JuneGoBlog/src/message"
"github.com/gin-gonic/gin"
"log"
)
func TagListLogin(ctx *gin.Context, req interface{}) interface{} {
var resp message.TagListResp
tags := make([]dao.Tag, 0)
if err:= dao.QueryAllTagsOrderByTime(&tags); err != nil {
log.Printf("QueryAllTagsOrderByTime Error!!!")
return consts.SystemErrorRespHeader
}
tagInfos := make([]message.TagInfo, 0)
for _, tag := range tags {
tagInfos = append(tagInfos, message.TagInfo{
// TODO: 从 article 表中查
ArticleTotal: 10,
Tag: tag,
})
}
resp.Tags = tagInfos
resp.Total = len(tagInfos)
resp.Header = consts.SuccessRespHeader
return resp
}
func TagAddLogin(ctx *gin.Context, req interface{}) interface{} {
reqA := req.(*message.TagAddReq)
var resp message.TagAddResp
if err := dao.AddTag(reqA.TagName); err != nil {
log.Printf("Add Tag Error, name = [%s]\n", reqA.TagName)
return consts.SystemErrorRespHeader
}
resp.Header = consts.SuccessRespHeader
return resp
}
......@@ -13,7 +13,7 @@ type LogicFunc func(ctx *gin.Context, req interface{}) interface{}
func EasyHandler(cf CheckFunc, lf LogicFunc, req interface{}) gin.HandlerFunc {
return func(context *gin.Context) {
var resp interface{}
if err := context.BindJSON(req); err != nil {
if err := context.ShouldBindJSON(&req); err != nil {
log.Printf("EasyHandler: BindJSON ERROR!!!")
resp = consts.ParamErrorRespHeader
} else {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册