From 7ffbce7759ca42cf9069daaf09eea64601e925b1 Mon Sep 17 00:00:00 2001 From: eson <9673575+githubcontent@user.noreply.gitee.com> Date: Thu, 1 Jun 2023 16:19:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=80=E6=96=B0=E6=88=90=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- ddl/fs_canteen_type.sql | 10 ++ home-user-auth.api | 17 +-- .../internal/handler/gettypehandler.go | 28 +++++ home-user-auth/internal/handler/routes.go | 5 + home-user-auth/internal/logic/gettypelogic.go | 41 +++++++ home-user-auth/internal/svc/servicecontext.go | 12 +- home-user-auth/internal/types/types.go | 8 +- model/fscanteentypemodel.go | 24 ++++ model/fscanteentypemodel_gen.go | 112 ++++++++++++++++++ 10 files changed, 241 insertions(+), 20 deletions(-) create mode 100644 ddl/fs_canteen_type.sql create mode 100644 home-user-auth/internal/handler/gettypehandler.go create mode 100644 home-user-auth/internal/logic/gettypelogic.go create mode 100755 model/fscanteentypemodel.go create mode 100755 model/fscanteentypemodel_gen.go diff --git a/README.md b/README.md index 98498991..eee63e12 100644 --- a/README.md +++ b/README.md @@ -42,8 +42,8 @@ #### 使用说明 -1. xxxx -2. xxxx +1. goctl api go -api home-user-auth.api -dir home-user-auth +2. goctl model mysql ddl --src ./ddl/fs_canteen_type.sql --dir model/ 3. xxxx #### 参与贡献 diff --git a/ddl/fs_canteen_type.sql b/ddl/fs_canteen_type.sql new file mode 100644 index 00000000..ea03ec71 --- /dev/null +++ b/ddl/fs_canteen_type.sql @@ -0,0 +1,10 @@ +-- fusentest.fs_canteen_type definition + +CREATE TABLE `fs_canteen_type` ( + `id` int(10) NOT NULL AUTO_INCREMENT COMMENT 'ID', + `name` varchar(50) NOT NULL DEFAULT '' COMMENT '餐厅名字', + `sort` smallint(6) NOT NULL DEFAULT '0' COMMENT '排序', + `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '状态位 1启用0停用', + `ctime` int(11) NOT NULL DEFAULT '0' COMMENT '添加时间', + PRIMARY KEY (`id`) +) ENGINE=MyISAM AUTO_INCREMENT=24 DEFAULT CHARSET=utf8 COMMENT='餐厅类型表'; \ No newline at end of file diff --git a/home-user-auth.api b/home-user-auth.api index ccace38f..c371b7a7 100644 --- a/home-user-auth.api +++ b/home-user-auth.api @@ -11,17 +11,18 @@ import "basic.api" type request { // TODO: add members here and delete this comment - Name string `form:"name"` // parameters are auto validated + // Name string `form:"name"` // parameters are auto validated +} + +type GetTypeData { + Id int64 `db:"id" json:"key"` // ID + Name string `db:"name" json:"name"` // 餐厅名字 } service home-user-auth { @handler UserFontsHandler get /user/fonts(request) returns (response); -} - -// FsFonts 字体表 -type FsFonts { - Title string `json:"title" gorm:"column:title"` - LinuxFontname string `json:"linux_fontname" gorm:"column:linux_fontname"` - Link string `json:"link" gorm:"link"` + + @handler GetTypeHandler + get /user/get-type(request) returns (response); } \ No newline at end of file diff --git a/home-user-auth/internal/handler/gettypehandler.go b/home-user-auth/internal/handler/gettypehandler.go new file mode 100644 index 00000000..f4356da6 --- /dev/null +++ b/home-user-auth/internal/handler/gettypehandler.go @@ -0,0 +1,28 @@ +package handler + +import ( + "net/http" + + "fusenapi/home-user-auth/internal/logic" + "fusenapi/home-user-auth/internal/svc" + "fusenapi/home-user-auth/internal/types" + "github.com/zeromicro/go-zero/rest/httpx" +) + +func GetTypeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.Request + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := logic.NewGetTypeLogic(r.Context(), svcCtx) + resp, err := l.GetType(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/home-user-auth/internal/handler/routes.go b/home-user-auth/internal/handler/routes.go index e1b03406..e9d323fc 100644 --- a/home-user-auth/internal/handler/routes.go +++ b/home-user-auth/internal/handler/routes.go @@ -17,6 +17,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/user/fonts", Handler: UserFontsHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/user/get-type", + Handler: GetTypeHandler(serverCtx), + }, }, ) } diff --git a/home-user-auth/internal/logic/gettypelogic.go b/home-user-auth/internal/logic/gettypelogic.go new file mode 100644 index 00000000..9951edd2 --- /dev/null +++ b/home-user-auth/internal/logic/gettypelogic.go @@ -0,0 +1,41 @@ +package logic + +import ( + "context" + + "fusenapi/home-user-auth/internal/svc" + "fusenapi/home-user-auth/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type GetTypeLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetTypeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTypeLogic { + return &GetTypeLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetTypeLogic) GetType(req *types.Request) (resp *types.Response, err error) { + // todo: add your logic here and delete this line + data, err := l.svcCtx.FsCanteenTypeModel.FindGetType(l.ctx) + if err != nil { + logx.Error(err) + return + } + + // logx.Info(f) + resp = &types.Response{ + Code: 200, + Message: "success", + Data: data, + } + return +} diff --git a/home-user-auth/internal/svc/servicecontext.go b/home-user-auth/internal/svc/servicecontext.go index a46c275b..32da9ad5 100644 --- a/home-user-auth/internal/svc/servicecontext.go +++ b/home-user-auth/internal/svc/servicecontext.go @@ -8,14 +8,16 @@ import ( ) type ServiceContext struct { - Config config.Config - FsFontModel model.FsFontModel + Config config.Config + FsFontModel model.FsFontModel + FsCanteenTypeModel model.FsCanteenTypeModel } func NewServiceContext(c config.Config) *ServiceContext { - + conn := sqlx.NewMysql(c.DataSource) return &ServiceContext{ - Config: c, - FsFontModel: model.NewFsFontModel(sqlx.NewMysql(c.DataSource)), + Config: c, + FsFontModel: model.NewFsFontModel(conn), + FsCanteenTypeModel: model.NewFsCanteenTypeModel(conn), } } diff --git a/home-user-auth/internal/types/types.go b/home-user-auth/internal/types/types.go index 6e276548..c6f09bae 100644 --- a/home-user-auth/internal/types/types.go +++ b/home-user-auth/internal/types/types.go @@ -2,13 +2,11 @@ package types type Request struct { - Name string `form:"name"` // parameters are auto validated } -type FsFonts struct { - Title string `json:"title" gorm:"column:title"` - LinuxFontname string `json:"linux_fontname" gorm:"column:linux_fontname"` - Link string `json:"link" gorm:"link"` +type GetTypeData struct { + Id int64 `db:"id" json:"key"` // ID + Name string `db:"name" json:"name"` // 餐厅名字 } type Response struct { diff --git a/model/fscanteentypemodel.go b/model/fscanteentypemodel.go new file mode 100755 index 00000000..de8ff09f --- /dev/null +++ b/model/fscanteentypemodel.go @@ -0,0 +1,24 @@ +package model + +import "github.com/zeromicro/go-zero/core/stores/sqlx" + +var _ FsCanteenTypeModel = (*customFsCanteenTypeModel)(nil) + +type ( + // FsCanteenTypeModel is an interface to be customized, add more methods here, + // and implement the added methods in customFsCanteenTypeModel. + FsCanteenTypeModel interface { + fsCanteenTypeModel + } + + customFsCanteenTypeModel struct { + *defaultFsCanteenTypeModel + } +) + +// NewFsCanteenTypeModel returns a model for the database table. +func NewFsCanteenTypeModel(conn sqlx.SqlConn) FsCanteenTypeModel { + return &customFsCanteenTypeModel{ + defaultFsCanteenTypeModel: newFsCanteenTypeModel(conn), + } +} diff --git a/model/fscanteentypemodel_gen.go b/model/fscanteentypemodel_gen.go new file mode 100755 index 00000000..5ec08874 --- /dev/null +++ b/model/fscanteentypemodel_gen.go @@ -0,0 +1,112 @@ +// Code generated by goctl. DO NOT EDIT. + +package model + +import ( + "context" + "database/sql" + "fmt" + "strings" + + "github.com/zeromicro/go-zero/core/stores/builder" + "github.com/zeromicro/go-zero/core/stores/sqlc" + "github.com/zeromicro/go-zero/core/stores/sqlx" + "github.com/zeromicro/go-zero/core/stringx" +) + +var ( + fsCanteenTypeFieldNames = builder.RawFieldNames(&FsCanteenType{}) + fsCanteenTypeRows = strings.Join(fsCanteenTypeFieldNames, ",") + // fsCanteenTypeGetTypeRows = strings.Join(stringx.Remove(fsCanteenTypeFieldNames, "`id`", "`name`", "`sort`", "`created_at`", "`status`"), ",") + // fsCanteenTypeGetTypeRows = builder.RawFieldNames(&FsGetTypeCanteenType{}) + fsCanteenTypeRowsExpectAutoSet = strings.Join(stringx.Remove(fsCanteenTypeFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), ",") + fsCanteenTypeRowsWithPlaceHolder = strings.Join(stringx.Remove(fsCanteenTypeFieldNames, "`id`", "`create_at`", "`create_time`", "`created_at`", "`update_at`", "`update_time`", "`updated_at`"), "=?,") + "=?" +) + +type ( + fsCanteenTypeModel interface { + Insert(ctx context.Context, data *FsCanteenType) (sql.Result, error) + FindOne(ctx context.Context, id int64) (*FsCanteenType, error) + Update(ctx context.Context, data *FsCanteenType) error + Delete(ctx context.Context, id int64) error + + FindGetType(ctx context.Context) ([]*FsGetTypeCanteenType, error) + } + + defaultFsCanteenTypeModel struct { + conn sqlx.SqlConn + table string + } + + FsCanteenType struct { + Id int64 `db:"id"` // ID + Name string `db:"name"` // 餐厅名字 + Sort int64 `db:"sort"` // 排序 + Status int64 `db:"status"` // 状态位 1启用0停用 + Ctime int64 `db:"ctime"` // 添加时间 + } + + FsGetTypeCanteenType struct { + Id int64 `db:"id" json:"key"` // ID + Name string `db:"name" json:"name"` // 餐厅名字 + } +) + +func newFsCanteenTypeModel(conn sqlx.SqlConn) *defaultFsCanteenTypeModel { + return &defaultFsCanteenTypeModel{ + conn: conn, + table: "`fs_canteen_type`", + } +} + +func (m *defaultFsCanteenTypeModel) Delete(ctx context.Context, id int64) error { + query := fmt.Sprintf("delete from %s where `id` = ?", m.table) + _, err := m.conn.ExecCtx(ctx, query, id) + return err +} + +func (m *defaultFsCanteenTypeModel) FindOne(ctx context.Context, id int64) (*FsCanteenType, error) { + query := fmt.Sprintf("select %s from %s where `id` = ? limit 1", fsCanteenTypeRows, m.table) + var resp FsCanteenType + err := m.conn.QueryRowCtx(ctx, &resp, query, id) + switch err { + case nil: + return &resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } +} + +func (m *defaultFsCanteenTypeModel) FindGetType(ctx context.Context) ([]*FsGetTypeCanteenType, error) { + + query := fmt.Sprintf("select X.id,X.name from (select %s from %s where status = 1 order by sort desc) X", fsCanteenTypeRows, m.table) + var resp []*FsGetTypeCanteenType + err := m.conn.QueryRows(&resp, query) + switch err { + case nil: + return resp, nil + case sqlc.ErrNotFound: + return nil, ErrNotFound + default: + return nil, err + } + +} + +func (m *defaultFsCanteenTypeModel) Insert(ctx context.Context, data *FsCanteenType) (sql.Result, error) { + query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?)", m.table, fsCanteenTypeRowsExpectAutoSet) + ret, err := m.conn.ExecCtx(ctx, query, data.Name, data.Sort, data.Status, data.Ctime) + return ret, err +} + +func (m *defaultFsCanteenTypeModel) Update(ctx context.Context, data *FsCanteenType) error { + query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, fsCanteenTypeRowsWithPlaceHolder) + _, err := m.conn.ExecCtx(ctx, query, data.Name, data.Sort, data.Status, data.Ctime, data.Id) + return err +} + +func (m *defaultFsCanteenTypeModel) tableName() string { + return m.table +}