Skip to content

Commit

Permalink
Merge pull request #31 from ystv/liam.burnand/general-improvements
Browse files Browse the repository at this point in the history
Adding general improvements and fixing TODOs
  • Loading branch information
COMTOP1 authored Jan 20, 2025
2 parents 3c649b6 + 147ab65 commit aa2157e
Show file tree
Hide file tree
Showing 86 changed files with 6,800 additions and 580 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v3
- uses: WillAbides/setup-go-faster@v1
with:
go-version: '1.22.5'
go-version: '1.23.4'
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nilaway.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v3
- uses: WillAbides/setup-go-faster@v1
with:
go-version: '1.22.5'
go-version: '1.23.4'
- name: download nilaway
run: go install go.uber.org/nilaway/cmd/nilaway@latest
- name: run nilaway
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.env.local
docker-compose.yml
web-api
/swagger
/swagger/*.json
*.sw[a-z]
/.idea
21 changes: 21 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
linters:
presets:
- bugs
- error
- unused
- comment
- format
- import
- metalinter
- module
- performance
- sql
disable:
- wrapcheck
- err113
- depguard
- tagalign
- godot
- gci
- gofumpt
- godox
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.22.5-alpine3.20 AS build
FROM golang:1.23.4-alpine3.21 AS build

LABEL site="api"
LABEL stage="builder"
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ The generation of usable JSON Web Tokens is available in [web-auth](https://gith
- [x] Path to video
- [x] Path to series
- [ ] Thumbnail inheritance
- [ ] Live
- [x] Streams (currently static)
- [x] Live
- [x] Streams
- [x] Teams
- [x] Get current team
- [x] List teams
Expand Down Expand Up @@ -67,7 +67,7 @@ The generation of usable JSON Web Tokens is available in [web-auth](https://gith
- [ ] Events
- [x] List by month
- [ ] List by term
- [ ] Sign ups
- [ ] Signups
- [x] Listing including positions
- [x] Creating
- [x] Updating
Expand All @@ -78,7 +78,11 @@ The generation of usable JSON Web Tokens is available in [web-auth](https://gith
- [ ] Delete
- [ ] Encoder
- [x] Video upload auth hook
- [x] Stream auth (experimental)
- [x] Live
- [x] List streams
- [x] Add a stream
- [x] Update stream
- [x] Delete stream
- [ ] Misc internal services

### Services
Expand Down
2 changes: 1 addition & 1 deletion architecture.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Architecture

This document aims to assist developers on understanding the codebase.
This document aims to assist developers in understanding the codebase.

This application follows the MVC architecture (sort of) at a high level.
With the `controllers` packages containing the code handling HTTP responses
Expand Down
1 change: 1 addition & 0 deletions controllers/v1/clapper/clapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package clapper

import (
"github.com/jmoiron/sqlx"

"github.com/ystv/web-api/services/clapper"
"github.com/ystv/web-api/services/clapper/crew"
"github.com/ystv/web-api/services/clapper/event"
Expand Down
12 changes: 12 additions & 0 deletions controllers/v1/clapper/crew.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ func (r *Repos) SetCrew(c echo.Context) error {
err = fmt.Errorf("SetCrew: failed to get token: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

crewID, err := strconv.Atoi(c.Param("crewid"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid crew ID")
}

err = r.crew.UpdateUserAndVerify(c.Request().Context(), crewID, p.UserID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

return c.NoContent(http.StatusOK)
}

Expand All @@ -57,10 +60,12 @@ func (r *Repos) ResetCrew(c echo.Context) error {
err = fmt.Errorf("ResetCrew: failed to get token: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

crewID, err := strconv.Atoi(c.Param("crewid"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid crew ID")
}

// get crew object
_, err = r.crew.Get(c.Request().Context(), crewID)
if err != nil {
Expand All @@ -69,12 +74,14 @@ func (r *Repos) ResetCrew(c echo.Context) error {
}
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

// TODO verify user has permission

err = r.crew.DeleteUser(c.Request().Context(), crewID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

return c.NoContent(http.StatusOK)
}

Expand All @@ -95,14 +102,17 @@ func (r *Repos) NewCrew(c echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid signup ID")
}

positionID, err := strconv.Atoi(c.Param("positionid"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid position ID")
}

err = r.crew.New(c.Request().Context(), signupID, positionID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Failed to insert crew: ", err)
}

return c.NoContent(http.StatusOK)
}

Expand All @@ -122,10 +132,12 @@ func (r *Repos) DeleteCrew(c echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid crew ID")
}

err = r.crew.Delete(c.Request().Context(), signupID)
if err != nil {
err = fmt.Errorf("DeleteCrew failed: %w", err)
return c.JSON(http.StatusInternalServerError, err)
}

return c.NoContent(http.StatusOK)
}
23 changes: 20 additions & 3 deletions controllers/v1/clapper/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"strconv"

"github.com/labstack/echo/v4"

"github.com/ystv/web-api/services/clapper"
"github.com/ystv/web-api/utils"
)

// ListMonth returns all events for a month.
Expand All @@ -26,16 +28,19 @@ func (r *Repos) ListMonth(c echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Year incorrect, format /yyyy/mm")
}

month, err := strconv.Atoi(c.Param("month"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Month incorrect, format /yyyy/mm")
}

e, err := r.event.ListMonth(c.Request().Context(), year, month)
if err != nil {
err = fmt.Errorf("ListMonth failed: %w", err)
return echo.NewHTTPError(http.StatusBadRequest, err)
}
return c.JSON(http.StatusOK, e)

return c.JSON(http.StatusOK, utils.NonNil(e))
}

// GetEvent handles getting all signups and roles for a given event
Expand All @@ -52,11 +57,13 @@ func (r *Repos) GetEvent(c echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid event ID")
}

e, err := r.event.Get(c.Request().Context(), eventID)
if err != nil {
err = fmt.Errorf("GetEvent failed: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

return c.JSON(http.StatusOK, e)
}

Expand All @@ -71,22 +78,26 @@ func (r *Repos) GetEvent(c echo.Context) error {
// @Success 201 body int "Event ID"
// @Router /v1/internal/clapper/event [post]
func (r *Repos) NewEvent(c echo.Context) error {
e := clapper.NewEvent{}
var e clapper.NewEvent

err := c.Bind(&e)
if err != nil {
err = fmt.Errorf("NewEvent: failed to bind to request json: %w", err)
return echo.NewHTTPError(http.StatusBadRequest, err)
}

p, err := r.access.GetToken(c.Request())
if err != nil {
err = fmt.Errorf("NewEvent: failed to get token: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

eventID, err := r.event.New(c.Request().Context(), &e, p.UserID)
if err != nil {
err = fmt.Errorf("NewEvent: failed to insert event: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

return c.JSON(http.StatusCreated, eventID)
}

Expand All @@ -101,17 +112,20 @@ func (r *Repos) NewEvent(c echo.Context) error {
// @Success 200
// @Router /v1/internal/clapper/event [put]
func (r *Repos) UpdateEvent(c echo.Context) error {
e := clapper.Event{}
var e clapper.Event

err := c.Bind(&e)
if err != nil {
err = fmt.Errorf("UpdateEvent: failed to bind to request json: %w", err)
return echo.NewHTTPError(http.StatusBadRequest, err)
}

p, err := r.access.GetToken(c.Request())
if err != nil {
err = fmt.Errorf("UpdateEvent: failed to get token: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

err = r.event.Update(c.Request().Context(), &e, p.UserID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
Expand All @@ -120,6 +134,7 @@ func (r *Repos) UpdateEvent(c echo.Context) error {
err = fmt.Errorf("UpdateEvent: failed to update: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

return c.NoContent(http.StatusOK)
}

Expand All @@ -138,9 +153,11 @@ func (r *Repos) DeleteEvent(c echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid event ID")
}

err = r.event.Delete(c.Request().Context(), eventID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to delete event: %w", err))
}

return c.NoContent(http.StatusOK)
}
23 changes: 17 additions & 6 deletions controllers/v1/clapper/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,27 @@ import (
"strconv"

"github.com/labstack/echo/v4"

"github.com/ystv/web-api/services/clapper"
"github.com/ystv/web-api/utils"
)

// ListPosition handles listing all possible positions
// ListPositions handles listing all possible positions
// @Summary List positions
// @Description Lists all positions.
// @ID get-positions
// @Tags clapper-positions
// @Produce json
// @Success 200 {array} clapper.Position
// @Router /v1/internal/clapper/positions [get]
func (r *Repos) ListPosition(c echo.Context) error {
func (r *Repos) ListPositions(c echo.Context) error {
p, err := r.position.List(c.Request().Context())
if err != nil {
err = fmt.Errorf("ListPosition: failed to list: %w", err)
err = fmt.Errorf("ListPositions: failed to list: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
return c.JSON(http.StatusOK, p)

return c.JSON(http.StatusOK, utils.NonNil(p))
}

// NewPosition handles creating a new position
Expand All @@ -37,17 +40,20 @@ func (r *Repos) ListPosition(c echo.Context) error {
// @Success 201 body int "Position ID"
// @Router /v1/internal/clapper/positions [post]
func (r *Repos) NewPosition(c echo.Context) error {
p := clapper.Position{}
var p clapper.Position

err := c.Bind(&p)
if err != nil {
err = fmt.Errorf("NewPosition: failed to bind to request json: %w", err)
return echo.NewHTTPError(http.StatusBadRequest, err)
}

positionID, err := r.position.New(c.Request().Context(), &p)
if err != nil {
err = fmt.Errorf("NewPosition: failed to insert position: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}

return c.JSON(http.StatusCreated, positionID)
}

Expand All @@ -60,12 +66,14 @@ func (r *Repos) NewPosition(c echo.Context) error {
// @Success 200
// @Router /v1/internal/clapper/positions [put]
func (r *Repos) UpdatePosition(c echo.Context) error {
p := clapper.Position{}
var p clapper.Position

err := c.Bind(&p)
if err != nil {
err = fmt.Errorf("UpdatePosition: failed to bind to request json: %w", err)
return echo.NewHTTPError(http.StatusBadRequest, err)
}

err = r.position.Update(c.Request().Context(), &p)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
Expand All @@ -74,6 +82,7 @@ func (r *Repos) UpdatePosition(c echo.Context) error {
err = fmt.Errorf("UpdatePosition failed: %w", err)
return c.JSON(http.StatusInternalServerError, err)
}

return c.NoContent(http.StatusOK)
}

Expand All @@ -91,9 +100,11 @@ func (r *Repos) DeletePosition(c echo.Context) error {
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid position ID")
}

err = r.position.Delete(c.Request().Context(), positionID)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to delete event: %w", err))
}

return c.NoContent(http.StatusOK)
}
Loading

0 comments on commit aa2157e

Please sign in to comment.