Skip to content

Commit aa2157e

Browse files
authored
Merge pull request #31 from ystv/liam.burnand/general-improvements
Adding general improvements and fixing TODOs
2 parents 3c649b6 + 147ab65 commit aa2157e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+6800
-580
lines changed

.github/workflows/golangci-lint.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- uses: actions/checkout@v3
1414
- uses: WillAbides/setup-go-faster@v1
1515
with:
16-
go-version: '1.22.5'
16+
go-version: '1.23.4'
1717
- name: golangci-lint
1818
uses: golangci/golangci-lint-action@v3
1919
with:

.github/workflows/nilaway.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- uses: actions/checkout@v3
1414
- uses: WillAbides/setup-go-faster@v1
1515
with:
16-
go-version: '1.22.5'
16+
go-version: '1.23.4'
1717
- name: download nilaway
1818
run: go install go.uber.org/nilaway/cmd/nilaway@latest
1919
- name: run nilaway

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.env.local
22
docker-compose.yml
33
web-api
4-
/swagger
4+
/swagger/*.json
55
*.sw[a-z]
66
/.idea

.golangci.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
linters:
2+
presets:
3+
- bugs
4+
- error
5+
- unused
6+
- comment
7+
- format
8+
- import
9+
- metalinter
10+
- module
11+
- performance
12+
- sql
13+
disable:
14+
- wrapcheck
15+
- err113
16+
- depguard
17+
- tagalign
18+
- godot
19+
- gci
20+
- gofumpt
21+
- godox

Dockerfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM golang:1.22.5-alpine3.20 AS build
1+
FROM golang:1.23.4-alpine3.21 AS build
22

33
LABEL site="api"
44
LABEL stage="builder"

README.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ The generation of usable JSON Web Tokens is available in [web-auth](https://gith
2020
- [x] Path to video
2121
- [x] Path to series
2222
- [ ] Thumbnail inheritance
23-
- [ ] Live
24-
- [x] Streams (currently static)
23+
- [x] Live
24+
- [x] Streams
2525
- [x] Teams
2626
- [x] Get current team
2727
- [x] List teams
@@ -67,7 +67,7 @@ The generation of usable JSON Web Tokens is available in [web-auth](https://gith
6767
- [ ] Events
6868
- [x] List by month
6969
- [ ] List by term
70-
- [ ] Sign ups
70+
- [ ] Signups
7171
- [x] Listing including positions
7272
- [x] Creating
7373
- [x] Updating
@@ -78,7 +78,11 @@ The generation of usable JSON Web Tokens is available in [web-auth](https://gith
7878
- [ ] Delete
7979
- [ ] Encoder
8080
- [x] Video upload auth hook
81-
- [x] Stream auth (experimental)
81+
- [x] Live
82+
- [x] List streams
83+
- [x] Add a stream
84+
- [x] Update stream
85+
- [x] Delete stream
8286
- [ ] Misc internal services
8387

8488
### Services

architecture.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Architecture
22

3-
This document aims to assist developers on understanding the codebase.
3+
This document aims to assist developers in understanding the codebase.
44

55
This application follows the MVC architecture (sort of) at a high level.
66
With the `controllers` packages containing the code handling HTTP responses

controllers/v1/clapper/clapper.go

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package clapper
22

33
import (
44
"github.com/jmoiron/sqlx"
5+
56
"github.com/ystv/web-api/services/clapper"
67
"github.com/ystv/web-api/services/clapper/crew"
78
"github.com/ystv/web-api/services/clapper/event"

controllers/v1/clapper/crew.go

+12
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ func (r *Repos) SetCrew(c echo.Context) error {
2828
err = fmt.Errorf("SetCrew: failed to get token: %w", err)
2929
return echo.NewHTTPError(http.StatusInternalServerError, err)
3030
}
31+
3132
crewID, err := strconv.Atoi(c.Param("crewid"))
3233
if err != nil {
3334
return echo.NewHTTPError(http.StatusBadRequest, "Invalid crew ID")
3435
}
36+
3537
err = r.crew.UpdateUserAndVerify(c.Request().Context(), crewID, p.UserID)
3638
if err != nil {
3739
return echo.NewHTTPError(http.StatusInternalServerError, err)
3840
}
41+
3942
return c.NoContent(http.StatusOK)
4043
}
4144

@@ -57,10 +60,12 @@ func (r *Repos) ResetCrew(c echo.Context) error {
5760
err = fmt.Errorf("ResetCrew: failed to get token: %w", err)
5861
return echo.NewHTTPError(http.StatusInternalServerError, err)
5962
}
63+
6064
crewID, err := strconv.Atoi(c.Param("crewid"))
6165
if err != nil {
6266
return echo.NewHTTPError(http.StatusBadRequest, "Invalid crew ID")
6367
}
68+
6469
// get crew object
6570
_, err = r.crew.Get(c.Request().Context(), crewID)
6671
if err != nil {
@@ -69,12 +74,14 @@ func (r *Repos) ResetCrew(c echo.Context) error {
6974
}
7075
return echo.NewHTTPError(http.StatusInternalServerError, err)
7176
}
77+
7278
// TODO verify user has permission
7379

7480
err = r.crew.DeleteUser(c.Request().Context(), crewID)
7581
if err != nil {
7682
return echo.NewHTTPError(http.StatusInternalServerError, err)
7783
}
84+
7885
return c.NoContent(http.StatusOK)
7986
}
8087

@@ -95,14 +102,17 @@ func (r *Repos) NewCrew(c echo.Context) error {
95102
if err != nil {
96103
return echo.NewHTTPError(http.StatusBadRequest, "Invalid signup ID")
97104
}
105+
98106
positionID, err := strconv.Atoi(c.Param("positionid"))
99107
if err != nil {
100108
return echo.NewHTTPError(http.StatusBadRequest, "Invalid position ID")
101109
}
110+
102111
err = r.crew.New(c.Request().Context(), signupID, positionID)
103112
if err != nil {
104113
return echo.NewHTTPError(http.StatusBadRequest, "Failed to insert crew: ", err)
105114
}
115+
106116
return c.NoContent(http.StatusOK)
107117
}
108118

@@ -122,10 +132,12 @@ func (r *Repos) DeleteCrew(c echo.Context) error {
122132
if err != nil {
123133
return echo.NewHTTPError(http.StatusBadRequest, "Invalid crew ID")
124134
}
135+
125136
err = r.crew.Delete(c.Request().Context(), signupID)
126137
if err != nil {
127138
err = fmt.Errorf("DeleteCrew failed: %w", err)
128139
return c.JSON(http.StatusInternalServerError, err)
129140
}
141+
130142
return c.NoContent(http.StatusOK)
131143
}

controllers/v1/clapper/event.go

+20-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ import (
88
"strconv"
99

1010
"github.com/labstack/echo/v4"
11+
1112
"github.com/ystv/web-api/services/clapper"
13+
"github.com/ystv/web-api/utils"
1214
)
1315

1416
// ListMonth returns all events for a month.
@@ -26,16 +28,19 @@ func (r *Repos) ListMonth(c echo.Context) error {
2628
if err != nil {
2729
return echo.NewHTTPError(http.StatusBadRequest, "Year incorrect, format /yyyy/mm")
2830
}
31+
2932
month, err := strconv.Atoi(c.Param("month"))
3033
if err != nil {
3134
return echo.NewHTTPError(http.StatusBadRequest, "Month incorrect, format /yyyy/mm")
3235
}
36+
3337
e, err := r.event.ListMonth(c.Request().Context(), year, month)
3438
if err != nil {
3539
err = fmt.Errorf("ListMonth failed: %w", err)
3640
return echo.NewHTTPError(http.StatusBadRequest, err)
3741
}
38-
return c.JSON(http.StatusOK, e)
42+
43+
return c.JSON(http.StatusOK, utils.NonNil(e))
3944
}
4045

4146
// GetEvent handles getting all signups and roles for a given event
@@ -52,11 +57,13 @@ func (r *Repos) GetEvent(c echo.Context) error {
5257
if err != nil {
5358
return echo.NewHTTPError(http.StatusBadRequest, "Invalid event ID")
5459
}
60+
5561
e, err := r.event.Get(c.Request().Context(), eventID)
5662
if err != nil {
5763
err = fmt.Errorf("GetEvent failed: %w", err)
5864
return echo.NewHTTPError(http.StatusInternalServerError, err)
5965
}
66+
6067
return c.JSON(http.StatusOK, e)
6168
}
6269

@@ -71,22 +78,26 @@ func (r *Repos) GetEvent(c echo.Context) error {
7178
// @Success 201 body int "Event ID"
7279
// @Router /v1/internal/clapper/event [post]
7380
func (r *Repos) NewEvent(c echo.Context) error {
74-
e := clapper.NewEvent{}
81+
var e clapper.NewEvent
82+
7583
err := c.Bind(&e)
7684
if err != nil {
7785
err = fmt.Errorf("NewEvent: failed to bind to request json: %w", err)
7886
return echo.NewHTTPError(http.StatusBadRequest, err)
7987
}
88+
8089
p, err := r.access.GetToken(c.Request())
8190
if err != nil {
8291
err = fmt.Errorf("NewEvent: failed to get token: %w", err)
8392
return echo.NewHTTPError(http.StatusInternalServerError, err)
8493
}
94+
8595
eventID, err := r.event.New(c.Request().Context(), &e, p.UserID)
8696
if err != nil {
8797
err = fmt.Errorf("NewEvent: failed to insert event: %w", err)
8898
return echo.NewHTTPError(http.StatusInternalServerError, err)
8999
}
100+
90101
return c.JSON(http.StatusCreated, eventID)
91102
}
92103

@@ -101,17 +112,20 @@ func (r *Repos) NewEvent(c echo.Context) error {
101112
// @Success 200
102113
// @Router /v1/internal/clapper/event [put]
103114
func (r *Repos) UpdateEvent(c echo.Context) error {
104-
e := clapper.Event{}
115+
var e clapper.Event
116+
105117
err := c.Bind(&e)
106118
if err != nil {
107119
err = fmt.Errorf("UpdateEvent: failed to bind to request json: %w", err)
108120
return echo.NewHTTPError(http.StatusBadRequest, err)
109121
}
122+
110123
p, err := r.access.GetToken(c.Request())
111124
if err != nil {
112125
err = fmt.Errorf("UpdateEvent: failed to get token: %w", err)
113126
return echo.NewHTTPError(http.StatusInternalServerError, err)
114127
}
128+
115129
err = r.event.Update(c.Request().Context(), &e, p.UserID)
116130
if err != nil {
117131
if errors.Is(err, sql.ErrNoRows) {
@@ -120,6 +134,7 @@ func (r *Repos) UpdateEvent(c echo.Context) error {
120134
err = fmt.Errorf("UpdateEvent: failed to update: %w", err)
121135
return echo.NewHTTPError(http.StatusInternalServerError, err)
122136
}
137+
123138
return c.NoContent(http.StatusOK)
124139
}
125140

@@ -138,9 +153,11 @@ func (r *Repos) DeleteEvent(c echo.Context) error {
138153
if err != nil {
139154
return echo.NewHTTPError(http.StatusBadRequest, "Invalid event ID")
140155
}
156+
141157
err = r.event.Delete(c.Request().Context(), eventID)
142158
if err != nil {
143159
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to delete event: %w", err))
144160
}
161+
145162
return c.NoContent(http.StatusOK)
146163
}

controllers/v1/clapper/position.go

+17-6
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,27 @@ import (
88
"strconv"
99

1010
"github.com/labstack/echo/v4"
11+
1112
"github.com/ystv/web-api/services/clapper"
13+
"github.com/ystv/web-api/utils"
1214
)
1315

14-
// ListPosition handles listing all possible positions
16+
// ListPositions handles listing all possible positions
1517
// @Summary List positions
1618
// @Description Lists all positions.
1719
// @ID get-positions
1820
// @Tags clapper-positions
1921
// @Produce json
2022
// @Success 200 {array} clapper.Position
2123
// @Router /v1/internal/clapper/positions [get]
22-
func (r *Repos) ListPosition(c echo.Context) error {
24+
func (r *Repos) ListPositions(c echo.Context) error {
2325
p, err := r.position.List(c.Request().Context())
2426
if err != nil {
25-
err = fmt.Errorf("ListPosition: failed to list: %w", err)
27+
err = fmt.Errorf("ListPositions: failed to list: %w", err)
2628
return echo.NewHTTPError(http.StatusInternalServerError, err)
2729
}
28-
return c.JSON(http.StatusOK, p)
30+
31+
return c.JSON(http.StatusOK, utils.NonNil(p))
2932
}
3033

3134
// NewPosition handles creating a new position
@@ -37,17 +40,20 @@ func (r *Repos) ListPosition(c echo.Context) error {
3740
// @Success 201 body int "Position ID"
3841
// @Router /v1/internal/clapper/positions [post]
3942
func (r *Repos) NewPosition(c echo.Context) error {
40-
p := clapper.Position{}
43+
var p clapper.Position
44+
4145
err := c.Bind(&p)
4246
if err != nil {
4347
err = fmt.Errorf("NewPosition: failed to bind to request json: %w", err)
4448
return echo.NewHTTPError(http.StatusBadRequest, err)
4549
}
50+
4651
positionID, err := r.position.New(c.Request().Context(), &p)
4752
if err != nil {
4853
err = fmt.Errorf("NewPosition: failed to insert position: %w", err)
4954
return echo.NewHTTPError(http.StatusInternalServerError, err)
5055
}
56+
5157
return c.JSON(http.StatusCreated, positionID)
5258
}
5359

@@ -60,12 +66,14 @@ func (r *Repos) NewPosition(c echo.Context) error {
6066
// @Success 200
6167
// @Router /v1/internal/clapper/positions [put]
6268
func (r *Repos) UpdatePosition(c echo.Context) error {
63-
p := clapper.Position{}
69+
var p clapper.Position
70+
6471
err := c.Bind(&p)
6572
if err != nil {
6673
err = fmt.Errorf("UpdatePosition: failed to bind to request json: %w", err)
6774
return echo.NewHTTPError(http.StatusBadRequest, err)
6875
}
76+
6977
err = r.position.Update(c.Request().Context(), &p)
7078
if err != nil {
7179
if errors.Is(err, sql.ErrNoRows) {
@@ -74,6 +82,7 @@ func (r *Repos) UpdatePosition(c echo.Context) error {
7482
err = fmt.Errorf("UpdatePosition failed: %w", err)
7583
return c.JSON(http.StatusInternalServerError, err)
7684
}
85+
7786
return c.NoContent(http.StatusOK)
7887
}
7988

@@ -91,9 +100,11 @@ func (r *Repos) DeletePosition(c echo.Context) error {
91100
if err != nil {
92101
return echo.NewHTTPError(http.StatusBadRequest, "Invalid position ID")
93102
}
103+
94104
err = r.position.Delete(c.Request().Context(), positionID)
95105
if err != nil {
96106
return echo.NewHTTPError(http.StatusInternalServerError, fmt.Errorf("failed to delete event: %w", err))
97107
}
108+
98109
return c.NoContent(http.StatusOK)
99110
}

0 commit comments

Comments
 (0)