Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 完成轮播图的基础功能 #2

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions biz/adaptor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ type SystemServerImpl struct {
SystemService service.SystemService
}

func (s *SystemServerImpl) GetSliders(ctx context.Context, req *system.GetSlidersReq) (resp *system.GetSlidersResp, err error) {
return s.SystemService.GetSliders(ctx, req)
}

func (s *SystemServerImpl) CreateSlider(ctx context.Context, req *system.CreateSliderReq) (resp *system.CreateSliderResp, err error) {
return s.SystemService.CreateSlider(ctx, req)
}

func (s *SystemServerImpl) UpdateSlider(ctx context.Context, req *system.UpdateSliderReq) (resp *system.UpdateSliderResp, err error) {
return s.SystemService.UpdateSlider(ctx, req)
}

func (s *SystemServerImpl) DeleteSlider(ctx context.Context, req *system.DeleteSliderReq) (resp *system.DeleteSliderResp, err error) {
return s.SystemService.DeleteSlider(ctx, req)
}

func (s *SystemServerImpl) ReadNotifications(ctx context.Context, req *system.ReadNotificationsReq) (res *system.ReadNotificationsResp, err error) {
return s.SystemService.ReadNotifications(ctx, req)
}
Expand Down
62 changes: 55 additions & 7 deletions biz/application/service/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"github.com/CloudStriver/cloudmind-system/biz/infrastructure/convertor"
notificationmapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/notification"
slidermapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/slider"
"github.com/CloudStriver/go-pkg/utils/pagination/mongop"
"github.com/CloudStriver/go-pkg/utils/pconvertor"
gensystem "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/system"
Expand All @@ -18,15 +19,62 @@ type SystemService interface {
CreateNotification(ctx context.Context, req *gensystem.CreateNotificationReq) (resp *gensystem.CreateNotificationResp, err error)
ReadNotifications(ctx context.Context, req *gensystem.ReadNotificationsReq) (resp *gensystem.ReadNotificationsResp, err error)
CleanNotification(ctx context.Context, req *gensystem.CleanNotificationReq) (resp *gensystem.CleanNotificationResp, err error)
DeleteSlider(ctx context.Context, req *gensystem.DeleteSliderReq) (resp *gensystem.DeleteSliderResp, err error)
UpdateSlider(ctx context.Context, req *gensystem.UpdateSliderReq) (resp *gensystem.UpdateSliderResp, err error)
CreateSlider(ctx context.Context, req *gensystem.CreateSliderReq) (resp *gensystem.CreateSliderResp, err error)
GetSliders(ctx context.Context, req *gensystem.GetSlidersReq) (resp *gensystem.GetSlidersResp, err error)
}

type SystemServiceImpl struct {
MongoMapper notificationmapper.INotificationMongoMapper
NotificationMongoMapper notificationmapper.INotificationMongoMapper
SliderMongoMapper slidermapper.ISliderMongoMapper
}

func (s *SystemServiceImpl) DeleteSlider(ctx context.Context, req *gensystem.DeleteSliderReq) (resp *gensystem.DeleteSliderResp, err error) {
resp = new(gensystem.DeleteSliderResp)
if err = s.SliderMongoMapper.DeleteOne(ctx, req.SliderId); err != nil {
return resp, err
}
return resp, nil
}

func (s *SystemServiceImpl) UpdateSlider(ctx context.Context, req *gensystem.UpdateSliderReq) (resp *gensystem.UpdateSliderResp, err error) {
resp = new(gensystem.UpdateSliderResp)
if err = s.SliderMongoMapper.UpdateOne(ctx, convertor.SliderToSliderMapper(req.Slider)); err != nil {
return resp, err
}
return resp, nil
}

func (s *SystemServiceImpl) CreateSlider(ctx context.Context, req *gensystem.CreateSliderReq) (resp *gensystem.CreateSliderResp, err error) {
resp = new(gensystem.CreateSliderResp)
if err = s.SliderMongoMapper.InsertOne(ctx, convertor.SliderToSliderMapper(req.Slider)); err != nil {
return resp, err
}
return resp, nil
}

func (s *SystemServiceImpl) GetSliders(ctx context.Context, req *gensystem.GetSlidersReq) (resp *gensystem.GetSlidersResp, err error) {
resp = new(gensystem.GetSlidersResp)
p := pconvertor.PaginationOptionsToModelPaginationOptions(req.PaginationOptions)
sliders, total, err := s.SliderMongoMapper.GetSlidersAndCount(ctx, convertor.SliderFilterOptionsToMapper(req.FilterOptions), p, mongop.IdCursorType)
if err != nil {
return resp, err
}
if p.LastToken != nil {
resp.Token = *p.LastToken
}
resp.Sliders = lo.Map[*slidermapper.Slider, *gensystem.Slider](sliders,
func(item *slidermapper.Slider, _ int) *gensystem.Slider {
return convertor.SliderMapperToSlider(item)
})
resp.Total = total
return resp, nil
}

func (s *SystemServiceImpl) ReadNotification(ctx context.Context, req *gensystem.ReadNotificationReq) (resp *gensystem.ReadNotificationResp, err error) {
resp = new(gensystem.ReadNotificationResp)
if err = s.MongoMapper.ReadNotification(ctx, req.NotificationId); err != nil {
if err = s.NotificationMongoMapper.ReadNotification(ctx, req.NotificationId); err != nil {
return resp, err
}
return resp, nil
Expand All @@ -35,7 +83,7 @@ func (s *SystemServiceImpl) ReadNotification(ctx context.Context, req *gensystem
func (s *SystemServiceImpl) GetNotifications(ctx context.Context, req *gensystem.GetNotificationsReq) (resp *gensystem.GetNotificationsResp, err error) {
resp = new(gensystem.GetNotificationsResp)
p := pconvertor.PaginationOptionsToModelPaginationOptions(req.PaginationOptions)
notifications, total, err := s.MongoMapper.GetNotificationsAndCount(ctx, convertor.NotificationFilterOptionsToMapper(req.FilterOptions), p, mongop.IdCursorType)
notifications, total, err := s.NotificationMongoMapper.GetNotificationsAndCount(ctx, convertor.NotificationFilterOptionsToMapper(req.FilterOptions), p, mongop.IdCursorType)
if err != nil {
return resp, err
}
Expand All @@ -52,31 +100,31 @@ func (s *SystemServiceImpl) GetNotifications(ctx context.Context, req *gensystem

func (s *SystemServiceImpl) CleanNotification(ctx context.Context, req *gensystem.CleanNotificationReq) (resp *gensystem.CleanNotificationResp, err error) {
resp = new(gensystem.CleanNotificationResp)
if err = s.MongoMapper.CleanNotification(ctx, req.UserId); err != nil {
if err = s.NotificationMongoMapper.CleanNotification(ctx, req.UserId); err != nil {
return resp, err
}
return resp, nil
}

func (s *SystemServiceImpl) GetNotificationCount(ctx context.Context, req *gensystem.GetNotificationCountReq) (resp *gensystem.GetNotificationCountResp, err error) {
resp = new(gensystem.GetNotificationCountResp)
if resp.Total, err = s.MongoMapper.Count(ctx, convertor.NotificationFilterOptionsToMapper(req.FilterOptions)); err != nil {
if resp.Total, err = s.NotificationMongoMapper.Count(ctx, convertor.NotificationFilterOptionsToMapper(req.FilterOptions)); err != nil {
return resp, err
}
return resp, nil
}

func (s *SystemServiceImpl) CreateNotification(ctx context.Context, req *gensystem.CreateNotificationReq) (resp *gensystem.CreateNotificationResp, err error) {
resp = new(gensystem.CreateNotificationResp)
if err = s.MongoMapper.InsertOne(ctx, convertor.NotificationToNotificationMapper(req.Notification)); err != nil {
if err = s.NotificationMongoMapper.InsertOne(ctx, convertor.NotificationToNotificationMapper(req.Notification)); err != nil {
return resp, err
}
return resp, nil
}

func (s *SystemServiceImpl) ReadNotifications(ctx context.Context, req *gensystem.ReadNotificationsReq) (resp *gensystem.ReadNotificationsResp, err error) {
resp = new(gensystem.ReadNotificationsResp)
if err = s.MongoMapper.ReadNotifications(ctx, convertor.NotificationFilterOptionsToMapper(req.FilterOptions)); err != nil {
if err = s.NotificationMongoMapper.ReadNotifications(ctx, convertor.NotificationFilterOptionsToMapper(req.FilterOptions)); err != nil {
return resp, err
}
return resp, nil
Expand Down
17 changes: 10 additions & 7 deletions biz/infrastructure/consts/field.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package consts

const (
NotificationId = "_id"
CreateAt = "createAt"
TargetUserId = "targetUserId"
IsRead = "isRead"
UpdateAt = "updateAt"
Type = "type"
TargetType = "targetType"
ID = "_id"
CreateAt = "createAt"
TargetUserId = "targetUserId"
IsRead = "isRead"
UpdateAt = "updateAt"
Type = "type"
TargetType = "targetType"
ImageUrl = "imageUrl"
LinkUrl = "linkUrl"
IsPublic = "isPublic"
)
34 changes: 34 additions & 0 deletions biz/infrastructure/convertor/convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package convertor

import (
notificationmapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/notification"
slidermapper "github.com/CloudStriver/cloudmind-system/biz/infrastructure/mapper/slider"
gensystem "github.com/CloudStriver/service-idl-gen-go/kitex_gen/cloudmind/system"
"go.mongodb.org/mongo-driver/bson/primitive"
)
Expand Down Expand Up @@ -47,3 +48,36 @@ func NotificationFilterOptionsToMapper(in *gensystem.NotificationFilterOptions)
OnlyIsRead: in.OnlyIsRead,
}
}

func SliderToSliderMapper(in *gensystem.Slider) *slidermapper.Slider {
oid, _ := primitive.ObjectIDFromHex(in.SliderId)
return &slidermapper.Slider{
ID: oid,
ImageUrl: in.ImageUrl,
LinkUrl: in.LinkUrl,
Type: in.Type,
IsPublic: in.IsPublic,
}
}

func SliderMapperToSlider(in *slidermapper.Slider) *gensystem.Slider {
return &gensystem.Slider{
SliderId: in.ID.Hex(),
ImageUrl: in.ImageUrl,
LinkUrl: in.LinkUrl,
Type: in.Type,
IsPublic: in.IsPublic,
CreateTime: in.CreateAt.UnixMilli(),
UpdateTime: in.UpdateAt.UnixMilli(),
}
}

func SliderFilterOptionsToMapper(in *gensystem.SliderFilterOptions) *slidermapper.FilterOptions {
if in == nil {
return &slidermapper.FilterOptions{}
}
return &slidermapper.FilterOptions{
OnlyType: in.OnlyType,
OnlyIsPublic: in.OnlyIsPublic,
}
}
2 changes: 1 addition & 1 deletion biz/infrastructure/mapper/notification/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (f *MongoFilter) CheckRange() {
if f.OnlyLastId != nil && f.OnlyFirstId != nil {
firstId, _ := primitive.ObjectIDFromHex(*f.OnlyFirstId)
lastId, _ := primitive.ObjectIDFromHex(*f.OnlyLastId)
f.m[consts.NotificationId] = bson.M{"$gte": firstId, "$lte": lastId}
f.m[consts.ID] = bson.M{"$gte": firstId, "$lte": lastId}
}
}

Expand Down
41 changes: 41 additions & 0 deletions biz/infrastructure/mapper/slider/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package slider

import (
"github.com/CloudStriver/cloudmind-system/biz/infrastructure/consts"
"go.mongodb.org/mongo-driver/bson"
)

type FilterOptions struct {
OnlyType *int64
OnlyIsPublic *int64
}

type MongoFilter struct {
m bson.M
*FilterOptions
}

func MakeBsonFilter(options *FilterOptions) bson.M {
return (&MongoFilter{
m: bson.M{},
FilterOptions: options,
}).toBson()
}

func (f *MongoFilter) toBson() bson.M {
f.CheckOnlyType()
f.CheckOnlyIsPublic()
return f.m
}

func (f *MongoFilter) CheckOnlyType() {
if f.OnlyType != nil {
f.m[consts.Type] = *f.OnlyType
}
}

func (f *MongoFilter) CheckOnlyIsPublic() {
if f.OnlyIsPublic != nil {
f.m[consts.IsPublic] = *f.OnlyIsPublic
}
}
Loading
Loading