-
Notifications
You must be signed in to change notification settings - Fork 57
[WIP] relationdomain refactor ( fork from old PR ) #145
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
Open
xwxb
wants to merge
8
commits into
TremblingV5:main
Choose a base branch
from
xwxb:refactor/relationDomain
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6e2da4f
refactor: relationDomain service
9e936e6
test: add simple ut
bf3743e
style: rename for cleaner
eea135c
refactor: remove RelationDomainServiceImpl
a803798
refactor: 主要是 redis 部分 oop 的部分重构
1223c33
remove relationdomain handler ut
9669ae1
Merge branch 'main' into refactor/relationDomain
ebd0de3
fix: remove some comment code and change repo func
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
applications/relationDomain/dal/repository/relation/repository.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package relation | ||
|
|
||
| import ( | ||
| "github.com/TremblingV5/DouTok/applications/relationDomain/dal/model" | ||
| "github.com/TremblingV5/DouTok/applications/relationDomain/dal/query" | ||
| "github.com/TremblingV5/DouTok/applications/relationDomain/pack" | ||
| "github.com/TremblingV5/DouTok/pkg/utils" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| type Repository interface { | ||
| CreateOrUpdate(relation *pack.Relation) error | ||
| CreateList(relationList []*pack.Relation) error | ||
| } | ||
|
|
||
| type PersistRepository struct { | ||
| relation query.IRelationDo | ||
| } | ||
|
|
||
| func New(db *gorm.DB) *PersistRepository { | ||
| return &PersistRepository{ | ||
| relation: query.Relation.WithContext(db.Statement.Context), | ||
| } | ||
| } | ||
|
|
||
| func packToModel(rel *pack.Relation) *model.Relation { | ||
| return &model.Relation{ | ||
| UserId: rel.UserId, | ||
| ToUserId: rel.ToUserId, | ||
| Status: int(rel.ActionType), | ||
| } | ||
| } | ||
|
|
||
| func (p *PersistRepository) CreateOrUpdate(rel *pack.Relation) error { | ||
| res, err := p.relation.Where( | ||
| query.Relation.UserId.Eq(rel.UserId), | ||
| query.Relation.ToUserId.Eq(rel.ToUserId), | ||
| ).Find() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if len(res) > 0 { | ||
| // 已经存在关注关系 | ||
| _, err := p.relation.Where( | ||
| query.Relation.UserId.Eq(rel.UserId), | ||
| query.Relation.ToUserId.Eq(rel.ToUserId), | ||
| ).Update( | ||
| query.Relation.Status, rel.ActionType, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } else { | ||
| // 不存在则插入 | ||
| id := utils.GetSnowFlakeId().Int64() | ||
| err := p.relation.Create( | ||
| &model.Relation{ | ||
| ID: id, | ||
| UserId: rel.UserId, | ||
| ToUserId: rel.ToUserId, | ||
| Status: int(rel.ActionType), | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (p *PersistRepository) CreateList(relations []*pack.Relation) error { | ||
| models := make([]*model.Relation, 0, len(relations)) | ||
| for _, relation := range relations { | ||
| models = append(models, packToModel(relation)) | ||
| } | ||
| err := p.relation.CreateInBatches(models, len(models)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "context" | ||
| "github.com/TremblingV5/DouTok/applications/relationDomain/pack" | ||
| "github.com/TremblingV5/DouTok/kitex_gen/relationDomain" | ||
| "github.com/TremblingV5/DouTok/pkg/errno" | ||
| ) | ||
|
|
||
| func (h *Handler) AddRelation(ctx context.Context, req *relationDomain.DoutokAddRelationRequest) (resp *relationDomain.DoutokAddRelationResponse, err error) { | ||
| resp = new(relationDomain.DoutokAddRelationResponse) | ||
|
|
||
| err = h.service.AddRelation(ctx, req.UserId, req.ToUserId) | ||
| if err != nil { | ||
| pack.BuildRelationActionResp(err, resp) | ||
| return resp, nil | ||
| } | ||
| pack.BuildRelationActionResp(errno.Success, resp) | ||
| return resp, nil | ||
| } | ||
|
|
||
| func (h *Handler) RmRelation(ctx context.Context, req *relationDomain.DoutokRmRelationRequest) (resp *relationDomain.DoutokRmRelationResponse, err error) { | ||
| resp = new(relationDomain.DoutokRmRelationResponse) | ||
|
|
||
| err = h.service.RmRelation(ctx, req.UserId, req.ToUserId) | ||
| if err != nil { | ||
| pack.BuildRmRelationActionResp(err, resp) | ||
| return resp, nil | ||
| } | ||
| pack.BuildRmRelationActionResp(errno.Success, resp) | ||
| return resp, nil | ||
| } | ||
|
|
||
| func (h *Handler) ListRelation(ctx context.Context, req *relationDomain.DoutokListRelationRequest) (resp *relationDomain.DoutokListRelationResponse, err error) { | ||
|
|
||
| resp = new(relationDomain.DoutokListRelationResponse) | ||
| if req.ActionType == 0 { | ||
| // 关注 | ||
| followList, err := h.service.ListFollowList(ctx, req.UserId) | ||
| if err != nil { | ||
| pack.BuildRelationFollowListResp(err, resp) | ||
| return resp, nil | ||
| } | ||
| resp.UserList = followList | ||
| pack.BuildRelationFollowListResp(errno.Success, resp) | ||
| return resp, nil | ||
| } else if req.ActionType == 1 { | ||
| // 粉丝 | ||
| // 关注 | ||
| followerList, err := h.service.ListFollowerList(ctx, req.UserId) | ||
| if err != nil { | ||
| pack.BuildRelationFollowListResp(err, resp) | ||
| return resp, nil | ||
| } | ||
| resp.UserList = followerList | ||
| pack.BuildRelationFollowListResp(errno.Success, resp) | ||
| return resp, nil | ||
| } else if req.ActionType == 2 { | ||
| // 互相关注 | ||
| // 关注 | ||
| friendList, err := h.service.ListFriendList(ctx, req.UserId) | ||
| if err != nil { | ||
| pack.BuildRelationFollowListResp(err, resp) | ||
| return resp, nil | ||
| } | ||
| resp.UserList = friendList | ||
| pack.BuildRelationFollowListResp(errno.Success, resp) | ||
| return resp, nil | ||
| } | ||
| return resp, nil | ||
| } | ||
|
|
||
| func (h *Handler) CountRelation(ctx context.Context, req *relationDomain.DoutokCountRelationRequest) (resp *relationDomain.DoutokCountRelationResponse, err error) { | ||
| resp = &relationDomain.DoutokCountRelationResponse{ | ||
| Result: make(map[int64]int64), | ||
| } | ||
|
|
||
| /* | ||
| 0 -> 关注数 1 -> 粉丝数 | ||
| */ | ||
| if req.ActionType == 0 { | ||
| for _, v := range req.UserId { | ||
| follow, err := h.service.GetFollowCount(ctx, v) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| resp.Result[v] = follow | ||
| } | ||
| } else if req.ActionType == 1 { | ||
| for _, v := range req.UserId { | ||
| follower, err := h.service.GetFollowerCount(ctx, v) | ||
| if err != nil { | ||
| continue | ||
| } | ||
| resp.Result[v] = follower | ||
| } | ||
| } | ||
|
|
||
| pack.BuildRelationCountResp(errno.Success, resp) | ||
| return resp, nil | ||
| } |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,15 @@ | ||
| package handler | ||
|
|
||
| type RelationDomainServiceImpl struct{} | ||
| import ( | ||
| "github.com/TremblingV5/DouTok/applications/relationDomain/service" | ||
| ) | ||
|
|
||
| type Handler struct { | ||
| service *service.Service | ||
| } | ||
|
|
||
| func New(service *service.Service) *Handler { | ||
| return &Handler{ | ||
| service: service, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.