Skip to content

Commit

Permalink
Merge branch 'main' into refactor/repository-infra
Browse files Browse the repository at this point in the history
  • Loading branch information
ras0q committed Oct 27, 2023
2 parents f0f6adb + dd01dc2 commit 0aa5faa
Show file tree
Hide file tree
Showing 28 changed files with 625 additions and 211 deletions.
13 changes: 9 additions & 4 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: CI
on:
push:
branches:
- 'main'
- main
pull_request:

jobs:
Expand All @@ -26,12 +26,17 @@ jobs:
lint:
name: Lint
runs-on: ubuntu-latest
needs:
- build
steps:
- uses: actions/checkout@v4
- name: Run linters
uses: reviewdog/action-golangci-lint@v2
- uses: actions/setup-go@v4
with:
go-version-file: ./go.mod
- uses: golangci/golangci-lint-action@v3
with:
filter_mode: nofilter
version: latest
install-mode: goinstall
test-unit:
name: Test (Unit)
runs-on: ubuntu-latest
Expand Down
24 changes: 19 additions & 5 deletions docs/swagger/traPortfolio.v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ components:
type: array
description: コンテストチーム
items:
$ref: "#/components/schemas/ContestTeam"
$ref: "#/components/schemas/ContestTeamWithoutMembers"
required:
- teams
ContestDetail:
Expand All @@ -1148,10 +1148,10 @@ components:
- link
- description
- teams
ContestTeam:
title: ContestTeam
ContestTeamWithoutMembers:
title: ContestTeamWithoutMembers
type: object
description: コンテストチーム情報
description: コンテストチーム情報(チームメンバーなし)
properties:
id:
type: string
Expand All @@ -1168,6 +1168,21 @@ components:
- id
- name
- result
ContestTeam:
title: ContestTeam
type: object
description: コンテストチーム情報
allOf:
- $ref: "#/components/schemas/ContestTeamWithoutMembers"
- type: object
properties:
members:
type: array
description: チームメンバーのユーザー情報
items:
$ref: '#/components/schemas/User'
required:
- members
ContestTeamDetail:
title: ContestTeamDetail
type: object
Expand All @@ -1191,7 +1206,6 @@ components:
required:
- link
- description
- members
PrPermitted:
title: PrPermitted
type: boolean
Expand Down
8 changes: 6 additions & 2 deletions domain/contest.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ type ContestDetail struct {
ContestTeams []*ContestTeam
}

type ContestTeam struct {
type ContestTeamWithoutMembers struct {
ID uuid.UUID
ContestID uuid.UUID
Name string
Result string
}

type ContestTeam struct {
ContestTeamWithoutMembers
Members []*User
}

type ContestTeamDetail struct {
ContestTeam
Link string
Description string
Members []*User
}
16 changes: 16 additions & 0 deletions domain/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"database/sql"
"database/sql/driver"
"fmt"
"github.com/traPtitech/traPortfolio/util/optional"
"time"

"github.com/gofrs/uuid"
Expand All @@ -27,6 +28,21 @@ type EventDetail struct {
RoomID uuid.UUID
}

// ApplyEventLevel EventDetailのLevelに応じてEventを返す
func ApplyEventLevel(e EventDetail) optional.Of[EventDetail] {
switch e.Level {
case EventLevelAnonymous:
e.HostName = nil
return optional.From(e)
case EventLevelPublic:
return optional.From(e)
case EventLevelPrivate:
return optional.Of[EventDetail]{}
default:
return optional.Of[EventDetail]{}
}
}

type EventLevel uint8

var (
Expand Down
2 changes: 1 addition & 1 deletion domain/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type UserContest struct {
Name string // コンテスト名
TimeStart time.Time
TimeEnd time.Time
Teams []*ContestTeam // ユーザーが所属するチームのリスト
Teams []*ContestTeamWithoutMembers // ユーザーが所属するチームのリスト
}

type UserGroup struct {
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/json-iterator/go v1.1.12
github.com/labstack/echo/v4 v4.11.1
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/samber/lo v1.38.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.16.0
github.com/stretchr/testify v1.8.4
Expand Down Expand Up @@ -46,6 +47,7 @@ require (
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
golang.org/x/mod v0.9.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM=
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/spf13/afero v1.9.5 h1:stMpOSZFs//0Lv29HduCmli3GUfpFoF3Y1Q/aXj/wVM=
github.com/spf13/afero v1.9.5/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
Expand Down Expand Up @@ -254,6 +256,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
99 changes: 85 additions & 14 deletions infrastructure/repository/contest_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func (r *ContestRepository) DeleteContest(ctx context.Context, contestID uuid.UU
}

func (r *ContestRepository) GetContestTeams(ctx context.Context, contestID uuid.UUID) ([]*domain.ContestTeam, error) {
//IDがcontestIDであるようなcontestが存在するかチェック
if err := r.h.
WithContext(ctx).
Where(&model.Contest{ID: contestID}).
Expand All @@ -186,6 +187,7 @@ func (r *ContestRepository) GetContestTeams(ctx context.Context, contestID uuid.
return nil, err
}

//ContestIDがcontestIDであるようなcontestTeamを10件まで列挙する
teams := make([]*model.ContestTeam, 10)
err := r.h.
WithContext(ctx).
Expand All @@ -195,15 +197,58 @@ func (r *ContestRepository) GetContestTeams(ctx context.Context, contestID uuid.
if err != nil {
return nil, err
}

//teamsの要素vについてTeamIDがv.IDである(TeamIDがteamsIDListに入っているID)ようなContestTeamUserBelongingを列挙する
var teamsIDList = make([]uuid.UUID, len(teams))
for i, v := range teams {
teamsIDList[i] = v.ID
}

var belongings = []*model.ContestTeamUserBelonging{}
err = r.h.
WithContext(ctx).
Preload("User").
Where("team_id IN (?)", teamsIDList).
Find(&belongings).
Error()

if err != nil {
return nil, err
}

belongingMap := make(map[uuid.UUID]([]*model.ContestTeamUserBelonging))
for _, v := range belongings {
belongingMap[v.TeamID] = append(belongingMap[v.TeamID], v)
}

nameMap, err := r.makeUserNameMap()
if err != nil {
return nil, err
}

result := make([]*domain.ContestTeam, 0, len(teams))
for _, v := range teams {
members := make([]*domain.User, len(belongingMap[v.ID]))
for i, w := range belongingMap[v.ID] {
u := w.User
members[i] = domain.NewUser(u.ID, u.Name, nameMap[u.Name], u.Check)
}

result = append(result, &domain.ContestTeam{
ID: v.ID,
ContestID: v.ContestID,
Name: v.Name,
Result: v.Result,
ContestTeamWithoutMembers: domain.ContestTeamWithoutMembers{
ID: v.ID,
ContestID: v.ContestID,
Name: v.Name,
Result: v.Result,
},
Members: members,
})
}

// if len(result) == 0 {
// result = []*domain.ContestTeam{}
// }

return result, nil
}

Expand All @@ -218,16 +263,40 @@ func (r *ContestRepository) GetContestTeam(ctx context.Context, contestID uuid.U
return nil, err
}

var belongings []*model.ContestTeamUserBelonging
err := r.h.
WithContext(ctx).
Preload("User").
Where(&model.ContestTeamUserBelonging{TeamID: teamID}).
Find(&belongings).
Error()
if err != nil {
return nil, err
}

nameMap, err := r.makeUserNameMap()
if err != nil {
return nil, err
}

members := make([]*domain.User, len(belongings))
for i, w := range belongings {
u := w.User
members[i] = domain.NewUser(u.ID, u.Name, nameMap[u.Name], u.Check)
}

res := &domain.ContestTeamDetail{
ContestTeam: domain.ContestTeam{
ID: team.ID,
ContestID: team.ContestID,
Name: team.Name,
Result: team.Result,
ContestTeamWithoutMembers: domain.ContestTeamWithoutMembers{
ID: team.ID,
ContestID: team.ContestID,
Name: team.Name,
Result: team.Result,
},
Members: members,
},
Link: team.Link,
Description: team.Description,
// Members:
}
return res, nil
}
Expand Down Expand Up @@ -257,14 +326,16 @@ func (r *ContestRepository) CreateContestTeam(ctx context.Context, contestID uui

result := &domain.ContestTeamDetail{
ContestTeam: domain.ContestTeam{
ID: contestTeam.ID,
ContestID: contestTeam.ContestID,
Name: contestTeam.Name,
Result: contestTeam.Result,
ContestTeamWithoutMembers: domain.ContestTeamWithoutMembers{
ID: contestTeam.ID,
ContestID: contestTeam.ContestID,
Name: contestTeam.Name,
Result: contestTeam.Result,
},
Members: make([]*domain.User, 0),
},
Link: contestTeam.Link,
Description: contestTeam.Description,
Members: nil,
}
return result, nil
}
Expand Down
Loading

0 comments on commit 0aa5faa

Please sign in to comment.