Skip to content

Commit

Permalink
GML init clean up / refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Feb 10, 2024
1 parent 78bd4ea commit 47d824e
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 463 deletions.
11 changes: 8 additions & 3 deletions backend/src/controllers/club_follower.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package controllers

import (
"strconv"

"github.com/GenerateNU/sac/backend/src/services"
"github.com/gofiber/fiber/v2"
)
Expand All @@ -13,11 +15,14 @@ func NewClubFollowerController(clubFollowerService services.ClubFollowerServiceI
return &ClubFollowerController{clubFollowerService: clubFollowerService}
}

func (cf *ClubFollowerController) GetUserFollowingClubs(c *fiber.Ctx) error {
clubs, err := cf.clubFollowerService.GetUserFollowingClubs(c.Params("userID"))
func (cf *ClubFollowerController) GetClubFollowers(c *fiber.Ctx) error {
defaultLimit := 10
defaultPage := 1

followers, err := cf.clubFollowerService.GetClubFollowers(c.Params("clubID"), c.Query("limit", strconv.Itoa(defaultLimit)), c.Query("page", strconv.Itoa(defaultPage)))
if err != nil {
return err.FiberError(c)
}

return c.Status(fiber.StatusOK).JSON(&clubs)
return c.Status(fiber.StatusOK).JSON(followers)
}
11 changes: 0 additions & 11 deletions backend/src/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,3 @@ func newFiberApp() *fiber.App {

return app
}

func userFollowerRoutes(router fiber.Router, userFollowerService services.UserFollowerServiceInterface) {
userFollowerController := controllers.NewUserFollowerController(userFollowerService)

userFollower := router.Group("/:userID/follower")

userFollower.Put("/:clubID", userFollowerController.CreateFollowing)
userFollower.Delete("/:clubID", userFollowerController.DeleteFollowing)
userFollower.Get("/", userFollowerController.GetAllFollowing)
}

20 changes: 15 additions & 5 deletions backend/src/services/club_follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type ClubFollowerServiceInterface interface {
GetUserFollowingClubs(userID string) ([]models.Club, *errors.Error)
GetClubFollowers(clubID string, limit string, page string) ([]models.User, *errors.Error)
}

type ClubFollowerService struct {
Expand All @@ -22,11 +22,21 @@ func NewClubFollowerService(db *gorm.DB, validate *validator.Validate) *ClubFoll
return &ClubFollowerService{DB: db, Validate: validate}
}

func (cf *ClubFollowerService) GetUserFollowingClubs(userID string) ([]models.Club, *errors.Error) {
userIDAsUUID, err := utilities.ValidateID(userID)
func (cf *ClubFollowerService) GetClubFollowers(clubID string, limit string, page string) ([]models.User, *errors.Error) {
idAsUUID, err := utilities.ValidateID(clubID)
if err != nil {
return nil, err
return nil, &errors.FailedToValidateID
}

return transactions.GetClubFollowing(cf.DB, *userIDAsUUID)
limitAsInt, err := utilities.ValidateNonNegative(limit)
if err != nil {
return nil, &errors.FailedToValidateLimit
}

pageAsInt, err := utilities.ValidateNonNegative(page)
if err != nil {
return nil, &errors.FailedToValidatePage
}

return transactions.GetClubFollowers(cf.DB, *idAsUUID, *limitAsInt, *pageAsInt)
}
9 changes: 1 addition & 8 deletions backend/src/transactions/club_follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@ import (
"gorm.io/gorm"
)

func GetUserFollowingClubs(db *gorm.DB, club_id uuid.UUID) ([]models.User, *errors.Error) {
func GetClubFollowers(db *gorm.DB, clubID uuid.UUID, limit int, page int) ([]models.User, *errors.Error) {
var users []models.User
club, err := GetClub(db, club_id)
if err != nil {
return nil, &errors.ClubNotFound
}

if err := db.Model(&club).Association("Follower").Find(&users); err != nil {
return nil, &errors.FailedToGetClubFollowers
}
return users, nil
}
39 changes: 0 additions & 39 deletions backend/src/transactions/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,3 @@ func DeleteContact(db *gorm.DB, id uuid.UUID) *errors.Error {
}
return 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)
if result.Error != nil {
if stdliberrors.Is(result.Error, gorm.ErrRecordNotFound) {
return nil, &errors.UserNotFound
} else {
return nil, &errors.FailedToUpdateClub
}
}
var existingClub models.Club

err := db.First(&existingClub, id).Error
if err != nil {
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 {
return nil, &errors.FailedToUpdateUser
}

return &existingClub, nil
}

func DeleteClub(db *gorm.DB, id uuid.UUID) *errors.Error {
if result := db.Delete(&models.Club{}, id); result.RowsAffected == 0 {
if result.Error == nil {
return &errors.ClubNotFound
} else {
return &errors.FailedToDeleteClub
}
}

return nil
}
6 changes: 0 additions & 6 deletions backend/src/transactions/user_follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"gorm.io/gorm"
)

// Create following for a user
func CreateFollowing(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error {
user, err := GetUserWithFollowers(db, userId)
if err != nil {
Expand All @@ -24,7 +23,6 @@ func CreateFollowing(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Er
return nil
}

// Delete following for a user
func DeleteFollowing(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error {
user, err := GetUser(db, userId)
if err != nil {
Expand All @@ -34,16 +32,12 @@ func DeleteFollowing(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Er
if err != nil {
return &errors.ClubNotFound
}
// What to return here?
// Should we return User or Success message?
if err := db.Model(&user).Association("Follower").Delete(club); err != nil {
return &errors.FailedToUpdateUser
}
return nil
}

// Get all following for a user

func GetClubFollowing(db *gorm.DB, userId uuid.UUID) ([]models.Club, *errors.Error) {
var clubs []models.Club

Expand Down
97 changes: 0 additions & 97 deletions backend/tests/api/club_follower_test.go
Original file line number Diff line number Diff line change
@@ -1,98 +1 @@
package tests

import (
stdliberrors "errors"
"fmt"
"net/http"
"testing"

"github.com/GenerateNU/sac/backend/src/errors"
"github.com/GenerateNU/sac/backend/src/models"
"github.com/GenerateNU/sac/backend/src/transactions"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/huandu/go-assert"
"gorm.io/gorm"
)


func TestGetClubFollowersWorks(t *testing.T) {
appAssert, userUUID, clubUUID := CreateSampleClub(t, nil)

TestRequest{
Method: fiber.MethodPut,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, clubUUID),
}.TestOnStatus(t, &appAssert, fiber.StatusCreated)

TestRequest{
Method: fiber.MethodGet,
Path: fmt.Sprintf("/api/v1/clubs/%s/follower", clubUUID),
}.TestOnStatusAndDB(t, &appAssert,
DBTesterWithStatus{
Status: fiber.StatusCreated,
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) {
var dbClub *models.Club
err := app.Conn.Preload("Follower").First(dbClub, clubUUID).Error
assert.NilError(err)
assert.Equal(len(dbClub.Follower), 1)

var user *models.User
err = app.Conn.First(user, userUUID).Error
assert.NilError(err)

user, _ = transactions.GetUser(app.Conn, userUUID)
userFollower := &dbClub.Follower[0]
assert.Equal(userFollower, user)
},
},
)
appAssert.Close()
}


func TestGetClubFollowersFailsClubNotExist(t *testing.T) {
appAssert, userUUID, clubUUID := CreateSampleClub(t, nil)
clubUUIDNotExist := uuid.New()

TestRequest{
Method: fiber.MethodPut,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, clubUUID),
}.TestOnStatus(t, &appAssert, fiber.StatusCreated)

TestRequest{
Method: fiber.MethodGet,
Path: fmt.Sprintf("/api/v1/clubs/%s/follower", clubUUIDNotExist),
}.TestOnErrorAndDB(t, &appAssert,
ErrorWithDBTester{
Error: errors.ClubNotFound,
DBTester: func(app TestApp, assert *assert.A, resp *http.Response) {
var club models.Club
err := app.Conn.Where("id = ?", clubUUIDNotExist).First(&club).Error
assert.Assert(stdliberrors.Is(err, gorm.ErrRecordNotFound))
},
},
).Close()
}

func TestGetClubFollowersFailsClubIdBadRequest(t *testing.T) {
appAssert, userUUID, clubUUID := CreateSampleClub(t, nil)

TestRequest{
Method: fiber.MethodPut,
Path: fmt.Sprintf("/api/v1/users/%s/follower/%s", userUUID, clubUUID),
}.TestOnStatus(t, &appAssert, fiber.StatusCreated)

badRequests := []string{
"0",
"-1",
"1.1",
"foo",
"null",
}
for _, badRequest := range badRequests {
TestRequest{
Method: fiber.MethodGet,
Path: fmt.Sprintf("/api/v1/clubs/%s/follower", badRequest),
}.TestOnError(t, &appAssert, errors.FailedToValidateID).Close()
}
}
Loading

0 comments on commit 47d824e

Please sign in to comment.