Skip to content

Commit

Permalink
resolved revolving door of issues
Browse files Browse the repository at this point in the history
  • Loading branch information
DOOduneye committed Feb 21, 2024
1 parent bc0a980 commit 5b1b17f
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 103 deletions.
13 changes: 10 additions & 3 deletions backend/src/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,24 @@ func createSuperUser(settings config.Settings, db *gorm.DB) error {
var user models.User

if err := db.Where("nuid = ?", superUser.NUID).First(&user).Error; err != nil {
tx := db.Begin()
tx := db.Begin().Session(&gorm.Session{SkipHooks: true})

if err := tx.Error; err != nil {
return err
}


superClub := SuperClub()
if err := tx.Create(&superClub).Error; err != nil {
tx.Rollback()
return err
}

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

if err := tx.Create(&superUser).Error; err != nil {
tx.Rollback()
return err
Expand Down Expand Up @@ -150,9 +156,10 @@ func createSuperUser(settings config.Settings, db *gorm.DB) error {
tx.Rollback()
return err
}



return tx.Commit().Error
}

return nil
}
4 changes: 4 additions & 0 deletions backend/src/errors/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ var (
StatusCode: fiber.StatusNotFound,
Message: "user not following club",
}
UserAlreadyFollowingClub = Error{
StatusCode: fiber.StatusConflict,
Message: "user already following club",
}
)
2 changes: 1 addition & 1 deletion backend/src/models/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@ func (c *Club) Namespace() string {

func (c *Club) EmbeddingString() string {
return fmt.Sprintf("%s %s %s %s %s", c.Name, c.Name, c.Name, c.Name, c.Description)
}
}
28 changes: 2 additions & 26 deletions backend/src/models/membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,11 @@ func (Membership) TableName() string {
}

type Membership struct {
UserID uuid.UUID `gorm:"type:uuid;not null;primary_key" json:"user_id" validate:"required,uuid4"`
ClubID uuid.UUID `gorm:"type:uuid;not null;primary_key" json:"club_id" validate:"required,uuid4"`
UserID uuid.UUID `gorm:"type:uuid;not null;primary_key;unique_index:idx_user_club_members" json:"user_id" validate:"required,uuid4"`
ClubID uuid.UUID `gorm:"type:uuid;not null;primary_key;unique_index:idx_user_club_members" json:"club_id" validate:"required,uuid4"`

Club *Club `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
User *User `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`

MembershipType MembershipType `gorm:"type:varchar(255);not null;default:member" json:"membership_type" validate:"required,oneof=member admin"`
}

// func (m *Membership) AfterUpdate(tx *gorm.DB) (err error) {
// if err := tx.Model(&Club{}).Where("id = ?", m.ClubID).Update("num_members", gorm.Expr("num_members + 1")).Error; err != nil {
// return err
// }

// if err := tx.Model(&Club{}).Where("id = ?", m.ClubID).Association("Follower").Append(&m.User); err != nil {
// return err
// }

// return nil
// }

// func (m *Membership) AfterDelete(tx *gorm.DB) (err error) {
// if err := tx.Model(&Club{}).Where("id = ?", m.ClubID).Update("num_members", gorm.Expr("num_members - 1")).Error; err != nil {
// return err
// }

// if err := tx.Model(&Club{}).Where("id = ?", m.ClubID).Association("Follower").Delete(&m.User); err != nil {
// return err
// }

// return nil
// }
78 changes: 38 additions & 40 deletions backend/src/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,51 +94,49 @@ type CreateUserTagsBody struct {
}

func (u *User) AfterCreate(tx *gorm.DB) (err error) {
if u.NUID != "000000000" {
sac := &Club{}
if err := tx.Where("name = ?", "SAC").First(sac).Error; err != nil {
return err
}

if err := tx.Model(u).Association("Member").Append(sac); err != nil {
tx.Rollback()
return err
}

if err := tx.Model(u).Association("Follower").Append(sac); err != nil {
tx.Rollback()
return err
}

if err := tx.Model(&Club{}).Where("id = ?", sac.ID).Update("num_members", gorm.Expr("num_members + 1")).Error; err != nil {
tx.Rollback()
return err
}
sac := &Club{}
if err := tx.Where("name = ?", "SAC").First(sac).Error; err != nil {
return err
}

if err := tx.Model(u).Association("Member").Append(sac); err != nil {
tx.Rollback()
return err
}

if err := tx.Model(u).Association("Follower").Append(sac); err != nil {
tx.Rollback()
return err
}

if err := tx.Model(&Club{}).Where("id = ?", sac.ID).Update("num_members", gorm.Expr("num_members + 1")).Error; err != nil {
tx.Rollback()
return err
}

return nil
}

func (u *User) AfterDelete(tx *gorm.DB) (err error) {
if u.NUID != "000000000" {
sac := &Club{}
if err := tx.Where("name = ?", "SAC").First(sac).Error; err != nil {
return err
}

if err := tx.Model(u).Association("Member").Delete(sac); err != nil {
tx.Rollback()
return err
}

if err := tx.Model(u).Association("Follower").Delete(sac); err != nil {
tx.Rollback()
return err
}

if err := tx.Model(&Club{}).Where("id = ?", sac.ID).Update("num_members", gorm.Expr("num_members - 1")).Error; err != nil {
tx.Rollback()
return err
}
sac := &Club{}
if err := tx.Where("name = ?", "SAC").First(sac).Error; err != nil {
return err
}

if err := tx.Model(u).Association("Member").Delete(sac); err != nil {
tx.Rollback()
return err
}

if err := tx.Model(u).Association("Follower").Delete(sac); err != nil {
tx.Rollback()
return err
}

if err := tx.Model(&Club{}).Where("id = ?", sac.ID).Update("num_members", gorm.Expr("num_members - 1")).Error; err != nil {
tx.Rollback()
return err
}

return nil
}
18 changes: 16 additions & 2 deletions backend/src/transactions/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func CreateClub(db *gorm.DB, userId uuid.UUID, club models.Club) (*models.Club,
return nil, err
}

tx := db.Begin()
tx := db.Begin().Session(&gorm.Session{SkipHooks: true})

if err := tx.Create(&club).Error; err != nil {
tx.Rollback()
Expand All @@ -73,7 +73,6 @@ func CreateClub(db *gorm.DB, userId uuid.UUID, club models.Club) (*models.Club,
return nil, &errors.FailedToCreateClub
}

// add the user to the club's members as an admin
membership := models.Membership{
ClubID: club.ID,
UserID: user.ID,
Expand All @@ -85,6 +84,21 @@ func CreateClub(db *gorm.DB, userId uuid.UUID, club models.Club) (*models.Club,
return nil, &errors.FailedToCreateClub
}

follower := models.Follower{
ClubID: club.ID,
UserID: user.ID,
}

if err := tx.Create(&follower).Error; err != nil {
tx.Rollback()
return nil, &errors.FailedToCreateClub
}

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

if err := tx.Commit().Error; err != nil {
tx.Rollback()
return nil, &errors.FailedToCreateClub
Expand Down
36 changes: 25 additions & 11 deletions backend/src/transactions/user_follower.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
package transactions

import (
"slices"

"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/google/uuid"
"gorm.io/gorm"
)

func CreateFollowing(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error {
user, err := GetUser(db, userId)
tx := db.Begin()

user, err := GetUser(tx, userId)
if err != nil {
return err
}

club, err := GetClub(db, clubId)
club, err := GetClub(tx, clubId)
if err != nil {
return err
}

var count int64
if err := tx.Model(&models.Follower{}).Where("user_id = ? AND club_id = ?", userId, clubId).Count(&count).Error; err != nil {
tx.Rollback()
return &errors.FailedToGetUserFollowing
}

if count > 0 {
tx.Rollback()
return &errors.UserAlreadyFollowingClub
}

if err := db.Model(&user).Association("Follower").Append(club); err != nil {
return &errors.FailedToUpdateUser
}
Expand All @@ -28,23 +39,26 @@ 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, PreloadFollwer())
tx := db.Begin()

user, err := GetUser(tx, userId)
if err != nil {
return err
}

club, err := GetClub(db, clubId, PreloadFollwer())
club, err := GetClub(tx, clubId)
if err != nil {
return err
}

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

for i, club := range user.Follower {
userFollowingClubIDs[i] = club.ID
var count int64
if err := tx.Model(&models.Follower{}).Where("user_id = ? AND club_id = ?", userId, clubId).Count(&count).Error; err != nil {
tx.Rollback()
return &errors.FailedToGetUserFollowing
}

if !slices.Contains(userFollowingClubIDs, club.ID) {
if count == 0 {
tx.Rollback()
return &errors.UserNotFollowingClub
}

Expand Down
Loading

0 comments on commit 5b1b17f

Please sign in to comment.