提交 89d38673 编写于 作者: 5 520MianXiangDuiXiang520

add api/friendship/list

上级 d9f0bc98
# JuneGoBlog
<centre>
<center>
<a href="https://travis-ci.org/">
<img src="https://travis-ci.com/520MianXiangDuiXiang520/JuneGoBlog.svg?token=7mqBvrpUUzHXp1nyitHA&branch=master">
</a>
......@@ -9,4 +9,47 @@
</a>
<a href='https://coveralls.io/github/520MianXiangDuiXiang520/JuneGoBlog?branch=master'><img src='https://coveralls.io/repos/github/520MianXiangDuiXiang520/JuneGoBlog/badge.svg?branch=master' alt='Coverage Status' /></a>
</centre>
\ No newline at end of file
</center>
## 接口列表
|路径|描述|详情|
|----|----|----|
|api/friendship/list|获取所有友链列表|[api/friendship/list](#apifriendshiplist)|
### 接口详情
#### api/friendship/list
请求:
1. Method: POST
2. 请求参数: 无
响应:
```json
{
"header": {
"code": 200,
"msg": "ok"
},
"total": 2,
"friendShipList": [
{
"id": 1,
"siteName": "DeepBlue的小站",
"link": "http://dlddw.xyz/",
"imgLink": "https://junebao.top/static/image/friends/dlddw.png",
"intro": ""
},
{
"id": 2,
"siteName": "异国迷宫的十字路口",
"link": "https://blog.fivezha.cn/",
"imgLink": "https://blog.fivezha.cn/img/avatar.png",
"intro": ""
}
]
}
```
......@@ -3,6 +3,7 @@ package main
import (
"JuneGoBlog/src/dao"
"github.com/gin-gonic/gin"
"log"
)
func init() {
......@@ -16,5 +17,5 @@ func main() {
engine := gin.Default()
defer engine.Run()
Register(engine)
defer dao.DB.Close()
defer log.Println("end。。。。")
}
package check
import (
"JuneGoBlog/src/utils"
"github.com/gin-gonic/gin"
"net/http"
)
func FriendShipListCheck(ctx *gin.Context) (utils.RespHeader, error) {
// 无请求参数,不需要校验
return http.StatusOK, nil
}
......@@ -6,3 +6,4 @@ import (
)
var SuccessHead = message.BaseRespHeader{Code: http.StatusOK, Msg: "ok"}
var SystemError = message.BaseRespHeader{Code: http.StatusInternalServerError, Msg: "系统异常"}
package dao
// 查询所有的友链信息
func QueryAllFriendLink(fl *[]FriendShipLink) error {
return DB.Find(&fl).Error
}
......@@ -2,9 +2,6 @@ package dao
import (
"JuneGoBlog/src/utils"
"database/sql"
"errors"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"log"
......@@ -34,5 +31,6 @@ func InitDB() error {
log.Println("Open DB Error!!!")
return err
}
log.Println("============== MySQL Connect Success ==============")
return nil
}
......@@ -2,17 +2,31 @@ package dao
// 单个标签信息
type TagInfo struct {
Name string `form:"name"` // 标签名
CreateTime int `form:"createTime"` // 创建时间(时间戳)
ArticleAmount int `form:"articleAmount"` // 该标签下的文章数
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"` // 标签信息
Name string `json:"name"` // 文章名
CreateTime int `json:"createTime"` // 创建时间(时间戳)
Abstract string `json:"abstract"` // 摘要
Text string `json:"text"` // 正文
ReadingAmount int `json:"readingAmount"` // 阅读量
Tags []TagInfo `json:"tags"` // 标签信息
}
// 友链信息
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"` // 简介
}
func (FriendShipLink) TableName() string {
return "friendship"
}
\ No newline at end of file
package message
import "JuneGoBlog/src/dao"
type FriendShipListResp struct {
Header BaseRespHeader `json:"header"` // 响应头
Total int `json:"total"` // 友链总数
FriendShipList []dao.FriendShipLink `json:"friendShipList"` // 友链列表
}
package routes
import (
"JuneGoBlog/src/check"
"JuneGoBlog/src/server"
"JuneGoBlog/src/utils"
"github.com/gin-gonic/gin"
)
func FriendShipRoutes (rg *gin.RouterGroup) {
rg.POST("list/", utils.EasyHandler(check.FriendShipListCheck, server.FriendShipListLogic))
}
package server
import (
"JuneGoBlog/src/consts"
"JuneGoBlog/src/dao"
"JuneGoBlog/src/message"
"JuneGoBlog/src/utils"
"github.com/gin-gonic/gin"
"log"
)
func FriendShipListLogic(ctx *gin.Context) utils.RespHeader {
resp := message.FriendShipListResp{}
// 从数据库中读取所有的 friendShip Link 信息
friendshipList := make([]dao.FriendShipLink, 0)
if err := dao.QueryAllFriendLink(&friendshipList); err != nil {
log.Printf("FriendShipListLogic dao.DB.Find ERROR [%v]\n", err)
return consts.SystemError
}
resp.Header = consts.SuccessHead
resp.FriendShipList = friendshipList
resp.Total = len(friendshipList)
return resp
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册