Skip to content

Commit

Permalink
Merge pull request #2 from SwimResults/master
Browse files Browse the repository at this point in the history
add endpoint for team and athlete amount by meeting
  • Loading branch information
konrad2002 authored Nov 12, 2023
2 parents d31f8d0 + c25aaaa commit 19327c8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
17 changes: 17 additions & 0 deletions controller/athlete_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func athleteController() {
router.GET("/athlete", getAthletes)

router.GET("/athlete/amount", getAthletesAmount)
router.GET("/athlete/meet/:meet_id/amount", getAthletesAmountByMeeting)

router.GET("/athlete/:id", getAthlete)
router.GET("/athlete/name_year", getAthleteByNameAndYear)
Expand Down Expand Up @@ -52,6 +53,22 @@ func getAthletesAmount(c *gin.Context) {
c.IndentedJSON(http.StatusOK, starts)
}

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

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

c.IndentedJSON(http.StatusOK, starts)
}

func getAthletesByMeeting(c *gin.Context) {
id := c.Param("meet_id")
if id == "" {
Expand Down
30 changes: 30 additions & 0 deletions controller/team_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import (
func teamController() {
router.GET("/team", getTeams)
router.GET("/team/:id", getTeam)

router.GET("/team/amount", getTeamsAmount)
router.GET("/team/meet/:meet_id/amount", getTeamsAmountByMeeting)

router.GET("/team/meet/:meet_id", getTeamsByMeeting)
router.GET("/team/name", getTeamByName)
router.GET("/team/alias", getTeamByAlias)
Expand Down Expand Up @@ -48,6 +52,32 @@ func getTeam(c *gin.Context) {
c.IndentedJSON(http.StatusOK, team)
}

func getTeamsAmount(c *gin.Context) {
starts, err := service.GetTeamsAmount()
if err != nil {
c.IndentedJSON(http.StatusInternalServerError, gin.H{"message": err.Error()})
return
}

c.IndentedJSON(http.StatusOK, starts)
}

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

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

c.IndentedJSON(http.StatusOK, starts)
}

func getTeamsByMeeting(c *gin.Context) {
id := c.Param("meet_id")
if id == "" {
Expand Down
12 changes: 12 additions & 0 deletions service/athlete_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ func GetAthletesAmount() (int, error) {
return int(count), nil
}

func GetAthletesAmountByMeeting(meeting string) (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

opts := options.Count().SetHint("_id_")
count, err := athleteCollection.CountDocuments(ctx, bson.D{{"participation", meeting}}, opts)
if err != nil {
return 0, err
}
return int(count), nil
}

func GetAthletes(paging Paging) ([]model.Athlete, error) {
return getAthletesByBsonDocumentWithOptions(
bson.M{
Expand Down
24 changes: 24 additions & 0 deletions service/team_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ func GetTeams(paging Paging) ([]model.Team, error) {
}, paging.getPaginatedOpts())
}

func GetTeamsAmount() (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

opts := options.Count().SetHint("_id_")
count, err := teamCollection.CountDocuments(ctx, bson.D{}, opts)
if err != nil {
return 0, err
}
return int(count), nil
}

func GetTeamsAmountByMeeting(meeting string) (int, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

opts := options.Count().SetHint("_id_")
count, err := teamCollection.CountDocuments(ctx, bson.D{{"participation", meeting}}, opts)
if err != nil {
return 0, err
}
return int(count), nil
}

func GetTeamsByMeeting(id string, paging Paging) ([]model.Team, error) {
return getTeamsByBsonDocumentWithOptions(bson.M{
"$and": []interface{}{
Expand Down

0 comments on commit 19327c8

Please sign in to comment.