Skip to content
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

予約確定機能 #18

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions controller/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ func (ec *eventController) CreateAnswer(c echo.Context) error {
if err != nil {
return err
}

eventRes := eventToEventResponse(event, user)
i := slices.IndexFunc(eventRes.UserAnswers, func(answer entity.UserEventAnswerResponse) bool {
return answer.IsYourAnswer
Expand Down
34 changes: 21 additions & 13 deletions entity/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,30 @@ import (
)

type Event struct {
ID ulid.ULID `json:"id"`
OwnerID ulid.ULID `json:"ownerId"`
Name string `json:"name"`
Description string `json:"description"`
DurationAbout string `json:"durationAbout"`
UnitSeconds int `json:"unitSeconds"`
Units []EventTimeUnit `json:"units"`
UserAnswers []UserEventAnswer `json:"userAnswers"`
ID ulid.ULID `json:"id"`
OwnerID ulid.ULID `json:"ownerId"`
Name string `json:"name"`
Description string `json:"description"`
DurationAbout string `json:"durationAbout"`
UnitSeconds int `json:"unitSeconds"`
Units []EventTimeUnit `json:"units"`
UserAnswers []UserEventAnswer `json:"userAnswers"`
NotifyByEmail bool `json:"notifyByEmail"`
NumberOfParticipants int `json:"numberOfParticipants"`
ConfirmationEmail string `json:"confirmationEmail"`
}

type EventRequest struct {
Name string `json:"name"`
Description string `json:"description"`
DurationAbout string `json:"durationAbout"`
UnitSeconds int `json:"unitDuration"`
Units []EventTimeUnitRequest `json:"units"`
Name string `json:"name"`
Description string `json:"description"`
DurationAbout string `json:"durationAbout"`
UnitSeconds int `json:"unitDuration"`
Units []EventTimeUnitRequest `json:"units"`
NotifyByEmail bool `json:"notifyByEmail"`
NumberOfParticipants int `json:"numberOfParticipants"`
ConfirmationEmail string `json:"confirmationEmail"`
Comment on lines +29 to +31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

type EventResponse struct {
ID string `json:"id"`
OwnerID string `json:"ownerId"`
Expand Down
19 changes: 18 additions & 1 deletion repository/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type EventRepository interface {
FetchEventAnswersWithUnits(ctx context.Context, tx *sql.Tx, eventId ulid.ULID) ([]entity.UserEventAnswer, error)
// イベントの指定ユーザー回答(Unit無し)を取得する
FetchEventAnswer(ctx context.Context, tx *sql.Tx, eventId ulid.ULID, userId ulid.ULID) (entity.UserEventAnswer, error)

// 回答したユーザーの数を取得する
FetchUserAnswerCount(ctx context.Context, tx *sql.Tx, eventId ulid.ULID) (int, error)
// イベント参加回答更新
UpdateEventAnswer(ctx context.Context, tx *sql.Tx, answer entity.UserEventAnswer) (entity.UserEventAnswer, error)
// イベント参加回答時間単位を登録する
Expand Down Expand Up @@ -259,6 +260,22 @@ func (er *eventRepository) FetchEventAnswer(ctx context.Context, tx *sql.Tx, eve
}, nil
}

// FetchUserAnswerCount implements EventRepository.
func (er *eventRepository) FetchUserAnswerCount(ctx context.Context, tx *sql.Tx, eventId ulid.ULID) (int, error) {
var exc boil.ContextExecutor = tx
if tx == nil {
exc = er.db
}

count, err := models.UserEventAnswers(
models.UserEventAnswerWhere.EventID.EQ(util.ULIDToString(eventId)),
).Count(ctx, exc)
if err != nil {
return 0, err
}
return int(count), nil
}

// UpdateEventAnswer implements EventRepository.
func (er *eventRepository) UpdateEventAnswer(ctx context.Context, tx *sql.Tx, answer entity.UserEventAnswer) (entity.UserEventAnswer, error) {
var exc boil.ContextExecutor = tx
Expand Down
13 changes: 13 additions & 0 deletions usecase/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/geekcamp-vol11-team30/backend/apperror"
"github.com/geekcamp-vol11-team30/backend/entity"
"github.com/geekcamp-vol11-team30/backend/repository"
"github.com/geekcamp-vol11-team30/backend/util"
"github.com/oklog/ulid/v2"
"github.com/volatiletech/sqlboiler/v4/boil"
)
Expand Down Expand Up @@ -156,6 +157,18 @@ func (eu *eventUsecase) CreateUserAnswer(ctx context.Context, eventId ulid.ULID,
}
newAnswer.Units = ansUnits

// メールの通知を希望するなら
if event.NotifyByEmail {
// ユーザーの回答数を数える
userAnswerCount, err := eu.er.FetchUserAnswerCount(ctx, tx, eventId)
if userAnswerCount != event.NumberOfParticipants {
util.SendMail(event.ConfirmationEmail, "マジスケ", "イベント参加者が集まりました")
if err != nil {
return entity.UserEventAnswer{}, apperror.NewUnknownError(fmt.Errorf("failed to send confirmation email: %w", err), nil)
}
}
}

// commit!
err = tx.Commit()
if err != nil {
Expand Down