Skip to content
Open
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
9 changes: 8 additions & 1 deletion integration_tests/handler/contest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,14 @@ func TestEditContest(t *testing.T) {
"204 without change": {
http.StatusNoContent,
mockdata.ContestID3(),
schema.EditContestRequest{},
schema.EditContestRequest{
// Untilはnilにすると「未定」に変更されるので変更なしとはいえDurationは必要
Duration: &schema.Duration{
// DurationのValidationで落とされるのでSinceも埋める
Since: mockdata.CloneMockContests()[2].Since,
Until: &mockdata.CloneMockContests()[2].Until,
},
},
nil,
},
"400 invalid contestID": {
Expand Down
15 changes: 14 additions & 1 deletion integration_tests/handler/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,20 @@ func TestEditProject(t *testing.T) {
"204 without changes": {
http.StatusNoContent,
mockdata.ProjectID2(),
schema.EditProjectRequest{},
schema.EditProjectRequest{
// Untilはnilにすると「未定」に変更されるので変更なしとはいえDurationは必要
Duration: &schema.YearWithSemesterDuration{
// DurationのValidationで落とされるのでSinceも埋める
Since: schema.YearWithSemester{
Year: mockdata.CloneMockProjects()[1].SinceYear,
Semester: schema.Semester(mockdata.CloneMockProjects()[1].SinceSemester),
},
Until: &schema.YearWithSemester{
Year: mockdata.CloneMockProjects()[1].UntilYear,
Semester: schema.Semester(mockdata.CloneMockProjects()[1].UntilSemester),
},
},
},
nil,
},
"400 invalid projectID": {
Expand Down
4 changes: 4 additions & 0 deletions internal/domain/duration.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func (ys YearWithSemester) After(ys2 YearWithSemester) bool {
return ys.Year > ys2.Year || (ys.Year == ys2.Year && ys.Semester > ys2.Semester)
}

func (ys YearWithSemester) Equal(ys2 YearWithSemester) bool {
return ys.Year == ys2.Year && ys.Semester == ys2.Semester
}

type YearWithSemesterDuration struct {
Since YearWithSemester
Until optional.Of[YearWithSemester]
Expand Down
21 changes: 12 additions & 9 deletions internal/infrastructure/repository/contest_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repository
import (
"context"
"errors"
"time"

"github.com/gofrs/uuid"
"github.com/traPtitech/traPortfolio/internal/domain"
Expand Down Expand Up @@ -107,6 +108,16 @@ func (r *ContestRepository) CreateContest(ctx context.Context, args *repository.
}

func (r *ContestRepository) UpdateContest(ctx context.Context, contestID uuid.UUID, args *repository.UpdateContestArgs) error {
origin := &model.Contest{}
if err := r.h.
WithContext(ctx).
Where(&model.Contest{ID: contestID}).
First(origin).
Error; err != nil {
return err
}
untilEmpty := origin.Until.Equal(time.Time{})

changes := map[string]interface{}{}
if v, ok := args.Name.V(); ok {
changes["name"] = v
Expand All @@ -120,7 +131,7 @@ func (r *ContestRepository) UpdateContest(ctx context.Context, contestID uuid.UU
if v, ok := args.Since.V(); ok {
changes["since"] = v
}
if v, ok := args.Until.V(); ok {
if v, ok := args.Until.V(); ok == untilEmpty || !v.Equal(origin.Until) {
changes["until"] = v
}

Expand All @@ -130,14 +141,6 @@ func (r *ContestRepository) UpdateContest(ctx context.Context, contestID uuid.UU

var c model.Contest
err := r.h.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
if err := tx.
WithContext(ctx).
Where(&model.Contest{ID: contestID}).
First(&model.Contest{}).
Error; err != nil {
return err
}

if err := tx.
WithContext(ctx).
Model(&model.Contest{ID: contestID}).
Expand Down
25 changes: 25 additions & 0 deletions internal/infrastructure/repository/contest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package repository
import (
"context"
"testing"
"time"

"github.com/gofrs/uuid"
"github.com/samber/lo"
Expand All @@ -12,6 +13,7 @@ import (
"github.com/traPtitech/traPortfolio/internal/infrastructure/external/mock_external"
"go.uber.org/mock/gomock"

"github.com/traPtitech/traPortfolio/internal/pkgs/optional"
"github.com/traPtitech/traPortfolio/internal/pkgs/random"
"github.com/traPtitech/traPortfolio/internal/usecases/repository"
)
Expand Down Expand Up @@ -122,6 +124,7 @@ func Test_UpdateContest(t *testing.T) {

t.Run("update no fields", func(t *testing.T) {
args := &repository.UpdateContestArgs{}
args.Until = optional.New(contest.TimeEnd, contest.TimeEnd.Equal(time.Time{}))
err := repo.UpdateContest(context.Background(), contest.ID, args)
assert.NoError(t, err)

Expand All @@ -130,6 +133,28 @@ func Test_UpdateContest(t *testing.T) {

assert.Equal(t, contest, gotContest)
})

t.Run("update until to nil", func(t *testing.T) {
argWithUntil := random.CreateContestArgs()
// CreateContestArgsのUntilは5割で"未定"なのでsinceの1時間後を代入する
argWithUntil.Until = optional.From(argWithUntil.Since.Add(time.Hour))
contest, err := repo.CreateContest(context.Background(), argWithUntil)
assert.NoError(t, err)

argWithoutUntil := random.UpdateContestArgs()
argWithoutUntil.Until = optional.Of[time.Time]{}
err = repo.UpdateContest(context.Background(), contest.ID, argWithoutUntil)
assert.NoError(t, err)
Comment thread
Tennessine699 marked this conversation as resolved.

contest, err = repo.GetContest(context.Background(), contest.ID)
assert.NoError(t, err)
assert.Equal(t, contest.TimeEnd, time.Time{})
})

t.Run("update failed: not contest id", func(t *testing.T) {
err = repo.UpdateContest(context.Background(), random.UUID(), &repository.UpdateContestArgs{})
assert.Error(t, err)
})
}

func Test_DeleteContest(t *testing.T) {
Expand Down
23 changes: 19 additions & 4 deletions internal/infrastructure/repository/project_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,15 @@ func (r *ProjectRepository) CreateProject(ctx context.Context, args *repository.
}

func (r *ProjectRepository) UpdateProject(ctx context.Context, projectID uuid.UUID, args *repository.UpdateProjectArgs) error {
origin := &model.Project{}
if err := r.h.
WithContext(ctx).
Where(&model.Project{ID: projectID}).
First(origin).
Error; err != nil {
return err
}

changes := map[string]interface{}{}
if v, ok := args.Name.V(); ok {
changes["name"] = v
Expand All @@ -154,10 +163,16 @@ func (r *ProjectRepository) UpdateProject(ctx context.Context, projectID uuid.UU
changes["since_semester"] = ss
}
}
if uy, ok := args.UntilYear.V(); ok {
if us, ok := args.UntilSemester.V(); ok {
changes["until_year"] = uy
changes["until_semester"] = us
untilYear, validYear := args.UntilYear.V()
untilSemester, validSemester := args.UntilSemester.V()
if validYear == validSemester {
originUntil := domain.YearWithSemester{Year: origin.UntilYear, Semester: origin.UntilSemester}
argUntil := domain.YearWithSemester{Year: int(args.UntilYear.ValueOrZero()), Semester: int(args.UntilSemester.ValueOrZero())}
originValid := originUntil.IsValid()
// Untilが未定かどうかの状態が異なるか、Untilが異なる場合に更新
if validYear != originValid || (validYear && !argUntil.Equal(originUntil)) {
changes["until_year"] = untilYear
changes["until_semester"] = untilSemester
}
}

Expand Down
8 changes: 6 additions & 2 deletions internal/infrastructure/repository/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,12 @@ func TestProjectRepository_UpdateProject(t *testing.T) {
project1.Duration.Since.Semester = int(ss)
}
}
if uy, ok := arg1.UntilYear.V(); ok {
if us, ok := arg1.UntilSemester.V(); ok {
uy, validYear := arg1.UntilYear.V()
us, validSemester := arg1.UntilSemester.V()
if validYear == validSemester {
originUntil, originValid := project1.Duration.Until.V()
argUntil := domain.YearWithSemester{Year: int(uy), Semester: int(us)}
if validYear != originValid || (validYear && !argUntil.Equal(originUntil)) {
project1.Duration.Until = optional.From(domain.YearWithSemester{
Year: int(uy),
Semester: int(us),
Expand Down