-
Notifications
You must be signed in to change notification settings - Fork 1
managers: added managers entity && chore #361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bogdanserdinov
wants to merge
15
commits into
develop
Choose a base branch
from
bs/managers-for-clubs
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
11d9e44
added manager entity + db + endpoint
bogdanserdinov 25bdec1
deleted output avatars
bogdanserdinov 33b4655
bugs fixed
bogdanserdinov d6f5c87
comment deleted
bogdanserdinov 715c905
linter remarks fixed
bogdanserdinov 5b4c047
conflicts resolved
bogdanserdinov 8e6be5f
list by user id refactored
bogdanserdinov 4baec94
file goimported
bogdanserdinov 245c498
chore fixed, added blocker to add/delete squad card
bogdanserdinov e48dc3f
added close chore method + output images deleted
bogdanserdinov 51213b1
linter remarks fixed
bogdanserdinov f0b6105
ownership statuses fixed
bogdanserdinov 9d3bf35
conflicts resolved
bogdanserdinov c08167b
added block to add card for owner
bogdanserdinov 8838ab4
deleted user id input from html template
bogdanserdinov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import ( | |
|
|
||
| "ultimatedivision/cards" | ||
| "ultimatedivision/divisions" | ||
| "ultimatedivision/managers" | ||
| "ultimatedivision/users" | ||
| ) | ||
|
|
||
|
|
@@ -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, | ||
| } | ||
| } | ||
|
|
@@ -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) | ||
|
|
@@ -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") | ||
| } | ||
| } | ||
|
|
||
| squadCards, err := service.clubs.ListSquadCards(ctx, squadID) | ||
| if err != nil { | ||
| return ErrClubs.Wrap(err) | ||
|
|
@@ -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)) | ||
| } | ||
|
|
||
|
|
@@ -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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.