Skip to content

Commit

Permalink
Done handlers for club clearance
Browse files Browse the repository at this point in the history
  • Loading branch information
Impervguin committed Sep 5, 2024
1 parent 61c787e commit e76867c
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 9 deletions.
75 changes: 75 additions & 0 deletions internal/app/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,3 +337,78 @@ func (s *ClubService) GetClearancePost(ctx context.Context, resp *responses.Chec
}
return &responses.GetClearance{Access: false, Comment: "only admins"}, nil
}

func (s *ClubService) GetClearanceUpdate(ctx context.Context, req *responses.CheckResponse, clubID int) (*responses.GetClearance, error) {
if req.IsAdmin {
return &responses.GetClearance{Access: true, Comment: ""}, nil
} else {
clubOrgs, err := s.storage.GetClubOrgs(ctx, clubID)
if err != nil {
return &responses.GetClearance{Access: false, Comment: "error"}, fmt.Errorf("can't storage.GetClubOrgs: %w", err)
}
for _, org := range clubOrgs {
if org.ID == req.MemberID {
return &responses.GetClearance{Access: true, Comment: ""}, nil
}
}
}
return &responses.GetClearance{Access: false, Comment: "only admins or club orgs"}, nil
}

func (s *ClubService) GetClearanceDelete(ctx context.Context, resp *responses.CheckResponse) (*responses.GetClearance, error) {
if resp.IsAdmin {
return &responses.GetClearance{Access: true, Comment: ""}, nil
}
return &responses.GetClearance{Access: false, Comment: "only admins"}, nil
}

func (s *ClubService) GetClearanceMediaPost(ctx context.Context, req *responses.CheckResponse, clubID int) (*responses.GetClearance, error) {
if req.IsAdmin {
return &responses.GetClearance{Access: true, Comment: ""}, nil
} else {
clubOrgs, err := s.storage.GetClubOrgs(ctx, clubID)
if err != nil {
return &responses.GetClearance{Access: false, Comment: "error"}, fmt.Errorf("can't storage.GetClubOrgs: %w", err)
}
for _, org := range clubOrgs {
if org.ID == req.MemberID {
return &responses.GetClearance{Access: true, Comment: ""}, nil
}
}
}
return &responses.GetClearance{Access: false, Comment: "only admins or club orgs"}, nil
}

func (s *ClubService) GetClearanceMediaUpdate(ctx context.Context, req *responses.CheckResponse, clubID int) (*responses.GetClearance, error) {
if req.IsAdmin {
return &responses.GetClearance{Access: true, Comment: ""}, nil
} else {
clubOrgs, err := s.storage.GetClubOrgs(ctx, clubID)
if err != nil {
return &responses.GetClearance{Access: false, Comment: "error"}, fmt.Errorf("can't storage.GetClubOrgs: %w", err)
}
for _, org := range clubOrgs {
if org.ID == req.MemberID {
return &responses.GetClearance{Access: true, Comment: ""}, nil
}
}
}
return &responses.GetClearance{Access: false, Comment: "only admins or club orgs"}, nil
}

func (s *ClubService) GetClearanceMediaDelete(ctx context.Context, req *responses.CheckResponse, clubID int) (*responses.GetClearance, error) {
if req.IsAdmin {
return &responses.GetClearance{Access: true, Comment: ""}, nil
} else {
clubOrgs, err := s.storage.GetClubOrgs(ctx, clubID)
if err != nil {
return &responses.GetClearance{Access: false, Comment: "error"}, fmt.Errorf("can't storage.GetClubOrgs: %w", err)
}
for _, org := range clubOrgs {
if org.ID == req.MemberID {
return &responses.GetClearance{Access: true, Comment: ""}, nil
}
}
}
return &responses.GetClearance{Access: false, Comment: "only admins or club orgs"}, nil
}
28 changes: 28 additions & 0 deletions internal/domain/requests/get-clearance-club-update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package requests

import (
"fmt"
"net/http"
"strconv"

"github.com/go-chi/chi"
)

type GetClearanceClubUpdate struct {
ClubID int `json:"club_id"`
}

func (p *GetClearanceClubUpdate) Bind(req *http.Request) error {
id, err := strconv.Atoi(chi.URLParam(req, "club_id"))
if err != nil {
return fmt.Errorf("can't Atoi id on DeleteMember.Bind: %w", err)
}

p.ClubID = id

return p.validate()
}

func (p *GetClearanceClubUpdate) validate() error {
return nil
}
154 changes: 146 additions & 8 deletions internal/ports/clubs.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ func (h *ClubsHandler) Routes() chi.Router {
r.Get("/clearance/update/{club_id}", h.r.Wrap(h.GetClearanceUpdate))
r.Get("/media/clearance/post/{club_id}", h.r.Wrap(h.GetMediaClearancePost))
r.Get("/media/clearance/delete/{club_id}", h.r.Wrap(h.GetMediaClearanceDelete))
r.Put("/media/clearance/update/{club_id}", h.r.Wrap(h.GetMediaClearanceUpdate))
r.Get("/media/clearance/update/{club_id}", h.r.Wrap(h.GetMediaClearanceUpdate))

return r
}

func (h *ClubsHandler) GetClearancePost(w http.ResponseWriter, req *http.Request) handler.Response {
h.logger.Info("ClubsHandler: got PostClub request")
h.logger.Info("ClubsHandler: got GetClearancePost request")

access, err := getAccessToken(req)
if err != nil {
Expand All @@ -78,31 +78,169 @@ func (h *ClubsHandler) GetClearancePost(w http.ResponseWriter, req *http.Request

response, err := h.clubs.GetClearancePost(context.Background(), resp)
if err != nil {
h.logger.Warnf("can't clubs.GetClearancePost GetClearancePost: %v", err)
h.logger.Warnf("can't clubs.GetClearancePost: %v", err)
return handler.InternalServerErrorResponse()
}

return handler.OkResponse(response)
}

func (h *ClubsHandler) GetClearanceDelete(w http.ResponseWriter, req *http.Request) handler.Response {
return handler.OkResponse(nil)
h.logger.Info("ClubsHandler: got GetClearanceDelete request")

access, err := getAccessToken(req)
if err != nil {
h.logger.Warnf("can't get access token: %v", err)
return handler.UnauthorizedResponse()
}

resp, err := h.guard.Check(context.Background(), &requests.CheckRequest{AccessToken: access})
if err != nil || !resp.Valid {
h.logger.Warnf("Unauthorized request: %v", err)
return handler.UnauthorizedResponse()
}

h.logger.Infof("ClubsHandler: GetClearanceDelete Authenticated: %v", resp.MemberID)

response, err := h.clubs.GetClearanceDelete(context.Background(), resp)
if err != nil {
h.logger.Warnf("can't clubs.GetClearanceDelete: %v", err)
return handler.InternalServerErrorResponse()
}

return handler.OkResponse(response)
}

func (h *ClubsHandler) GetClearanceUpdate(w http.ResponseWriter, req *http.Request) handler.Response {
return handler.OkResponse(nil)
h.logger.Info("ClubsHandler: got GetClearanceUpdate request")

access, err := getAccessToken(req)
if err != nil {
h.logger.Warnf("can't get access token: %v", err)
return handler.UnauthorizedResponse()
}

resp, err := h.guard.Check(context.Background(), &requests.CheckRequest{AccessToken: access})
if err != nil || !resp.Valid {
h.logger.Warnf("Unauthorized request: %v", err)
return handler.UnauthorizedResponse()
}

h.logger.Infof("ClubsHandler: GetClearanceUpdate Authenticated: %v", resp.MemberID)

parsed_req := &requests.GetClearanceClubUpdate{}
err = parsed_req.Bind(req)
if err != nil {
h.logger.Warnf("can't parse request: %v", err)
return handler.BadRequestResponse()
}

response, err := h.clubs.GetClearanceUpdate(context.Background(), resp, parsed_req.ClubID)
if err != nil {
h.logger.Warnf("can't clubs.GetClearanceUpdate: %v", err)
return handler.InternalServerErrorResponse()
}

return handler.OkResponse(response)
}

func (h *ClubsHandler) GetMediaClearancePost(w http.ResponseWriter, req *http.Request) handler.Response {
return handler.OkResponse(nil)
h.logger.Info("ClubsHandler: got GetMediaClearancePost request")

access, err := getAccessToken(req)
if err != nil {
h.logger.Warnf("can't get access token: %v", err)
return handler.UnauthorizedResponse()
}

resp, err := h.guard.Check(context.Background(), &requests.CheckRequest{AccessToken: access})
if err != nil || !resp.Valid {
h.logger.Warnf("Unauthorized request: %v", err)
return handler.UnauthorizedResponse()
}

h.logger.Infof("ClubsHandler: GetMediaClearancePost Authenticated: %v", resp.MemberID)

parsed_req := &requests.GetClearanceClubUpdate{}
err = parsed_req.Bind(req)
if err != nil {
h.logger.Warnf("can't parse request: %v", err)
return handler.BadRequestResponse()
}

response, err := h.clubs.GetClearanceMediaPost(context.Background(), resp, parsed_req.ClubID)
if err != nil {
h.logger.Warnf("can't clubs.GetMediaClearancePost: %v", err)
return handler.InternalServerErrorResponse()
}

return handler.OkResponse(response)
}

func (h *ClubsHandler) GetMediaClearanceDelete(w http.ResponseWriter, req *http.Request) handler.Response {
return handler.OkResponse(nil)
h.logger.Info("ClubsHandler: got GetMediaClearanceDelete request")

access, err := getAccessToken(req)
if err != nil {
h.logger.Warnf("can't get access token: %v", err)
return handler.UnauthorizedResponse()
}

resp, err := h.guard.Check(context.Background(), &requests.CheckRequest{AccessToken: access})
if err != nil || !resp.Valid {
h.logger.Warnf("Unauthorized request: %v", err)
return handler.UnauthorizedResponse()
}

h.logger.Infof("ClubsHandler: GetMediaClearanceDelete Authenticated: %v", resp.MemberID)

parsed_req := &requests.GetClearanceClubUpdate{}
err = parsed_req.Bind(req)
if err != nil {
h.logger.Warnf("can't parse request: %v", err)
return handler.BadRequestResponse()
}

response, err := h.clubs.GetClearanceMediaDelete(context.Background(), resp, parsed_req.ClubID)
if err != nil {
h.logger.Warnf("can't clubs.GetMediaClearanceDelete: %v", err)
return handler.InternalServerErrorResponse()
}

return handler.OkResponse(response)
}

func (h *ClubsHandler) GetMediaClearanceUpdate(w http.ResponseWriter, req *http.Request) handler.Response {
return handler.OkResponse(nil)
h.logger.Info("ClubsHandler: got GetMediaClearanceUpdate request")

access, err := getAccessToken(req)
if err != nil {
h.logger.Warnf("can't get access token: %v", err)
return handler.UnauthorizedResponse()
}

resp, err := h.guard.Check(context.Background(), &requests.CheckRequest{AccessToken: access})
if err != nil || !resp.Valid {
h.logger.Warnf("Unauthorized request: %v", err)
return handler.UnauthorizedResponse()
}

h.logger.Infof("ClubsHandler: GetMediaClearanceUpdate Authenticated: %v", resp.MemberID)

parsed_req := &requests.GetClearanceClubUpdate{}
err = parsed_req.Bind(req)
if err != nil {
h.logger.Warnf("can't parse request: %v", err)
return handler.BadRequestResponse()
}

response, err := h.clubs.GetClearanceMediaUpdate(context.Background(), resp, parsed_req.ClubID)
if err != nil {
h.logger.Warnf("can't clubs.GetMediaClearanceUpdate: %v", err)
return handler.InternalServerErrorResponse()
}

return handler.OkResponse(response)
}

// GetAllClubs
Expand Down
2 changes: 1 addition & 1 deletion migrations/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
sleep 5
goose postgres "host=$DB_HOST user=$DB_USER password=$DB_PASSWORD dbname=$DB_NAME" reset
# goose postgres "host=$DB_HOST user=$DB_USER password=$DB_PASSWORD dbname=$DB_NAME" down
goose postgres "host=$DB_HOST user=$DB_USER password=$DB_PASSWORD dbname=$DB_NAME" up

0 comments on commit e76867c

Please sign in to comment.