Skip to content

Commit

Permalink
update naming + fix limit and offset (#324)
Browse files Browse the repository at this point in the history
  • Loading branch information
DOOduneye authored Mar 6, 2024
1 parent 8111e21 commit ec66f2e
Show file tree
Hide file tree
Showing 21 changed files with 60 additions and 54 deletions.
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
4 changes: 2 additions & 2 deletions backend/src/transactions/club.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ 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) {
user, err := GetUser(db, userId)
func CreateClub(db *gorm.DB, userID uuid.UUID, club models.Club) (*models.Club, *errors.Error) {
user, err := GetUser(db, userID)
if err != nil {
return nil, err
}
Expand Down
4 changes: 3 additions & 1 deletion backend/src/transactions/club_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"gorm.io/gorm"
)

func GetClubEvents(db *gorm.DB, clubID uuid.UUID, limit int, offset int) ([]models.Event, *errors.Error) {
func GetClubEvents(db *gorm.DB, clubID uuid.UUID, limit int, page int) ([]models.Event, *errors.Error) {
var events []models.Event

offset := (page - 1) * limit

if err := db.Where("club_id = ?", clubID).Limit(limit).Offset(offset).Find(&events).Error; err != nil {
return nil, &errors.FailedToGetEvents
}
Expand Down
4 changes: 3 additions & 1 deletion backend/src/transactions/club_follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func GetClubFollowers(db *gorm.DB, clubID uuid.UUID, limit int, page int) ([]mod

var users []models.User

if err := db.Model(&club).Association("Follower").Find(&users); err != nil {
offset := (page - 1) * limit

if err := db.Limit(limit).Offset(offset).Model(&club).Association("Follower").Find(&users); err != nil {
return nil, &errors.FailedToGetClubFollowers
}

Expand Down
4 changes: 3 additions & 1 deletion backend/src/transactions/club_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ func GetClubMembers(db *gorm.DB, clubID uuid.UUID, limit int, page int) ([]model

var users []models.User

if err := db.Model(&club).Association("Member").Find(&users); err != nil {
offset := (page - 1) * limit

if err := db.Limit(limit).Offset(offset).Model(&club).Association("Member").Find(&users); err != nil {
return nil, &errors.FailedToGetClubMembers
}

Expand Down
4 changes: 2 additions & 2 deletions backend/src/transactions/club_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func GetClubTags(db *gorm.DB, id uuid.UUID) ([]models.Tag, *errors.Error) {
return tags, nil
}

func DeleteClubTag(db *gorm.DB, id uuid.UUID, tagId uuid.UUID) *errors.Error {
func DeleteClubTag(db *gorm.DB, id uuid.UUID, tagID uuid.UUID) *errors.Error {
club, err := GetClub(db, id)
if err != nil {
return err
}

tag, err := GetTag(db, tagId)
tag, err := GetTag(db, tagID)
if err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion backend/src/transactions/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
"gorm.io/gorm"
)

func GetContacts(db *gorm.DB, limit int, offset int) ([]models.Contact, *errors.Error) {
func GetContacts(db *gorm.DB, limit int, page int) ([]models.Contact, *errors.Error) {
var contacts []models.Contact

offset := (page - 1) * limit

result := db.Limit(limit).Offset(offset).Find(&contacts)
if result.Error != nil {
return nil, &errors.FailedToGetContacts
Expand Down
4 changes: 3 additions & 1 deletion backend/src/transactions/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"gorm.io/gorm"
)

func GetEvents(db *gorm.DB, limit int, offset int) ([]models.Event, *errors.Error) {
func GetEvents(db *gorm.DB, limit int, page int) ([]models.Event, *errors.Error) {
var events []models.Event

offset := (page - 1) * limit

if db.Limit(limit).Offset(offset).Find(&events).Error != nil {
return nil, &errors.FailedToGetEvents
}
Expand Down
4 changes: 3 additions & 1 deletion backend/src/transactions/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ func GetTag(db *gorm.DB, tagID uuid.UUID) (*models.Tag, *errors.Error) {
return &tag, nil
}

func GetTags(db *gorm.DB, limit int, offset int) ([]models.Tag, *errors.Error) {
func GetTags(db *gorm.DB, limit int, page int) ([]models.Tag, *errors.Error) {
var tags []models.Tag

offset := (page - 1) * limit

if err := db.Limit(limit).Offset(offset).Find(&tags).Error; err != nil {
return nil, &errors.FailedToGetTags
}
Expand Down
4 changes: 3 additions & 1 deletion backend/src/transactions/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ func GetUserByEmail(db *gorm.DB, email string) (*models.User, *errors.Error) {
return &user, nil
}

func GetUsers(db *gorm.DB, limit int, offset int) ([]models.User, *errors.Error) {
func GetUsers(db *gorm.DB, limit int, page int) ([]models.User, *errors.Error) {
var users []models.User

offset := (page - 1) * limit

if err := db.Omit("password_hash").Limit(limit).Offset(offset).Find(&users).Error; err != nil {
return nil, &errors.FailedToGetUsers
}
Expand Down
16 changes: 8 additions & 8 deletions backend/src/transactions/user_follower.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"gorm.io/gorm"
)

func CreateFollowing(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error {
user, err := GetUser(db, userId)
func CreateFollowing(db *gorm.DB, userID uuid.UUID, clubID uuid.UUID) *errors.Error {
user, err := GetUser(db, userID)
if err != nil {
return err
}

club, err := GetClub(db, clubId)
club, err := GetClub(db, clubID)
if err != nil {
return err
}
Expand All @@ -25,13 +25,13 @@ func CreateFollowing(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Er
return nil
}

func DeleteFollowing(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error {
user, err := GetUser(db, userId)
func DeleteFollowing(db *gorm.DB, userID uuid.UUID, clubID uuid.UUID) *errors.Error {
user, err := GetUser(db, userID)
if err != nil {
return err
}

club, err := GetClub(db, clubId)
club, err := GetClub(db, clubID)
if err != nil {
return err
}
Expand All @@ -43,10 +43,10 @@ func DeleteFollowing(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Er
return nil
}

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

user, err := GetUser(db, userId)
user, err := GetUser(db, userID)
if err != nil {
return nil, err
}
Expand Down
22 changes: 11 additions & 11 deletions backend/src/transactions/user_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ import (
"gorm.io/gorm"
)

func CreateMember(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error {
user, err := GetUser(db, userId)
func CreateMember(db *gorm.DB, userID uuid.UUID, clubID uuid.UUID) *errors.Error {
user, err := GetUser(db, userID)
if err != nil {
return err
}

club, err := GetClub(db, clubId)
club, err := GetClub(db, clubID)
if err != nil {
return err
}

tx := db.Begin()

var count int64
if err := tx.Model(&models.Membership{}).Where("user_id = ? AND club_id = ?", userId, clubId).Count(&count).Error; err != nil {
if err := tx.Model(&models.Membership{}).Where("user_id = ? AND club_id = ?", userID, clubID).Count(&count).Error; err != nil {
return &errors.FailedToGetUserMemberships
}

Expand All @@ -34,7 +34,7 @@ func CreateMember(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error
return &errors.FailedToUpdateUser
}

if err := CreateFollowing(tx, userId, clubId); err != nil {
if err := CreateFollowing(tx, userID, clubID); err != nil {
tx.Rollback()
return err
}
Expand All @@ -51,13 +51,13 @@ func CreateMember(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error
return nil
}

func DeleteMember(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error {
user, err := GetUser(db, userId)
func DeleteMember(db *gorm.DB, userID uuid.UUID, clubID uuid.UUID) *errors.Error {
user, err := GetUser(db, userID)
if err != nil {
return err
}

club, err := GetClub(db, clubId)
club, err := GetClub(db, clubID)
if err != nil {
return err
}
Expand All @@ -69,7 +69,7 @@ func DeleteMember(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error
return &errors.FailedToUpdateUser
}

if err := DeleteFollowing(tx, userId, clubId); err != nil {
if err := DeleteFollowing(tx, userID, clubID); err != nil {
tx.Rollback()
return err
}
Expand All @@ -86,10 +86,10 @@ func DeleteMember(db *gorm.DB, userId uuid.UUID, clubId uuid.UUID) *errors.Error
return nil
}

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

user, err := GetUser(db, userId)
user, err := GetUser(db, userID)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/api/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestGetUsersWorksForSuper(t *testing.T) {
eaa.Assert.Equal(models.College("KCCS"), respUser.College)
eaa.Assert.Equal(models.Year(1), respUser.Year)

dbUsers, err := transactions.GetUsers(eaa.App.Conn, 1, 0)
dbUsers, err := transactions.GetUsers(eaa.App.Conn, 1, 1)

eaa.Assert.NilError(&err)

Expand Down

0 comments on commit ec66f2e

Please sign in to comment.