Skip to content

Commit

Permalink
Testing: Complete Membership | Delete Nonexistent Edge from 204 -> 404 (
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley authored Feb 15, 2024
1 parent f806439 commit 7d874a1
Show file tree
Hide file tree
Showing 21 changed files with 496 additions and 517 deletions.
2 changes: 1 addition & 1 deletion backend/src/controllers/user_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (um *UserMemberController) DeleteMembership(c *fiber.Ctx) error {
}

func (um *UserMemberController) GetMembership(c *fiber.Ctx) error {
followers, err := um.clubMemberService.GetMembership(c.Params("clubID"))
followers, err := um.clubMemberService.GetMembership(c.Params("userID"))
if err != nil {
return err.FiberError(c)
}
Expand Down
8 changes: 8 additions & 0 deletions backend/src/errors/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,16 @@ var (
StatusCode: fiber.StatusInternalServerError,
Message: "failed to get user memberships",
}
UserNotMemberOfClub = Error{
StatusCode: fiber.StatusNotFound,
Message: "user not member of club",
}
FailedToGetUserFollowing = Error{
StatusCode: fiber.StatusInternalServerError,
Message: "failed to get user following",
}
UserNotFollowingClub = Error{
StatusCode: fiber.StatusNotFound,
Message: "user not following club",
}
)
4 changes: 2 additions & 2 deletions backend/src/server/routes/club_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
func ClubMember(clubsIDRouter fiber.Router, clubMemberService services.ClubMemberServiceInterface) {
clubMemberController := controllers.NewClubMemberController(clubMemberService)

clubMember := clubsIDRouter.Group("/member")
clubMember := clubsIDRouter.Group("/members")

// api/v1/clubs/:clubID/member/*
// api/v1/clubs/:clubID/members/*
clubMember.Get("/", clubMemberController.GetClubMembers)
}
4 changes: 2 additions & 2 deletions backend/src/server/routes/user_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func UserMember(usersRouter fiber.Router, userMembershipService services.UserMem
userMember := usersRouter.Group("/:userID/member")

// api/v1/users/:userID/member/*
userMember.Post("/", userMemberController.CreateMembership)
userMember.Delete("/", userMemberController.DeleteMembership)
userMember.Post("/:clubID", userMemberController.CreateMembership)
userMember.Delete("/:clubID", userMemberController.DeleteMembership)
userMember.Get("/", userMemberController.GetMembership)
}
13 changes: 10 additions & 3 deletions backend/src/transactions/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func GetClubs(db *gorm.DB, limit int, offset int) ([]models.Club, *errors.Error)
func CreateClub(db *gorm.DB, userId uuid.UUID, club models.Club) (*models.Club, *errors.Error) {
user, err := GetUser(db, userId)
if err != nil {
return nil, &errors.UserNotFound
return nil, err
}

tx := db.Begin()
Expand All @@ -61,9 +61,16 @@ func CreateClub(db *gorm.DB, userId uuid.UUID, club models.Club) (*models.Club,
return &club, nil
}

func GetClub(db *gorm.DB, id uuid.UUID) (*models.Club, *errors.Error) {
func GetClub(db *gorm.DB, id uuid.UUID, preloads ...OptionalPreload) (*models.Club, *errors.Error) {
var club models.Club
if err := db.First(&club, id).Error; err != nil {

query := db

for _, preload := range preloads {
query = preload(query)
}

if err := query.First(&club, id).Error; err != nil {
if stdliberrors.Is(err, gorm.ErrRecordNotFound) {
return nil, &errors.ClubNotFound
} else {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/transactions/club_follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func GetClubFollowers(db *gorm.DB, clubID uuid.UUID, limit int, page int) ([]models.User, *errors.Error) {
club, err := GetClub(db, clubID)
if err != nil {
return nil, &errors.ClubNotFound
return nil, err
}

var users []models.User
Expand Down
4 changes: 2 additions & 2 deletions backend/src/transactions/club_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
func GetClubMembers(db *gorm.DB, clubID uuid.UUID, limit int, page int) ([]models.User, *errors.Error) {
club, err := GetClub(db, clubID)
if err != nil {
return nil, &errors.ClubNotFound
return nil, err
}

var users []models.User

if err := db.Model(&club).Association("Members").Find(&users); err != nil {
if err := db.Model(&club).Association("Member").Find(&users); err != nil {
return nil, &errors.FailedToGetClubMembers
}

Expand Down
11 changes: 4 additions & 7 deletions backend/src/transactions/club_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"gorm.io/gorm"
)

// Create tags for a club
func CreateClubTags(db *gorm.DB, id uuid.UUID, tags []models.Tag) ([]models.Tag, *errors.Error) {
user, err := GetClub(db, id)
if err != nil {
return nil, &errors.UserNotFound
return nil, err
}

if err := db.Model(&user).Association("Tag").Replace(tags); err != nil {
Expand All @@ -22,13 +21,12 @@ func CreateClubTags(db *gorm.DB, id uuid.UUID, tags []models.Tag) ([]models.Tag,
return tags, nil
}

// Get tags for a club
func GetClubTags(db *gorm.DB, id uuid.UUID) ([]models.Tag, *errors.Error) {
var tags []models.Tag

club, err := GetClub(db, id)
if err != nil {
return nil, &errors.ClubNotFound
return nil, err
}

if err := db.Model(&club).Association("Tag").Find(&tags); err != nil {
Expand All @@ -37,16 +35,15 @@ func GetClubTags(db *gorm.DB, id uuid.UUID) ([]models.Tag, *errors.Error) {
return tags, nil
}

// Delete tag for a club
func DeleteClubTag(db *gorm.DB, id uuid.UUID, tagId uuid.UUID) *errors.Error {
club, err := GetClub(db, id)
if err != nil {
return &errors.ClubNotFound
return err
}

tag, err := GetTag(db, tagId)
if err != nil {
return &errors.TagNotFound
return err
}

if err := db.Model(&club).Association("Tag").Delete(&tag); err != nil {
Expand Down
17 changes: 17 additions & 0 deletions backend/src/transactions/preloaders.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package transactions

import "gorm.io/gorm"

type OptionalPreload func(*gorm.DB) *gorm.DB

func PreloadFollwer() OptionalPreload {
return func(db *gorm.DB) *gorm.DB {
return db.Preload("Follower")
}
}

func PreloadMember() OptionalPreload {
return func(db *gorm.DB) *gorm.DB {
return db.Preload("Member")
}
}
11 changes: 9 additions & 2 deletions backend/src/transactions/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,16 @@ func GetUsers(db *gorm.DB, limit int, offset int) ([]models.User, *errors.Error)
return users, nil
}

func GetUser(db *gorm.DB, id uuid.UUID) (*models.User, *errors.Error) {
func GetUser(db *gorm.DB, id uuid.UUID, preloads ...OptionalPreload) (*models.User, *errors.Error) {
var user models.User
if err := db.Omit("password_hash").First(&user, id).Error; err != nil {

query := db

for _, preload := range preloads {
query = preload(query)
}

if err := query.Omit("password_hash").First(&user, id).Error; err != nil {
if stdliberrors.Is(err, gorm.ErrRecordNotFound) {
return nil, &errors.UserNotFound
} else {
Expand Down
30 changes: 23 additions & 7 deletions backend/src/transactions/user_follower.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package transactions

import (
"slices"

"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/google/uuid"
Expand All @@ -10,12 +12,12 @@ import (
func CreateFollowing(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error {
user, err := GetUser(db, userId)
if err != nil {
return &errors.UserNotFound
return err
}

club, err := GetClub(db, clubId)
if err != nil {
return &errors.ClubNotFound
return err
}

user.Follower = append(user.Follower, *club)
Expand All @@ -28,17 +30,30 @@ func CreateFollowing(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Er
}

func DeleteFollowing(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error {
user, err := GetUser(db, userId)
user, err := GetUser(db, userId, PreloadFollwer())
if err != nil {
return &errors.UserNotFound
return err
}
club, err := GetClub(db, clubId)

club, err := GetClub(db, clubId, PreloadFollwer())
if err != nil {
return &errors.ClubNotFound
return err
}

userFollowingClubIDs := make([]uuid.UUID, len(user.Follower))

for i, club := range user.Follower {
userFollowingClubIDs[i] = club.ID
}

if !slices.Contains(userFollowingClubIDs, club.ID) {
return &errors.UserNotFollowingClub
}

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

return nil
}

Expand All @@ -47,11 +62,12 @@ func GetClubFollowing(db *gorm.DB, userId uuid.UUID) ([]models.Club, *errors.Err

user, err := GetUser(db, userId)
if err != nil {
return nil, &errors.UserNotFound
return nil, err
}

if err := db.Model(&user).Association("Follower").Find(&clubs); err != nil {
return nil, &errors.FailedToGetUserFollowing
}

return clubs, nil
}
32 changes: 24 additions & 8 deletions backend/src/transactions/user_member.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package transactions

import (
"slices"

"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/google/uuid"
Expand All @@ -10,34 +12,48 @@ import (
func CreateMember(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error {
user, err := GetUser(db, userId)
if err != nil {
return &errors.UserNotFound
return err
}

club, err := GetClub(db, clubId)
if err != nil {
return &errors.ClubNotFound
return err
}

if err := db.Model(&user).Association("Member").Append(&club); err != nil {
user.Member = append(user.Member, *club)

if err := db.Model(&user).Association("Member").Replace(user.Member); err != nil {
return &errors.FailedToUpdateUser
}

return nil
}

func DeleteMember(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error {
user, err := GetUser(db, userId)
user, err := GetUser(db, userId, PreloadMember())
if err != nil {
return &errors.UserNotFound
return err
}

club, err := GetClub(db, clubId)
club, err := GetClub(db, clubId, PreloadMember())
if err != nil {
return &errors.ClubNotFound
return err
}

userMemberClubIDs := make([]uuid.UUID, len(user.Member))

for i, club := range user.Member {
userMemberClubIDs[i] = club.ID
}

if !slices.Contains(userMemberClubIDs, club.ID) {
return &errors.UserNotMemberOfClub
}

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

return nil
}

Expand All @@ -46,7 +62,7 @@ func GetClubMembership(db *gorm.DB, userId uuid.UUID) ([]models.Club, *errors.Er

user, err := GetUser(db, userId)
if err != nil {
return nil, &errors.UserNotFound
return nil, err
}

if err := db.Model(&user).Association("Member").Find(&clubs); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions backend/src/transactions/user_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func GetUserTags(db *gorm.DB, id uuid.UUID) ([]models.Tag, *errors.Error) {

user, err := GetUser(db, id)
if err != nil {
return nil, &errors.UserNotFound
return nil, err
}

if err := db.Model(&user).Association("Tag").Find(&tags); err != nil {
Expand All @@ -24,7 +24,7 @@ func GetUserTags(db *gorm.DB, id uuid.UUID) ([]models.Tag, *errors.Error) {
func CreateUserTags(db *gorm.DB, id uuid.UUID, tags []models.Tag) ([]models.Tag, *errors.Error) {
user, err := GetUser(db, id)
if err != nil {
return nil, &errors.UserNotFound
return nil, err
}

if err := db.Model(&user).Association("Tag").Replace(tags); err != nil {
Expand Down
4 changes: 1 addition & 3 deletions backend/tests/api/club_follower_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import (
)

func TestClubFollowerWorks(t *testing.T) {
appAssert := h.InitTest(t)

appAssert, _, clubUUID := CreateSampleClub(appAssert)
appAssert, _, clubUUID := CreateSampleClub(h.InitTest(t))

appAssert.TestOnStatus(
h.TestRequest{
Expand Down
Loading

0 comments on commit 7d874a1

Please sign in to comment.