15.社区接口开发
# 01.创建表结构
# 1.1 user用户表
CREATE TABLE `user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` bigint(20) NOT NULL,
`username` varchar(64) COLLATE utf8mb4_general_ci NOT NULL,
`password` varchar(64) COLLATE utf8mb4_general_ci NOT NULL,
`email` varchar(64) COLLATE utf8mb4_general_ci,
`gender` tinyint(4) NOT NULL DEFAULT '0',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_username` (`username`) USING BTREE,
UNIQUE KEY `idx_user_id` (`user_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 1.2 community分类表
create table community
(
id int auto_increment primary key,
community_id int unsigned not null,
community_name varchar(128) not null,
introduction varchar(256) not null,
create_time timestamp default CURRENT_TIMESTAMP not null,
update_time timestamp default CURRENT_TIMESTAMP not null on update CURRENT_TIMESTAMP,
constraint idx_community_id unique (community_id),
constraint idx_community_name unique (community_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
INSERT INTO community (id, community_id, community_name, introduction, create_time, update_time) VALUES (1, 1, 'Go', 'Golang', '2016-11-01 08:10:10', '2016-11-01 08:10:10');
INSERT INTO community (id, community_id, community_name, introduction, create_time, update_time) VALUES (2, 2, 'leetcode', '刷题刷题刷题', '2020-01-01 08:00:00', '2020-01-01 08:00:00');
INSERT INTO community (id, community_id, community_name, introduction, create_time, update_time) VALUES (3, 3, 'CS:GO', 'Rush B。。。', '2018-08-07 08:30:00', '2018-08-07 08:30:00');
INSERT INTO community (id, community_id, community_name, introduction, create_time, update_time) VALUES (4, 4, 'LOL', '欢迎来到英雄联盟!', '2016-01-01 08:00:00', '2016-01-01 08:00:00');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1.3 post文章表
create table post
(
id bigint auto_increment primary key,
post_id bigint not null comment '帖子id',
title varchar(128) not null comment '标题',
content varchar(8192) not null comment '内容',
author_id bigint not null comment '作者的用户id',
community_id bigint not null comment '所属社区',
status tinyint default 1 not null comment '帖子状态',
create_time timestamp default CURRENT_TIMESTAMP null comment '创建时间',
update_time timestamp default CURRENT_TIMESTAMP null on update CURRENT_TIMESTAMP comment '更新时间',
constraint idx_post_id unique (post_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
create index idx_author_id on post (author_id);
create index idx_community_id on post (community_id);
INSERT INTO post (id, post_id, title, content, author_id, community_id, status, create_time, update_time) VALUES (1, 14283784123846656, '学习使我快乐', '只有学习才能变得更强', 28018727488323585, 1, 1, '2020-08-09 09:58:39', '2020-08-09 09:58:39');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 02.CLD
# 2.1 routes/routes.go 处理路由 C
package routes
import (
"gin_bbs/controllers"
"gin_bbs/logger"
"github.com/gin-gonic/gin"
)
func Setup(mode string) *gin.Engine {
if mode == gin.ReleaseMode {
gin.SetMode(gin.ReleaseMode)
}
r := gin.New()
r.Use(logger.GinLogger(), logger.GinRecovery(true))
r.POST("/test", controller.TestHandler)
v1 := r.Group("/api/v1")
// 注册
v1.POST("/signup", controller.SignUpHandler)
// 登录
v1.POST("/login", controller.LoginHandler)
v1.GET("/community", controller.CommunityHandler)
return r
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 2.2 controller/community.go 参数校验 C
package controller
import (
"bluebell/logic"
"strconv"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
// ---- 跟社区相关的 ----
func CommunityHandler(c *gin.Context) {
// 查询到所有的社区(community_id, community_name) 以列表的形式返回
data, err := logic.GetCommunityList()
if err != nil {
zap.L().Error("logic.GetCommunityList() failed", zap.Error(err))
ResponseError(c, CodeServerBusy) // 不轻易把服务端报错暴露给外面
return
}
ResponseSuccess(c, data)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 2.3 logic/community.go 业务逻辑 L
package logic
import (
"bluebell/dao/mysql"
"bluebell/models"
)
func GetCommunityList() ([]*models.Community, error) {
// 查数据库 查找到所有的community 并返回
return mysql.GetCommunityList()
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 2.4 models/community.go 表模型 D
package models
import "time"
type Community struct {
ID int64 `json:"id" db:"community_id"`
Name string `json:"name" db:"community_name"`
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 2.5 dao/mysql/community.go 数据库 D
package mysql
import (
"bluebell/models"
"database/sql"
"go.uber.org/zap"
)
func GetCommunityList() (communityList []*models.Community, err error) {
sqlStr := "select community_id, community_name from community"
if err := db.Select(&communityList, sqlStr); err != nil {
if err == sql.ErrNoRows {
zap.L().Warn("there is no community in db")
err = nil
}
}
return
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 03.测试接口
# 3.1 postman测试
- http://127.0.0.1:8080/api/v1/community
# 3.2 返回结果
{
"code": 1000,
"msg": "success",
"data": [
{
"id": 1,
"name": "Go"
},
{
"id": 2,
"name": "leetcode"
},
{
"id": 3,
"name": "CS:GO"
},
{
"id": 4,
"name": "LOL"
}
]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
上次更新: 2024/3/13 15:35:10