Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/GenerateNU/sac
Browse files Browse the repository at this point in the history
  • Loading branch information
Alder Whiteford authored and Alder Whiteford committed May 6, 2024
2 parents 2ede462 + 5188b8a commit 45ae090
Show file tree
Hide file tree
Showing 24 changed files with 233 additions and 230 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ on:
- backend/**
- config/**
- .github/workflows/backend.yml
pull_request:
types: [opened]
paths:
- backend/**
- config/**
- .github/workflows/backend.yml

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/backend_codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ on:
paths:
- "backend/**"
- ".github/workflows/backend_codeql.yml"
pull_request:
types: [opened]
paths:
- "backend/**"
- ".github/workflows/backend_codeql.yml"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down
6 changes: 0 additions & 6 deletions .github/workflows/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,6 @@ on:
- cli/**
- config/**
- .github/workflows/cli.yml
pull_request:
types: [opened]
paths:
- cli/**
- config/**
- .github/workflows/cli.yml

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/cli_codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ on:
paths:
- "cli/**"
- ".github/workflows/cli_codeql.yml"
pull_request:
types: [opened]
paths:
- "cli/**"
- ".github/workflows/cli_codeql.yml"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/dashboard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ on:
paths:
- frontend/dashboard/**
- .github/workflows/dashboard.yml
pull_request:
types: opened
paths:
- frontend/dashboard/**
- .github/workflows/dashboard.yml

jobs:
format:
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/dashboard_codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ on:
paths:
- "frontend/dashboard/**"
- ".github/workflows/dashboard_codeql.yml"
pull_request:
types: [opened]
paths:
- "frontend/dashboard/**"
- ".github/workflows/dashboard_codeql.yml"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down
7 changes: 1 addition & 6 deletions .github/workflows/frontend_lib.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ on:
paths:
- frontend/lib/**
- .github/workflows/frontend_lib.yml
pull_request:
types: opened
paths:
- frontend/lib/**
- .github/workflows/frontend_lib.yml

jobs:
format:
Expand Down Expand Up @@ -85,7 +80,7 @@ jobs:
id: get_new_version
run: |
echo "new_version=$(jq -r '.version' frontend/lib/package.json)" >> $GITHUB_OUTPUT
- name: Checkout main
uses: actions/checkout@v3
with:
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/frontend_lib_codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ on:
paths:
- "frontend/lib/**"
- ".github/workflows/frontend_lib_codeql.yml"
pull_request:
types: [opened]
paths:
- "frontend/lib/**"
- ".github/workflows/frontend_lib_codeql.yml"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/mobile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ on:
paths:
- frontend/mobile/**
- .github/workflows/mobile.yml
pull_request:
types: opened
paths:
- frontend/mobile/**
- .github/workflows/mobile.yml

jobs:
format:
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/mobile_codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ on:
paths:
- "frontend/mobile/**"
- ".github/workflows/mobile_codeql.yml"
pull_request:
types: [opened]
paths:
- "frontend/mobile/**"
- ".github/workflows/mobile_codeql.yml"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/web.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ on:
paths:
- frontend/web/**
- .github/workflows/web.yml
pull_request:
types: opened
paths:
- frontend/web/**
- .github/workflows/web.yml

jobs:
format:
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/web_codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ on:
paths:
- "frontend/web/**"
- ".github/workflows/web_codeql.yml"
pull_request:
types: [opened]
paths:
- "frontend/web/**"
- ".github/workflows/web_codeql.yml"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down
49 changes: 49 additions & 0 deletions backend/entities/clubs/member/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,52 @@ func (cm *ClubMemberController) GetClubMembers(c *fiber.Ctx) error {

return c.Status(fiber.StatusOK).JSON(followers)
}

// CreateClubMember godoc
//
// @Summary Create a new member for a club
// @Description Creates a new member associated with a club
// @ID create-member-for-club
// @Tags club-member
// @Accept json
// @Produce json
// @Param clubID path string true "Club ID"
// @Param userID path string true "User ID"
// @Success 201 {object} models.User
// @Failure 400 {object} errors.Error
// @Failure 401 {object} errors.Error
// @Failure 404 {object} errors.Error
// @Failure 500 {object} errors.Error
// @Router /clubs/{clubID}/members/ [post]
func (cm *ClubMemberController) CreateClubMember(c *fiber.Ctx) error {
err := cm.clubMemberService.CreateClubMember(c.Params("clubID"), c.Params("userID"))
if err != nil {
return err.FiberError(c)
}

return c.SendStatus(fiber.StatusCreated)
}

// DeleteClubMember godoc
//
// @Summary Delete a member from a club
// @Description Deletes a member associated with a club
// @ID delete-member-from-club
// @Tags club-member
// @Produce json
// @Param clubID path string true "Club ID"
// @Param userID path string true "User ID"
// @Success 204 {object} models.User
// @Failure 400 {object} errors.Error
// @Failure 401 {object} errors.Error
// @Failure 404 {object} errors.Error
// @Failure 500 {object} errors.Error
// @Router /clubs/{clubID}/members/ [delete]
func (cm *ClubMemberController) DeleteClubMember(c *fiber.Ctx) error {
err := cm.clubMemberService.DeleteClubMember(c.Params("clubID"), c.Params("userID"))
if err != nil {
return err.FiberError(c)
}

return c.SendStatus(fiber.StatusNoContent)
}
2 changes: 2 additions & 0 deletions backend/entities/clubs/member/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ func ClubMember(clubParams types.RouteParams) {

// api/v1/clubs/:clubID/members/*
clubMembers.Get("/", clubMemberController.GetClubMembers)
clubMembers.Post("/:userID", clubParams.AuthMiddleware.ClubAuthorizeById, clubMemberController.CreateClubMember)
clubMembers.Delete("/:userID", clubParams.AuthMiddleware.ClubAuthorizeById, clubMemberController.DeleteClubMember)
}
34 changes: 32 additions & 2 deletions backend/entities/clubs/member/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (

type ClubMemberServiceInterface interface {
GetClubMembers(clubID string, limit string, page string) ([]models.User, *errors.Error)
CreateClubMember(clubID string, userID string) *errors.Error
DeleteClubMember(clubID string, userID string) *errors.Error
}

type ClubMemberService struct {
Expand All @@ -20,7 +22,7 @@ func NewClubMemberService(params types.ServiceParams) ClubMemberServiceInterface
}

func (cms *ClubMemberService) GetClubMembers(clubID string, limit string, page string) ([]models.User, *errors.Error) {
idAsUUID, err := utilities.ValidateID(clubID)
clubIDAsUUID, err := utilities.ValidateID(clubID)
if err != nil {
return nil, &errors.FailedToValidateID
}
Expand All @@ -35,5 +37,33 @@ func (cms *ClubMemberService) GetClubMembers(clubID string, limit string, page s
return nil, &errors.FailedToValidatePage
}

return GetClubMembers(cms.DB, *idAsUUID, *limitAsInt, *pageAsInt)
return GetClubMembers(cms.DB, *clubIDAsUUID, *limitAsInt, *pageAsInt)
}

func (cms *ClubMemberService) CreateClubMember(clubID string, userID string) *errors.Error {
clubIDAsUUID, err := utilities.ValidateID(clubID)
if err != nil {
return &errors.FailedToValidateID
}

userIDAsUUID, err := utilities.ValidateID(userID)
if err != nil {
return &errors.FailedToValidateID
}

return CreateClubMember(cms.DB, *clubIDAsUUID, *userIDAsUUID)
}

func (cms *ClubMemberService) DeleteClubMember(clubID string, userID string) *errors.Error {
clubIDAsUUID, err := utilities.ValidateID(clubID)
if err != nil {
return &errors.FailedToValidateID
}

userIDAsUUID, err := utilities.ValidateID(userID)
if err != nil {
return &errors.FailedToValidateID
}

return DeleteClubMember(cms.DB, *clubIDAsUUID, *userIDAsUUID)
}
81 changes: 81 additions & 0 deletions backend/entities/clubs/member/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package member
import (
"github.com/GenerateNU/sac/backend/entities/clubs"
"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/entities/users"
"github.com/GenerateNU/sac/backend/entities/users/follower"
"github.com/GenerateNU/sac/backend/errors"

"github.com/google/uuid"
Expand All @@ -25,3 +27,82 @@ func GetClubMembers(db *gorm.DB, clubID uuid.UUID, limit int, page int) ([]model

return users, nil
}

func CreateClubMember(db *gorm.DB, clubID uuid.UUID, userID uuid.UUID) *errors.Error {
user, err := users.GetUser(db, userID)
if err != nil {
return err
}

club, err := clubs.GetClub(db, clubID)
if err != nil {
return err
}

tx := db.Begin()

var count int64
if err := tx.Model(&models.Membership{}).Where("user_id = ? AND club_id = ?", userID, clubID).Count(&count).Error; err != nil {
return &errors.FailedToGetUserMemberships
}

if count > 0 {
return nil
}

if err := tx.Model(&user).Association("Member").Append(club); err != nil {
tx.Rollback()
return &errors.FailedToUpdateUser
}

if err := follower.CreateFollowing(tx, userID, clubID); err != nil {
tx.Rollback()
return err
}

if err := tx.Model(&club).Update("num_members", gorm.Expr("num_members + 1")).Error; err != nil {
tx.Rollback()
return &errors.FailedToUpdateUser
}

if err := tx.Commit().Error; err != nil {
return &errors.FailedToUpdateUser
}

return nil
}

func DeleteClubMember(db *gorm.DB, clubID uuid.UUID, userID uuid.UUID) *errors.Error {
user, err := users.GetUser(db, userID)
if err != nil {
return err
}

club, err := clubs.GetClub(db, clubID)
if err != nil {
return err
}

tx := db.Begin()

if err := tx.Model(&user).Association("Member").Delete(club); err != nil {
tx.Rollback()
return &errors.FailedToUpdateUser
}

if err := follower.DeleteFollowing(tx, userID, clubID); err != nil {
tx.Rollback()
return err
}

if err := tx.Model(&club).Update("num_members", gorm.Expr("num_members - 1")).Error; err != nil {
tx.Rollback()
return &errors.FailedToUpdateUser
}

if err := tx.Commit().Error; err != nil {
return &errors.FailedToUpdateUser
}

return nil
}
Loading

0 comments on commit 45ae090

Please sign in to comment.