Skip to content

Commit

Permalink
athlete amount endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad2002 committed Sep 25, 2023
1 parent b691062 commit cc3abe6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
13 changes: 13 additions & 0 deletions controller/athlete_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (

func athleteController() {
router.GET("/athlete", getAthletes)

router.GET("/athlete/amount", getAthletesAmount)

router.GET("/athlete/:id", getAthlete)
router.GET("/athlete/name_year", getAthleteByNameAndYear)
router.GET("/athlete/alias_year", getAthleteByAliasAndYear)
Expand Down Expand Up @@ -39,6 +42,16 @@ func getAthletes(c *gin.Context) {
c.IndentedJSON(http.StatusOK, athletes)
}

func getAthletesAmount(c *gin.Context) {
starts, err := service.GetAthletesAmount()
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
12 changes: 12 additions & 0 deletions service/athlete_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ func getAthletesByBsonDocumentWithOptions(d interface{}, fOps options.FindOption
return athletes, nil
}

func GetAthletesAmount() (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{}, opts)
if err != nil {
return 0, err
}
return int(count), nil
}

func GetAthletes(paging Paging) ([]model.Athlete, error) {
return getAthletesByBsonDocumentWithOptions(
bson.M{
Expand Down

0 comments on commit cc3abe6

Please sign in to comment.