Skip to content

Commit

Permalink
get team by name request and client impl
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad2002 committed Jun 19, 2023
1 parent f5d8fb1 commit 10bc6a6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
29 changes: 29 additions & 0 deletions client/team_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,32 @@ func (c *TeamClient) ImportTeam(team model.Team, meeting string) (*model.Team, b
}
return newTeam, res.StatusCode == http.StatusCreated, nil
}

func (c *TeamClient) GetTeamByName(name string) (*model.Team, bool, error) {
fmt.Printf("request '%s'\n", c.apiUrl+"team/name?name="+name)

params := map[string]string{
"name": name,
}

res, err := client.Get(c.apiUrl, "team/name", params)
if err != nil {
return nil, false, err
}
defer res.Body.Close()

if res.StatusCode != http.StatusOK {
if res.StatusCode == http.StatusNotFound {
return nil, false, nil
}
return nil, false, fmt.Errorf("GetTeamByName received error: %d\n", res.StatusCode)
}

team := &model.Team{}
err = json.NewDecoder(res.Body).Decode(team)
if err != nil {
return nil, false, err
}

return team, true, nil
}
17 changes: 17 additions & 0 deletions controller/team_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func teamController() {
router.GET("/team", getTeams)
router.GET("/team/:id", getTeam)
router.GET("/team/meet/:meet_id", getTeamsByMeeting)
router.GET("/team/name", getTeamByName)
router.POST("/team", addTeam)
router.POST("/team/import", importTeam)

Expand Down Expand Up @@ -62,6 +63,22 @@ func getTeamsByMeeting(c *gin.Context) {
c.IndentedJSON(http.StatusOK, teams)
}

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

team, err := service.GetTeamByName(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
4 changes: 2 additions & 2 deletions service/athlete_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ func ImportAthlete(athlete model.Athlete, meetId string) (*model.Athlete, bool,
if athlete.Team.DsvId != 0 {
team, err = GetTeamByDsvId(athlete.Team.DsvId)
} else {
team, err = getTeamByName(athlete.Team.Name)
team, err = GetTeamByName(athlete.Team.Name)
}
if err != nil {
return nil, true, err
Expand All @@ -319,7 +319,7 @@ func ImportAthlete(athlete model.Athlete, meetId string) (*model.Athlete, bool,

// if dsv_id, search by dsv_id (dsv_id '==')
// -> not found
// search by name, team and year (name aliasified in aliases; team (getTeamByName), year '==')
// search by name, team and year (name aliasified in aliases; team (GetTeamByName), year '==')
//
// -> not found: create
// add participation
Expand Down
4 changes: 2 additions & 2 deletions service/team_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func GetTeamByDsvId(dsvId int) (model.Team, error) {
return teams[0], nil
}

func getTeamByName(name string) (model.Team, error) {
func GetTeamByName(name string) (model.Team, error) {
teams, err := getTeamsByBsonDocument(
bson.M{
"$or": []interface{}{
Expand Down Expand Up @@ -155,7 +155,7 @@ func AddTeamParticipation(id primitive.ObjectID, meetId string) (model.Team, err
}

func ImportTeam(team model.Team, meetId string) (model.Team, bool, error) {
existingTeam, err := getTeamByName(team.Name)
existingTeam, err := GetTeamByName(team.Name)

if err != nil {
if err.Error() == "no team with given name found" {
Expand Down

0 comments on commit 10bc6a6

Please sign in to comment.