go-zero docker-compose 搭建课件服务(二):编写courseware rpc服务

2年前 (2022) 程序员胖胖胖虎阿
171 0 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
}

相关文章

暂无评论

暂无评论...