Skip to content

Commit

Permalink
add request to get a list of athletes
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad2002 committed Nov 20, 2024
1 parent 81437e9 commit 56d7c9b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
23 changes: 23 additions & 0 deletions controller/athlete_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func athleteController() {
router.GET("/athlete/name_year", getAthleteByNameAndYear)
router.GET("/athlete/alias_year", getAthleteByAliasAndYear)
router.GET("/athlete/meet/:meet_id", getAthletesByMeeting)
router.GET("/athlete/meet/:meet_id/id-list/", getAthletesByMeetingAndIdList)
router.GET("/athlete/team/:team_id", getAthletesByTeam)
router.GET("/athlete/team/:team_id/meet/:meet_id", getAthletesByTeamAndMeeting)

Expand Down Expand Up @@ -85,6 +86,28 @@ func getAthletesByMeeting(c *gin.Context) {
c.IndentedJSON(http.StatusOK, athletes)
}

func getAthletesByMeetingAndIdList(c *gin.Context) {
id := c.Param("meet_id")
if id == "" {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "given meet_id is empty"})
return
}

var data dto.AthleteIdList
if err := c.BindJSON(&data); err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}

athletes, err := service.GetAthletesByMeetingAndIdList(id, data.Athletes, extractPagingParams(c))
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}

c.IndentedJSON(http.StatusOK, athletes)
}

func getAthletesByTeam(c *gin.Context) {
id, convErr := primitive.ObjectIDFromHex(c.Param("team_id"))
if convErr != nil {
Expand Down
7 changes: 7 additions & 0 deletions dto/athlete_id_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dto

import "go.mongodb.org/mongo-driver/bson/primitive"

type AthleteIdList struct {
Athletes []primitive.ObjectID `json:"athletes"`
}
18 changes: 18 additions & 0 deletions service/athlete_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ func GetAthletesByMeetingId(id string, paging Paging) ([]model.Athlete, error) {
}, paging.getPaginatedOpts())
}

func GetAthletesByMeetingAndIdList(id string, athletes []primitive.ObjectID, paging Paging) ([]model.Athlete, error) {
return getAthletesByBsonDocumentWithOptions(bson.M{
"$and": []interface{}{
bson.M{"_id": bson.M{"$in": athletes}},
bson.M{"participation": id},
bson.M{
"$or": []interface{}{
bson.M{"name": bson.M{"$regex": paging.Query, "$options": "i"}},
bson.M{"firstname": bson.M{"$regex": paging.Query, "$options": "i"}},
bson.M{"lastname": bson.M{"$regex": paging.Query, "$options": "i"}},
bson.M{"dsv_id": bson.M{"$regex": paging.Query, "$options": "i"}},
bson.M{"alias": bson.M{"$regex": paging.Query, "$options": "i"}},
},
},
},
}, paging.getPaginatedOpts())
}

func GetAthletesByTeamAndMeeting(id primitive.ObjectID, meeting string, paging Paging) ([]model.Athlete, error) {
return getAthletesByBsonDocumentWithOptions(bson.M{
"$and": []interface{}{
Expand Down

0 comments on commit 56d7c9b

Please sign in to comment.