GeezerVison is showing github.com — refactor(private-api): add group to topic detail response (#212) · gitLibs/server@ad69a80
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: internal/dal/dao/topic.go +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -74,11 +74,11 @@ func (t *SubjectTopic) GetReplies() uint32 { | 74 | 74 | return t.Replies | 75 | 75 | } | 76 | 76 |
| 77 | | -func (t *SubjectTopic) GetObjectID() uint32 { | | 77 | +func (t *SubjectTopic) GetParentID() uint32 { | 78 | 78 | return t.SubjectID | 79 | 79 | } | 80 | 80 |
| 81 | | -func (t *GroupTopic) GetObjectID() uint32 { | | 81 | +func (t *GroupTopic) GetParentID() uint32 { | 82 | 82 | return t.GroupID | 83 | 83 | } | 84 | 84 |
| |
Collapse file Copy file name to clipboardExpand all lines: internal/domain/group.go +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -22,6 +22,7 @@ import ( | 22 | 22 |
| 23 | 23 | type GroupRepo interface { | 24 | 24 | GetByName(ctx context.Context, name string) (model.Group, error) | | 25 | + GetByID(ctx context.Context, id model.GroupID) (model.Group, error) | 25 | 26 |
| 26 | 27 | CountMembersByID(ctx context.Context, id model.GroupID, memberType GroupMemberType) (int64, error) | 27 | 28 | ListMembersByID( | |
Collapse file Copy file name to clipboardExpand all lines: internal/group/mysql_repo.go +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,6 +55,20 @@ func (r mysqlRepo) getIDByName(ctx context.Context, name string) (model.GroupID, | 55 | 55 | return g.ID, nil | 56 | 56 | } | 57 | 57 |
| | 58 | +func (r mysqlRepo) GetByID(ctx context.Context, id model.GroupID) (model.Group, error) { | | 59 | + g, err := r.q.Group.WithContext(ctx).Where(r.q.Group.ID.Eq(id)).First() | | 60 | + if err != nil { | | 61 | + if errors.Is(err, gorm.ErrRecordNotFound) { | | 62 | + return model.Group{}, domain.ErrNotFound | | 63 | + } | | 64 | + | | 65 | + r.log.Error("un-expected error when getting single group", zap.Error(err), id.Zap()) | | 66 | + return model.Group{}, errgo.Wrap(err, "dal") | | 67 | + } | | 68 | + | | 69 | + return convertDao(g), nil | | 70 | +} | | 71 | + | 58 | 72 | func (r mysqlRepo) GetByName(ctx context.Context, name string) (model.Group, error) { | 59 | 73 | g, err := r.q.Group.WithContext(ctx).Where(r.q.Group.Name.Eq(name)).First() | 60 | 74 | if err != nil { | |
Collapse file Copy file name to clipboardExpand all lines: internal/group/mysql_repo_test.go +37-2Lines changed: 37 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,6 +32,9 @@ import ( | 32 | 32 | "github.com/bangumi/server/internal/pkg/test" | 33 | 33 | ) | 34 | 34 |
| | 35 | +const notFoundGroupID = 600 | | 36 | +const notFoundGroupName = "not-exist-group-name" | | 37 | + | 35 | 38 | func getRepo(t *testing.T) (domain.GroupRepo, *query.Query) { | 36 | 39 | t.Helper() | 37 | 40 | q := query.Use(test.GetGorm(t)) | @@ -144,12 +147,41 @@ func assertHaveID(t *testing.T, members []model.GroupMember, id ...model.UserID) | 144 | 147 | } | 145 | 148 | } | 146 | 149 |
| 147 | | -// $ task test-all -- -run '^TestMysqlRepo_GetByName$' | | 150 | +func TestMysqlRepo_GetByID(t *testing.T) { | | 151 | + test.RequireEnv(t, "mysql") | | 152 | + t.Parallel() | | 153 | + const groupID model.GroupID = 201 | | 154 | + const groupName = "group-201" | | 155 | + | | 156 | + repo, q := getRepo(t) | | 157 | + | | 158 | + test.RunAndCleanup(t, func() { | | 159 | + _, err := q.WithContext(context.Background()).Group.Where(q.Group.ID.Eq(groupID)).Delete() | | 160 | + assert.NoError(t, err) | | 161 | + }) | | 162 | + | | 163 | + err := q.WithContext(context.Background()).Group.Create(&dao.Group{ | | 164 | + ID: groupID, | | 165 | + Name: groupName, | | 166 | + CreatorID: 1, | | 167 | + CreatedTime: uint32(time.Now().Unix()), | | 168 | + }) | | 169 | + require.NoError(t, err) | | 170 | + | | 171 | + g, err := repo.GetByID(context.Background(), groupID) | | 172 | + require.NoError(t, err) | | 173 | + require.Equal(t, groupID, g.ID) | | 174 | + require.Equal(t, groupName, g.Name) | | 175 | + | | 176 | + _, err = repo.GetByID(context.Background(), notFoundGroupID) | | 177 | + require.ErrorIs(t, err, domain.ErrNotFound) | | 178 | +} | | 179 | + | 148 | 180 | func TestMysqlRepo_GetByName(t *testing.T) { | 149 | 181 | test.RequireEnv(t, "mysql") | 150 | 182 | t.Parallel() | 151 | 183 | const groupID model.GroupID = 200 | 152 | | - const groupName = "aabbccdeeeffgg" | | 184 | + const groupName = "group-200" | 153 | 185 |
| 154 | 186 | repo, q := getRepo(t) | 155 | 187 |
| @@ -170,6 +202,9 @@ func TestMysqlRepo_GetByName(t *testing.T) { | 170 | 202 | require.NoError(t, err) | 171 | 203 | require.Equal(t, groupID, g.ID) | 172 | 204 | require.Equal(t, groupName, g.Name) | | 205 | + | | 206 | + _, err = repo.GetByName(context.Background(), notFoundGroupName) | | 207 | + require.ErrorIs(t, err, domain.ErrNotFound) | 173 | 208 | } | 174 | 209 |
| 175 | 210 | // $ task test-all -- -run '^TestMysqlRepo_GetByName_not_found' | |
Collapse file Copy file name to clipboardExpand all lines: internal/mocks/GroupRepo.go +55-10Lines changed: 55 additions & 10 deletions Collapse file Copy file name to clipboardExpand all lines: internal/model/topic.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,7 +23,7 @@ type Topic struct { | 23 | 23 | ID TopicID | 24 | 24 | CreatorID UserID | 25 | 25 | Replies uint32 | 26 | | - ObjectID uint32 | | 26 | + ParentID uint32 | 27 | 27 | State CommentState | 28 | 28 | Status TopicStatus | 29 | 29 | } | |
Collapse file Copy file name to clipboardExpand all lines: internal/model/type.go +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 |
| 15 | 15 | package model | 16 | 16 |
| 17 | | -import "database/sql/driver" | | 17 | +import ( | | 18 | + "database/sql/driver" | | 19 | + | | 20 | + "go.uber.org/zap" | | 21 | +) | 18 | 22 |
| 19 | 23 | type EpisodeID uint32 | 20 | 24 | type SubjectID uint32 | @@ -61,3 +65,7 @@ func (v EpisodeID) Value() (driver.Value, error) { | 61 | 65 | func (v GroupID) Value() (driver.Value, error) { | 62 | 66 | return int64(v), nil | 63 | 67 | } | | 68 | + | | 69 | +func (v GroupID) Zap() zap.Field { | | 70 | + return zap.Uint32("group_id", uint32(v)) | | 71 | +} |
Collapse file Copy file name to clipboardExpand all lines: internal/topic/mysql_compat.go +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,7 +29,7 @@ type mysqlTopic interface { | 29 | 29 | GetCreatorID() uint32 | 30 | 30 | GetState() uint8 | 31 | 31 | GetReplies() uint32 | 32 | | - GetObjectID() uint32 | | 32 | + GetParentID() uint32 | 33 | 33 | GetStatus() uint8 | 34 | 34 | } | 35 | 35 |
| @@ -51,7 +51,7 @@ func wrapDao[T mysqlTopic](data []T, err error) ([]model.Topic, error) { | 51 | 51 | CreatorID: model.UserID(item.GetCreatorID()), | 52 | 52 | State: model.CommentState(item.GetState()), | 53 | 53 | Replies: item.GetReplies(), | 54 | | - ObjectID: item.GetObjectID(), | | 54 | + ParentID: item.GetParentID(), | 55 | 55 | Status: model.TopicStatus(item.GetStatus()), | 56 | 56 | } | 57 | 57 | } | @@ -67,7 +67,7 @@ type mysqlComment interface { | 67 | 67 | GetContent() string | 68 | 68 | GetState() uint8 | 69 | 69 | RelatedTo() model.CommentID | 70 | | - // GetObjectID() uint32 | | 70 | + // GetParentID() uint32 | 71 | 71 | } | 72 | 72 |
| 73 | 73 | func wrapCommentDao[T mysqlComment](data []T, err error) ([]mysqlComment, error) { | |
Collapse file Copy file name to clipboardExpand all lines: internal/topic/mysql_repo_topic.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -57,7 +57,7 @@ func (r mysqlRepo) Get(ctx context.Context, topicType domain.TopicType, id model | 57 | 57 | CreatorID: model.UserID(topic.GetCreatorID()), | 58 | 58 | State: model.CommentState(topic.GetState()), | 59 | 59 | Replies: topic.GetReplies(), | 60 | | - ObjectID: topic.GetObjectID(), | | 60 | + ParentID: topic.GetParentID(), | 61 | 61 | Status: model.TopicStatus(topic.GetStatus()), | 62 | 62 | }, nil | 63 | 63 | } | |
Collapse file Copy file name to clipboardExpand all lines: internal/web/handler.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -128,7 +128,7 @@ func AddRouters( | 128 | 128 | private.Get("/groups/:name/topics", addMetrics(h.ListGroupTopics)) | 129 | 129 | private.Get("/subjects/:id/topics", addMetrics(h.ListSubjectTopics)) | 130 | 130 |
| 131 | | - private.Get("/groups/:name/topics/:topic_id", addMetrics(h.GetGroupTopic)) | | 131 | + private.Get("/groups/-/topics/:topic_id", addMetrics(h.GetGroupTopic)) | 132 | 132 | private.Get("/subjects/:id/topics/:topic_id", addMetrics(h.GetSubjectTopic)) | 133 | 133 | private.Get("/indices/:id/comments", addMetrics(h.GetIndexComments)) | 134 | 134 | private.Get("/episodes/:id/comments", addMetrics(h.GetEpisodeComments)) | |
|