GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — refactor(private-api): add group to topic detail response (#212) · gitLibs/server@ad69a80

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • internal
    • dal/dao
      • topic.go
    • domain
      • group.go
    • group
      • mysql_repo.go
      • mysql_repo_test.go
    • mocks
      • GroupRepo.go
    • model
      • topic.go
      • type.go
    • topic
      • mysql_compat.go
      • mysql_repo_topic.go
    • web
      • handler.go
      • handler
        • comments.go
        • comments_test.go
        • topic.go
      • res
        • group_private.go
        • topic.go
  • openapi
    • components/private
      • group_profile.yaml
      • private_topic_detail.yaml
    • private.yaml
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎internal/dal/dao/topic.go‎

Copy file name to clipboardExpand all lines: internal/dal/dao/topic.go
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ func (t *SubjectTopic) GetReplies() uint32 {
7474
return t.Replies
7575
}
7676

77-
func (t *SubjectTopic) GetObjectID() uint32 {
77+
func (t *SubjectTopic) GetParentID() uint32 {
7878
return t.SubjectID
7979
}
8080

81-
func (t *GroupTopic) GetObjectID() uint32 {
81+
func (t *GroupTopic) GetParentID() uint32 {
8282
return t.GroupID
8383
}
8484

Collapse file

‎internal/domain/group.go‎

Copy file name to clipboardExpand all lines: internal/domain/group.go
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
type GroupRepo interface {
2424
GetByName(ctx context.Context, name string) (model.Group, error)
25+
GetByID(ctx context.Context, id model.GroupID) (model.Group, error)
2526

2627
CountMembersByID(ctx context.Context, id model.GroupID, memberType GroupMemberType) (int64, error)
2728
ListMembersByID(
Collapse file

‎internal/group/mysql_repo.go‎

Copy file name to clipboardExpand all lines: internal/group/mysql_repo.go
+14Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ func (r mysqlRepo) getIDByName(ctx context.Context, name string) (model.GroupID,
5555
return g.ID, nil
5656
}
5757

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+
5872
func (r mysqlRepo) GetByName(ctx context.Context, name string) (model.Group, error) {
5973
g, err := r.q.Group.WithContext(ctx).Where(r.q.Group.Name.Eq(name)).First()
6074
if err != nil {
Collapse file

‎internal/group/mysql_repo_test.go‎

Copy file name to clipboardExpand all lines: internal/group/mysql_repo_test.go
+37-2Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ import (
3232
"github.com/bangumi/server/internal/pkg/test"
3333
)
3434

35+
const notFoundGroupID = 600
36+
const notFoundGroupName = "not-exist-group-name"
37+
3538
func getRepo(t *testing.T) (domain.GroupRepo, *query.Query) {
3639
t.Helper()
3740
q := query.Use(test.GetGorm(t))
@@ -144,12 +147,41 @@ func assertHaveID(t *testing.T, members []model.GroupMember, id ...model.UserID)
144147
}
145148
}
146149

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+
148180
func TestMysqlRepo_GetByName(t *testing.T) {
149181
test.RequireEnv(t, "mysql")
150182
t.Parallel()
151183
const groupID model.GroupID = 200
152-
const groupName = "aabbccdeeeffgg"
184+
const groupName = "group-200"
153185

154186
repo, q := getRepo(t)
155187

@@ -170,6 +202,9 @@ func TestMysqlRepo_GetByName(t *testing.T) {
170202
require.NoError(t, err)
171203
require.Equal(t, groupID, g.ID)
172204
require.Equal(t, groupName, g.Name)
205+
206+
_, err = repo.GetByName(context.Background(), notFoundGroupName)
207+
require.ErrorIs(t, err, domain.ErrNotFound)
173208
}
174209

175210
// $ task test-all -- -run '^TestMysqlRepo_GetByName_not_found'
Collapse file

‎internal/mocks/GroupRepo.go‎

Copy file name to clipboardExpand all lines: internal/mocks/GroupRepo.go
+55-10Lines changed: 55 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file

‎internal/model/topic.go‎

Copy file name to clipboardExpand all lines: internal/model/topic.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type Topic struct {
2323
ID TopicID
2424
CreatorID UserID
2525
Replies uint32
26-
ObjectID uint32
26+
ParentID uint32
2727
State CommentState
2828
Status TopicStatus
2929
}
Collapse file

‎internal/model/type.go‎

Copy file name to clipboardExpand all lines: internal/model/type.go
+9-1Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
package model
1616

17-
import "database/sql/driver"
17+
import (
18+
"database/sql/driver"
19+
20+
"go.uber.org/zap"
21+
)
1822

1923
type EpisodeID uint32
2024
type SubjectID uint32
@@ -61,3 +65,7 @@ func (v EpisodeID) Value() (driver.Value, error) {
6165
func (v GroupID) Value() (driver.Value, error) {
6266
return int64(v), nil
6367
}
68+
69+
func (v GroupID) Zap() zap.Field {
70+
return zap.Uint32("group_id", uint32(v))
71+
}
Collapse file

‎internal/topic/mysql_compat.go‎

Copy file name to clipboardExpand all lines: internal/topic/mysql_compat.go
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type mysqlTopic interface {
2929
GetCreatorID() uint32
3030
GetState() uint8
3131
GetReplies() uint32
32-
GetObjectID() uint32
32+
GetParentID() uint32
3333
GetStatus() uint8
3434
}
3535

@@ -51,7 +51,7 @@ func wrapDao[T mysqlTopic](data []T, err error) ([]model.Topic, error) {
5151
CreatorID: model.UserID(item.GetCreatorID()),
5252
State: model.CommentState(item.GetState()),
5353
Replies: item.GetReplies(),
54-
ObjectID: item.GetObjectID(),
54+
ParentID: item.GetParentID(),
5555
Status: model.TopicStatus(item.GetStatus()),
5656
}
5757
}
@@ -67,7 +67,7 @@ type mysqlComment interface {
6767
GetContent() string
6868
GetState() uint8
6969
RelatedTo() model.CommentID
70-
// GetObjectID() uint32
70+
// GetParentID() uint32
7171
}
7272

7373
func wrapCommentDao[T mysqlComment](data []T, err error) ([]mysqlComment, error) {
Collapse file

‎internal/topic/mysql_repo_topic.go‎

Copy file name to clipboardExpand all lines: internal/topic/mysql_repo_topic.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func (r mysqlRepo) Get(ctx context.Context, topicType domain.TopicType, id model
5757
CreatorID: model.UserID(topic.GetCreatorID()),
5858
State: model.CommentState(topic.GetState()),
5959
Replies: topic.GetReplies(),
60-
ObjectID: topic.GetObjectID(),
60+
ParentID: topic.GetParentID(),
6161
Status: model.TopicStatus(topic.GetStatus()),
6262
}, nil
6363
}
Collapse file

‎internal/web/handler.go‎

Copy file name to clipboardExpand all lines: internal/web/handler.go
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func AddRouters(
128128
private.Get("/groups/:name/topics", addMetrics(h.ListGroupTopics))
129129
private.Get("/subjects/:id/topics", addMetrics(h.ListSubjectTopics))
130130

131-
private.Get("/groups/:name/topics/:topic_id", addMetrics(h.GetGroupTopic))
131+
private.Get("/groups/-/topics/:topic_id", addMetrics(h.GetGroupTopic))
132132
private.Get("/subjects/:id/topics/:topic_id", addMetrics(h.GetSubjectTopic))
133133
private.Get("/indices/:id/comments", addMetrics(h.GetIndexComments))
134134
private.Get("/episodes/:id/comments", addMetrics(h.GetEpisodeComments))

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.