Skip to content

Commit

Permalink
get athletes by meeting and team
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad2002 committed Aug 17, 2023
1 parent 10bc6a6 commit b38dbf2
Show file tree
Hide file tree
Showing 2 changed files with 41 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 @@ -16,6 +16,7 @@ func athleteController() {
router.GET("/athlete/name_year", getAthleteByNameAndYear)
router.GET("/athlete/meet/:meet_id", getAthletesByMeeting)
router.GET("/athlete/team/:team_id", getAthletesByTeam)
router.GET("/athlete/team/:team_id/meet/:meet_id", getAthletesByTeamAndMeeting)

router.DELETE("/athlete/:id", removeAthlete)
router.POST("/athlete", addAthlete)
Expand Down Expand Up @@ -69,6 +70,28 @@ func getAthletesByTeam(c *gin.Context) {
c.IndentedJSON(http.StatusOK, athletes)
}

func getAthletesByTeamAndMeeting(c *gin.Context) {
id, convErr := primitive.ObjectIDFromHex(c.Param("team_id"))
if convErr != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "given team_id was not of type ObjectID"})
return
}

meeting := c.Param("meet_id")
if meeting == "" {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "given meet_id is empty"})
return
}

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

c.IndentedJSON(http.StatusOK, athletes)
}

func getAthlete(c *gin.Context) {
id, convErr := primitive.ObjectIDFromHex(c.Param("id"))
if convErr != nil {
Expand Down
18 changes: 18 additions & 0 deletions service/athlete_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,24 @@ func GetAthletesByMeetingId(id string, paging Paging) ([]model.Athlete, error) {
}, paging.getPaginatedOpts())
}

func GetAthletesByTeamAndMeeting(id primitive.ObjectID, meeting string, paging Paging) ([]model.Athlete, error) {
return getAthletesByBsonDocumentWithOptions(bson.M{
"$and": []interface{}{
bson.M{"participation": meeting},
bson.M{"team_id": 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 GetAthletesByTeamId(id primitive.ObjectID, paging Paging) ([]model.Athlete, error) {
return getAthletesByBsonDocumentWithOptions(bson.M{
"$and": []interface{}{
Expand Down

0 comments on commit b38dbf2

Please sign in to comment.