Skip to content

Commit

Permalink
Merge pull request #11 from ystv/crewSignup
Browse files Browse the repository at this point in the history
Crew signup
  • Loading branch information
rmil authored Nov 20, 2020
2 parents 9cfd5c8 + 3d711d3 commit 1a92cef
Show file tree
Hide file tree
Showing 14 changed files with 1,286 additions and 63 deletions.
4 changes: 3 additions & 1 deletion controllers/v1/clapper/clapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ 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"
"github.com/ystv/web-api/services/clapper/position"
"github.com/ystv/web-api/services/clapper/signup"
)

// Repos encapsulates the dependency
type Repos struct {
crew clapper.CrewRepo
event clapper.EventRepo
signup clapper.SignupRepo
position clapper.PositionRepo
}

// NewRepos creates our data store
func NewRepos(db *sqlx.DB) *Repos {
return &Repos{event.NewStore(db), signup.NewStore(db), position.NewStore(db)}
return &Repos{crew.NewStore(db), event.NewStore(db), signup.NewStore(db), position.NewStore(db)}
}
125 changes: 125 additions & 0 deletions controllers/v1/clapper/crew.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package clapper

import (
"database/sql"
"errors"
"fmt"
"net/http"
"strconv"

"github.com/labstack/echo/v4"
"github.com/ystv/web-api/controllers/v1/people"
)

// SetCrew handles setting the user ID for a crew object,
// essentially just signing a person up to the position.
//
// @Summary Set crew user by user token
// @Description Uses JWT to set who is doing the crew position
// @ID set-crew-user-token
// @Tags clapper, crews
// @Param crewid path int true "Crew ID"
// @Success 200
// @Router /v1/internal/clapper/crews/{crewid} [put]
func (r *Repos) SetCrew(c echo.Context) error {
p, err := people.GetToken(c)
if err != nil {
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)
}

// ResetCrew handles setting the crew position back to empty for
// when a user changes their mind.
//
// @Summary Set crew user by user token
// @Description Uses JWT to set who is doing the crew position to empty
// @ID delete-crew-user-token
// @Tags clapper, crews
// @Param crewid path int true "Crew ID"
// @Success 200
// @Router /v1/internal/clapper/crews/{crewid} [delete]
func (r *Repos) ResetCrew(c echo.Context) error {
_, err := people.GetToken(c)
if err != nil {
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 {
if errors.Is(err, sql.ErrNoRows) {
return echo.NewHTTPError(http.StatusBadRequest, "Crew not found")
}
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)
}

// NewCrew handles creating a new crew, this being a single person
//
// @Summary NewCrew
// @Description Creates a new crew object, that being a single person.
// @ID new-crew
// @Tags clapper, crews
// @Accept json
// @Param eventid path int true "Event ID"
// @Param signupid path int true "Signup ID"
// @Param crewid path int true "Position ID"
// @Success 200
// @Router /v1/internal/clapper/event/{eventid}/{signupid}/{positionid} [post]
func (r *Repos) NewCrew(c echo.Context) error {
signupID, err := strconv.Atoi(c.Param("signupid"))
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)
return c.NoContent(http.StatusOK)
}

// DeleteCrew handles deleting crew position
//
// @Summary Delete crew
// @Description deletes a crew position by ID.
// @ID delete-signup
// @Tags clapper, signups
// @Param signupid path int true "Event ID"
// @Param signupid path int true "Signup ID"
// @Param signupid path int true "Crew ID"
// @Success 200
// @Router /v1/internal/clapper/{eventid}/{signupid}/{crewid} [delete]
func (r *Repos) DeleteCrew(c echo.Context) error {
signupID, err := strconv.Atoi(c.Param("crewid"))
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)
}
14 changes: 7 additions & 7 deletions controllers/v1/clapper/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import (
"github.com/ystv/web-api/services/clapper"
)

// MonthList returns all events for a month.
// ListMonth returns all events for a month.
// @Summary List events by month
// @Description Lists events by month. The signup section will be null.
// @ID get-events-month
// @Tags events
// @Tags clapper, events
// @Produce json
// @Param year path int true "year"
// @Param month path int true "month"
// @Success 200 {array} clapper.Event
// @Router /v1/internal/clapper/calendar/{year}/{month} [get]
func (r *Repos) MonthList(c echo.Context) error {
func (r *Repos) ListMonth(c echo.Context) error {
year, err := strconv.Atoi(c.Param("year"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Year incorrect, format /yyyy/mm")
Expand All @@ -40,23 +40,23 @@ func (r *Repos) MonthList(c echo.Context) error {
return c.JSON(http.StatusOK, e)
}

// EventGet handles getting all signups and roles for a given event
// GetEvent handles getting all signups and roles for a given event
// @Summary Get event by ID
// @Description Get a event including signup-sheets and roles.
// @ID get-event
// @Tags events
// @Tags clapper, events
// @Produce json
// @Param eventid path int true "Event ID"
// @Success 200 {object} clapper.Event
// @Router /v1/internal/clapper/event/{eventid} [get]
func (r *Repos) EventGet(c echo.Context) error {
func (r *Repos) GetEvent(c echo.Context) error {
id, err := strconv.Atoi(c.Param("eventid"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid event ID")
}
e, err := r.event.Get(c.Request().Context(), id)
if err != nil {
err = fmt.Errorf("EventGet failed: %w", err)
err = fmt.Errorf("GetEvent failed: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
return c.JSON(http.StatusOK, e)
Expand Down
22 changes: 11 additions & 11 deletions controllers/v1/clapper/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import (
"github.com/ystv/web-api/services/clapper"
)

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

// PositionNew handles creating a new position
// NewPosition handles creating a new position
// @Summary New position
// @Description creates a new position.
// @ID new-position
Expand All @@ -36,22 +36,22 @@ func (r *Repos) PositionList(c echo.Context) error {
// @Param event body clapper.Position true "Position object"
// @Success 201 body int "Position ID"
// @Router /v1/internal/clapper/positions [post]
func (r *Repos) PositionNew(c echo.Context) error {
func (r *Repos) NewPosition(c echo.Context) error {
p := clapper.Position{}
err := c.Bind(&p)
if err != nil {
err = fmt.Errorf("PositionNew: failed to bind to request json: %w", err)
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("PositionNew: failed to insert position: %w", err)
err = fmt.Errorf("NewPosition: failed to insert position: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
return c.JSON(http.StatusCreated, positionID)
}

// PositionUpdate updates an existing position
// UpdatePosition updates an existing position
// @Summary Update position
// @Description updates a position.
// @ID update-position
Expand All @@ -60,19 +60,19 @@ func (r *Repos) PositionNew(c echo.Context) error {
// @Param quote body clapper.Position true "Position object"
// @Success 200
// @Router /v1/internal/clapper/positions [put]
func (r *Repos) PositionUpdate(c echo.Context) error {
func (r *Repos) UpdatePosition(c echo.Context) error {
p := clapper.Position{}
err := c.Bind(&p)
if err != nil {
err = fmt.Errorf("PositionUpdate: failed to bind to request json: %w", err)
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) {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
err = fmt.Errorf("PositionUpdate failed: %w", err)
err = fmt.Errorf("UpdatePosition failed: %w", err)
return c.JSON(http.StatusInternalServerError, err)
}
return c.NoContent(http.StatusOK)
Expand Down
80 changes: 74 additions & 6 deletions controllers/v1/clapper/signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,28 @@ import (
"github.com/ystv/web-api/services/clapper"
)

// SignupNew handles a creating a signup sheet
func (r *Repos) SignupNew(c echo.Context) error {
// NewSignup handles a creating a signup sheet
//
// @Summary New signup sheet
// @Description Creates a new signup sheet, this is the sub part of an event
// @Description containing the list of crew, with a little metadata on top.
// @ID new-signup
// @Tags clapper, signups
// @Accept json
// @Param event body clapper.Signup true "Signup object"
// @Success 201 body int "Event ID"
// @Router /v1/internal/clapper/event/{eventid}/signup [post]
func (r *Repos) NewSignup(c echo.Context) error {
// Validate event ID
eventID, err := strconv.Atoi(c.Param("eventID"))
eventID, err := strconv.Atoi(c.Param("signupid"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Bad event ID")
return echo.NewHTTPError(http.StatusBadRequest, "Bad signup ID")
}
// Bind request json to signup
s := clapper.Signup{}
err = c.Bind(&s)
if err != nil {
err = fmt.Errorf("SignupNew: failed to bind to request json: %w", err)
err = fmt.Errorf("NewSignup: failed to bind to request json: %w", err)
return echo.NewHTTPError(http.StatusBadRequest, err)
}

Expand All @@ -39,8 +49,66 @@ func (r *Repos) SignupNew(c echo.Context) error {
// Insert new signup sheet
signupID, err := r.signup.New(c.Request().Context(), e.EventID, s)
if err != nil {
err = fmt.Errorf("SignupNew: failed to insert new signup: %w", err)
err = fmt.Errorf("NewSignup: failed to insert new signup: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
return c.JSON(http.StatusCreated, signupID)
}

// UpdateSignup updates an existing signup
//
// @Summary Update signup
// @Description updates a signup sheet, to the body.
// @ID update-signup
// @Tags clapper, signups
// @Param eventid path int true "Event ID"
// @Param signupid path int true "Signup ID"
// @Accept json
// @Param quote body clapper.Signup true "Signup object"
// @Success 200
// @Router /v1/internal/clapper/event/{eventid}/{signupid} [put]
func (r *Repos) UpdateSignup(c echo.Context) error {
s := clapper.Signup{}
err := c.Bind(&s)
if err != nil {
err = fmt.Errorf("UpdateSignup: failed to bind to request json: %w", err)
return echo.NewHTTPError(http.StatusBadRequest, err)
}
signupID, err := strconv.Atoi(c.Param("signupid"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid signup ID")
}
s.SignupID = signupID
err = r.signup.Update(c.Request().Context(), s)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
err = fmt.Errorf("UpdateSignup failed: %w", err)
return c.JSON(http.StatusInternalServerError, err)
}
return c.NoContent(http.StatusOK)
}

// DeleteSignup handles deleting signup
//
// @Summary Delete signup
// @Description deletes a signup by ID.
// @ID delete-signup
// @Tags clapper, signups
// @Param signupid path int true "Event ID"
// @Param signupid path int true "Signup ID"
// @Success 200
// @Router /v1/internal/clapper/{eventid}/{signupid} [delete]
func (r *Repos) DeleteSignup(c echo.Context) error {
signupID, err := strconv.Atoi(c.Param("signupid"))
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid signup ID")
}
err = r.signup.Delete(c.Request().Context(), signupID)
if err != nil {
err = fmt.Errorf("DeleteSignup failed: %w", err)
return c.JSON(http.StatusInternalServerError, err)
}
return c.NoContent(http.StatusOK)
}
2 changes: 1 addition & 1 deletion controllers/v1/misc/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (r *Repos) NewQuote(c echo.Context) error {
}
claims, err := people.GetToken(c)
if err != nil {
err = fmt.Errorf("VideoNew failed to get user ID: %w", err)
err = fmt.Errorf("NewQupte failed to get user ID: %w", err)
return echo.NewHTTPError(http.StatusInternalServerError, err)
}
q.CreatedBy = claims.UserID
Expand Down
Loading

0 comments on commit 1a92cef

Please sign in to comment.