diff --git a/biz/adaptor/server.go b/biz/adaptor/server.go index 48567ba..7f7f100 100644 --- a/biz/adaptor/server.go +++ b/biz/adaptor/server.go @@ -3,7 +3,6 @@ package adaptor import ( "context" - "github.com/xh-polaris/service-idl-gen-go/kitex_gen/meowcloud/content" "github.com/xh-polaris/service-idl-gen-go/kitex_gen/meowcloud/user" "github.com/xh-polaris/meowcloud-user/biz/application/service" @@ -12,8 +11,7 @@ import ( type UserServerImpl struct { *config.Config - UserService service.UserService - CatAlbumService service.CatAlbumService + UserService service.UserService } func (s *UserServerImpl) GetUser(ctx context.Context, req *user.GetUserReq) (res *user.GetUserResp, err error) { @@ -27,23 +25,3 @@ func (s *UserServerImpl) GetUserDetail(ctx context.Context, req *user.GetUserDet func (s *UserServerImpl) UpdateUser(ctx context.Context, req *user.UpdateUserReq) (res *user.UpdateUserResp, err error) { return s.UserService.UpdateUser(ctx, req) } - -func (s *UserServerImpl) CreateCatAlbum(ctx context.Context, req *content.CreateCatAlbumReq) (res *content.CreateCatAlbumResp, err error) { - return s.CatAlbumService.CreateCatAlbum(ctx, req) -} - -func (s *UserServerImpl) RetrieveCatAlbum(ctx context.Context, req *content.RetrieveCatAlbumReq) (res *content.RetrieveCatAlbumResp, err error) { - return s.CatAlbumService.RetrieveCatAlbum(ctx, req) -} - -func (s *UserServerImpl) UpdateCatAlbum(ctx context.Context, req *content.UpdateCatAlbumReq) (res *content.UpdateCatAlbumResp, err error) { - return s.CatAlbumService.UpdateCatAlbum(ctx, req) -} - -func (s *UserServerImpl) DeleteCatAlbum(ctx context.Context, req *content.DeleteCatAlbumReq) (res *content.DeleteCatAlbumResp, err error) { - return s.CatAlbumService.DeleteCatAlbum(ctx, req) -} - -func (s *UserServerImpl) ListCatAlbum(ctx context.Context, req *content.ListCatAlbumReq) (res *content.ListCatAlbumResp, err error) { - return s.CatAlbumService.ListCatAlbum(ctx, req) -} diff --git a/biz/application/service/catalbum.go b/biz/application/service/catalbum.go deleted file mode 100644 index 7dcdfe5..0000000 --- a/biz/application/service/catalbum.go +++ /dev/null @@ -1,98 +0,0 @@ -package service - -import ( - "context" - "github.com/google/wire" - "github.com/jinzhu/copier" - "github.com/xh-polaris/meowcloud-user/biz/infrastructure/config" - catalbummapper "github.com/xh-polaris/meowcloud-user/biz/infrastructure/mapper/catalbum" - "github.com/xh-polaris/service-idl-gen-go/kitex_gen/meowcloud/content" -) - -type CatAlbumService interface { - CreateCatAlbum(ctx context.Context, req *content.CreateCatAlbumReq) (res *content.CreateCatAlbumResp, err error) - RetrieveCatAlbum(ctx context.Context, req *content.RetrieveCatAlbumReq) (res *content.RetrieveCatAlbumResp, err error) - UpdateCatAlbum(ctx context.Context, req *content.UpdateCatAlbumReq) (res *content.UpdateCatAlbumResp, err error) - DeleteCatAlbum(ctx context.Context, req *content.DeleteCatAlbumReq) (res *content.DeleteCatAlbumResp, err error) - ListCatAlbum(ctx context.Context, req *content.ListCatAlbumReq) (res *content.ListCatAlbumResp, err error) -} - -type CatAlbumServiceImpl struct { - Config *config.Config - CatAlbumMongoMapper catalbummapper.IMongoMapper -} - -var CatAlbumSet = wire.NewSet( - wire.Struct(new(CatAlbumServiceImpl), "*"), - wire.Bind(new(CatAlbumService), new(*CatAlbumServiceImpl)), -) - -func (s *CatAlbumServiceImpl) CreateCatAlbum(ctx context.Context, req *content.CreateCatAlbumReq) (res *content.CreateCatAlbumResp, err error) { - catalbum := &catalbummapper.CatAlbum{} - err = copier.Copy(catalbum, req.CatAlbum) - if err != nil { - return nil, err - } - err = s.CatAlbumMongoMapper.Insert(ctx, catalbum) - if err != nil { - return nil, err - } - return &content.CreateCatAlbumResp{CatAlbum: &content.CatAlbum{ - Id: catalbum.ID.Hex(), - }}, nil -} - -func (s *CatAlbumServiceImpl) RetrieveCatAlbum(ctx context.Context, req *content.RetrieveCatAlbumReq) (res *content.RetrieveCatAlbumResp, err error) { - catalbum, err := s.CatAlbumMongoMapper.FindOne(ctx, req.Id) - if err != nil { - return nil, err - } - c := &content.CatAlbum{} - err = copier.Copy(c, catalbum) - if err != nil { - return nil, err - } - return &content.RetrieveCatAlbumResp{CatAlbum: &content.CatAlbum{}}, nil -} - -func (s *CatAlbumServiceImpl) UpdateCatAlbum(ctx context.Context, req *content.UpdateCatAlbumReq) (res *content.UpdateCatAlbumResp, err error) { - catalbum := &catalbummapper.CatAlbum{} - err = copier.Copy(catalbum, req.CatAlbum) - if err != nil { - return nil, err - } - err = s.CatAlbumMongoMapper.Upsert(ctx, catalbum) - if err != nil { - return nil, err - } - return &content.UpdateCatAlbumResp{CatAlbum: &content.CatAlbum{ - Id: catalbum.ID.Hex(), - }}, nil -} - -func (s *CatAlbumServiceImpl) DeleteCatAlbum(ctx context.Context, req *content.DeleteCatAlbumReq) (res *content.DeleteCatAlbumResp, err error) { - err = s.CatAlbumMongoMapper.Delete(ctx, req.Id) - if err != nil { - return nil, err - } - return &content.DeleteCatAlbumResp{}, nil -} - -func (s *CatAlbumServiceImpl) ListCatAlbum(ctx context.Context, req *content.ListCatAlbumReq) (res *content.ListCatAlbumResp, err error) { - catalbums, count, err := s.CatAlbumMongoMapper.FindMany(ctx, req.PaginationOptions.GetOffset(), req.PaginationOptions.GetLimit()) - if err != nil { - return nil, err - } - - var catAlbumList []*content.CatAlbum - for _, album := range catalbums { - catAlbum := &content.CatAlbum{} - err = copier.Copy(catAlbum, album) - if err != nil { - return nil, err - } - catAlbumList = append(catAlbumList, catAlbum) - } - - return &content.ListCatAlbumResp{CatAlbums: catAlbumList, Total: count}, nil -} diff --git a/biz/infrastructure/mapper/catalbum/mongo.go b/biz/infrastructure/mapper/catalbum/mongo.go deleted file mode 100644 index dc6b713..0000000 --- a/biz/infrastructure/mapper/catalbum/mongo.go +++ /dev/null @@ -1,197 +0,0 @@ -package user - -import ( - "context" - "errors" - "time" - - "github.com/zeromicro/go-zero/core/stores/monc" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo/options" - - "github.com/xh-polaris/meowcloud-user/biz/infrastructure/config" - "github.com/xh-polaris/meowcloud-user/biz/infrastructure/consts" -) - -const ( - prefixUserCacheKey = "cache:catalbum:" - CollectionName = "user" -) - -type ( - // IMongoMapper is an interface to be customized, add more methods here, - // and implement the added methods in MongoMapper. - IMongoMapper interface { - Insert(ctx context.Context, data *CatAlbum) error - FindOne(ctx context.Context, id string) (*CatAlbum, error) - Upsert(ctx context.Context, data *CatAlbum) error - Delete(ctx context.Context, id string) error - FindOneNoCache(ctx context.Context, id string) (*CatAlbum, error) - FindMany(ctx context.Context, skip int64, count int64) ([]*CatAlbum, int64, error) - FindManyByCreatorId(ctx context.Context, creatorId string, skip int64, count int64) ([]*CatAlbum, int64, error) - } - - MongoMapper struct { - conn *monc.Model - } - - CatAlbum struct { - ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` - Type int32 `bson:"type,omitempty" json:"type,omitempty"` - CreatorId string `bson:"creatorId,omitempty" json:"creatorId,omitempty"` - AlbumName string `bson:"albumName,omitempty" json:"albumName,omitempty"` - Visibility int32 `bson:"visibility,omitempty" json:"visibility,omitempty"` - TotalPhotos int32 `bson:"totalPhotos,omitempty" json:"totalPhotos,omitempty"` - AvailablePhotos int32 `bson:"availablePhotos,omitempty" json:"availablePhotos,omitempty"` - UpdatedAt time.Time `bson:"updatedAt,omitempty" json:"updatedAt,omitempty"` - DeletedAt time.Time `bson:"deletedAt,omitempty" json:"deletedAt,omitempty"` - CreatedAt time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty"` - CreatedLocation string `bson:"createdLocation,omitempty" json:"createdLocation,omitempty"` - CreatedLocationLongitude float64 `bson:"createdLocationLongitude,omitempty" json:"createdLocationLongitude,omitempty"` - CreatedLocationLatitude float64 `bson:"createdLocationLatitude,omitempty" json:"createdLocationLatitude,omitempty"` - CatInfo *CatInfo `bson:"catInfo,omitempty" json:"catInfo,omitempty"` - } - - CatInfo struct { - CoverUrl string `bson:"coverUrl,omitempty" json:"coverUrl,omitempty"` - Color string `bson:"color,omitempty" json:"color,omitempty"` - Gender string `bson:"gender,omitempty" json:"gender,omitempty"` - BirthDate time.Time `bson:"birthDate,omitempty" json:"birthDate,omitempty"` - } -) - -func NewMongoMapper(config *config.Config) IMongoMapper { - conn := monc.MustNewModel(config.Mongo.URL, config.Mongo.DB, CollectionName, config.CacheConf) - return &MongoMapper{ - conn: conn, - } -} - -func (m *MongoMapper) Upsert(ctx context.Context, data *CatAlbum) error { - key := prefixUserCacheKey + data.ID.Hex() - - filter := bson.M{ - consts.ID: data.ID, - } - - set := bson.M{ - "updatedAt": time.Now(), - "type": data.Type, - "creatorId": data.CreatorId, - "albumName": data.AlbumName, - "visibility": data.Visibility, - "createdLocation": data.CreatedLocation, - "createdLocationLongitude": data.CreatedLocationLongitude, - "createdLocationLatitude": data.CreatedLocationLatitude, - "catInfo": data.CatInfo, - } - - update := bson.M{ - "$set": set, - "$setOnInsert": bson.M{ - consts.ID: data.ID, - consts.CreateAt: time.Now(), - consts.UpdateAt: time.Now(), - }, - } - - option := options.UpdateOptions{} - option.SetUpsert(true) - - _, err := m.conn.UpdateOne(ctx, key, filter, update, &option) - return err -} - -func (m *MongoMapper) Insert(ctx context.Context, data *CatAlbum) error { - if data.ID.IsZero() { - data.ID = primitive.NewObjectID() - data.CreatedAt = time.Now() - data.UpdatedAt = time.Now() - } - - key := prefixUserCacheKey + data.ID.Hex() - _, err := m.conn.InsertOne(ctx, key, data) - return err -} - -func (m *MongoMapper) FindOne(ctx context.Context, id string) (*CatAlbum, error) { - oid, err := primitive.ObjectIDFromHex(id) - if err != nil { - return nil, consts.ErrInvalidObjectId - } - - var data CatAlbum - key := prefixUserCacheKey + id - err = m.conn.FindOne(ctx, key, &data, bson.M{consts.ID: oid}) - switch { - case err == nil: - return &data, nil - case errors.Is(err, monc.ErrNotFound): - return nil, consts.ErrNotFound - default: - return nil, err - } -} - -func (m *MongoMapper) Delete(ctx context.Context, id string) error { - oid, err := primitive.ObjectIDFromHex(id) - if err != nil { - return consts.ErrInvalidObjectId - } - key := prefixUserCacheKey + id - _, err = m.conn.DeleteOne(ctx, key, bson.M{consts.ID: oid}) - return err -} - -func (m *MongoMapper) FindOneNoCache(ctx context.Context, id string) (*CatAlbum, error) { - oid, err := primitive.ObjectIDFromHex(id) - if err != nil { - return nil, consts.ErrInvalidObjectId - } - - var data CatAlbum - err = m.conn.FindOneNoCache(ctx, &data, bson.M{consts.ID: oid}) - switch { - case err == nil: - return &data, nil - case errors.Is(err, monc.ErrNotFound): - return nil, consts.ErrNotFound - default: - return nil, err - } -} - -func (m *MongoMapper) FindMany(ctx context.Context, skip int64, count int64) ([]*CatAlbum, int64, error) { - data := make([]*CatAlbum, 0, 20) - err := m.conn.Find(ctx, &data, bson.M{}, &options.FindOptions{ - Skip: &skip, - Limit: &count, - Sort: bson.M{consts.ID: -1}, - }) - if err != nil { - return nil, 0, err - } - total, err := m.conn.CountDocuments(ctx, bson.M{}) - if err != nil { - return nil, 0, err - } - return data, total, nil -} - -func (m *MongoMapper) FindManyByCreatorId(ctx context.Context, creatorId string, skip int64, count int64) ([]*CatAlbum, int64, error) { - data := make([]*CatAlbum, 0, 20) - err := m.conn.Find(ctx, &data, bson.M{"creatorId": creatorId}, &options.FindOptions{ - Skip: &skip, - Limit: &count, - Sort: bson.M{consts.ID: -1}, - }) - if err != nil { - return nil, 0, err - } - total, err := m.conn.CountDocuments(ctx, bson.M{"creatorId": creatorId}) - if err != nil { - return nil, 0, err - } - return data, total, nil -} diff --git a/biz/infrastructure/mapper/locationalbum/mongo.go b/biz/infrastructure/mapper/locationalbum/mongo.go deleted file mode 100644 index 26c398d..0000000 --- a/biz/infrastructure/mapper/locationalbum/mongo.go +++ /dev/null @@ -1,186 +0,0 @@ -package user - -import ( - "context" - "errors" - "time" - - "github.com/zeromicro/go-zero/core/stores/monc" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo/options" - - "github.com/xh-polaris/meowcloud-user/biz/infrastructure/config" - "github.com/xh-polaris/meowcloud-user/biz/infrastructure/consts" -) - -const ( - prefixUserCacheKey = "cache:user:" - CollectionName = "user" -) - -type ( - // IMongoMapper is an interface to be customized, add more methods here, - // and implement the added methods in MongoMapper. - IMongoMapper interface { - Insert(ctx context.Context, data *User) error - FindOne(ctx context.Context, id string) (*User, error) - Update(ctx context.Context, data *User) error - Delete(ctx context.Context, id string) error - UpsertUser(ctx context.Context, data *User) error - FindOneNoCache(ctx context.Context, id string) (*User, error) - } - - MongoMapper struct { - conn *monc.Model - } - - UserPreview struct { - ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` - Nickname string `bson:"nickname,omitempty" json:"nickname,omitempty"` - Avatar string `bson:"avatar,omitempty" json:"avatar,omitempty"` - } - - User struct { - ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` - Nickname string `bson:"username,omitempty" json:"username,omitempty"` - Bio string `bson:"bio,omitempty" json:"bio,omitempty"` - Avatar string `bson:"avatar,omitempty" json:"avatar,omitempty"` - UpdatedAt time.Time `bson:"updatedAt,omitempty" json:"updatedAt,omitempty"` - CreatedAt time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty"` - DeletedAt time.Time `bson:"deletedAt,omitempty" json:"deletedAt,omitempty"` - Membership *Membership `bson:"membership,omitempty" json:"membership,omitempty"` - TeamCount int32 `bson:"teamCount,omitempty" json:"teamCount,omitempty"` - TeamIds []string `bson:"teamIds,omitempty" json:"teamIds,omitempty"` - MyAlbumCount int32 `bson:"myAlbumCount,omitempty" json:"myAlbumCount,omitempty"` - MyAlbumIds []int32 `bson:"myAlbumIds,omitempty" json:"myAlbumIds,omitempty"` - FollowedAlbumCount int32 `bson:"followedAlbumCount,omitempty" json:"followedAlbumCount,omitempty"` - FollowedAlbumIds []int32 `bson:"followedAlbumIds,omitempty" json:"followedAlbumIds,omitempty"` - StorageInfo *StorageInfo `bson:"storageInfo,omitempty" json:"storageInfo,omitempty"` - Points int32 `bson:"points,omitempty" json:"points,omitempty"` - Achievements []string `bson:"achievements,omitempty" json:"achievements,omitempty"` - } - - Membership struct { - MemberId string `bson:"memberId,omitempty" json:"memberId,omitempty"` - MemberLevel int32 `bson:"memberLevel,omitempty" json:"memberLevel,omitempty"` // 0: 普通用户, 1: 普通会员, 2: 高级会员 - } - - StorageInfo struct { - AvailablePhotos int32 `bson:"availablePhotos,omitempty" json:"availablePhotos,omitempty"` - UsedPhotos int32 `bson:"usedPhotos,omitempty" json:"usedPhotos,omitempty"` - AvailableMemory int64 `bson:"availableMemory,omitempty" json:"availableMemory,omitempty"` - UsedMemory int64 `bson:"usedMemory,omitempty" json:"usedMemory,omitempty"` - AvailableAlbums int32 `bson:"availableAlbums,omitempty" json:"availableAlbums,omitempty"` - UsedAlbums int32 `bson:"usedAlbums,omitempty" json:"usedAlbums,omitempty"` - } -) - -func NewMongoMapper(config *config.Config) IMongoMapper { - conn := monc.MustNewModel(config.Mongo.URL, config.Mongo.DB, CollectionName, config.CacheConf) - return &MongoMapper{ - conn: conn, - } -} - -func (m *MongoMapper) UpsertUser(ctx context.Context, data *User) error { - key := prefixUserCacheKey + data.ID.Hex() - - filter := bson.M{ - consts.ID: data.ID, - } - - set := bson.M{ - consts.UpdateAt: time.Now(), - } - if data.Bio != "" { - set["bio"] = data.Bio - } - if data.Avatar != "" { - set["avatar"] = data.Avatar - } - if data.Nickname != "" { - set["username"] = data.Nickname - } - - update := bson.M{ - "$set": set, - "$setOnInsert": bson.M{ - consts.ID: data.ID, - consts.CreateAt: time.Now(), - consts.UpdateAt: time.Now(), - }, - } - - option := options.UpdateOptions{} - option.SetUpsert(true) - - _, err := m.conn.UpdateOne(ctx, key, filter, update, &option) - return err -} - -func (m *MongoMapper) Insert(ctx context.Context, data *User) error { - if data.ID.IsZero() { - data.ID = primitive.NewObjectID() - data.CreatedAt = time.Now() - data.UpdatedAt = time.Now() - } - - key := prefixUserCacheKey + data.ID.Hex() - _, err := m.conn.InsertOne(ctx, key, data) - return err -} - -func (m *MongoMapper) FindOne(ctx context.Context, id string) (*User, error) { - oid, err := primitive.ObjectIDFromHex(id) - if err != nil { - return nil, consts.ErrInvalidObjectId - } - - var data User - key := prefixUserCacheKey + id - err = m.conn.FindOne(ctx, key, &data, bson.M{consts.ID: oid}) - switch { - case err == nil: - return &data, nil - case errors.Is(err, monc.ErrNotFound): - return nil, consts.ErrNotFound - default: - return nil, err - } -} - -func (m *MongoMapper) Update(ctx context.Context, data *User) error { - data.UpdatedAt = time.Now() - key := prefixUserCacheKey + data.ID.Hex() - _, err := m.conn.UpdateOne(ctx, key, bson.M{consts.ID: data.ID}, bson.M{"$set": data}) - return err -} - -func (m *MongoMapper) Delete(ctx context.Context, id string) error { - oid, err := primitive.ObjectIDFromHex(id) - if err != nil { - return consts.ErrInvalidObjectId - } - key := prefixUserCacheKey + id - _, err = m.conn.DeleteOne(ctx, key, bson.M{consts.ID: oid}) - return err -} - -func (m *MongoMapper) FindOneNoCache(ctx context.Context, id string) (*User, error) { - oid, err := primitive.ObjectIDFromHex(id) - if err != nil { - return nil, consts.ErrInvalidObjectId - } - - var data User - err = m.conn.FindOneNoCache(ctx, &data, bson.M{consts.ID: oid}) - switch { - case err == nil: - return &data, nil - case errors.Is(err, monc.ErrNotFound): - return nil, consts.ErrNotFound - default: - return nil, err - } -} diff --git a/biz/infrastructure/mapper/photo/mongo.go b/biz/infrastructure/mapper/photo/mongo.go deleted file mode 100644 index 26c398d..0000000 --- a/biz/infrastructure/mapper/photo/mongo.go +++ /dev/null @@ -1,186 +0,0 @@ -package user - -import ( - "context" - "errors" - "time" - - "github.com/zeromicro/go-zero/core/stores/monc" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/bson/primitive" - "go.mongodb.org/mongo-driver/mongo/options" - - "github.com/xh-polaris/meowcloud-user/biz/infrastructure/config" - "github.com/xh-polaris/meowcloud-user/biz/infrastructure/consts" -) - -const ( - prefixUserCacheKey = "cache:user:" - CollectionName = "user" -) - -type ( - // IMongoMapper is an interface to be customized, add more methods here, - // and implement the added methods in MongoMapper. - IMongoMapper interface { - Insert(ctx context.Context, data *User) error - FindOne(ctx context.Context, id string) (*User, error) - Update(ctx context.Context, data *User) error - Delete(ctx context.Context, id string) error - UpsertUser(ctx context.Context, data *User) error - FindOneNoCache(ctx context.Context, id string) (*User, error) - } - - MongoMapper struct { - conn *monc.Model - } - - UserPreview struct { - ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` - Nickname string `bson:"nickname,omitempty" json:"nickname,omitempty"` - Avatar string `bson:"avatar,omitempty" json:"avatar,omitempty"` - } - - User struct { - ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"` - Nickname string `bson:"username,omitempty" json:"username,omitempty"` - Bio string `bson:"bio,omitempty" json:"bio,omitempty"` - Avatar string `bson:"avatar,omitempty" json:"avatar,omitempty"` - UpdatedAt time.Time `bson:"updatedAt,omitempty" json:"updatedAt,omitempty"` - CreatedAt time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty"` - DeletedAt time.Time `bson:"deletedAt,omitempty" json:"deletedAt,omitempty"` - Membership *Membership `bson:"membership,omitempty" json:"membership,omitempty"` - TeamCount int32 `bson:"teamCount,omitempty" json:"teamCount,omitempty"` - TeamIds []string `bson:"teamIds,omitempty" json:"teamIds,omitempty"` - MyAlbumCount int32 `bson:"myAlbumCount,omitempty" json:"myAlbumCount,omitempty"` - MyAlbumIds []int32 `bson:"myAlbumIds,omitempty" json:"myAlbumIds,omitempty"` - FollowedAlbumCount int32 `bson:"followedAlbumCount,omitempty" json:"followedAlbumCount,omitempty"` - FollowedAlbumIds []int32 `bson:"followedAlbumIds,omitempty" json:"followedAlbumIds,omitempty"` - StorageInfo *StorageInfo `bson:"storageInfo,omitempty" json:"storageInfo,omitempty"` - Points int32 `bson:"points,omitempty" json:"points,omitempty"` - Achievements []string `bson:"achievements,omitempty" json:"achievements,omitempty"` - } - - Membership struct { - MemberId string `bson:"memberId,omitempty" json:"memberId,omitempty"` - MemberLevel int32 `bson:"memberLevel,omitempty" json:"memberLevel,omitempty"` // 0: 普通用户, 1: 普通会员, 2: 高级会员 - } - - StorageInfo struct { - AvailablePhotos int32 `bson:"availablePhotos,omitempty" json:"availablePhotos,omitempty"` - UsedPhotos int32 `bson:"usedPhotos,omitempty" json:"usedPhotos,omitempty"` - AvailableMemory int64 `bson:"availableMemory,omitempty" json:"availableMemory,omitempty"` - UsedMemory int64 `bson:"usedMemory,omitempty" json:"usedMemory,omitempty"` - AvailableAlbums int32 `bson:"availableAlbums,omitempty" json:"availableAlbums,omitempty"` - UsedAlbums int32 `bson:"usedAlbums,omitempty" json:"usedAlbums,omitempty"` - } -) - -func NewMongoMapper(config *config.Config) IMongoMapper { - conn := monc.MustNewModel(config.Mongo.URL, config.Mongo.DB, CollectionName, config.CacheConf) - return &MongoMapper{ - conn: conn, - } -} - -func (m *MongoMapper) UpsertUser(ctx context.Context, data *User) error { - key := prefixUserCacheKey + data.ID.Hex() - - filter := bson.M{ - consts.ID: data.ID, - } - - set := bson.M{ - consts.UpdateAt: time.Now(), - } - if data.Bio != "" { - set["bio"] = data.Bio - } - if data.Avatar != "" { - set["avatar"] = data.Avatar - } - if data.Nickname != "" { - set["username"] = data.Nickname - } - - update := bson.M{ - "$set": set, - "$setOnInsert": bson.M{ - consts.ID: data.ID, - consts.CreateAt: time.Now(), - consts.UpdateAt: time.Now(), - }, - } - - option := options.UpdateOptions{} - option.SetUpsert(true) - - _, err := m.conn.UpdateOne(ctx, key, filter, update, &option) - return err -} - -func (m *MongoMapper) Insert(ctx context.Context, data *User) error { - if data.ID.IsZero() { - data.ID = primitive.NewObjectID() - data.CreatedAt = time.Now() - data.UpdatedAt = time.Now() - } - - key := prefixUserCacheKey + data.ID.Hex() - _, err := m.conn.InsertOne(ctx, key, data) - return err -} - -func (m *MongoMapper) FindOne(ctx context.Context, id string) (*User, error) { - oid, err := primitive.ObjectIDFromHex(id) - if err != nil { - return nil, consts.ErrInvalidObjectId - } - - var data User - key := prefixUserCacheKey + id - err = m.conn.FindOne(ctx, key, &data, bson.M{consts.ID: oid}) - switch { - case err == nil: - return &data, nil - case errors.Is(err, monc.ErrNotFound): - return nil, consts.ErrNotFound - default: - return nil, err - } -} - -func (m *MongoMapper) Update(ctx context.Context, data *User) error { - data.UpdatedAt = time.Now() - key := prefixUserCacheKey + data.ID.Hex() - _, err := m.conn.UpdateOne(ctx, key, bson.M{consts.ID: data.ID}, bson.M{"$set": data}) - return err -} - -func (m *MongoMapper) Delete(ctx context.Context, id string) error { - oid, err := primitive.ObjectIDFromHex(id) - if err != nil { - return consts.ErrInvalidObjectId - } - key := prefixUserCacheKey + id - _, err = m.conn.DeleteOne(ctx, key, bson.M{consts.ID: oid}) - return err -} - -func (m *MongoMapper) FindOneNoCache(ctx context.Context, id string) (*User, error) { - oid, err := primitive.ObjectIDFromHex(id) - if err != nil { - return nil, consts.ErrInvalidObjectId - } - - var data User - err = m.conn.FindOneNoCache(ctx, &data, bson.M{consts.ID: oid}) - switch { - case err == nil: - return &data, nil - case errors.Is(err, monc.ErrNotFound): - return nil, consts.ErrNotFound - default: - return nil, err - } -}