Skip to content

Commit

Permalink
modified crud endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
zacklassetter committed Feb 26, 2024
1 parent d904e21 commit bec6c1b
Show file tree
Hide file tree
Showing 6 changed files with 1,730 additions and 16 deletions.
6 changes: 5 additions & 1 deletion backend/src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/GenerateNU/sac/backend/src/config"
"github.com/GenerateNU/sac/backend/src/database"
"github.com/GenerateNU/sac/backend/src/search"
"github.com/GenerateNU/sac/backend/src/server"
)

Expand Down Expand Up @@ -34,6 +35,9 @@ func main() {
panic(fmt.Sprintf("Error configuring database: %s", err.Error()))
}

openAi := search.NewOpenAIClient(config.OpenAISettings)
pinecone := search.NewPineconeClient(openAi, config.PineconeSettings)

if *onlyMigrate {
return
}
Expand All @@ -43,7 +47,7 @@ func main() {
panic(fmt.Sprintf("Error connecting to database: %s", err.Error()))
}

app := server.Init(db, *config)
app := server.Init(db, pinecone, *config)

err = app.Listen(fmt.Sprintf("%s:%d", config.Application.Host, config.Application.Port))
if err != nil {
Expand Down
1,662 changes: 1,662 additions & 0 deletions backend/src/migrations/data.sql

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion backend/src/models/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Club struct {

Name string `gorm:"type:varchar(255)" 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
Description string `gorm:"type:varchar(8191)" json:"description" validate:"required,http_url,mongo_url,max=255"` // MongoDB URL
NumMembers int `gorm:"type:int" json:"num_members" validate:"required,min=1"`
IsRecruiting bool `gorm:"type:bool;default:false" json:"is_recruiting" validate:"required"`
RecruitmentCycle RecruitmentCycle `gorm:"type:varchar(255);default:always" json:"recruitment_cycle" validate:"required,max=255,oneof=fall spring fallSpring always"`
Expand Down
5 changes: 3 additions & 2 deletions backend/src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"github.com/GenerateNU/sac/backend/src/config"
"github.com/GenerateNU/sac/backend/src/middleware"
"github.com/GenerateNU/sac/backend/src/search"
"github.com/GenerateNU/sac/backend/src/server/routes"
"github.com/GenerateNU/sac/backend/src/services"
"github.com/GenerateNU/sac/backend/src/utilities"
Expand All @@ -23,7 +24,7 @@ import (
// @contact.email [email protected] and [email protected]
// @host 127.0.0.1:8080
// @BasePath /
func Init(db *gorm.DB, settings config.Settings) *fiber.App {
func Init(db *gorm.DB, pinecone *search.PineconeClient, settings config.Settings) *fiber.App {
app := newFiberApp()

validate, err := utilities.RegisterCustomValidators()
Expand All @@ -47,7 +48,7 @@ func Init(db *gorm.DB, settings config.Settings) *fiber.App {

routes.Contact(apiv1, services.NewContactService(db, validate))

clubsIDRouter := routes.Club(apiv1, services.NewClubService(db, validate), middlewareService)
clubsIDRouter := routes.Club(apiv1, services.NewClubService(db, pinecone, validate), middlewareService)
routes.ClubTag(clubsIDRouter, services.NewClubTagService(db, validate))
routes.ClubFollower(clubsIDRouter, services.NewClubFollowerService(db))
routes.ClubMember(clubsIDRouter, services.NewClubMemberService(db, validate))
Expand Down
12 changes: 7 additions & 5 deletions backend/src/services/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/GenerateNU/sac/backend/src/search"
"github.com/GenerateNU/sac/backend/src/transactions"
"github.com/GenerateNU/sac/backend/src/utilities"
"github.com/go-playground/validator/v10"
Expand All @@ -20,11 +21,12 @@ type ClubServiceInterface interface {

type ClubService struct {
DB *gorm.DB
Pinecone *search.PineconeClient
Validate *validator.Validate
}

func NewClubService(db *gorm.DB, validate *validator.Validate) *ClubService {
return &ClubService{DB: db, Validate: validate}
func NewClubService(db *gorm.DB, pinecone *search.PineconeClient, validate *validator.Validate) *ClubService {
return &ClubService{DB: db, Pinecone: pinecone, Validate: validate}
}

func (c *ClubService) GetClubs(queryParams *models.ClubQueryParams) ([]models.Club, *errors.Error) {
Expand All @@ -49,7 +51,7 @@ func (c *ClubService) CreateClub(clubBody models.CreateClubRequestBody) (*models
return nil, &errors.FailedToMapRequestToModel
}

return transactions.CreateClub(c.DB, clubBody.UserID, *club)
return transactions.CreateClub(c.DB, c.Pinecone, clubBody.UserID, *club)
}

func (c *ClubService) GetClub(id string) (*models.Club, *errors.Error) {
Expand All @@ -76,7 +78,7 @@ func (c *ClubService) UpdateClub(id string, clubBody models.UpdateClubRequestBod
return nil, &errors.FailedToMapRequestToModel
}

return transactions.UpdateClub(c.DB, *idAsUUID, *club)
return transactions.UpdateClub(c.DB, c.Pinecone, *idAsUUID, *club)
}

func (c *ClubService) DeleteClub(id string) *errors.Error {
Expand All @@ -85,5 +87,5 @@ func (c *ClubService) DeleteClub(id string) *errors.Error {
return &errors.FailedToValidateID
}

return transactions.DeleteClub(c.DB, *idAsUUID)
return transactions.DeleteClub(c.DB, c.Pinecone, *idAsUUID)
}
59 changes: 52 additions & 7 deletions backend/src/transactions/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/GenerateNU/sac/backend/src/search"

"github.com/google/uuid"
"gorm.io/gorm"
Expand Down Expand Up @@ -54,7 +55,7 @@ func GetClubs(db *gorm.DB, queryParams *models.ClubQueryParams) ([]models.Club,
return clubs, nil
}

func CreateClub(db *gorm.DB, userId uuid.UUID, club models.Club) (*models.Club, *errors.Error) {
func CreateClub(db *gorm.DB, pinecone *search.PineconeClient, userId uuid.UUID, club models.Club) (*models.Club, *errors.Error) {
user, err := GetUser(db, userId)
if err != nil {
return nil, err
Expand All @@ -72,6 +73,12 @@ func CreateClub(db *gorm.DB, userId uuid.UUID, club models.Club) (*models.Club,
return nil, &errors.FailedToCreateClub
}

if err := pinecone.Upsert(&club); err != nil {

Check failure on line 76 in backend/src/transactions/club.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary leading newline (whitespace)

tx.Rollback()
return nil, &errors.FailedToCreateClub
}

if err := tx.Commit().Error; err != nil {
tx.Rollback()
return nil, &errors.FailedToCreateClub
Expand Down Expand Up @@ -100,9 +107,13 @@ func GetClub(db *gorm.DB, id uuid.UUID, preloads ...OptionalQuery) (*models.Club
return &club, nil
}

func UpdateClub(db *gorm.DB, id uuid.UUID, club models.Club) (*models.Club, *errors.Error) {
result := db.Model(&models.User{}).Where("id = ?", id).Updates(club)
func UpdateClub(db *gorm.DB, pinecone *search.PineconeClient, id uuid.UUID, club models.Club) (*models.Club, *errors.Error) {
tx := db.Begin()

// TODO: is this block needed? (updating user based off club data)
result := tx.Model(&models.User{}).Where("id = ?", id).Updates(club)
if result.Error != nil {
tx.Rollback()
if stdliberrors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &errors.UserNotFound
} else {
Expand All @@ -111,29 +122,63 @@ func UpdateClub(db *gorm.DB, id uuid.UUID, club models.Club) (*models.Club, *err
}
var existingClub models.Club

err := db.First(&existingClub, id).Error
err := tx.First(&existingClub, id).Error
if err != nil {
tx.Rollback()
if stdliberrors.Is(err, gorm.ErrRecordNotFound) {
return nil, &errors.ClubNotFound
} else {
return nil, &errors.FailedToCreateClub
}
}

if err := db.Model(&existingClub).Updates(&club).Error; err != nil {
if err := tx.Model(&existingClub).Updates(&club).Error; err != nil {
tx.Rollback()
return nil, &errors.FailedToUpdateUser
}

if pinecone.Upsert(&existingClub) != nil {
tx.Rollback()
return nil, &errors.FailedToUpsertToPinecone
}

if err := tx.Commit().Error; err != nil {
tx.Rollback()
return nil, &errors.FailedToUpdateClub
}

return &existingClub, nil
}

func DeleteClub(db *gorm.DB, id uuid.UUID) *errors.Error {
if result := db.Delete(&models.Club{}, id); result.RowsAffected == 0 {
func DeleteClub(db *gorm.DB, pinecone *search.PineconeClient, id uuid.UUID) *errors.Error {
tx := db.Begin()

var existingClub models.Club
err := tx.First(&existingClub, id)
if err != nil {
tx.Rollback()
return &errors.ClubNotFound
}

pineconeErr := pinecone.Delete(&existingClub)
if pineconeErr != nil {
tx.Rollback()
return &errors.FailedToDeleteClub
}

if result := tx.Delete(&models.Club{}, id); result.RowsAffected == 0 {
tx.Rollback()
if result.Error == nil {
return &errors.ClubNotFound
} else {
return &errors.FailedToDeleteClub
}
}

if err := tx.Commit().Error; err != nil {
tx.Rollback()
return &errors.FailedToDeleteClub
}

return nil
}

0 comments on commit bec6c1b

Please sign in to comment.