go-zero docker-compose 搭建课件服务(二):编写 courseware rpc 服务
作者:六月的
- 2022-10-19 上海
本文字数:3405 字
阅读完需:约 1 分钟
0、转载
go-zero docker-compose 搭建课件服务(二):编写courseware rpc服务
0.1 源码地址
https://github.com/liuyuede123/go-zero-courseware
1、创建 model
mysql 中创建数据库
create database go_zero_courseware default character set utf8mb4 collate utf8mb4_unicode_ci;
复制代码
创建 sql 文件
touch courseware/rpc/model/courseware.sql
复制代码
CREATE TABLE `courseware`
(
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`code` varchar(255) NOT NULL DEFAULT '' COMMENT '编号',
`name` varchar(255) NOT NULL DEFAULT '' COMMENT '用户姓名',
`type` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '课件类型 1-h5 2-scorm 3-多媒体多章节',
`is_delete` tinyint(4) NOT NULL DEFAULT 0 COMMENT '是否删除 0-未删除 1-已删除',
`create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `idx_mobile_unique` (`mobile`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
复制代码
生成 model
# 到对应model目录下
cd courseware/rpc/model
# 生成model
goctl model mysql ddl -src="./*.sql" -dir="./" -c
复制代码
2、生成 rpc 文件
需要提前安装了 protoc & protoc-gen-go,自行搜索安装
# 回到courseware rpc目录 ./go-zero-courseware/courseware/rpc
cd ../
goctl rpc protoc courseware.proto --go_out=. --go-grpc_out=. --zrpc_out=.
复制代码
3、初始化 module
# 路径 courseware
go mod init
go mod tidy
复制代码
4、添加数据库源
courseware.yaml 中增加 mysql 配置
# 路径
# go-zero-courseware/courseware/rpc/etc/courseware.yaml
Name: courseware.rpc
ListenOn: 0.0.0.0:9400
Etcd:
Hosts:
- etcd:2379
Key: courseware.rpc
# mysql数据源
Mysql:
DataSource: root:liufutian@tcp(192.168.0.110:3306)/go_zero_courseware?charset=utf8mb4&parseTime=true&loc=Asia%2FShanghai
# redis缓存
CacheRedis:
- Host: 192.168.0.110:6379
Pass:
复制代码
config.go 文件中增加 mysql 配置项
// 路径 courseware/rpc/internal/config/config.go
package config
import (
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/zrpc"
)
type Config struct {
zrpc.RpcServerConf
Mysql struct {
DataSource string
}
CacheRedis cache.CacheConf
}
复制代码
servicecontext.go 中增加 CoursewareModel 配置
// 路径 courseware/rpc/internal/svc/servicecontext.go
package svc
import (
"github.com/zeromicro/go-zero/core/stores/sqlx"
"go-zero-courseware/courseware/rpc/internal/config"
"go-zero-courseware/courseware/rpc/model"
)
type ServiceContext struct {
Config config.Config
CoursewareModel model.CoursewareModel
}
func NewServiceContext(c config.Config) *ServiceContext {
conn := sqlx.NewMysql(c.Mysql.DataSource)
return &ServiceContext{
Config: c,
CoursewareModel: model.NewCoursewareModel(conn, c.CacheRedis),
}
}
复制代码
5、编写增删改查逻辑
courseware/rpc/internal/logic/addlogic.go 修改如下
package logic
import (
"context"
"go-zero-courseware/courseware/rpc/courseware"
"go-zero-courseware/courseware/rpc/internal/svc"
"go-zero-courseware/courseware/rpc/model"
"google.golang.org/grpc/status"
"github.com/zeromicro/go-zero/core/logx"
)
type AddLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
return &AddLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *AddLogic) Add(in *courseware.AddRequest) (*courseware.AddResponse, error) {
_, err := l.svcCtx.CoursewareModel.FindOneByCode(l.ctx, in.Code)
if err == nil {
return nil, status.Error(5000, "课件已存在")
}
if err == model.ErrNotFound {
newCourseware := model.Courseware{
Code: in.Code,
Name: in.Name,
Type: in.Type,
}
_, err = l.svcCtx.CoursewareModel.Insert(l.ctx, &newCourseware)
if err != nil {
return nil, status.Error(500, err.Error())
}
return &courseware.AddResponse{}, nil
}
return nil, status.Error(500, err.Error())
}
复制代码
courseware/rpc/internal/logic/deletelogic.go 修改如下
package logic
import (
"context"
"google.golang.org/grpc/status"
"go-zero-courseware/courseware/rpc/courseware"
"go-zero-courseware/courseware/rpc/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type DeleteLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteLogic {
return &DeleteLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *DeleteLogic) Delete(in *courseware.DeleteRequest) (*courseware.DeleteResponse, error) {
err := l.svcCtx.CoursewareModel.Delete(l.ctx, in.Id)
if err != nil {
return nil, status.Error(500, err.Error())
}
return &courseware.DeleteResponse{}, nil
}
复制代码
courseware/rpc/internal/logic/getlogic.go 修改如下
package logic
import (
"context"
"google.golang.org/grpc/status"
"go-zero-courseware/courseware/rpc/courseware"
"go-zero-courseware/courseware/rpc/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type GetLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetLogic {
return &GetLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetLogic) Get(in *courseware.GetRequest) (*courseware.GetResponse, error) {
cw, err := l.svcCtx.CoursewareModel.FindOne(l.ctx, in.Id)
if err != nil {
return nil, status.Error(5000, "课件不存在")
}
return &courseware.GetResponse{
Id: cw.Id,
Code: cw.Code,
Name: cw.Name,
Type: cw.Type,
}, nil
}
复制代码
courseware/rpc/internal/logic/updatelogic.go 修改如下:
package logic
import (
"context"
"google.golang.org/grpc/status"
"go-zero-courseware/courseware/rpc/courseware"
"go-zero-courseware/courseware/rpc/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateLogic {
return &UpdateLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UpdateLogic) Update(in *courseware.UpdateRequest) (*courseware.UpdateResponse, error) {
cw, err := l.svcCtx.CoursewareModel.FindOne(l.ctx, in.Id)
if err != nil {
return nil, status.Error(5000, "课件不存在")
}
_, err = l.svcCtx.CoursewareModel.FindOneByCode(l.ctx, in.Code)
if err == nil {
return nil, status.Error(5000, "课件编号已存在")
}
if in.Code != "" {
cw.Code = in.Code
}
if in.Name != "" {
cw.Name = in.Name
}
if in.Type > 0 {
cw.Type = in.Type
}
err = l.svcCtx.CoursewareModel.Update(l.ctx, cw)
if err != nil {
return nil, status.Error(500, err.Error())
}
return &courseware.UpdateResponse{}, nil
}
复制代码
划线
评论
复制
发布于: 刚刚阅读数: 3
六月的
关注
还未添加个人签名 2019-07-23 加入
还未添加个人简介
评论