Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Root Model ID from uint -> UUID4 #69

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/goccy/go-json v0.10.2
github.com/google/uuid v1.5.0 // indirect
github.com/google/uuid v1.5.0
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/huandu/go-assert v1.1.6
github.com/jackc/pgpassfile v1.0.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions backend/src/database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func ConfigureDB(settings config.Settings) (*gorm.DB, error) {
return nil, err
}

err = db.Exec("CREATE EXTENSION IF NOT EXISTS \"uuid-ossp\"").Error

if err != nil {
return nil, err
}

if err := MigrateDB(settings, db); err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions backend/src/errors/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ type Error struct {
Message string
}

func (e Error) FiberError(c *fiber.Ctx) error {
func (e *Error) FiberError(c *fiber.Ctx) error {
return c.Status(e.StatusCode).JSON(fiber.Map{"error": e.Message})
}

func (e Error) Error() string {
func (e *Error) Error() string {
return e.Message
}
4 changes: 1 addition & 3 deletions backend/src/models/category.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package models

import "github.com/GenerateNU/sac/backend/src/types"

type Category struct {
types.Model
Model

Name string `gorm:"type:varchar(255);unique" json:"name" validate:"required,max=255"`
Tag []Tag `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Expand Down
8 changes: 4 additions & 4 deletions backend/src/models/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package models
import (
"time"

"github.com/GenerateNU/sac/backend/src/types"
"github.com/google/uuid"
)

type RecruitmentCycle string
Expand All @@ -24,7 +24,7 @@ const (
)

type Club struct {
types.Model
Model

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

Expand All @@ -38,8 +38,8 @@ type Club struct {
ApplicationLink string `gorm:"type:varchar(255);default:NULL" json:"application_link" validate:"required,max=255"`
Logo string `gorm:"type:varchar(255);default:NULL" json:"logo" validate:"url,max=255"` // S3 URL

Parent *uint `gorm:"foreignKey:Parent" json:"-" validate:"min=1"`
Tag []Tag `gorm:"many2many:club_tags;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Parent *uuid.UUID `gorm:"foreignKey:Parent" json:"-" validate:"uuid4"`
Tag []Tag `gorm:"many2many:club_tags;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
// User
Member []User `gorm:"many2many:user_club_members;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"required"`
Follower []User `gorm:"many2many:user_club_followers;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Expand Down
18 changes: 8 additions & 10 deletions backend/src/models/comment.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package models

import (
"github.com/GenerateNU/sac/backend/src/types"
)
import "github.com/google/uuid"

type Comment struct {
types.Model
Model

Question string `gorm:"type:varchar(255)" json:"question" validate:"required,max=255"`
Answer string `gorm:"type:varchar(255)" json:"answer" validate:",max=255"`
NumFoundHelpful uint `gorm:"type:int;default:0" json:"num_found_helpful" validate:"min=0"`

AskedByID uint `gorm:"type:uuid" json:"-" validate:"min=1"`
AskedBy User `gorm:"foreignKey:AskedByID" json:"-" validate:"-"`
AskedByID uuid.UUID `gorm:"type:uuid" json:"-" validate:"uuid4"`
AskedBy User `gorm:"foreignKey:AskedByID" json:"-" validate:"-"`

ClubID uint `gorm:"type:uuid" json:"-" validate:"min=1"`
Club Club `gorm:"foreignKey:ClubID" json:"-" validate:"-"`
ClubID uuid.UUID `gorm:"type:uuid" json:"-" validate:"uuid4"`
Club Club `gorm:"foreignKey:ClubID" json:"-" validate:"-"`

AnsweredByID *uint `gorm:"type:uuid" json:"-" validate:"min=1"`
AnsweredBy *User `gorm:"foreignKey:AnsweredBy" json:"-" validate:"-"`
AnsweredByID *uuid.UUID `gorm:"type:uuid" json:"-" validate:"uuid4"`
AnsweredBy *User `gorm:"foreignKey:AnsweredBy" json:"-" validate:"-"`
}
8 changes: 3 additions & 5 deletions backend/src/models/contact.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package models

import (
"github.com/GenerateNU/sac/backend/src/types"
)
import "github.com/google/uuid"

type Media string

Expand All @@ -17,10 +15,10 @@ const (
)

type Contact struct {
types.Model
Model

Type Media `gorm:"type:varchar(255)" json:"type" validate:"required,max=255"`
Content string `gorm:"type:varchar(255)" json:"content" validate:"required,url,max=255"` // media URL

ClubID uint `gorm:"foreignKey:ClubID" json:"-" validate:"min=1"`
ClubID uuid.UUID `gorm:"foreignKey:ClubID" json:"-" validate:"uuid4"`
}
4 changes: 1 addition & 3 deletions backend/src/models/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package models

import (
"time"

"github.com/GenerateNU/sac/backend/src/types"
)

type EventType string
Expand All @@ -14,7 +12,7 @@ const (
)

type Event struct {
types.Model
Model

Name string `gorm:"type:varchar(255)" json:"name" validate:"required,max=255"`
Preview string `gorm:"type:varchar(255)" json:"preview" validate:"required,max=255"`
Expand Down
6 changes: 3 additions & 3 deletions backend/src/models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package models
import (
"time"

"github.com/GenerateNU/sac/backend/src/types"
"github.com/google/uuid"
)

type NotificationType string
Expand All @@ -14,14 +14,14 @@ const (
)

type Notification struct {
types.Model
Model

SendAt time.Time `gorm:"type:timestamptz" json:"send_at" validate:"required"`
Title string `gorm:"type:varchar(255)" json:"title" validate:"required,max=255"`
Content string `gorm:"type:varchar(255)" json:"content" validate:"required,max=255"`
DeepLink string `gorm:"type:varchar(255)" json:"deep_link" validate:"required,max=255"`
Icon string `gorm:"type:varchar(255)" json:"icon" validate:"required,url,max=255"` // S3 URL

ReferenceID uint `gorm:"type:int" json:"-" validate:"min=1"`
ReferenceID uuid.UUID `gorm:"type:int" json:"-" validate:"uuid4"`
ReferenceType NotificationType `gorm:"type:varchar(255)" json:"-" validate:"max=255"`
}
8 changes: 3 additions & 5 deletions backend/src/models/point_of_contact.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package models

import (
"github.com/GenerateNU/sac/backend/src/types"
)
import "github.com/google/uuid"

type PointOfContact struct {
types.Model
Model

Name string `gorm:"type:varchar(255)" json:"name" validate:"required,max=255"`
Email string `gorm:"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 uint `gorm:"foreignKey:ClubID" json:"-" validate:"min=1"`
ClubID uuid.UUID `gorm:"foreignKey:ClubID" json:"-" validate:"uuid4"`
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package types
package models

import (
"time"

"github.com/google/uuid"
)

type Model struct {
ID uint `gorm:"primarykey" json:"id" example:"1"`
ID uuid.UUID `gorm:"type:uuid;primary_key;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"`
}
14 changes: 6 additions & 8 deletions backend/src/models/tag.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
package models

import (
"github.com/GenerateNU/sac/backend/src/types"
)
import "github.com/google/uuid"

type Tag struct {
types.Model
Model

Name string `gorm:"type:varchar(255)" json:"name" validate:"required,max=255"`

CategoryID uint `gorm:"foreignKey:CategoryID" json:"category_id" validate:"required,min=1"`
CategoryID uuid.UUID `gorm:"foreignKey:CategoryID" json:"category_id" validate:"required,uuid4"`

User []User `gorm:"many2many:user_tags;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Club []Club `gorm:"many2many:club_tags;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
Event []Event `gorm:"many2many:event_tags;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;" json:"-" validate:"-"`
}

type TagRequestBody struct {
Name string `json:"name" validate:"required,max=255"`
CategoryID uint `json:"category_id" validate:"required,min=1"`
}
Name string `json:"name" validate:"required,max=255"`
CategoryID uuid.UUID `json:"category_id" validate:"required,uuid4"`
}
4 changes: 1 addition & 3 deletions backend/src/models/user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package models

import "github.com/GenerateNU/sac/backend/src/types"

type UserRole string

const (
Expand Down Expand Up @@ -36,7 +34,7 @@ const (
)

type User struct {
types.Model
Model

Role UserRole `gorm:"type:varchar(255);" json:"user_role,omitempty" validate:"required,max=255"`
NUID string `gorm:"column:nuid;type:varchar(9);unique" json:"nuid" validate:"required,numeric,len=9"`
Expand Down
12 changes: 6 additions & 6 deletions backend/src/services/category.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,17 @@ func (c *CategoryService) GetCategories(limit string, page string) ([]models.Cat
}

func (c *CategoryService) GetCategory(id string) (*models.Category, *errors.Error) {
uintId, err := utilities.ValidateID(id)
idAsUUID, err := utilities.ValidateID(id)

if err != nil {
return nil, err
}

return transactions.GetCategory(c.DB, *uintId)
return transactions.GetCategory(c.DB, *idAsUUID)
}

func (c *CategoryService) UpdateCategory(id string, categoryBody models.CategoryRequestBody) (*models.Category, *errors.Error) {
idAsUint, idErr := utilities.ValidateID(id)
idAsUUID, idErr := utilities.ValidateID(id)
if idErr != nil {
return nil, idErr
}
Expand All @@ -87,14 +87,14 @@ func (c *CategoryService) UpdateCategory(id string, categoryBody models.Category

category.Name = cases.Title(language.English).String(category.Name)

return transactions.UpdateCategory(c.DB, *idAsUint, *category)
return transactions.UpdateCategory(c.DB, *idAsUUID, *category)
}

func (c *CategoryService) DeleteCategory(id string) *errors.Error {
idAsUInt, err := utilities.ValidateID(id)
idAsUUID, err := utilities.ValidateID(id)
if err != nil {
return err
}

return transactions.DeleteCategory(c.DB, *idAsUInt)
return transactions.DeleteCategory(c.DB, *idAsUUID)
}
12 changes: 6 additions & 6 deletions backend/src/services/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ func (t *TagService) CreateTag(tagBody models.TagRequestBody) (*models.Tag, *err
}

func (t *TagService) GetTag(id string) (*models.Tag, *errors.Error) {
idAsUint, err := utilities.ValidateID(id)
idAsUUID, err := utilities.ValidateID(id)
if err != nil {
return nil, err
}

return transactions.GetTag(t.DB, *idAsUint)
return transactions.GetTag(t.DB, *idAsUUID)
}

func (t *TagService) UpdateTag(id string, tagBody models.TagRequestBody) (*models.Tag, *errors.Error) {
idAsUint, idErr := utilities.ValidateID(id)
idAsUUID, idErr := utilities.ValidateID(id)
if idErr != nil {
return nil, idErr
}
Expand All @@ -58,14 +58,14 @@ func (t *TagService) UpdateTag(id string, tagBody models.TagRequestBody) (*model
return nil, &errors.FailedToMapRequestToModel
}

return transactions.UpdateTag(t.DB, *idAsUint, *tag)
return transactions.UpdateTag(t.DB, *idAsUUID, *tag)
}

func (t *TagService) DeleteTag(id string) *errors.Error {
idAsUint, err := utilities.ValidateID(id)
idAsUUID, err := utilities.ValidateID(id)
if err != nil {
return &errors.FailedToValidateID
}

return transactions.DeleteTag(t.DB, *idAsUint)
return transactions.DeleteTag(t.DB, *idAsUUID)
}
12 changes: 6 additions & 6 deletions backend/src/services/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ func (u *UserService) GetUsers(limit string, page string) ([]models.User, *error
}

func (u *UserService) GetUser(id string) (*models.User, *errors.Error) {
idAsUint, err := utilities.ValidateID(id)
idAsUUID, err := utilities.ValidateID(id)
if err != nil {
return nil, &errors.FailedToValidateID
}

return transactions.GetUser(u.DB, *idAsUint)
return transactions.GetUser(u.DB, *idAsUUID)
}

func (u *UserService) UpdateUser(id string, userBody models.UpdateUserRequestBody) (*models.User, *errors.Error) {
idAsUint, idErr := utilities.ValidateID(id)
idAsUUID, idErr := utilities.ValidateID(id)
if idErr != nil {
return nil, idErr
}
Expand All @@ -96,14 +96,14 @@ func (u *UserService) UpdateUser(id string, userBody models.UpdateUserRequestBod

user.PasswordHash = *passwordHash

return transactions.UpdateUser(u.DB, *idAsUint, *user)
return transactions.UpdateUser(u.DB, *idAsUUID, *user)
}

func (u *UserService) DeleteUser(id string) *errors.Error {
idAsInt, err := utilities.ValidateID(id)
idAsUUID, err := utilities.ValidateID(id)
if err != nil {
return err
}

return transactions.DeleteUser(u.DB, *idAsInt)
return transactions.DeleteUser(u.DB, *idAsUUID)
}
Loading
Loading