作者 yangfu

用户模型

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}
}
... ...
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
}
... ...
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
... ...