Skip to content

Commit

Permalink
feat(backend): Add list leagues endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstojda committed Jul 24, 2023
1 parent efbbe0a commit 3b7302c
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 3 deletions.
2 changes: 2 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ components:
items:
$ref: '#/components/schemas/league'
type: object
required:
- leagues
leagueCreate:
properties:
name:
Expand Down
2 changes: 1 addition & 1 deletion internal/app/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ func (s *Server) PostLeagues(c *gin.Context) {
}

func (s *Server) GetLeagues(c *gin.Context) {
//
s.League.ListLeagues(c)
}
27 changes: 27 additions & 0 deletions internal/app/api/league/league.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,30 @@ func (c *Controller) CreateLeague(ctx *gin.Context) {
},
})
}

func (c *Controller) ListLeagues(ctx *gin.Context) {
var dbResults []models.League
result := c.DB.Find(&dbResults)
if result.Error != nil {
log.Err(result.Error).Msg("failed to list leagues")
errors.AbortWithError(http.StatusInternalServerError, "failed to list leagues", ctx)
return
}

var leagues []generated.League
for _, league := range dbResults {
leagues = append(leagues, generated.League{
Id: league.ID.String(),
Name: league.Name,
Slug: league.Slug,
Location: league.Location,
OwnerId: league.Owner.ID.String(),
CreatedAt: utils.FormatTime(league.CreatedAt),
UpdatedAt: utils.FormatTime(league.UpdatedAt),
})
}

ctx.JSON(http.StatusOK, generated.LeagueListResponse{
Leagues: leagues,
})
}
67 changes: 67 additions & 0 deletions internal/app/api/league/league_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,71 @@ var _ = ginkgo.Describe("Controller", func() {
})
})
})

ginkgo.When("ListLeagues receives a request", func() {
ginkgo.Context("with valid request", func() {
ginkgo.It("succeeds", func() {
router.Use(func(ctx *gin.Context) {
ctx.Set(auth.IdentityKey, userObj)
})

leagueObj := &models.League{
ID: uuid.New(),
Name: "Test League",
Slug: "test-league",
Owner: *userObj,
Location: "Test Location",
CreatedAt: time.Now().Add(-1 * time.Hour),
UpdatedAt: time.Now(),
}

mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "leagues"`)).
WillReturnRows(
sqlmock.NewRows([]string{"id", "name", "slug", "owner_id", "location", "created_at", "updated_at"}).
AddRow(leagueObj.ID, leagueObj.Name, leagueObj.Slug, leagueObj.Owner.ID, leagueObj.Location, leagueObj.CreatedAt, leagueObj.UpdatedAt),
)

req, err := http.NewRequest("GET", "/", nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())

router.GET("/", controller.ListLeagues)
router.ServeHTTP(rr, req)

gomega.Expect(rr.Code).To(gomega.Equal(http.StatusOK))
response := &generated.LeagueListResponse{}
err = json.Unmarshal(rr.Body.Bytes(), response)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(mock.ExpectationsWereMet()).ToNot(gomega.HaveOccurred())

gomega.Expect(response.Leagues).To(gomega.HaveLen(1))
gomega.Expect(response.Leagues[0].Name).To(gomega.Equal(leagueObj.Name))
gomega.Expect(response.Leagues[0].Slug).To(gomega.Equal(leagueObj.Slug))
gomega.Expect(response.Leagues[0].Location).To(gomega.Equal(leagueObj.Location))
})
})
ginkgo.Context("with unknown sql error", func() {
ginkgo.It("fails", func() {
router.Use(func(ctx *gin.Context) {
ctx.Set(auth.IdentityKey, userObj)
})

mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "leagues"`)).
WillReturnError(fmt.Errorf("unknown error"))

req, err := http.NewRequest("GET", "/", nil)
gomega.Expect(err).ToNot(gomega.HaveOccurred())

router.GET("/", controller.ListLeagues)
router.ServeHTTP(rr, req)

gomega.Expect(rr.Code).To(gomega.Equal(http.StatusInternalServerError))
response := &generated.ErrorResponse{}
err = json.Unmarshal(rr.Body.Bytes(), response)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(mock.ExpectationsWereMet()).ToNot(gomega.HaveOccurred())

gomega.Expect(response.Detail).To(gomega.Equal("failed to list leagues"))
})
})
})
})
4 changes: 3 additions & 1 deletion internal/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"pinman/internal/app/api/errors"
"pinman/internal/app/api/health"
"pinman/internal/app/api/hello"
"pinman/internal/app/api/league"
"pinman/internal/app/api/user"
"pinman/internal/app/generated"
"pinman/internal/utils"
Expand Down Expand Up @@ -81,7 +82,8 @@ func (s *Server) StartServer() error {
generated.RegisterHandlersWithOptions(
router,
&api.Server{
User: user.NewController(s.Db),
User: user.NewController(s.Db),
League: league.NewController(s.Db),
AuthHandlers: api.AuthHandlers{
Login: authMiddleware.LoginHandler,
Refresh: authMiddleware.RefreshHandler,
Expand Down
3 changes: 2 additions & 1 deletion internal/models/league.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ type League struct {
Name string `gorm:"type:varchar(255);not null"`
Slug string `gorm:"type:varchar(255);not null;uniqueIndex"`
OwnerID uuid.UUID `gorm:"type:uuid;not null"`
Location string `gorm:"type:varchar(255)"`
Owner User
Location string `gorm:"type:varchar(255)"`
CreatedAt time.Time
UpdatedAt time.Time
}

0 comments on commit 3b7302c

Please sign in to comment.