155 lines
4.0 KiB
Go
155 lines
4.0 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
type IDS []int
|
|
|
|
// 合并为以逗号分隔的字符串
|
|
func (ids IDS) ToString() (str string) {
|
|
return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(ids)), ","), "[]")
|
|
}
|
|
|
|
type ImageList []Image
|
|
|
|
// 按照ID排序
|
|
func (image *ImageList) SortByIDList(id_list []string) {
|
|
var sortedImageList ImageList
|
|
for _, id := range id_list {
|
|
id_number, _ := strconv.Atoi(id)
|
|
for _, image := range *image {
|
|
if image.ID == id_number {
|
|
sortedImageList = append(sortedImageList, image)
|
|
}
|
|
}
|
|
}
|
|
*image = sortedImageList
|
|
}
|
|
|
|
// 取到所有的文章ID, 去除重复
|
|
func (images *ImageList) ToAllArticleID() (uniqueIds IDS) {
|
|
article_ids := make(map[int]bool)
|
|
for _, image := range *images {
|
|
article_ids[image.ArticleID] = true
|
|
}
|
|
for id := range article_ids {
|
|
uniqueIds = append(uniqueIds, id)
|
|
}
|
|
return uniqueIds
|
|
}
|
|
|
|
// 取到所有的用户ID, 去除重复
|
|
func (images *ImageList) ToAllUserID() (uniqueIds IDS) {
|
|
user_ids := make(map[int]bool)
|
|
for _, image := range *images {
|
|
user_ids[image.UserID] = true
|
|
}
|
|
for id := range user_ids {
|
|
uniqueIds = append(uniqueIds, id)
|
|
}
|
|
return uniqueIds
|
|
}
|
|
|
|
// 为每个图像设置用户信息
|
|
func (images *ImageList) SetUser(userList []User) {
|
|
for i, image := range *images {
|
|
for _, user := range userList {
|
|
if image.UserID == user.ID {
|
|
(*images)[i].User = user
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 为每个图像设置文章信息
|
|
func (images *ImageList) SetArticle(articleList []Article) {
|
|
for i, image := range *images {
|
|
for _, article := range articleList {
|
|
if image.ArticleID == article.ID {
|
|
(*images)[i].Article = article
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
type Image struct {
|
|
ID int `json:"id" db:"id"`
|
|
Width int `json:"width" db:"width"`
|
|
Height int `json:"height" db:"height"`
|
|
Content string `json:"content" db:"content"`
|
|
Remark string `json:"remark" db:"remark"`
|
|
Description string `json:"description" db:"description"`
|
|
Tags string `json:"tags" db:"tags"`
|
|
Rank string `json:"rank" db:"rank"`
|
|
CommentNum int `json:"comment_num" db:"comment_num"`
|
|
PraiseCount int `json:"praise_count" db:"praise_count"`
|
|
CollectCount int `json:"collect_count" db:"collect_count"`
|
|
ArticleID int `json:"article_id" db:"article_id"`
|
|
UserID int `json:"user_id" db:"user_id"`
|
|
CreateTime time.Time `json:"create_time" db:"create_time"`
|
|
UpdateTime time.Time `json:"update_time" db:"update_time"`
|
|
Text TextList `json:"text" db:"text"`
|
|
User User `json:"user" db:"-"`
|
|
Article Article `json:"article" db:"-"`
|
|
}
|
|
|
|
type TextList []struct {
|
|
Text string `json:"text"`
|
|
Confidence float64 `json:"confidence"`
|
|
Coordinate [][]float64 `json:"coordinate"`
|
|
}
|
|
|
|
func (a *TextList) Scan(value interface{}) error {
|
|
return json.Unmarshal(value.([]byte), a)
|
|
}
|
|
|
|
type User struct {
|
|
ID int `json:"id" db:"id"`
|
|
UserName *string `json:"user_name" db:"user_name"`
|
|
Avatar *string `json:"avatar" db:"avatar"`
|
|
Rank *string `json:"rank" db:"rank"`
|
|
CreateTime time.Time `json:"create_time" db:"create_time"`
|
|
UpdateTime time.Time `json:"update_time" db:"update_time"`
|
|
}
|
|
|
|
type Article struct {
|
|
ID int `json:"id" db:"id"`
|
|
Title string `json:"title" db:"title"`
|
|
Tags string `json:"tags" db:"tags"`
|
|
CreateTime time.Time `json:"create_time" db:"create_time"`
|
|
UpdateTime time.Time `json:"update_time" db:"update_time"`
|
|
}
|
|
|
|
type Category struct {
|
|
ID int
|
|
Title string
|
|
Keyword string
|
|
ParentID int
|
|
CreateTime time.Time
|
|
UpdateTime time.Time
|
|
Status int
|
|
Content string
|
|
Sort int
|
|
Image string
|
|
ImageNum int
|
|
ArticleNum int
|
|
}
|
|
|
|
// 输入配置
|
|
type ConfigMysql struct {
|
|
Host string
|
|
Port int
|
|
Database string
|
|
UserName string
|
|
Password string
|
|
}
|
|
|
|
type Config struct {
|
|
Mysql ConfigMysql
|
|
}
|