作者 郑周

Merge remote-tracking branch 'origin/dev' into dev

syntax = "v1"
info(
title: "xx实例"
desc: "xx实例"
author: "author"
email: "email"
version: "v1"
)
@server(
prefix: department/v1
group: department
jwt: JwtAuth
)
service Core {
@handler getDepartment
post /department/:id (DepartmentGetRequest) returns (DepartmentGetResponse)
@handler saveDepartment
post /department (DepartmentSaveRequest) returns (DepartmentSaveResponse)
@handler deleteDepartment
delete /department/:id (DepartmentDeleteRequest) returns (DepartmentDeleteResponse)
@handler updateDepartment
put /department/:id (DepartmentUpdateRequest) returns (DepartmentUpdateResponse)
@handler searchDepartment
post /department/search (DepartmentSearchRequest) returns (DepartmentSearchResponse)
}
type (
DepartmentGetRequest {
Id int64 `path:"id"`
}
DepartmentGetResponse struct{
Department DepartmentItem `json:"department"`
}
DepartmentSaveRequest struct{
Department DepartmentItem `json:"department"`
}
DepartmentSaveResponse struct{}
DepartmentDeleteRequest struct{
Id int64 `path:"id"`
}
DepartmentDeleteResponse struct{}
DepartmentUpdateRequest struct{
Id int64 `path:"id"`
Department DepartmentItem `json:"department"`
}
DepartmentUpdateResponse struct{}
DepartmentSearchRequest struct{
Page int `json:"page"`
Size int `json:"size"`
}
DepartmentSearchResponse{
List []DepartmentItem `json:"list"`
Total int64 `json:"total"`
}
DepartmentItem struct{
}
)
... ...
syntax = "v1"
info(
title: "xx实例"
desc: "xx实例"
author: "author"
email: "email"
version: "v1"
)
@server(
prefix: role/v1
group: role
jwt: JwtAuth
)
service Core {
@handler getRole
post /role/:id (RoleGetRequest) returns (RoleGetResponse)
@handler saveRole
post /role (RoleSaveRequest) returns (RoleSaveResponse)
@handler deleteRole
delete /role/:id (RoleDeleteRequest) returns (RoleDeleteResponse)
@handler updateRole
put /role/:id (RoleUpdateRequest) returns (RoleUpdateResponse)
@handler searchRole
post /role/search (RoleSearchRequest) returns (RoleSearchResponse)
}
type (
RoleGetRequest {
Id int64 `path:"id"`
}
RoleGetResponse struct{
Role RoleItem `json:"role"`
}
RoleSaveRequest struct{
Role RoleItem `json:"role"`
}
RoleSaveResponse struct{}
RoleDeleteRequest struct{
Id int64 `path:"id"`
}
RoleDeleteResponse struct{}
RoleUpdateRequest struct{
Id int64 `path:"id"`
Role RoleItem `json:"role"`
}
RoleUpdateResponse struct{}
RoleSearchRequest struct{
Page int `json:"page"`
Size int `json:"size"`
}
RoleSearchResponse{
List []RoleItem `json:"list"`
Total int64 `json:"total"`
}
RoleItem struct{
}
)
... ...
syntax = "v1"
info(
title: "xx实例"
desc: "xx实例"
author: "author"
email: "email"
version: "v1"
)
@server(
prefix: user/v1
group: user
jwt: JwtAuth
)
service Core {
@handler getUser
post /user/:id (UserGetRequest) returns (UserGetResponse)
@handler saveUser
post /user (UserSaveRequest) returns (UserSaveResponse)
@handler deleteUser
delete /user/:id (UserDeleteRequest) returns (UserDeleteResponse)
@handler updateUser
put /user/:id (UserUpdateRequest) returns (UserUpdateResponse)
@handler searchUser
post /user/search (UserSearchRequest) returns (UserSearchResponse)
}
type (
UserGetRequest {
Id int64 `path:"id"`
}
UserGetResponse struct{
User UserItem `json:"user"`
}
UserSaveRequest struct{
User UserItem `json:"user"`
}
UserSaveResponse struct{}
UserDeleteRequest struct{
Id int64 `path:"id"`
}
UserDeleteResponse struct{}
UserUpdateRequest struct{
Id int64 `path:"id"`
User UserItem `json:"user"`
}
UserUpdateResponse struct{}
UserSearchRequest struct{
Page int `json:"page"`
Size int `json:"size"`
}
UserSearchResponse{
List []UserItem `json:"list"`
Total int64 `json:"total"`
}
UserItem struct{
}
)
... ...
syntax = "proto3";
option go_package ="./pb";
package pb;
message DepartmentGetReq {
int64 Id = 1;
}
message DepartmentGetResp{
DepartmentItem User = 1;
}
message DepartmentSaveReq {
}
message DepartmentSaveResp{
}
message DepartmentDeleteReq {
int64 Id = 1;
}
message DepartmentDeleteResp{
}
message DepartmentUpdateReq {
int64 Id = 1;
}
message DepartmentUpdateResp{
}
message DepartmentSearchReq {
int64 PageNumber = 1;
int64 PageSize = 2;
}
message DepartmentSearchResp{
repeated DepartmentItem List =1;
int64 Total =2;
}
message DepartmentItem {
}
service DepartmentService {
rpc DepartmentGet(DepartmentGetReq) returns(DepartmentGetResp);
rpc DepartmentSave(DepartmentSaveReq) returns(DepartmentSaveResp);
rpc DepartmentDelete(DepartmentDeleteReq) returns(DepartmentDeleteResp);
rpc DepartmentUpdate(DepartmentUpdateReq) returns(DepartmentUpdateResp);
rpc DepartmentSearch(DepartmentSearchReq) returns(DepartmentSearchResp);
}
... ...
syntax = "proto3";
option go_package ="./pb";
package pb;
message RoleGetReq {
int64 Id = 1;
}
message RoleGetResp{
RoleItem User = 1;
}
message RoleSaveReq {
}
message RoleSaveResp{
}
message RoleDeleteReq {
int64 Id = 1;
}
message RoleDeleteResp{
}
message RoleUpdateReq {
int64 Id = 1;
}
message RoleUpdateResp{
}
message RoleSearchReq {
int64 PageNumber = 1;
int64 PageSize = 2;
}
message RoleSearchResp{
repeated RoleItem List =1;
int64 Total =2;
}
message RoleItem {
}
service RoleService {
rpc RoleGet(RoleGetReq) returns(RoleGetResp);
rpc RoleSave(RoleSaveReq) returns(RoleSaveResp);
rpc RoleDelete(RoleDeleteReq) returns(RoleDeleteResp);
rpc RoleUpdate(RoleUpdateReq) returns(RoleUpdateResp);
rpc RoleSearch(RoleSearchReq) returns(RoleSearchResp);
}
... ...
syntax = "proto3";
option go_package ="./pb";
package pb;
message UserGetReq {
int64 Id = 1;
}
message UserGetResp{
UserItem User = 1;
}
message UserSaveReq {
}
message UserSaveResp{
}
message UserDeleteReq {
int64 Id = 1;
}
message UserDeleteResp{
}
message UserUpdateReq {
int64 Id = 1;
}
message UserUpdateResp{
}
message UserSearchReq {
int64 PageNumber = 1;
int64 PageSize = 2;
}
message UserSearchResp{
repeated UserItem List =1;
int64 Total =2;
}
message UserItem {
}
service UserService {
rpc UserGet(UserGetReq) returns(UserGetResp);
rpc UserSave(UserSaveReq) returns(UserSaveResp);
rpc UserDelete(UserDeleteReq) returns(UserDeleteResp);
rpc UserUpdate(UserUpdateReq) returns(UserUpdateResp);
rpc UserSearch(UserSearchReq) returns(UserSearchResp);
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type Department struct {
Id int64 // 唯一标识
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
DeletedAt int64 `json:"deletedAt,omitempty"`
Version int `json:"version,omitempty"`
}
func (m *Department) TableName() string {
return "department"
}
func (m *Department) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *Department) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *Department) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *Department) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*Department); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *Department) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
"time"
)
type Role struct {
Id int64 // 唯一标识
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
DeletedAt int64 `json:"deletedAt,omitempty"`
Version int `json:"version,omitempty"`
}
func (m *Role) TableName() string {
return "role"
}
func (m *Role) BeforeCreate(tx *gorm.DB) (err error) {
m.CreatedAt = time.Now().Unix()
m.UpdatedAt = time.Now().Unix()
return
}
func (m *Role) BeforeUpdate(tx *gorm.DB) (err error) {
m.UpdatedAt = time.Now().Unix()
return
}
func (m *Role) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *Role) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*Role); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *Role) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
"time"
)
type User struct {
Id int64 // 唯一标识
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
DeletedAt int64 `json:"deletedAt,omitempty"`
Version int `json:"version,omitempty"`
}
func (m *User) TableName() string {
return "user"
}
func (m *User) BeforeCreate(tx *gorm.DB) (err error) {
m.CreatedAt = time.Now().Unix()
m.UpdatedAt = time.Now().Unix()
return
}
func (m *User) BeforeUpdate(tx *gorm.DB) (err error) {
m.UpdatedAt = time.Now().Unix()
return
}
func (m *User) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *User) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*User); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *User) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
package repository
import (
"context"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/tiptok/gocomm/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/models"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type DepartmentRepository struct {
*cache.CachedRepository
}
func (repository *DepartmentRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.Department) (*domain.Department, error) {
var (
err error
m = &models.Department{}
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
if tx = tx.Model(m).Save(m); tx.Error != nil {
return nil, tx.Error
}
dm.Id = m.Id
return repository.ModelToDomainModel(m)
}
func (repository *DepartmentRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.Department) (*domain.Department, error) {
var (
err error
m *models.Department
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(m)
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *DepartmentRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.Department) (*domain.Department, error) {
var (
err error
m *models.Department
tx = transaction.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
oldVersion := dm.Version
m.Version += 1
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
if tx.RowsAffected == 0 {
return nil, domain.ErrUpdateFail
}
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *DepartmentRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.Department) (*domain.Department, error) {
var (
tx = conn.DB()
m = &models.Department{Id: dm.Identify().(int64)}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("id = ?", m.Id).Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return dm, err
}
return repository.ModelToDomainModel(m)
}
func (repository *DepartmentRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.Department, error) {
var (
err error
tx = conn.DB()
m = new(models.Department)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.Department)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *DepartmentRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.Department, error) {
var (
tx = conn.DB()
ms []*models.Department
dms = make([]*domain.Department, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
func (repository *DepartmentRepository) ModelToDomainModel(from *models.Department) (*domain.Department, error) {
to := &domain.Department{}
err := copier.Copy(to, from)
return to, err
}
func (repository *DepartmentRepository) DomainModelToModel(from *domain.Department) (*models.Department, error) {
to := &models.Department{}
err := copier.Copy(to, from)
return to, err
}
func NewDepartmentRepository(cache *cache.CachedRepository) domain.DepartmentRepository {
return &DepartmentRepository{CachedRepository: cache}
}
... ...
package repository
import (
"context"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/tiptok/gocomm/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/models"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type RoleRepository struct {
*cache.CachedRepository
}
func (repository *RoleRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.Role) (*domain.Role, error) {
var (
err error
m = &models.Role{}
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
if tx = tx.Model(m).Save(m); tx.Error != nil {
return nil, tx.Error
}
dm.Id = m.Id
return repository.ModelToDomainModel(m)
}
func (repository *RoleRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.Role) (*domain.Role, error) {
var (
err error
m *models.Role
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(m)
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *RoleRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.Role) (*domain.Role, error) {
var (
err error
m *models.Role
tx = transaction.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
oldVersion := dm.Version
m.Version += 1
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
if tx.RowsAffected == 0 {
return nil, domain.ErrUpdateFail
}
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *RoleRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.Role) (*domain.Role, error) {
var (
tx = conn.DB()
m = &models.Role{Id: dm.Identify().(int64)}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("id = ?", m.Id).Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return dm, err
}
return repository.ModelToDomainModel(m)
}
func (repository *RoleRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.Role, error) {
var (
err error
tx = conn.DB()
m = new(models.Role)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.Role)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *RoleRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.Role, error) {
var (
tx = conn.DB()
ms []*models.Role
dms = make([]*domain.Role, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
func (repository *RoleRepository) ModelToDomainModel(from *models.Role) (*domain.Role, error) {
to := &domain.Role{}
err := copier.Copy(to, from)
return to, err
}
func (repository *RoleRepository) DomainModelToModel(from *domain.Role) (*models.Role, error) {
to := &models.Role{}
err := copier.Copy(to, from)
return to, err
}
func NewRoleRepository(cache *cache.CachedRepository) domain.RoleRepository {
return &RoleRepository{CachedRepository: cache}
}
... ...
package repository
import (
"context"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/tiptok/gocomm/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/models"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type UserRepository struct {
*cache.CachedRepository
}
func (repository *UserRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.User) (*domain.User, error) {
var (
err error
m = &models.User{}
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
if tx = tx.Model(m).Save(m); tx.Error != nil {
return nil, tx.Error
}
dm.Id = m.Id
return repository.ModelToDomainModel(m)
}
func (repository *UserRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.User) (*domain.User, error) {
var (
err error
m *models.User
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(m)
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *UserRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.User) (*domain.User, error) {
var (
err error
m *models.User
tx = transaction.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
oldVersion := dm.Version
m.Version += 1
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
if tx.RowsAffected == 0 {
return nil, domain.ErrUpdateFail
}
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *UserRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.User) (*domain.User, error) {
var (
tx = conn.DB()
m = &models.User{Id: dm.Identify().(int64)}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("id = ?", m.Id).Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return dm, err
}
return repository.ModelToDomainModel(m)
}
func (repository *UserRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.User, error) {
var (
err error
tx = conn.DB()
m = new(models.User)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.User)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *UserRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.User, error) {
var (
tx = conn.DB()
ms []*models.User
dms = make([]*domain.User, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
func (repository *UserRepository) ModelToDomainModel(from *models.User) (*domain.User, error) {
to := &domain.User{}
err := copier.Copy(to, from)
return to, err
}
func (repository *UserRepository) DomainModelToModel(from *domain.User) (*models.User, error) {
to := &models.User{}
err := copier.Copy(to, from)
return to, err
}
func NewUserRepository(cache *cache.CachedRepository) domain.UserRepository {
return &UserRepository{CachedRepository: cache}
}
... ...
... ... @@ -8,17 +8,20 @@ import (
)
type Article struct {
Id int64
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
AuthorId int64 // 发布人
Author UserSimple // 发布人
Title string // 文章标题
Images []Image // 图片
WhoRead []int64 // 谁可以看
WhoReview []int64 // 评论人
Location Location // 坐标
Id int64 `json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt *time.Time `json:"deletedAt"`
AuthorId int64 `json:"authorId"` // 发布人
Author UserSimple `json:"author"` // 发布人
Title string `json:"title"` // 文章标题
Images []Image `json:"images"` // 图片
WhoRead []int64 `json:"whoRead"` // 谁可以看
WhoReview []int64 `json:"whoReview"` // 评论人
Location Location `json:"location"` // 坐标
CountLove int `json:"countLove"` // 点赞数量
CountComment int `json:"countComment"` // 评论数量
Tags []int `json:"tags"` // 标签
// ...more
}
... ...
package domain
import "time"
// 编辑文章后保存的历史记录
type ArticleBackup struct {
Id int64
Id int64
UpdatedAt time.Time `json:"updatedAt"` // 更新时间
DeletedAt *time.Time `json:"deletedAt"` //
CreatedAt time.Time `json:"createdAt"` //
Operator UserSimple `json:"operator"` // 操作人
Title string `json:"title"` // 标题
Section []ArticleSection `json:"section"` // 分段内容
Images []Image `json:"images"` // 图片
Action string `json:"action"` // 操作
}
... ...
package domain
import "time"
import (
"context"
"time"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)
// 文章评论
type ArticleComment struct {
Id int64 // 评论id
UpdatedAt time.Time // 更新时间
DeletedAt *time.Time
CreatedAt time.Time
Pid int64 // 对哪个评论进行回复
TopId int64 // 归属于最上级的哪个评论
ArticleId int64 // 文章id
ArticleSectionId int64 // 文本内容id
SectionContent string // 引用的文章内容文本
FromUserId int64 // 谁填写的评论
FromUser UserSimple // 谁填写的评论
ToUserId int64 // 回复谁的评论
ToUser UserSimple // 回复谁的评论
Content string // 评论内容
CountReply int // 回复数量
CountUserLove int // 用户点赞数量
CountAdminLove int // 运营点赞数量
Id int64 `json:"id"` // 评论id
UpdatedAt time.Time `json:"updatedAt"` // 更新时间
DeletedAt *time.Time `json:"deletedAt"`
CreatedAt time.Time `json:"createdAt"`
Pid int64 `json:"pid"` // 对哪个评论进行回复
TopId int64 `json:"topId"` // 归属于最上级的哪个评论
ArticleId int64 `json:"articleId"` // 文章id
ArticleSectionId int64 `json:"articleSectionId"` // 文本内容id
SectionContent string `json:"sectionContent"` // 引用的文章内容文本
FromUserId int64 `json:"fromUserId"` // 谁填写的评论
FromUser UserSimple `json:"fromUser"` // 谁填写的评论
ToUserId int64 `json:"toUserId"` // 回复谁的评论
ToUser UserSimple `json:"toUser"` // 回复谁的评论
Content string `json:"content"` // 评论内容
CountReply int `json:"countReply"` // 回复数量
CountUserLove int `json:"countUserLove"` // 用户点赞数量
CountAdminLove int `json:"countAdminLove"` // 运营点赞数量
Show int `json:"showState"` // 评论的展示状态(0显示、1不显示)
// ...more
}
// 评论的展示状态(0显示、1不显示)
type CommentShow int
const (
CommentShowEnable CommentShow = 0
CommentShowDisable CommentShow = 1
)
type ArticleCommentRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *ArticleComment) (*ArticleComment, error)
Update(ctx context.Context, conn transaction.Conn, dm *ArticleComment) (*ArticleComment, error)
Delete(ctx context.Context, conn transaction.Conn, dm *ArticleComment) (*ArticleComment, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleComment, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleComment, error)
}
... ...
package domain
import "time"
import (
"context"
"time"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)
// 填写文章时保存的草稿
type ArticleDraft struct {
Id int64
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
Template int // 填写内容时用的样板 1、演绎式 2、归纳式
Content []string // 文章内容
AuthorId int64 // 发布人
Title string // 文章标题
Images []Image // 图片
WhoRead []int64 // 谁可以看
WhoReview []int64 // 评论人
Location Location // 坐标
Id int64 `json:"id"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt *time.Time `json:"deletedAt"`
Template int `json:"template"` // 填写内容时用的样板 1、演绎式 2、归纳式
Content []string `json:"content"` // 文章内容
AuthorId int64 `json:"authorId"` // 发布人
Title string `json:"title"` // 文章标题
Images []Image `json:"images"` // 图片
WhoRead []int64 `json:"whoRead"` // 谁可以看
WhoReview []int64 `json:"whoReview"` // 评论人
Location Location `json:"location"` // 坐标
}
type ArticleDraftRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *ArticleDraft) (*ArticleDraft, error)
Update(ctx context.Context, conn transaction.Conn, dm *ArticleDraft) (*ArticleDraft, error)
Delete(ctx context.Context, conn transaction.Conn, dm *ArticleDraft) (*ArticleDraft, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleDraft, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleDraft, error)
}
... ...
... ... @@ -9,15 +9,14 @@ import (
// 文章段落
type ArticleSection struct {
Id int64
UpdatedAt time.Time // 更新时间
DeletedAt *time.Time //
CreatedAt time.Time //
ArticleId int64 // 文章id
Content string // 文本内容
SortBy int // 排序
TotalComment int // 评论的数量
// ...more
Id int64 `json:"id"`
UpdatedAt time.Time `json:"updatedAt"` // 更新时间
DeletedAt *time.Time `json:"deletedAt"` //
CreatedAt time.Time `json:"createdAt"` //
ArticleId int64 `json:"articleId"` // 文章id
Content string `json:"content"` // 文本内容
SortBy int `json:"sortBy"` // 排序
TotalComment int `json:"totalComment"` // 评论的数量
}
type ArticleSectionRepository interface {
... ...
package domain
import (
"context"
"time"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)
// 文章的标签
type ArticleTag struct {
Id int64 `json:"id"`
UpdatedAt time.Time `json:"updatedAt"` // 更新时间
DeletedAt *time.Time `json:"deletedAt"` //
CreatedAt time.Time `json:"createdAt"` //
Image Image `json:"image"` //图片
Name string `json:"name"` //标签名称
Group string `json:"group"` //标签分类
Remark string `json:"remark"` //备注
}
type ArticleTagRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *ArticleTag) (*ArticleTag, error)
Update(ctx context.Context, conn transaction.Conn, dm *ArticleTag) (*ArticleSection, error)
Delete(ctx context.Context, conn transaction.Conn, dm *ArticleTag) (*ArticleTag, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleTag, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleTag, error)
}
... ...
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)
type Department struct {
Id int64 `json:"id,omitempty"` // 部门ID
CompanyId int64 `json:"companyId,omitempty"` // 公司ID
ParentId int64 `json:"parentId,omitempty"` // 父级ID
Name string `json:"name,omitempty"` // 部门名称
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
DeletedAt int64 `json:"deletedAt,omitempty"`
Version int `json:"version,omitempty"`
}
type DepartmentRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *Department) (*Department, error)
Update(ctx context.Context, conn transaction.Conn, dm *Department) (*Department, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Department) (*Department, error)
Delete(ctx context.Context, conn transaction.Conn, dm *Department) (*Department, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Department, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*Department, error)
}
func (m *Department) Identify() interface{} {
if m.Id == 0 {
return nil
}
return m.Id
}
... ...
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)
type Role struct {
Id int64 `json:"id"` // 角色ID
Name string `json:"name"` // 角色名称
Auths []int64 `json:"auths"` // 角色权限列表
Remark string `json:"remark"` // 备注
Users []int64 `json:"users"` // 绑定的用户
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
DeletedAt int64 `json:"deletedAt,omitempty"`
Version int `json:"version,omitempty"`
}
type RoleRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *Role) (*Role, error)
Update(ctx context.Context, conn transaction.Conn, dm *Role) (*Role, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Role) (*Role, error)
Delete(ctx context.Context, conn transaction.Conn, dm *Role) (*Role, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Role, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*Role, error)
}
func (m *Role) Identify() interface{} {
if m.Id == 0 {
return nil
}
return m.Id
}
... ...
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)
type User struct {
Id int64 `json:"id,omitempty"` // 用户ID
CompanyId int64 `json:"companyId,omitempty"` // 公司ID
DepartmentId int64 `json:"departmentId,omitempty"` // 部门ID
Roles []int64 `json:"roleId,omitempty"` // 角色
Flag int `json:"flag"` // 标识 1:管理员 2:普通用户 (有绑定角色是管理员)
Name string `json:"name,omitempty"` // 名称
Avatar string `json:"avatar,omitempty"` // 头像
Phone string `json:"phone,omitempty"` // 手机号 唯一
Position string `json:"position,omitempty"` // 职位
Enable int `json:"enable,omitempty"` // 启用状态 1:启用 2:禁用
AuditStatus int `json:"auditStatus,omitempty"` // 审核状态 0:待审核 1:审核通过 2:拒绝
CreatedAt int64 `json:"createdAt,omitempty"`
UpdatedAt int64 `json:"updatedAt,omitempty"`
DeletedAt int64 `json:"deletedAt,omitempty"`
Version int `json:"version,omitempty"`
}
type UserRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *User) (*User, error)
Update(ctx context.Context, conn transaction.Conn, dm *User) (*User, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *User) (*User, error)
Delete(ctx context.Context, conn transaction.Conn, dm *User) (*User, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*User, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*User, error)
}
func (m *User) Identify() interface{} {
if m.Id == 0 {
return nil
}
return m.Id
}
... ...
... ... @@ -5,11 +5,11 @@ import "time"
// 人员点赞标记
type UserLoveFlag struct {
Id int64
ArticleId int64 //点赞文章时,填文章id
CommentId int64 //点赞评论时,填评论id
UserId int64
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time
Id int64 `json:"id"`
ArticleId int64 `json:"articleId"` //点赞文章时,填文章id
CommentId int64 `json:"commentId"` //点赞评论时,填评论id
UserId int64 `json:"userId"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt *time.Time `json:"deletedAt"`
}
... ...
... ... @@ -2,4 +2,10 @@ package domain
// 人员查看文章详情后,标记一个记录
type UserReadArticle struct{}
type UserReadArticle struct {
Id int64 `json:"id"`
UserId int64 `json:"userId"`
ArticleId int64 `json:"articleId"`
Title string `json:"title"`
ArticleAuthor UserSimple `json:"author"` // 发布人
}
... ...
CREATE TABLE `comment` (
`id` int(0) NOT NULL COMMENT '唯一标识',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
CREATE TABLE `department` (
`id` int(0) NOT NULL COMMENT '唯一标识',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
CREATE TABLE `role` (
`id` int(0) NOT NULL COMMENT '唯一标识',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
CREATE TABLE `user` (
`id` int(0) NOT NULL COMMENT '唯一标识',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;
\ No newline at end of file
... ...