Skip to content

Commit

Permalink
Merge branch 'main' into fix-sac-cli-format
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley authored Mar 7, 2024
2 parents 5ed46c1 + ec66f2e commit ade9f01
Show file tree
Hide file tree
Showing 40 changed files with 295 additions and 182 deletions.
1 change: 1 addition & 0 deletions backend/src/controllers/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func NewCategoryController(categoryService services.CategoryServiceInterface) *C
// @Failure 400 {string} errors.Error
// @Failure 401 {string} errors.Error
// @Failure 404 {string} errors.Error
// @Failure 409 {string} errors.Error
// @Failure 500 {string} errors.Error
// @Router /categories/ [post]
func (cat *CategoryController) CreateCategory(c *fiber.Ctx) error {
Expand Down
1 change: 1 addition & 0 deletions backend/src/controllers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func NewUserController(userService services.UserServiceInterface) *UserControlle
// @Failure 400 {object} errors.Error
// @Failure 401 {object} errors.Error
// @Failure 404 {object} errors.Error
// @Failure 409 {object} errors.Error
// @Failure 500 {object} errors.Error
// @Router /users/ [post]
func (u *UserController) CreateUser(c *fiber.Ctx) error {
Expand Down
3 changes: 2 additions & 1 deletion backend/src/controllers/user_follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"github.com/GenerateNU/sac/backend/src/services"
"github.com/GenerateNU/sac/backend/src/utilities"
"github.com/gofiber/fiber/v2"
)

Expand Down Expand Up @@ -33,7 +34,7 @@ func (uf *UserFollowerController) CreateFollowing(c *fiber.Ctx) error {
if err != nil {
return err.FiberError(c)
}
return c.SendStatus(fiber.StatusCreated)
return utilities.FiberMessage(c, fiber.StatusCreated, "Successfully followed club")
}

// DeleteFollowing godoc
Expand Down
3 changes: 2 additions & 1 deletion backend/src/controllers/user_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"github.com/GenerateNU/sac/backend/src/services"
"github.com/GenerateNU/sac/backend/src/utilities"
"github.com/gofiber/fiber/v2"
)

Expand Down Expand Up @@ -34,7 +35,7 @@ func (um *UserMemberController) CreateMembership(c *fiber.Ctx) error {
return err.FiberError(c)
}

return c.SendStatus(fiber.StatusCreated)
return utilities.FiberMessage(c, fiber.StatusCreated, "Successfully joined club")
}

// DeleteMembership godoc
Expand Down
28 changes: 21 additions & 7 deletions backend/src/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ func MigrateDB(settings config.Settings, db *gorm.DB) error {
return err
}

// Check if the database already has a super user
var superUser models.User
if err := db.Where("role = ?", models.Super).First(&superUser).Error; err != nil {
if err := createSuperUser(settings, db); err != nil {
Expand All @@ -112,26 +111,30 @@ 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
}

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

SuperUserUUID = superUser.ID

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

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

SuperUserUUID = superUser.ID

membership := models.Membership{
ClubID: superClub.ID,
UserID: superUser.ID,
Expand All @@ -143,7 +146,18 @@ func createSuperUser(settings config.Settings, db *gorm.DB) error {
return err
}

follower := models.Follower{
ClubID: superClub.ID,
UserID: superUser.ID,
}

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

return tx.Commit().Error
}

return nil
}
8 changes: 8 additions & 0 deletions backend/src/errors/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ var (
StatusCode: fiber.StatusInternalServerError,
Message: "failed to get club events",
}
FailedToJoinClub = Error{
StatusCode: fiber.StatusInternalServerError,
Message: "failed to join club",
}
AlreadyMemberOfClub = Error{
StatusCode: fiber.StatusBadRequest,
Message: "already member of club",
}
)
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",
}
FailedToFollowClub = Error{
StatusCode: fiber.StatusInternalServerError,
Message: "failed to follow club",
}
)
12 changes: 1 addition & 11 deletions backend/src/models/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Club struct {

SoftDeletedAt gorm.DeletedAt `gorm:"type:timestamptz;default:NULL" json:"-" validate:"-"`

Name string `gorm:"type:varchar(255)" json:"name" validate:"required,max=255"`
Name string `gorm:"type:varchar(255);unique" json:"name" validate:"required,max=255"`
Preview string `gorm:"type:varchar(255)" json:"preview" validate:"required,max=255"`
Description string `gorm:"type:varchar(255)" json:"description" validate:"required,http_url,mongo_url,max=255"` // MongoDB URL
NumMembers int `gorm:"type:int" json:"num_members" validate:"required,min=1"`
Expand Down Expand Up @@ -114,16 +114,6 @@ func (cqp *ClubQueryParams) IntoWhere() string {
return "WHERE " + strings.Join(conditions, " AND ")
}

func (c *Club) AfterCreate(tx *gorm.DB) (err error) {
tx.Model(&c).Update("num_members", c.NumMembers+1)
return
}

func (c *Club) AfterDelete(tx *gorm.DB) (err error) {
tx.Model(&c).Update("num_members", c.NumMembers-1)
return
}

func (c *Club) SearchId() string {
return c.ID.String()
}
Expand Down
2 changes: 1 addition & 1 deletion backend/src/models/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Series struct {

// TODO: add not null to required fields on all gorm models
type EventSeries struct {
EventID uuid.UUID `gorm:"not null; type:uuid; primary_key;" json:"event_id" validate:"uuid4"`
EventID uuid.UUID `gorm:"not null; type:uuid; primaryKey;" json:"event_id" validate:"uuid4"`
Event Event `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
SeriesID uuid.UUID `gorm:"not null; type:uuid;" json:"series_id" validate:"uuid4"`
Series Series `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Expand Down
17 changes: 17 additions & 0 deletions backend/src/models/follower.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package models

import (
"github.com/google/uuid"
)

func (Follower) TableName() string {
return "user_club_followers"
}

type Follower struct {
UserID uuid.UUID `gorm:"type:uuid;not null;primaryKey" json:"user_id" validate:"required,uuid4"`
ClubID uuid.UUID `gorm:"type:uuid;not null;primaryKey" 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:"-"`
}
14 changes: 5 additions & 9 deletions backend/src/models/membership.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package models

import "github.com/google/uuid"
import (
"github.com/google/uuid"
)

type MembershipType string

Expand All @@ -9,19 +11,13 @@ const (
MembershipTypeAdmin MembershipType = "admin"
)

type Tabler interface {
TableName() string
}

func (Membership) TableName() string {
return "user_club_members"
}

type Membership struct {
Model

UserID uuid.UUID `gorm:"type:uuid;not null" json:"user_id" validate:"required,uuid4"`
ClubID uuid.UUID `gorm:"type:uuid;not null" json:"club_id" validate:"required,uuid4"`
UserID uuid.UUID `gorm:"type:uuid;not null;primaryKey" json:"user_id" validate:"required,uuid4"`
ClubID uuid.UUID `gorm:"type:uuid;not null;primaryKey" 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:"-"`
Expand Down
6 changes: 5 additions & 1 deletion backend/src/models/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import (
"github.com/google/uuid"
)

type Tabler interface {
TableName() string
}

type Model struct {
ID uuid.UUID `gorm:"type:uuid;primary_key;default:uuid_generate_v4()" json:"id" example:"123e4567-e89b-12d3-a456-426614174000"`
ID uuid.UUID `gorm:"type:uuid;primaryKey;default:uuid_generate_v4()" json:"id" example:"123e4567-e89b-12d3-a456-426614174000"`
CreatedAt time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP" json:"created_at" example:"2023-09-20T16:34:50Z"`
UpdatedAt time.Time `gorm:"type:timestamp;default:CURRENT_TIMESTAMP" json:"updated_at" example:"2023-09-20T16:34:50Z"`
}
35 changes: 35 additions & 0 deletions backend/src/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package models

import (
"github.com/google/uuid"
"gorm.io/gorm"
)

type UserRole string
Expand Down Expand Up @@ -89,3 +90,37 @@ type UpdatePasswordRequestBody struct {
type CreateUserTagsBody struct {
Tags []uuid.UUID `json:"tags" validate:"required"`
}

func (u *User) AfterCreate(tx *gorm.DB) (err error) {
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 {
return err
}

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

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

return nil
}

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

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

return nil
}
2 changes: 1 addition & 1 deletion backend/src/services/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func (a *AuthService) UpdatePassword(id string, passwordBody models.UpdatePasswo
return err
}

correct, passwordErr := auth.ComparePasswordAndHash(passwordBody.OldPassword, passwordHash)
correct, passwordErr := auth.ComparePasswordAndHash(passwordBody.OldPassword, *passwordHash)
if passwordErr != nil || !correct {
return &errors.FailedToValidateUser
}
Expand Down
4 changes: 1 addition & 3 deletions backend/src/services/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ func (c *CategoryService) GetCategories(limit string, page string) ([]models.Cat
return nil, &errors.FailedToValidatePage
}

offset := (*pageAsInt - 1) * *limitAsInt

return transactions.GetCategories(c.DB, *limitAsInt, offset)
return transactions.GetCategories(c.DB, *limitAsInt, *pageAsInt)
}

func (c *CategoryService) GetCategory(id string) (*models.Category, *errors.Error) {
Expand Down
4 changes: 1 addition & 3 deletions backend/src/services/category_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ func (t *CategoryTagService) GetTagsByCategory(categoryID string, limit string,
return nil, &errors.FailedToValidatePage
}

offset := (*pageAsInt - 1) * *limitAsInt

return transactions.GetTagsByCategory(t.DB, *categoryIDAsUUID, *limitAsInt, offset)
return transactions.GetTagsByCategory(t.DB, *categoryIDAsUUID, *limitAsInt, *pageAsInt)
}

func (t *CategoryTagService) GetTagByCategory(categoryID string, tagID string) (*models.Tag, *errors.Error) {
Expand Down
4 changes: 1 addition & 3 deletions backend/src/services/club_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,5 @@ func (c *ClubEventService) GetClubEvents(clubID string, limit string, page strin
return nil, &errors.FailedToValidatePage
}

offset := (*pageAsInt - 1) * *limitAsInt

return transactions.GetClubEvents(c.DB, *idAsUUID, *limitAsInt, offset)
return transactions.GetClubEvents(c.DB, *idAsUUID, *limitAsInt, *pageAsInt)
}
4 changes: 1 addition & 3 deletions backend/src/services/contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ func (c *ContactService) GetContacts(limit string, page string) ([]models.Contac
return nil, &errors.FailedToValidatePage
}

offset := (*pageAsInt - 1) * *limitAsInt

return transactions.GetContacts(c.DB, *limitAsInt, offset)
return transactions.GetContacts(c.DB, *limitAsInt, *pageAsInt)
}

func (c *ContactService) GetContact(contactID string) (*models.Contact, *errors.Error) {
Expand Down
4 changes: 1 addition & 3 deletions backend/src/services/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ func (e *EventService) GetEvents(limit string, page string) ([]models.Event, *er
return nil, &errors.FailedToValidatePage
}

offset := (*pageAsInt - 1) * *limitAsInt

return transactions.GetEvents(e.DB, *limitAsInt, offset)
return transactions.GetEvents(e.DB, *limitAsInt, *pageAsInt)
}

// TODO: add logic for creating the []event here
Expand Down
4 changes: 1 addition & 3 deletions backend/src/services/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ func (t *TagService) GetTags(limit string, page string) ([]models.Tag, *errors.E
return nil, &errors.FailedToValidatePage
}

offset := (*pageAsInt - 1) * *limitAsInt

return transactions.GetTags(t.DB, *limitAsInt, offset)
return transactions.GetTags(t.DB, *limitAsInt, *pageAsInt)
}

func (t *TagService) GetTag(tagID string) (*models.Tag, *errors.Error) {
Expand Down
4 changes: 1 addition & 3 deletions backend/src/services/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ func (u *UserService) GetUsers(limit string, page string) ([]models.User, *error
return nil, &errors.FailedToValidatePage
}

offset := (*pageAsInt - 1) * *limitAsInt

return transactions.GetUsers(u.DB, *limitAsInt, offset)
return transactions.GetUsers(u.DB, *limitAsInt, *pageAsInt)
}

func (u *UserService) GetUser(id string) (*models.User, *errors.Error) {
Expand Down
4 changes: 3 additions & 1 deletion backend/src/transactions/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ func CreateCategory(db *gorm.DB, category models.Category) (*models.Category, *e
return &category, nil
}

func GetCategories(db *gorm.DB, limit int, offset int) ([]models.Category, *errors.Error) {
func GetCategories(db *gorm.DB, limit int, page int) ([]models.Category, *errors.Error) {
var categories []models.Category

offset := (page - 1) * limit

if err := db.Limit(limit).Offset(offset).Find(&categories).Error; err != nil {
return nil, &errors.FailedToGetCategories
}
Expand Down
5 changes: 4 additions & 1 deletion backend/src/transactions/category_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"gorm.io/gorm"
)

func GetTagsByCategory(db *gorm.DB, categoryID uuid.UUID, limit int, offset int) ([]models.Tag, *errors.Error) {
func GetTagsByCategory(db *gorm.DB, categoryID uuid.UUID, limit int, page int) ([]models.Tag, *errors.Error) {
var category models.Category

if err := db.Where("id = ?", categoryID).First(&category).Error; err != nil {
Expand All @@ -22,6 +22,9 @@ func GetTagsByCategory(db *gorm.DB, categoryID uuid.UUID, limit int, offset int)
}

var tags []models.Tag

offset := (page - 1) * limit

if err := db.Where("category_id = ?", categoryID).Limit(limit).Offset(offset).Find(&tags).Error; err != nil {
return nil, &errors.FailedToGetTags
}
Expand Down
Loading

0 comments on commit ade9f01

Please sign in to comment.