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

合宿への参加登録 #129

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
92 changes: 92 additions & 0 deletions backend/handler/api.gen.go

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

48 changes: 48 additions & 0 deletions backend/handler/camps.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,51 @@ func (s *Server) PutCamp(e echo.Context, campID CampId, params PutCampParams) er

return e.JSON(http.StatusOK, response)
}

func (s *Server) PostCampParticipant(e echo.Context, campID CampId, params PostCampParticipantParams) error {
user, err := s.repo.GetOrCreateUser(*params.XForwardedUser)

if err != nil {
e.Logger().Errorf("failed to get or create user: %v", err)

return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
}

if err := s.repo.AddUserToCamp(uint(campID), user); err != nil {
if errors.Is(err, model.ErrNotFound) {
return echo.NewHTTPError(http.StatusNotFound, "Not found")
}

if errors.Is(err, model.ErrAlreadyExists) {
return echo.NewHTTPError(http.StatusConflict, "User already exists in camp")
}

e.Logger().Errorf("failed to add user to camp: %v", err)

return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
}

return e.NoContent(http.StatusNoContent)
}

func (s *Server) DeleteCampParticipant(e echo.Context, campID CampId, params DeleteCampParticipantParams) error {
user, err := s.repo.GetOrCreateUser(*params.XForwardedUser)

if err != nil {
e.Logger().Errorf("failed to get or create user: %v", err)

return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
}

if err := s.repo.RemoveUserFromCamp(uint(campID), user); err != nil {
if errors.Is(err, model.ErrNotFound) {
return echo.NewHTTPError(http.StatusNotFound, "Not found")
}

e.Logger().Errorf("failed to remove user from camp: %v", err)

return echo.NewHTTPError(http.StatusInternalServerError, "Internal server error")
}

return e.NoContent(http.StatusNoContent)
}
1 change: 1 addition & 0 deletions backend/model/camp.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type Camp struct {
Name string
Description string
IsDraft bool
Participants []User `gorm:"many2many:camp_participants;"`
Budgets []Budget
Events []Event
QuestionGroups []QuestionGroup
Expand Down
53 changes: 53 additions & 0 deletions backend/repository/camp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repository

import (
"errors"
"fmt"

"github.com/go-sql-driver/mysql"
"github.com/traP-jp/rucQ/backend/model"
Expand Down Expand Up @@ -73,3 +74,55 @@ func (r *Repository) UpdateCamp(campID uint, camp *model.Camp) error {

return nil
}

func (r *Repository) AddUserToCamp(campID uint, user *model.User) error {
var camp model.Camp

if err := r.db.First(&camp, campID).Error; err != nil {
return fmt.Errorf("failed to get camp: %w", err)
}

count := r.db.
Model(&camp).
Where(&model.User{
TraqID: user.TraqID,
}).
Association("Participants").
Count()

if count > 0 {
return model.ErrAlreadyExists
}

if err := r.db.Model(&camp).Association("Participants").Append(user); err != nil {
return fmt.Errorf("failed to append participant: %w", err)
}

return nil
}

func (r *Repository) RemoveUserFromCamp(campID uint, user *model.User) error {
var camp model.Camp

if err := r.db.First(&camp, campID).Error; err != nil {
return fmt.Errorf("failed to get camp: %w", err)
}

count := r.db.
Model(&camp).
Where(&model.User{
TraqID: user.TraqID,
}).
Association("Participants").
Count()

if count == 0 {
return model.ErrNotFound
}

if err := r.db.Model(&camp).Association("Participants").Delete(user); err != nil {
return fmt.Errorf("failed to delete participant: %w", err)
}

return nil
}
Loading