Skip to content

Commit

Permalink
Merge pull request #7 from isd-sgcu/group-svc
Browse files Browse the repository at this point in the history
Group svc
  • Loading branch information
bookpanda authored Jul 6, 2024
2 parents f2fcd1a + 854705b commit 824902a
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 286 deletions.
3 changes: 3 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=5678

GROUP_CAPACITY=2
GROUP_CACHE_TTL=3600

PIN_WORKSHOP_CODE=workshop
PIN_WORKSHOP_COUNT=5
PIN_LANDMARK_CODE=landmark
Expand Down
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func main() {
stampSvc := stamp.NewService(stampRepo, constant.ActivityIdToIdx, logger.Named("stampSvc"))

groupRepo := group.NewRepository(db)
groupSvc := group.NewService(groupRepo, cacheRepo, logger.Named("groupSvc"))
groupSvc := group.NewService(groupRepo, cacheRepo, &conf.Group, logger.Named("groupSvc"))

selectionRepo := selection.NewRepository(db)
selectionSvc := selection.NewService(selectionRepo, cacheRepo, logger.Named("selectionSvc"))
Expand Down
21 changes: 21 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ type RedisConfig struct {
Password string
}

type GroupConfig struct {
Capacity int
CacheTTL int
}

type PinConfig struct {
WorkshopCode string
WorkshopCount int
Expand All @@ -32,6 +37,7 @@ type Config struct {
App AppConfig
Db DbConfig
Redis RedisConfig
Group GroupConfig
Pin PinConfig
}

Expand Down Expand Up @@ -72,6 +78,20 @@ func LoadConfig() (*Config, error) {
return nil, err
}

groupCapacity, err := strconv.ParseInt(os.Getenv("GROUP_CAPACITY"), 10, 64)
if err != nil {
return nil, err
}
groupCacheTTL, err := strconv.ParseInt(os.Getenv("GROUP_CACHE_TTL"), 10, 64)
if err != nil {
return nil, err
}

groupConfig := GroupConfig{
Capacity: int(groupCapacity),
CacheTTL: int(groupCacheTTL),
}

pinConfig := PinConfig{
WorkshopCode: os.Getenv("PIN_WORKSHOP_CODE"),
WorkshopCount: int(workshopCount),
Expand All @@ -83,6 +103,7 @@ func LoadConfig() (*Config, error) {
App: appConfig,
Db: dbConfig,
Redis: redisConfig,
Group: groupConfig,
Pin: pinConfig,
}, nil
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.4
require (
github.com/golang/mock v1.6.0
github.com/google/uuid v1.6.0
github.com/isd-sgcu/rpkm67-go-proto v0.2.8
github.com/isd-sgcu/rpkm67-go-proto v0.4.2
github.com/isd-sgcu/rpkm67-model v0.0.7
github.com/joho/godotenv v1.5.1
github.com/redis/go-redis/v9 v9.5.3
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/isd-sgcu/rpkm67-go-proto v0.2.8 h1:YDkxRcu204XD70E+xJSYt/4XmwXuM13nVNiEWflc73c=
github.com/isd-sgcu/rpkm67-go-proto v0.2.8/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-go-proto v0.2.9 h1:1bX29QfCTYJuNtxJ36RvRsXwd5qP+eXyFZyGi5K21Ck=
github.com/isd-sgcu/rpkm67-go-proto v0.2.9/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-go-proto v0.3.0 h1:/9cbmncyYGPqhDLfq6JUKTO44iz+8trn7H0D2IG52Y0=
github.com/isd-sgcu/rpkm67-go-proto v0.3.0/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-go-proto v0.3.1 h1:dARnz6iX9a1DYodCcQXcwmnLXXDbVOE4vyOmI2KZlxc=
github.com/isd-sgcu/rpkm67-go-proto v0.3.1/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-go-proto v0.4.0 h1:KIsBxT9MuUFsTGfvMYig3UUPf8nPSSxM6OB6kFIGDos=
github.com/isd-sgcu/rpkm67-go-proto v0.4.0/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-go-proto v0.4.1 h1:g2D8Oe3hazZoqsNHoogVge1oPoFLFmP6aJKgebcw8wk=
github.com/isd-sgcu/rpkm67-go-proto v0.4.1/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-go-proto v0.4.2 h1:MS7tvXNITdxm638ODEBU2yr6rV0eg99RhzCwRT99PR4=
github.com/isd-sgcu/rpkm67-go-proto v0.4.2/go.mod h1:w+UCeQnJ3wBuJ7Tyf8LiBiPZVb1KlecjMNCB7kBeL7M=
github.com/isd-sgcu/rpkm67-model v0.0.6 h1:pYlqOmeXGQIfHdOhyAta4kXkqnoLc4X3KWcAjPrAuds=
github.com/isd-sgcu/rpkm67-model v0.0.6/go.mod h1:dxgLSkrFpbQOXsrzqgepZoEOyZUIG2LBGtm5gsuBbVc=
github.com/isd-sgcu/rpkm67-model v0.0.7 h1:3b8gf1Ocg+Ky4xocKtCqVCB3rFDg90IgEXRwNmHt0OE=
Expand Down
87 changes: 28 additions & 59 deletions internal/group/group.repository.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package group

import (
"context"
"errors"
"time"

"github.com/google/uuid"
"github.com/isd-sgcu/rpkm67-model/model"
Expand All @@ -12,13 +10,13 @@ import (

type Repository interface {
WithTransaction(txFunc func(*gorm.DB) error) error
FindOne(userId *uuid.UUID) (*model.Group, error)
FindByToken(token string) (*model.Group, error)
Update(leaderUUID *uuid.UUID, group *model.Group) error
DeleteMemberFromGroupWithTX(ctx context.Context, tx *gorm.DB, userUUID, groupUUID uuid.UUID) error
CreateNewGroupWithTX(ctx context.Context, tx *gorm.DB, leaderId *uuid.UUID) (*model.Group, error)
JoinGroupWithTX(ctx context.Context, tx *gorm.DB, userUUID, groupUUID uuid.UUID) error
DeleteGroup(ctx context.Context, tx *gorm.DB, groupUUID uuid.UUID) error
FindByUserId(userId string, group *model.Group) error
FindByToken(token string, group *model.Group) error
Update(id string, group *model.Group) error
CreateTX(tx *gorm.DB, group *model.Group) error
MoveUserToNewGroupTX(tx *gorm.DB, userId string, groupId *uuid.UUID) error
JoinGroupTX(tx *gorm.DB, userId string, groupId *uuid.UUID) error
DeleteGroupTX(tx *gorm.DB, groupId *uuid.UUID) error
}

type repositoryImpl struct {
Expand Down Expand Up @@ -51,56 +49,39 @@ func (r *repositoryImpl) WithTransaction(txFunc func(*gorm.DB) error) error {
return tx.Commit().Error
}

func (r *repositoryImpl) FindOne(userId *uuid.UUID) (*model.Group, error) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

var group model.Group
if err := r.Db.WithContext(ctx).
Preload("Members").
func (r *repositoryImpl) FindByUserId(userId string, group *model.Group) error {
return r.Db.Preload("Members").Preload("Selections").
Where("id = (SELECT group_id FROM users WHERE id = ?)", userId).
First(&group).Error; err != nil {
return nil, err
}

return &group, nil
First(&group).Error
}

func (r *repositoryImpl) FindByToken(token string) (*model.Group, error) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

var group model.Group
if err := r.Db.WithContext(ctx).
Preload("Members").
func (r *repositoryImpl) FindByToken(token string, group *model.Group) error {
return r.Db.Preload("Members").
Joins("JOIN users ON users.id = groups.leader_id").
First(&group, "token = ?", token).Error; err != nil {
return nil, err
}

return &group, nil
First(&group, "token = ?", token).Error
}

func (r *repositoryImpl) Update(leaderUUID *uuid.UUID, group *model.Group) error {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()

result := r.Db.WithContext(ctx).Model(&model.Group{}).Where("leader_id = ?", leaderUUID).Update("is_confirmed", group.IsConfirmed)
func (r *repositoryImpl) Update(id string, group *model.Group) error {
result := r.Db.Model(&model.Group{}).Where("id = ?", id).Update("is_confirmed", group.IsConfirmed)
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return errors.New("no promotion found with the given ID")
return errors.New("no group found with the given id")
}

return nil
}

func (r *repositoryImpl) DeleteMemberFromGroupWithTX(ctx context.Context, tx *gorm.DB, userUUID, groupUUID uuid.UUID) error {
func (r *repositoryImpl) CreateTX(tx *gorm.DB, group *model.Group) error {
return tx.Create(&group).Error
}

func (r *repositoryImpl) MoveUserToNewGroupTX(tx *gorm.DB, userId string, groupId *uuid.UUID) error {
updateMap := map[string]interface{}{
"group_id": groupUUID,
"group_id": groupId,
}
result := r.Db.WithContext(ctx).Model(&model.User{}).Where("id = ?", userUUID).Updates(updateMap)
result := tx.Model(&model.User{}).Where("id = ?", userId).Updates(updateMap)
if result.Error != nil {
return result.Error
}
Expand All @@ -111,24 +92,12 @@ func (r *repositoryImpl) DeleteMemberFromGroupWithTX(ctx context.Context, tx *go
return nil
}

func (r *repositoryImpl) CreateNewGroupWithTX(ctx context.Context, tx *gorm.DB, leaderId *uuid.UUID) (*model.Group, error) {
group := model.Group{
LeaderID: leaderId,
}

if err := r.Db.WithContext(ctx).Create(&group).Error; err != nil {
return nil, err
}

return &group, nil
}

func (r *repositoryImpl) JoinGroupWithTX(ctx context.Context, tx *gorm.DB, userUUID, groupUUID uuid.UUID) error {
func (r *repositoryImpl) JoinGroupTX(tx *gorm.DB, userId string, groupId *uuid.UUID) error {
updateMap := map[string]interface{}{
"group_id": groupUUID,
"group_id": groupId,
}

result := r.Db.WithContext(ctx).Model(&model.User{}).Where("id = ?", userUUID).Updates(updateMap)
result := tx.Model(&model.User{}).Where("id = ?", userId).Updates(updateMap)
if result.Error != nil {
return result.Error
}
Expand All @@ -139,8 +108,8 @@ func (r *repositoryImpl) JoinGroupWithTX(ctx context.Context, tx *gorm.DB, userU
return nil
}

func (r *repositoryImpl) DeleteGroup(ctx context.Context, tx *gorm.DB, groupUUID uuid.UUID) error {
result := r.Db.Delete(&model.Group{}, "id = ?", groupUUID)
func (r *repositoryImpl) DeleteGroupTX(tx *gorm.DB, groupId *uuid.UUID) error {
result := tx.Delete(&model.Group{}, "id = ?", groupId)
if result.Error != nil {
return result.Error
}
Expand Down
Loading

0 comments on commit 824902a

Please sign in to comment.