-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ea15ec
commit 4c692c1
Showing
6 changed files
with
181 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1 +1,93 @@ | ||
package rsvps | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/GenerateNU/sac/backend/database/cache" | ||
"github.com/GenerateNU/sac/backend/entities/clubs" | ||
"github.com/GenerateNU/sac/backend/entities/events" | ||
"github.com/GenerateNU/sac/backend/entities/models" | ||
"github.com/GenerateNU/sac/backend/entities/users" | ||
"github.com/GenerateNU/sac/backend/utilities" | ||
"github.com/garrettladley/fiberpaginate" | ||
"github.com/google/uuid" | ||
"gorm.io/gorm" | ||
) | ||
|
||
func CreateEventRSVP(db *gorm.DB, eventID uuid.UUID, userID uuid.UUID) error { | ||
db = cache.SetUseCache(db, true) | ||
|
||
event, err := events.GetEvent(db, eventID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if event.IsArchived { | ||
return utilities.ForbiddenReason(errors.New("cannot RSVP to an archived event")) | ||
} | ||
|
||
if event.IsDraft { | ||
return utilities.ForbiddenReason(errors.New("cannot RSVP to a draft event")) | ||
} | ||
|
||
user, err := users.GetUser(db, userID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !event.IsPublic { | ||
isMember, err := clubs.IsMember(db, *event.Host, userID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !isMember { | ||
return utilities.ForbiddenReason(errors.New("cannot RSVP to a private event when not a member")) | ||
} | ||
} | ||
|
||
if err := db.Model(&user).Association("RSVP").Append(event); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func GetEventRSVPs(db *gorm.DB, eventID uuid.UUID, pageInfo fiberpaginate.PageInfo) ([]models.User, error) { | ||
db = cache.SetUseCache(db, true) | ||
|
||
event, err := events.GetEvent(db, eventID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var users []models.User | ||
if err := db.Scopes(utilities.IntoScope(pageInfo, db)).Model(&event).Association("RSVP").Find(&users); err != nil { | ||
return nil, err | ||
} | ||
|
||
return users, nil | ||
} | ||
|
||
func DeleteEventRSVP(db *gorm.DB, eventID uuid.UUID, userID uuid.UUID) error { | ||
db = cache.SetUseCache(db, true) | ||
|
||
user, err := users.GetUser(db, userID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
event, err := events.GetEvent(db, eventID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := db.Model(&user).Association("RSVP").Delete(event); err != nil { | ||
if errors.Is(err, gorm.ErrRecordNotFound) { | ||
return utilities.BadRequest(errors.New("user was never RSVPed to this event")) | ||
} | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains 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