不做大哥好多年 不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • Langchain
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核

逍遥子

不做大哥好多年
首页
  • MySQL
  • Redis
  • Elasticsearch
  • Kafka
  • Etcd
  • MongoDB
  • TiDB
  • RabbitMQ
  • 01.Python
  • 02.GO
  • 03.Java
  • 04.业务问题
  • 05.关键技术
  • 06.项目常识
  • 10.计算机基础
  • Docker
  • K8S
  • 容器原理
  • Istio
  • 01.GO基础
  • 02.面向对象
  • 03.并发编程
  • 04.常用库
  • 05.数据库操作
  • 06.Beego框架
  • 07.Beego商城
  • 08.GIN框架
  • 09.GIN论坛
  • 10.微服务
  • 01.Python基础
  • 02.Python模块
  • 03.Django
  • 04.Flask
  • 05.SYL
  • 06.Celery
  • 10.微服务
  • 01.Java基础
  • 02.面向对象
  • 03.Java进阶
  • 04.Web基础
  • 05.Spring框架
  • 100.微服务
  • 数据结构
  • 算法基础
  • 算法题分类
  • 前置知识
  • PyTorch
  • Langchain
  • Linux基础
  • Linux高级
  • Nginx
  • KeepAlive
  • ansible
  • zabbix
  • Shell
  • Linux内核
  • GO基础

  • 面向对象

  • 并发编程

  • 常用库

  • 数据库操作

  • Beego框架

  • Beego商城

  • GIN框架

  • GIN论坛

    • 01.gin源码
    • 02.go内置sql模块
    • 03.sqlx库
    • 04.sqlx-In函数
    • 05.gin中使用zap日志
    • 06.gin优雅地关机
    • 07.大型WEB项目CLD分层
    • 08.web脚手架
    • 09.validator校验器
    • 10.雪花算法用户ID
    • 11.返回格式封装
    • 12.注册模块
    • 13.JWT
    • 14.登录模块
    • 15.社区接口开发
      • 01.创建表结构
        • 1.1 user用户表
        • 1.2 community分类表
        • 1.3 post文章表
      • 02.CLD
        • 2.1 routes/routes.go 处理路由 C
        • 2.2 controller/community.go 参数校验 C
        • 2.3 logic/community.go 业务逻辑 L
        • 2.4 models/community.go 表模型 D
        • 2.5 dao/mysql/community.go 数据库 D
      • 03.测试接口
        • 3.1 postman测试
        • 3.2 返回结果
    • 16.认证中间件
    • 100.docker部署
    • 101.nginx部署
  • 微服务

  • 设计模式

  • Go
  • GIN论坛
xiaonaiqiang
2021-06-15
目录

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

# 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

# 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

# 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.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 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.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.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

# 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
上次更新: 2024/3/13 15:35:10
14.登录模块
16.认证中间件

← 14.登录模块 16.认证中间件→

最近更新
01
05.快递Agent智能体
06-04
02
200.AI Agent核心概念
06-04
03
105.Agent智能体梳理
06-04
更多文章>
Theme by Vdoing | Copyright © 2019-2025 逍遥子 技术博客 京ICP备2021005373号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式