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

/events/{eventID}/applicantsの修正 #46

Merged
merged 4 commits into from
Jun 15, 2024
Merged
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
33 changes: 23 additions & 10 deletions server/handler/applicants.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handler

import (
"fmt"
"net/http"

"github.com/google/uuid"
Expand Down Expand Up @@ -32,7 +33,7 @@ func (h *Handler) GetEventsEventIDApplicants(ctx echo.Context, eventID api.Event
res := make(api.GetEventApplicantsResponse, 0, len(dateOptions))
for traQID, dateIDs := range dateOptions {
res = append(res, api.Applicant{
TraqID: &traQID,
TraqID: &traQID,
DateOptionIDs: &dateIDs,
})
}
Expand All @@ -45,24 +46,36 @@ func (h *Handler) PostEventsEventIDApplicants(ctx echo.Context, eventID api.Even
if err := ctx.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
if len(*req.DateOptionIDs) == 0 {
return ctx.NoContent(http.StatusOK)
}
// eventIDがeventDateIDのそれぞれの値が齟齬がないか確認
err := h.repo.ValidateEventDateIDsFromEventID(eventID, *req.DateOptionIDs)
pirosiki197 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("invalid event ID and DateOptionIDs: %v", err))
}

traQID := ctx.Get(traQIDKey).(string)

dateVotes := []model.DateVote{}
// traqIDがeventDateIDのそれぞれの値が齟齬がないか確認
err = h.repo.ValidateEventDateIDsFromTraqID(traQID, *req.DateOptionIDs)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("already exists traqID and DateOptionIDs: %v", err))
}

dateVotes := make([]model.DateVote, 0, len(*req.DateOptionIDs))
for _, dateOption := range *req.DateOptionIDs {
id, err := uuid.NewV7()
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

traQID := ctx.Get(traQIDKey).(string)
dateVotes = append(dateVotes, model.DateVote{
ID: id,
EventID: eventID,
TraQID: traQID,
DateID: dateOption,
ID: id,
TraQID: traQID,
DateID: dateOption,
})
}

err := h.repo.CreateDateVotes(dateVotes)
err = h.repo.CreateDateVotes(dateVotes)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
Expand Down
3 changes: 1 addition & 2 deletions server/model/date_vote.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

type DateVote struct {
ID uuid.UUID `db:"id"`
EventID uuid.UUID `db:"event_id"`
TraQID string `db:"traq_id"`
DateID uuid.UUID `db:"event_date_id"`
CreatedAt time.Time `db:"created_at"`
Expand Down Expand Up @@ -38,6 +37,6 @@ func (repo *Repository) CreateDateVotes(votes []DateVote) error {
if err != nil {
return err
}

return tx.Commit()
}
38 changes: 38 additions & 0 deletions server/model/event_date.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package model

import (
"fmt"
"log"
"time"

"github.com/google/uuid"
"github.com/jmoiron/sqlx"
)

type EventDate struct {
Expand Down Expand Up @@ -38,3 +41,38 @@ func (repo *Repository) CreateEventDates(dates []EventDate) error {

return tx.Commit()
}

func (repo *Repository) ValidateEventDateIDsFromEventID(eventID uuid.UUID, dateIDs []uuid.UUID) error {
var count int
query, args, err := sqlx.In("SELECT COUNT(*) FROM event_dates WHERE event_id = ? AND id IN (?)", eventID, dateIDs)
if err != nil {
log.Println("Error creating SQL query")
return err
}
err = repo.db.Get(&count, query, args...)
if err != nil {
log.Println("Error getting count from event_dates")
return err
}
if count != len(dateIDs) {
return fmt.Errorf("invalid date IDs: %v", dateIDs)
}
return nil
}

func (repo *Repository) ValidateEventDateIDsFromTraqID(traQID string, dateIDs []uuid.UUID) error {
// すでにtraQIDとdateIDsの組み合わせが存在するか確認
var count int
query, args, err := sqlx.In("SELECT COUNT(*) FROM date_votes WHERE traq_id = ? AND event_date_id IN (?)", traQID, dateIDs)
if err != nil {
return err
}
err = repo.db.Get(&count, query, args...)
if err != nil {
return err
}
if count > 0 {
return fmt.Errorf("invalid date IDs: %v", dateIDs)
}
return nil
}
Loading