Skip to content

Commit

Permalink
Partial fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
akshayd2020 committed Feb 21, 2024
1 parent f018c3a commit 9de1e1e
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 42 deletions.
2 changes: 1 addition & 1 deletion backend/src/controllers/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (f *FileController) CreateFile(c *fiber.Ctx) error {
return errors.FailedToValidatedData.FiberError(c)
}
defer fileData.Close()
print(file.AssociationID.String())
print(file.OwnerID.String())
fileCreated, errFile := f.fileService.CreateFile(fileRequestBody, file, formFile, fileData)
if errFile != nil {
return errFile.FiberError(c)
Expand Down
8 changes: 4 additions & 4 deletions backend/src/models/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ type File struct {
Tags []*Tag `gorm:"many2many:file_tags;" json:"tags"`
S3Url string `gorm:"type:varchar(255);default:NULL" json:"photo" validate:"url,max=255"`

AssociationType string `gorm:"type:varchar(255)" json:"association_type"` // association with files (club/event/user)
AssociationID uuid.UUID `gorm:"type:varchar(255)" json:"association_id" validate:"min=1"` // association id (club/event/user)
OwnerType string `json:"owner_type"` // association with files (club/event/user)
OwnerID uuid.UUID `json:"owner_id"` // association id (club/event/user)
}

type FileBody struct {
AssociationType string `gorm:"type:varchar(255)" json:"association_type"` // association with files (club/event/user)
AssociationID uuid.UUID `gorm:"type:varchar(255)" json:"association_id" validate:"min=1"` // association id (club/event/user)
OwnerType string `gorm:"type:varchar(255)" json:"owner_type"` // association with files (club/event/user)
OwnerID uuid.UUID `gorm:"type:varchar(255)" json:"owner_id" validate:"min=1"` // association id (club/event/user)
}
20 changes: 9 additions & 11 deletions backend/src/models/point_of_contact.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ import "github.com/google/uuid"
type PointOfContact struct {
Model

Name string `gorm:"type:varchar(255)" json:"name" validate:"required,max=255"`
Email string `gorm:"uniqueIndex:compositeindex;index;not null;type:varchar(255)" json:"email" validate:"required,email,max=255"`
Photo string `gorm:"type:varchar(255);default:NULL" json:"photo" validate:"url,max=255"` // S3 URL, fallback to default logo if null
Position string `gorm:"type:varchar(255);" json:"position" validate:"required,max=255"`

ClubID uuid.UUID `gorm:"uniqueIndex:compositeindex;index;not null;foreignKey:ClubID" json:"-" validate:"min=1"`
PhotoFileID uuid.UUID `gorm:"uniqueIndex:compositeindex;index;not null;foreignKey:FileID" json:"photo_file_id" validate:"min=1"`
Name string `gorm:"type:varchar(255)" json:"name" validate:"required,max=255"`
Email string `gorm:"uniqueIndex:compositeindex;index;not null;type:varchar(255)" json:"email" validate:"required,email,max=255"`
Position string `gorm:"type:varchar(255);" json:"position" validate:"required,max=255"`
ClubID uuid.UUID `gorm:"uniqueIndex:compositeindex;index;not null;foreignKey:ClubID" json:"-" validate:"min=1"`
PhotoFile *File `gorm:"polymorphic:Owner;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;" json:"photo_file"`
}

type CreatePointOfContactBody struct {
Name string `json:"name" validate:"required,max=255"`
Email string `json:"email" validate:"required,email,max=255"`
PhotoFileID string `json:"photo_file_id" validate:"uuid4, max=255"` // S3 URL, fallback to default logo if null
Position string `json:"position" validate:"required,max=255"`
Name string `json:"name" validate:"required,max=255"`
Email string `json:"email" validate:"required,email,max=255"`
PhotoFileID uuid.UUID `json:"photo_file_id" validate:"uuid4"`
Position string `json:"position" validate:"required,max=255"`
}
21 changes: 9 additions & 12 deletions backend/src/services/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,35 +34,32 @@ func (u *ClubService) UpsertPointOfContact(clubId string, pointOfContactBody mod
}
pointOfContact, err := utilities.MapRequestToModel(pointOfContactBody, &models.PointOfContact{})
if err != nil {
print(err.Error())
return nil, &errors.FailedToMapRequestToModel
}
clubIdAsUUID, idErr := utilities.ValidateID(clubId)
var file models.File
if (pointOfContact.PhotoFileID != uuid.Nil) {
result := u.DB.Where("id = ?", pointOfContact.PhotoFileID).RowsAffected
if result == 0 {
return nil, &errors.InvalidFileID
}

if err := u.DB.First(&file, "id = ?", pointOfContact.PhotoFileID).Error; err != nil {
if pointOfContactBody.PhotoFileID != uuid.Nil {
if err := u.DB.First(&file, "id = ?", pointOfContactBody.PhotoFileID).Error; err != nil {
return nil, &errors.CannotFindFile
}

}
pointOfContact.ClubID = *clubIdAsUUID
if idErr != nil {
return nil, &errors.FailedToValidateClub
}
poc, upsertErr := transactions.UpsertPointOfContact(u.DB, pointOfContact)

if (upsertErr != nil) {
if upsertErr != nil {
return poc, upsertErr
}

if (pointOfContact.PhotoFileID != uuid.Nil) {
file.AssociationType = "point_of_contact"
file.AssociationID = poc.ID
if pointOfContactBody.PhotoFileID != uuid.Nil {
file.OwnerType = "point_of_contact"
file.OwnerID = poc.ID
}
u.DB.Save(&file)
pointOfContact.PhotoFile = &file
return poc, nil
}

Expand Down
12 changes: 6 additions & 6 deletions backend/src/services/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,17 @@ func (f *FileService) CreateFile(fileBody models.FileBody, file models.File, dat

// if file type and file id are populated, if both are then search to see if id exists in corresponding table. Throw error
// if entry does not exist
associationType := fileBody.AssociationType
associationID := fileBody.AssociationID
print(associationID.String())
ownerType := fileBody.OwnerType
ownerID := fileBody.OwnerID
print(ownerID.String())

possibleTypes := []string{"users", "clubs", "events", "point_of_contacts"}

if associationID != uuid.Nil {
if !isInArray(possibleTypes, associationType) {
if ownerID != uuid.Nil {
if !isInArray(possibleTypes, ownerType) {
return nil, &errors.InvalidAssociationType
}
result := f.DB.Table(associationType).Where("id = ?", associationID).RowsAffected
result := f.DB.Table(ownerType).Where("id = ?", ownerID).RowsAffected
if result == 0 {
return nil, &errors.FailedToFindAssociationID
}
Expand Down
22 changes: 14 additions & 8 deletions backend/src/transactions/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@ import (
"github.com/GenerateNU/sac/backend/src/models"
"github.com/google/uuid"
"gorm.io/gorm"
"gorm.io/gorm/clause"
// "gorm.io/gorm/clause"
)

// Upsert Point of Contact
func UpsertPointOfContact(db *gorm.DB, pointOfContact *models.PointOfContact) (*models.PointOfContact, *errors.Error) {
err := db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "email"}, {Name: "club_id"}},
DoUpdates: clause.AssignmentColumns([]string{"name", "photo_file_id", "email", "position"}),
}).Create(&pointOfContact).Error
if err != nil {
return nil, &errors.FailedToUpsertPointOfContact
}
pocExist, errPOCExist := GetPointOfContact(db, pointOfContact.ID, pointOfContact.ClubID)
if errPOCExist != nil {
db.Model(&pointOfContact).Association("File").Replace(pocExist, pointOfContact)
} else {
db.Model(&pointOfContact).Association("File").Append(pointOfContact)
}
// err := db.Clauses(clause.OnConflict{
// Columns: []clause.Column{{Name: "email"}, {Name: "club_id"}},
// DoUpdates: clause.AssignmentColumns([]string{"name", "email", "position"}),
// }).Create(&pointOfContact).Error
// if err != nil {
// return nil, &errors.FailedToUpsertPointOfContact
// }
return pointOfContact, nil
}

Expand Down

0 comments on commit 9de1e1e

Please sign in to comment.