Skip to content
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions admin/adminserver/controllers/clubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ func (controller *Clubs) Add(w http.ResponseWriter, r *http.Request) {
return
}

userID, err := uuid.Parse(params["userId"])
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

switch r.Method {
case http.MethodGet:
if err = controller.templates.AddCard.Execute(w, squadID); err != nil {
Expand Down Expand Up @@ -317,7 +323,7 @@ func (controller *Clubs) Add(w http.ResponseWriter, r *http.Request) {
CardID: cardID,
}

if err = controller.clubs.AddSquadCard(ctx, squadID, squadCard); err != nil {
if err = controller.clubs.AddSquadCard(ctx, userID, squadID, squadCard); err != nil {
controller.log.Error("could not add card to the squad", ErrClubs.Wrap(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down Expand Up @@ -386,6 +392,12 @@ func (controller *Clubs) DeleteCard(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
params := mux.Vars(r)

userID, err := uuid.Parse(params["userId"])
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

squadID, err := uuid.Parse(params["squadId"])
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
Expand All @@ -398,7 +410,10 @@ func (controller *Clubs) DeleteCard(w http.ResponseWriter, r *http.Request) {
return
}

if err = controller.clubs.Delete(ctx, squadID, cardID); err != nil {
if err = controller.clubs.Delete(ctx, userID, squadID, cardID); err != nil {
if clubs.ForbiddenAction.Has(err) {
http.Error(w, err.Error(), http.StatusForbidden)
}
controller.log.Error("could not delete card", ErrClubs.Wrap(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
Expand Down
2 changes: 1 addition & 1 deletion admin/adminserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"net"
"net/http"
"path/filepath"
"ultimatedivision/seasons"

"github.com/gorilla/mux"
"github.com/zeebo/errs"
Expand All @@ -30,6 +29,7 @@ import (
"ultimatedivision/marketplace"
"ultimatedivision/pkg/auth"
"ultimatedivision/queue"
"ultimatedivision/seasons"
"ultimatedivision/users"
)

Expand Down
14 changes: 14 additions & 0 deletions clubs/clubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var ErrNoSquad = errs.Class("squad does not exist")
// ErrNoSquadCard indicated that squad card does not exist.
var ErrNoSquadCard = errs.Class("squad card does not exist")

// ForbiddenAction indicated that user is forbidden to make action with club.
var ForbiddenAction = errs.Class("forbidden action")

// DB is exposing access to clubs db.
//
// architecture: DB
Expand Down Expand Up @@ -83,6 +86,7 @@ type Club struct {
Name string `json:"name"`
Status Status `json:"status"`
DivisionID uuid.UUID `json:"divisionId"`
Ownership Ownership `json:"ownership"`
CreatedAt time.Time `json:"createdAt"`
}

Expand All @@ -106,6 +110,16 @@ type SquadCard struct {
// SquadSize defines number of cards in the full squad.
const SquadSize int = 11

// Ownership defines a list of possible ownerships.
type Ownership int

const (
// OwnershipManager defines manager type of ownership.
OwnershipManager Ownership = 0
// OwnershipOwner defines owner type of ownership.
OwnershipOwner Ownership = 1
)

// Formation defines a list of possible formations.
type Formation int

Expand Down
4 changes: 4 additions & 0 deletions clubs/clubs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestTeam(t *testing.T) {
Name: testUser.NickName,
Status: clubs.StatusActive,
DivisionID: division1.ID,
Ownership: clubs.OwnershipOwner,
CreatedAt: time.Now().UTC(),
}

Expand All @@ -55,6 +56,7 @@ func TestTeam(t *testing.T) {
Name: testUser.NickName,
Status: clubs.StatusInactive,
DivisionID: division1.ID,
Ownership: clubs.OwnershipOwner,
CreatedAt: time.Now().UTC(),
}

Expand All @@ -63,6 +65,7 @@ func TestTeam(t *testing.T) {
OwnerID: testUser.ID,
Name: testUser.NickName,
Status: clubs.StatusInactive,
Ownership: clubs.OwnershipOwner,
CreatedAt: time.Now().UTC(),
}

Expand All @@ -71,6 +74,7 @@ func TestTeam(t *testing.T) {
OwnerID: testUser.ID,
Name: testUser.NickName,
Status: clubs.StatusActive,
Ownership: clubs.OwnershipOwner,
CreatedAt: time.Now().UTC(),
}

Expand Down
61 changes: 56 additions & 5 deletions clubs/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"ultimatedivision/cards"
"ultimatedivision/divisions"
"ultimatedivision/managers"
"ultimatedivision/users"
)

Expand All @@ -26,15 +27,17 @@ type Service struct {
clubs DB
users *users.Service
cards *cards.Service
managers *managers.Service
divisions divisions.DB
}

// NewService is a constructor for clubs service.
func NewService(clubs DB, users *users.Service, cards *cards.Service, divisions divisions.DB) *Service {
func NewService(clubs DB, users *users.Service, cards *cards.Service, managers *managers.Service, divisions divisions.DB) *Service {
return &Service{
clubs: clubs,
users: users,
cards: cards,
managers: managers,
divisions: divisions,
}
}
Expand All @@ -57,6 +60,7 @@ func (service *Service) Create(ctx context.Context, userID uuid.UUID) (uuid.UUID
Name: nickname,
CreatedAt: time.Now().UTC(),
DivisionID: division.ID,
Ownership: OwnershipOwner,
}

allClubs, err := service.ListByUserID(ctx, userID)
Expand Down Expand Up @@ -96,7 +100,23 @@ func (service *Service) CreateSquad(ctx context.Context, clubID uuid.UUID) (uuid
}

// AddSquadCard adds card to the squad.
func (service *Service) AddSquadCard(ctx context.Context, squadID uuid.UUID, newSquadCard SquadCard) error {
func (service *Service) AddSquadCard(ctx context.Context, userID, squadID uuid.UUID, newSquadCard SquadCard) error {
squad, err := service.GetSquad(ctx, squadID)
if err != nil {
return ErrClubs.Wrap(err)
}

managedClubs, err := service.managers.ListByUserID(ctx, userID)
if err != nil {
return ErrClubs.Wrap(err)
}

for _, club := range managedClubs {
if club.ClubID == squad.ClubID {
return ForbiddenAction.New("invalid action")
Comment thread
bogdanserdinov marked this conversation as resolved.
Outdated
}
}

squadCards, err := service.clubs.ListSquadCards(ctx, squadID)
if err != nil {
return ErrClubs.Wrap(err)
Expand Down Expand Up @@ -130,7 +150,23 @@ func (service *Service) AddSquadCard(ctx context.Context, squadID uuid.UUID, new
}

// Delete deletes card from squad.
func (service *Service) Delete(ctx context.Context, squadID, cardID uuid.UUID) error {
func (service *Service) Delete(ctx context.Context, userID, squadID, cardID uuid.UUID) error {
squad, err := service.GetSquad(ctx, squadID)
if err != nil {
return ErrClubs.Wrap(err)
}

managedClubs, err := service.managers.ListByUserID(ctx, userID)
if err != nil {
return ErrClubs.Wrap(err)
}

for _, club := range managedClubs {
if club.ClubID == squad.ClubID {
return ForbiddenAction.New("invalid action")
}
}

return ErrClubs.Wrap(service.clubs.DeleteSquadCard(ctx, squadID, cardID))
}

Expand Down Expand Up @@ -247,8 +283,23 @@ func (service *Service) ListSquadCards(ctx context.Context, squadID uuid.UUID) (

// ListByUserID returns user's clubs.
func (service *Service) ListByUserID(ctx context.Context, userID uuid.UUID) ([]Club, error) {
club, err := service.clubs.ListByUserID(ctx, userID)
return club, ErrClubs.Wrap(err)
userClubs, err := service.clubs.ListByUserID(ctx, userID)
if err != nil {
return userClubs, ErrClubs.Wrap(err)
}

managedClubs, err := service.managers.ListByUserID(ctx, userID)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe one db method needs to be done for this?

for _, managedClub := range managedClubs {
club, err := service.Get(ctx, managedClub.ClubID)
if err != nil {
return userClubs, ErrClubs.Wrap(err)
}
club.Ownership = OwnershipManager

userClubs = append(userClubs, club)
}

return userClubs, ErrClubs.Wrap(err)
}

// Get returns club.
Expand Down
28 changes: 24 additions & 4 deletions console/consoleserver/controllers/clubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ func (controller *Clubs) Add(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
var squadCard clubs.SquadCard

claims, err := auth.GetClaims(ctx)
if err != nil {
controller.serveError(w, http.StatusUnauthorized, ErrClubs.Wrap(err))
return
}

squadID, err := uuid.Parse(params["squadId"])
if err != nil {
controller.serveError(w, http.StatusBadRequest, ErrClubs.Wrap(err))
Expand All @@ -321,8 +327,6 @@ func (controller *Clubs) Add(w http.ResponseWriter, r *http.Request) {
return
}

squadCard.CardID = cardID

if err = json.NewDecoder(r.Body).Decode(&squadCard); err != nil {
controller.serveError(w, http.StatusBadRequest, ErrClubs.Wrap(err))
}
Expand All @@ -334,7 +338,12 @@ func (controller *Clubs) Add(w http.ResponseWriter, r *http.Request) {
return
}

if err = controller.clubs.AddSquadCard(ctx, squadID, squadCard); err != nil {
if err = controller.clubs.AddSquadCard(ctx, claims.UserID, squadID, squadCard); err != nil {
if clubs.ForbiddenAction.Has(err) {
controller.serveError(w, http.StatusForbidden, ErrClubs.Wrap(err))
return
}

controller.log.Error("could not add card to the squad", ErrClubs.Wrap(err))
controller.serveError(w, http.StatusInternalServerError, ErrClubs.Wrap(err))
return
Expand All @@ -347,6 +356,12 @@ func (controller *Clubs) Delete(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
params := mux.Vars(r)

claims, err := auth.GetClaims(ctx)
if err != nil {
controller.serveError(w, http.StatusUnauthorized, ErrClubs.Wrap(err))
return
}

cardID, err := uuid.Parse(params["cardId"])
if err != nil {
controller.serveError(w, http.StatusBadRequest, ErrClubs.Wrap(err))
Expand All @@ -359,14 +374,19 @@ func (controller *Clubs) Delete(w http.ResponseWriter, r *http.Request) {
return
}

if err = controller.clubs.Delete(ctx, squadID, cardID); err != nil {
if err = controller.clubs.Delete(ctx, claims.UserID, squadID, cardID); err != nil {
controller.log.Error("could not delete card from the squad", ErrClubs.Wrap(err))

if clubs.ErrNoSquadCard.Has(err) {
controller.serveError(w, http.StatusNotFound, ErrClubs.Wrap(err))
return
}

if clubs.ForbiddenAction.Has(err) {
controller.serveError(w, http.StatusForbidden, ErrClubs.Wrap(err))
return
}

controller.serveError(w, http.StatusInternalServerError, ErrClubs.Wrap(err))
return
}
Expand Down
80 changes: 80 additions & 0 deletions console/consoleserver/controllers/managers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (C) 2021 Creditor Corp. Group.
// See LICENSE for copying information.

package controllers

import (
"encoding/json"
"net/http"

"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/zeebo/errs"

"ultimatedivision/internal/logger"
"ultimatedivision/managers"
)

var (
// ErrManagers is an internal error type for managers controller.
ErrManagers = errs.Class("managers controller error")
)

// Managers is a mvc controller that handles all managers related views.
type Managers struct {
log logger.Logger

managers *managers.Service
}

// NewManagers is a constructor for managers controller.
func NewManagers(log logger.Logger, managers *managers.Service) *Managers {
managersController := &Managers{
log: log,
managers: managers,
}

return managersController
}

// Create is ann endpoint that creates manager for club.
func (controller *Managers) Create(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
ctx := r.Context()
params := mux.Vars(r)

clubID, err := uuid.Parse(params["clubId"])
if err != nil {
controller.serveError(w, http.StatusBadRequest, ErrManagers.Wrap(err))
return
}

var manager managers.Manager

if err = json.NewDecoder(r.Body).Decode(&manager); err != nil {
controller.serveError(w, http.StatusBadRequest, ErrManagers.Wrap(err))
return
}

err = controller.managers.Create(ctx, manager.EndedAt, manager.UserID, clubID)
if err != nil {
controller.log.Error("could not create manager", ErrManagers.Wrap(err))
controller.serveError(w, http.StatusInternalServerError, ErrManagers.Wrap(err))
return
}
}

// serveError replies to the request with specific code and error message.
func (controller *Managers) serveError(w http.ResponseWriter, status int, err error) {
w.WriteHeader(status)

var response struct {
Error string `json:"error"`
}

response.Error = err.Error()

if err = json.NewEncoder(w).Encode(response); err != nil {
controller.log.Error("failed to write json error response", ErrManagers.Wrap(err))
}
}
Loading