Skip to content

Commit

Permalink
/events/meのエンドポイント作成
Browse files Browse the repository at this point in the history
  • Loading branch information
Luftalian committed Jun 15, 2024
1 parent 99a61c3 commit 1892df9
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 26 deletions.
Empty file removed docs/compose.yaml
Empty file.
13 changes: 9 additions & 4 deletions docs/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ paths:
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/EventMeResponse'
$ref: "#/components/schemas/EventMeResponses"
/events/{eventID}:
parameters:
- $ref: "#/components/parameters/eventID"
Expand Down Expand Up @@ -211,11 +209,18 @@ components:
type: boolean
isAnswered:
type: boolean
isHost:
type: boolean
required:
- event_id
- title
- isConfirmed
- isAnswered
- isHost
EventMeResponses:
type: array
items:
$ref: "#/components/schemas/EventMeResponse"
GetEventResponse:
type: object
properties:
Expand All @@ -235,7 +240,7 @@ components:
items:
$ref: "#/components/schemas/dateOption"
required:
- event_id
- id
- title
- description
- isConfirmed
Expand Down
38 changes: 32 additions & 6 deletions server/api/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions server/handler/applicants.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,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 @@ -52,13 +52,13 @@ func (h *Handler) PostEventsEventIDApplicants(ctx echo.Context, eventID api.Even
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,
// EventID: eventID,
TraQID: traQID,
DateID: dateOption,
})
}

Expand Down
79 changes: 76 additions & 3 deletions server/handler/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handler

import (
"database/sql"
"log"
"net/http"
"slices"

Expand Down Expand Up @@ -77,6 +78,78 @@ func (h *Handler) PostEvents(ctx echo.Context) error {
return ctx.JSON(http.StatusCreated, res)
}

// (GET /events/me)
func (h *Handler) GetEventsMe(ctx echo.Context) error {
userID := ctx.Get(traQIDKey).(string)
// ホストとなっているイベントの取得
eventsByHost, err := h.repo.GetEventsByHost(userID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

// 回答候補となっているイベントの取得
eventsByTarget, err := h.repo.GetEventsByTargetUser(userID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

// ホストとなっていないイベントのみに限定
removedHostEvent := removeEventHostFromTarget(eventsByHost, eventsByTarget)

// 回答済みのイベントの取得
eventAnsweredStruct, err := h.repo.GetDateVotesByUser(userID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
eventAnsweredMap := make(map[uuid.UUID]struct{})
for _, e := range eventAnsweredStruct {
eventAnsweredMap[e.EventID] = struct{}{}
log.Printf("eventAnsweredMap: %v", e.EventID)
}

res := make([]api.EventMeResponse, 0, len(eventsByHost)+len(removedHostEvent))
for _, event := range eventsByHost {
_, isAnswered := eventAnsweredMap[event.ID]
res = append(res, api.EventMeResponse{
Description: &event.Description,
EventId: event.ID,
IsAnswered: isAnswered,
IsConfirmed: event.IsConfirmed,
IsHost: true,
Title: event.Title,
})
}
for _, event := range removedHostEvent {
_, isAnswered := eventAnsweredMap[event.ID]
res = append(res, api.EventMeResponse{
Description: &event.Description,
EventId: event.ID,
IsAnswered: isAnswered,
IsConfirmed: event.IsConfirmed,
IsHost: false,
Title: event.Title,
})
}
return ctx.JSON(http.StatusOK, res)
}

func removeEventHostFromTarget(hostEvent, targetEvent []model.Event) []model.Event {
removedEvent := []model.Event{}
for i := 0; i < len(targetEvent); i++ {
check := false
for j := 0; j < len(hostEvent); j++ {
if hostEvent[i].ID == targetEvent[j].ID {
check = true
break
}
}
if !check {
removedEvent = append(removedEvent, targetEvent[i])
}
}
return removedEvent
}

// (GET /events/{eventID})
func (h *Handler) GetEventsEventID(ctx echo.Context, eventID api.EventID) error {
event, err := h.repo.GetEventByEventID(eventID)
Expand All @@ -96,10 +169,10 @@ func (h *Handler) GetEventsEventID(ctx echo.Context, eventID api.EventID) error
})
}
getEventsByEventIDResponse := api.GetEventResponse{
DateOptions: &dateOptionsResponse,
DateOptions: dateOptionsResponse,
Description: event.Description,
Id: &event.ID,
IsConfirmed: &event.IsConfirmed,
Id: event.ID,
IsConfirmed: event.IsConfirmed,
Title: event.Title,
}
if event.Start.Valid && event.End.Valid {
Expand Down
31 changes: 23 additions & 8 deletions server/model/date_vote.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
package model

import (
"log"
"time"

"github.com/google/uuid"
)

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"`
}
type (
DateVote struct {
ID uuid.UUID `db:"id"`
TraQID string `db:"traq_id"`
DateID uuid.UUID `db:"event_date_id"`
CreatedAt time.Time `db:"created_at"`
}
DateVoteAndEventID struct {
EventID uuid.UUID `db:"event_id"`
}
)

func (repo *Repository) GetEventDateVotes(eventDateID uuid.UUID) ([]DateVote, error) {
dateVotes := make([]DateVote, 0)
Expand All @@ -38,6 +43,16 @@ func (repo *Repository) CreateDateVotes(votes []DateVote) error {
if err != nil {
return err
}

return tx.Commit()
}

func (repo *Repository) GetDateVotesByUser(traqID string) ([]DateVoteAndEventID, error) {
dateVoteAndEventID := make([]DateVoteAndEventID, 0)
err := repo.db.Select(&dateVoteAndEventID, "SELECT event_dates.event_id FROM date_votes JOIN event_dates ON date_votes.event_date_id = event_dates.id WHERE date_votes.traq_id = ?;", traqID)
if err != nil {
return nil, err
}
log.Println(dateVoteAndEventID)
return dateVoteAndEventID, nil
}
9 changes: 9 additions & 0 deletions server/model/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ func (repo *Repository) GetEvents() ([]Event, error) {
return events, nil
}

func (repo *Repository) GetEventsByHost(host string) ([]Event, error) {
var events []Event
err := repo.db.Select(&events, "SELECT * FROM events WHERE host_id = ?", host)
if err != nil {
return nil, err
}
return events, nil
}

func (repo *Repository) CreateEvent(event Event) error {
_, err := repo.db.Exec("INSERT INTO events (id, title, start, end, host_id, location, description, is_confirmed) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
event.ID, event.Title, event.Start, event.End, event.HostID, event.Location, event.Description, event.IsConfirmed)
Expand Down
9 changes: 9 additions & 0 deletions server/model/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,12 @@ func (repo *Repository) CreateEventTargets(targets []EventTarget) error {

return tx.Commit()
}

func (repo *Repository) GetEventsByTargetUser(targetUser string) ([]Event, error) {
var events []Event
err := repo.db.Select(&events, "SELECT * FROM events WHERE id IN (SELECT event_id FROM targets WHERE traq_id = ?)", targetUser)
if err != nil {
return nil, err
}
return events, nil
}

0 comments on commit 1892df9

Please sign in to comment.