commit code
This commit is contained in:
@ -1,5 +1,12 @@
|
||||
Name: gallery.rpc
|
||||
ListenOn: 0.0.0.0:50000
|
||||
|
||||
DB:
|
||||
DataSource: postgres://postgres:postgres@localhost:19732/godemo?sslmode=disable
|
||||
MaxOpenConns: 100
|
||||
MaxIdleConns: 20
|
||||
ConnMaxLifetime: 3600
|
||||
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 127.0.0.1:2379
|
||||
@ -9,4 +16,10 @@ FileRpc:
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 127.0.0.1:2379
|
||||
Key: file.rpc
|
||||
Key: file.rpc
|
||||
|
||||
CategoryRpc:
|
||||
Etcd:
|
||||
Hosts:
|
||||
- 127.0.0.1:2379
|
||||
Key: category.rpc
|
||||
@ -4,5 +4,12 @@ import "github.com/zeromicro/go-zero/zrpc"
|
||||
|
||||
type Config struct {
|
||||
zrpc.RpcServerConf
|
||||
FileRpc zrpc.RpcClientConf
|
||||
FileRpc zrpc.RpcClientConf
|
||||
CategoryRpc zrpc.RpcClientConf
|
||||
DB struct {
|
||||
DataSource string
|
||||
MaxOpenConns int
|
||||
MaxIdleConns int
|
||||
ConnMaxLifetime int
|
||||
}
|
||||
}
|
||||
|
||||
@ -3,11 +3,14 @@ package logic
|
||||
import (
|
||||
"context"
|
||||
|
||||
"godemo/category/category"
|
||||
"godemo/file/file"
|
||||
"godemo/gallery/gallery"
|
||||
"godemo/gallery/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
type UploadImageLogic struct {
|
||||
@ -27,13 +30,18 @@ func NewUploadImageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Uploa
|
||||
// 图片上传
|
||||
func (l *UploadImageLogic) UploadImage(in *gallery.UploadImageRequest) (*gallery.UploadImageResponse, error) {
|
||||
// 1. 根据 in.CategoryId 查询分类信息,获取分类名称
|
||||
var categoryName string
|
||||
getCategoryRpcResp, getCategoryRpcErr := l.svcCtx.CategoryRpc.GetCategory(l.ctx, &category.GetCategoryRequest{
|
||||
Id: *in.CategoryId,
|
||||
})
|
||||
if getCategoryRpcErr != nil {
|
||||
return nil, status.Error(codes.Internal, getCategoryRpcErr.Error())
|
||||
}
|
||||
|
||||
// 2. 调用 file rpc Upload 接口,上传文件到MinIO
|
||||
l.svcCtx.FileRpc.Upload(l.ctx, &file.UploadRequest{
|
||||
fileUploadRpcResp, fileUploadRpcErr := l.svcCtx.FileRpc.Upload(l.ctx, &file.UploadRequest{
|
||||
Filename: in.FileName,
|
||||
Content: in.FileContent,
|
||||
Folder: categoryName,
|
||||
Folder: getCategoryRpcResp.Category.Name,
|
||||
})
|
||||
|
||||
// 3. 图片信息存入数据库
|
||||
|
||||
29
gallery/internal/model/imagesmodel.go
Normal file
29
gallery/internal/model/imagesmodel.go
Normal file
@ -0,0 +1,29 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var _ ImagesModel = (*customImagesModel)(nil)
|
||||
|
||||
type (
|
||||
// ImagesModel is an interface to be customized, add more methods here,
|
||||
// and implement the added methods in customImagesModel.
|
||||
ImagesModel interface {
|
||||
imagesModel
|
||||
withSession(session sqlx.Session) ImagesModel
|
||||
}
|
||||
|
||||
customImagesModel struct {
|
||||
*defaultImagesModel
|
||||
}
|
||||
)
|
||||
|
||||
// NewImagesModel returns a model for the database table.
|
||||
func NewImagesModel(conn sqlx.SqlConn) ImagesModel {
|
||||
return &customImagesModel{
|
||||
defaultImagesModel: newImagesModel(conn),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *customImagesModel) withSession(session sqlx.Session) ImagesModel {
|
||||
return NewImagesModel(sqlx.NewSqlConnFromSession(session))
|
||||
}
|
||||
97
gallery/internal/model/imagesmodel_gen.go
Normal file
97
gallery/internal/model/imagesmodel_gen.go
Normal file
@ -0,0 +1,97 @@
|
||||
// Code generated by goctl. DO NOT EDIT.
|
||||
// versions:
|
||||
// goctl version: 1.8.3
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/lib/pq"
|
||||
"github.com/zeromicro/go-zero/core/stores/builder"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/core/stringx"
|
||||
)
|
||||
|
||||
var (
|
||||
imagesFieldNames = builder.RawFieldNames(&Images{}, true)
|
||||
imagesRows = strings.Join(imagesFieldNames, ",")
|
||||
imagesRowsExpectAutoSet = strings.Join(stringx.Remove(imagesFieldNames, "create_at", "create_time", "created_at", "update_at", "update_time", "updated_at"), ",")
|
||||
imagesRowsWithPlaceHolder = builder.PostgreSqlJoin(stringx.Remove(imagesFieldNames, "image_id", "create_at", "create_time", "created_at", "update_at", "update_time", "updated_at"))
|
||||
)
|
||||
|
||||
type (
|
||||
imagesModel interface {
|
||||
Insert(ctx context.Context, data *Images) (sql.Result, error)
|
||||
FindOne(ctx context.Context, imageId string) (*Images, error)
|
||||
Update(ctx context.Context, data *Images) error
|
||||
Delete(ctx context.Context, imageId string) error
|
||||
}
|
||||
|
||||
defaultImagesModel struct {
|
||||
conn sqlx.SqlConn
|
||||
table string
|
||||
}
|
||||
|
||||
Images struct {
|
||||
ImageId string `db:"image_id"`
|
||||
StorageKey string `db:"storage_key"`
|
||||
FileName string `db:"file_name"`
|
||||
MimeType string `db:"mime_type"`
|
||||
FileSize int64 `db:"file_size"`
|
||||
Width sql.NullInt64 `db:"width"`
|
||||
Height sql.NullInt64 `db:"height"`
|
||||
CategoryId sql.NullString `db:"category_id"`
|
||||
Tags pq.StringArray `db:"tags"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
IsDeleted bool `db:"is_deleted"`
|
||||
}
|
||||
)
|
||||
|
||||
func newImagesModel(conn sqlx.SqlConn) *defaultImagesModel {
|
||||
return &defaultImagesModel{
|
||||
conn: conn,
|
||||
table: `"public"."images"`,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultImagesModel) Delete(ctx context.Context, imageId string) error {
|
||||
query := fmt.Sprintf("delete from %s where image_id = $1", m.table)
|
||||
_, err := m.conn.ExecCtx(ctx, query, imageId)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultImagesModel) FindOne(ctx context.Context, imageId string) (*Images, error) {
|
||||
query := fmt.Sprintf("select %s from %s where image_id = $1 limit 1", imagesRows, m.table)
|
||||
var resp Images
|
||||
err := m.conn.QueryRowCtx(ctx, &resp, query, imageId)
|
||||
switch err {
|
||||
case nil:
|
||||
return &resp, nil
|
||||
case sqlx.ErrNotFound:
|
||||
return nil, ErrNotFound
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (m *defaultImagesModel) Insert(ctx context.Context, data *Images) (sql.Result, error) {
|
||||
query := fmt.Sprintf("insert into %s (%s) values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)", m.table, imagesRowsExpectAutoSet)
|
||||
ret, err := m.conn.ExecCtx(ctx, query, data.ImageId, data.StorageKey, data.FileName, data.MimeType, data.FileSize, data.Width, data.Height, data.CategoryId, data.Tags, data.IsDeleted)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (m *defaultImagesModel) Update(ctx context.Context, data *Images) error {
|
||||
query := fmt.Sprintf("update %s set %s where image_id = $1", m.table, imagesRowsWithPlaceHolder)
|
||||
_, err := m.conn.ExecCtx(ctx, query, data.ImageId, data.StorageKey, data.FileName, data.MimeType, data.FileSize, data.Width, data.Height, data.CategoryId, data.Tags, data.IsDeleted)
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *defaultImagesModel) tableName() string {
|
||||
return m.table
|
||||
}
|
||||
5
gallery/internal/model/vars.go
Normal file
5
gallery/internal/model/vars.go
Normal file
@ -0,0 +1,5 @@
|
||||
package model
|
||||
|
||||
import "github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
|
||||
var ErrNotFound = sqlx.ErrNotFound
|
||||
@ -1,21 +1,28 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"godemo/category/category"
|
||||
"godemo/file/file"
|
||||
"godemo/gallery/internal/config"
|
||||
"godemo/gallery/internal/model"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/zrpc"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
FileRpc file.FileClient
|
||||
Config config.Config
|
||||
FileRpc file.FileClient
|
||||
CategoryRpc category.CategoryClient
|
||||
ImagesModel model.ImagesModel
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
conn := zrpc.MustNewClient(c.FileRpc).Conn()
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
FileRpc: file.NewFileClient(conn),
|
||||
Config: c,
|
||||
FileRpc: file.NewFileClient(zrpc.MustNewClient(c.FileRpc).Conn()),
|
||||
CategoryRpc: category.NewCategoryClient(zrpc.MustNewClient(c.CategoryRpc).Conn()),
|
||||
ImagesModel: model.NewImagesModel(sqlx.NewSqlConn("postgres", c.DB.DataSource)),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user