Skip to content

Commit

Permalink
get team and athlete by alias (+year)
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad2002 committed Sep 3, 2023
1 parent 29fdfb5 commit b691062
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
22 changes: 22 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/:id", getAthlete)
router.GET("/athlete/name_year", getAthleteByNameAndYear)
router.GET("/athlete/alias_year", getAthleteByAliasAndYear)
router.GET("/athlete/meet/:meet_id", getAthletesByMeeting)
router.GET("/athlete/team/:team_id", getAthletesByTeam)
router.GET("/athlete/team/:team_id/meet/:meet_id", getAthletesByTeamAndMeeting)
Expand Down Expand Up @@ -129,6 +130,27 @@ func getAthleteByNameAndYear(c *gin.Context) {
c.IndentedJSON(http.StatusOK, athlete)
}

func getAthleteByAliasAndYear(c *gin.Context) {
name := c.Query("alias")
if name == "" {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "given alias was empty"})
return
}
year, err := strconv.Atoi(c.Query("year"))
if err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": err.Error()})
return
}

athlete, err := service.GetAthleteByAliasAndYear(name, year)
if err != nil {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()})
return
}

c.IndentedJSON(http.StatusOK, athlete)
}

func removeAthlete(c *gin.Context) {
id, convErr := primitive.ObjectIDFromHex(c.Param("id"))
if convErr != nil {
Expand Down
17 changes: 17 additions & 0 deletions controller/team_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func teamController() {
router.GET("/team/:id", getTeam)
router.GET("/team/meet/:meet_id", getTeamsByMeeting)
router.GET("/team/name", getTeamByName)
router.GET("/team/alias", getTeamByAlias)
router.POST("/team", addTeam)
router.POST("/team/import", importTeam)

Expand Down Expand Up @@ -79,6 +80,22 @@ func getTeamByName(c *gin.Context) {
c.IndentedJSON(http.StatusOK, team)
}

func getTeamByAlias(c *gin.Context) {
name := c.Query("alias")
if name == "" {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "given alias was empty"})
return
}

team, err := service.GetTeamByAlias(name)
if err != nil {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()})
return
}

c.IndentedJSON(http.StatusOK, team)
}

func addTeam(c *gin.Context) {
var team model.Team
if err := c.BindJSON(&team); err != nil {
Expand Down
19 changes: 19 additions & 0 deletions service/athlete_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,25 @@ func GetAthleteByNameAndYear(name string, year int) (model.Athlete, error) {
return model.Athlete{}, errors.New("no entry found")
}

func GetAthleteByAliasAndYear(alias string, year int) (model.Athlete, error) {
athletes, err := getAthletesByBsonDocument(bson.M{
"$and": []interface{}{
bson.M{"year": year},
bson.M{"alias": bson.M{"$regex": alias, "$options": "i"}},
},
})
if err != nil {
return model.Athlete{}, err
}

if len(athletes) > 0 {
return athletes[0], nil
}

fmt.Printf("no athlete with given alias '%s' and year %d found\n", alias, year)
return model.Athlete{}, errors.New("no entry found")
}

func RemoveAthleteById(id primitive.ObjectID) error {

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
Expand Down
18 changes: 18 additions & 0 deletions service/team_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,24 @@ func GetTeamByName(name string) (model.Team, error) {
return teams[0], nil
}

func GetTeamByAlias(alias string) (model.Team, error) {
teams, err := getTeamsByBsonDocument(
bson.M{
"$or": []interface{}{
bson.M{"name": bson.M{"$regex": alias, "$options": "i"}},
bson.M{"alias": bson.M{"$regex": alias, "$options": "i"}},
},
})
if err != nil {
return model.Team{}, err
}
if len(teams) < 1 {
fmt.Printf("no team with given alias '%s' found\n", alias)
return model.Team{}, errors.New("no team with given alias found")
}
return teams[0], nil
}

func AddTeam(team model.Team) (model.Team, error) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
Expand Down

0 comments on commit b691062

Please sign in to comment.