Skip to content

Commit

Permalink
Merge pull request #4 from isd-sgcu/selection
Browse files Browse the repository at this point in the history
Selection service
  • Loading branch information
flagrantii authored Jul 3, 2024
2 parents 77cc5c4 + 22bfcf9 commit 8393e41
Show file tree
Hide file tree
Showing 11 changed files with 644 additions and 52 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ mock-gen:
mockgen -source ./internal/pin/pin.utils.go -destination ./mocks/pin/pin.utils.go
mockgen -source ./internal/stamp/stamp.repository.go -destination ./mocks/stamp/stamp.repository.go
mockgen -source ./internal/stamp/stamp.service.go -destination ./mocks/stamp/stamp.service.go
mockgen -source ./internal/selection/selection.repository.go -destination ./mocks/selection/selection.repository.go
mockgen -source ./internal/selection/selection.service.go -destination ./mocks/selection/selection.service.go

test:
go vet ./...
Expand Down
15 changes: 13 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,15 @@ import (
"github.com/isd-sgcu/rpkm67-backend/config"
"github.com/isd-sgcu/rpkm67-backend/constant"
"github.com/isd-sgcu/rpkm67-backend/database"
"github.com/isd-sgcu/rpkm67-backend/internal/cache"
"github.com/isd-sgcu/rpkm67-backend/internal/group"
"github.com/isd-sgcu/rpkm67-backend/internal/pin"
"github.com/isd-sgcu/rpkm67-backend/internal/selection"
"github.com/isd-sgcu/rpkm67-backend/internal/stamp"
"github.com/isd-sgcu/rpkm67-backend/logger"
groupProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/group/v1"
pinProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/pin/v1"
selectionProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/selection/v1"
stampProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/stamp/v1"
"go.uber.org/zap"
"google.golang.org/grpc"
Expand All @@ -43,7 +48,7 @@ func main() {
panic(fmt.Sprintf("Failed to connect to redis: %v", err))
}

// cacheRepo := cache.NewRepository(redis)
cacheRepo := cache.NewRepository(redis)

pinRepo := pin.NewRepository(redis)
pinUtils := pin.NewUtils()
Expand All @@ -52,7 +57,11 @@ func main() {
stampRepo := stamp.NewRepository(db)
stampSvc := stamp.NewService(stampRepo, constant.ActivityIdToIdx, logger.Named("stampSvc"))

// selectionRepo := selection.NewRepository(db)
groupRepo := group.NewRepository(db)
groupSvc := group.NewService(groupRepo, cacheRepo, logger.Named("groupSvc"))

selectionRepo := selection.NewRepository(db)
selectionSvc := selection.NewService(selectionRepo, cacheRepo, logger.Named("selectionSvc"))

listener, err := net.Listen("tcp", fmt.Sprintf(":%v", conf.App.Port))
if err != nil {
Expand All @@ -63,6 +72,8 @@ func main() {
grpc_health_v1.RegisterHealthServer(grpcServer, health.NewServer())
pinProto.RegisterPinServiceServer(grpcServer, pinSvc)
stampProto.RegisterStampServiceServer(grpcServer, stampSvc)
groupProto.RegisterGroupServiceServer(grpcServer, groupSvc)
selectionProto.RegisterSelectionServiceServer(grpcServer, selectionSvc)

reflection.Register(grpcServer)
go func() {
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@ go 1.22.4

require (
github.com/golang/mock v1.6.0
github.com/isd-sgcu/rpkm67-go-proto v0.2.5
github.com/google/uuid v1.6.0
github.com/isd-sgcu/rpkm67-go-proto v0.2.8
github.com/isd-sgcu/rpkm67-model v0.0.6
github.com/joho/godotenv v1.5.1
github.com/redis/go-redis/v9 v9.5.3
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
google.golang.org/grpc v1.64.0
google.golang.org/grpc v1.65.0
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.10
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.5 // indirect
Expand All @@ -31,11 +30,12 @@ require (
github.com/rogpeppe/go-internal v1.12.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
36 changes: 10 additions & 26 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -15,24 +15,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
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.1.6 h1:6mubghe7HuGJYv+hgpIxJBBrmVk5UdjHjdvzykLLhQ0=
github.com/isd-sgcu/rpkm67-go-proto v0.1.6/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/isd-sgcu/rpkm67-go-proto v0.1.7 h1:FO8B4A7iuVS8PM4cEXe3HxZxlqdDY8va1zxy7TAnkqY=
github.com/isd-sgcu/rpkm67-go-proto v0.1.7/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/isd-sgcu/rpkm67-go-proto v0.1.9 h1:UqCu7uucb6o6NgmGEKbc6In0hBR5HDtvn+FiJ1mipVw=
github.com/isd-sgcu/rpkm67-go-proto v0.1.9/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/isd-sgcu/rpkm67-go-proto v0.2.0 h1:tPfNgCuqS4g0f+2hzcpY+8hYXSa7DZDPvRejRzOk2cI=
github.com/isd-sgcu/rpkm67-go-proto v0.2.0/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/isd-sgcu/rpkm67-go-proto v0.2.3 h1:u4ROlwsTmzcXgW3ED2UobPvF1OF+jykHH9AEH8F3XcU=
github.com/isd-sgcu/rpkm67-go-proto v0.2.3/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/isd-sgcu/rpkm67-go-proto v0.2.5 h1:lNpRUnPr6QiLBnexeCM2MvlNHeUlJ/jbKdrYWj1/qtk=
github.com/isd-sgcu/rpkm67-go-proto v0.2.5/go.mod h1:Z5SYz5kEe4W+MdqPouF0zEOiaqvg+s9I1S5d0q6e+Jw=
github.com/isd-sgcu/rpkm67-model v0.0.1 h1:LFn7jaawkZP1golE9B32a2KL/U/w20UFjQo2Cd/3Fhc=
github.com/isd-sgcu/rpkm67-model v0.0.1/go.mod h1:dxgLSkrFpbQOXsrzqgepZoEOyZUIG2LBGtm5gsuBbVc=
github.com/isd-sgcu/rpkm67-model v0.0.4 h1:tk6z6pXnhWBoG2SaSIoyLxNnwRaXwdbSIEEa/cSi8EY=
github.com/isd-sgcu/rpkm67-model v0.0.4/go.mod h1:dxgLSkrFpbQOXsrzqgepZoEOyZUIG2LBGtm5gsuBbVc=
github.com/isd-sgcu/rpkm67-model v0.0.5 h1:S+uza3ps1CP+JzgGILg6WwqlFJFaBsOIsuNBiJWJug8=
github.com/isd-sgcu/rpkm67-model v0.0.5/go.mod h1:dxgLSkrFpbQOXsrzqgepZoEOyZUIG2LBGtm5gsuBbVc=
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-model v0.0.6 h1:pYlqOmeXGQIfHdOhyAta4kXkqnoLc4X3KWcAjPrAuds=
github.com/isd-sgcu/rpkm67-model v0.0.6/go.mod h1:dxgLSkrFpbQOXsrzqgepZoEOyZUIG2LBGtm5gsuBbVc=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
Expand Down Expand Up @@ -81,8 +65,8 @@ golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
Expand All @@ -105,10 +89,10 @@ golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237 h1:NnYq6UN9ReLM9/Y01KWNOWyI5xQ9kbIms5GGJVwS/Yc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240318140521-94a12d6c2237/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY=
google.golang.org/grpc v1.64.0 h1:KH3VH9y/MgNQg1dE7b3XfVK0GsPSIzJwdF617gUSbvY=
google.golang.org/grpc v1.64.0/go.mod h1:oxjF8E3FBnjp+/gVFYdWacaLDx9na1aqy9oovLpxQYg=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157 h1:Zy9XzmMEflZ/MAaA7vNcoebnRAld7FsPW1EeBB7V0m8=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240528184218-531527333157/go.mod h1:EfXuqaE1J41VCDicxHzUDm+8rk+7ZdXzHV0IhO/I6s0=
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
22 changes: 22 additions & 0 deletions internal/group/group.repository.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package group

import (
"context"
"time"

"github.com/google/uuid"
"github.com/isd-sgcu/rpkm67-model/model"
"gorm.io/gorm"
)

type Repository interface {
FindOne(userId uuid.UUID) (*model.Group, error)
}

type repositoryImpl struct {
Expand All @@ -16,3 +22,19 @@ func NewRepository(db *gorm.DB) Repository {
Db: db,
}
}

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").
Joins("JOIN users ON users.group_id = groups.id").
Where("users.id = ?", userId).
First(&group).Error; err != nil {
return nil, err
}

return &group, nil
}
75 changes: 68 additions & 7 deletions internal/group/group.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package group

import (
"context"
"fmt"

"github.com/google/uuid"
"github.com/isd-sgcu/rpkm67-backend/internal/cache"
proto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/group/v1"
"go.uber.org/zap"
)
Expand All @@ -13,17 +16,75 @@ type Service interface {

type serviceImpl struct {
proto.UnimplementedGroupServiceServer
repo Repository
log *zap.Logger
repo Repository
cache cache.Repository
log *zap.Logger
}

func NewService(repo Repository, log *zap.Logger) Service {
func NewService(repo Repository, cache cache.Repository, log *zap.Logger) Service {
return &serviceImpl{
repo: repo,
log: log,
repo: repo,
cache: cache,
log: log,
}
}

func (s *serviceImpl) FindOne(_ context.Context, in *proto.FindOneGroupRequest) (res *proto.FindOneGroupResponse, err error) {
return nil, nil
func (s *serviceImpl) FindOne(ctx context.Context, in *proto.FindOneGroupRequest) (*proto.FindOneGroupResponse, error) {
cacheKey := fmt.Sprintf("group:%s", in.UserId)
var cachedGroup proto.Group

// try to retreive from cache
err := s.cache.GetValue(cacheKey, &cachedGroup)
if err == nil {
s.log.Info("Group found in cache", zap.String("user_id", in.UserId))
return &proto.FindOneGroupResponse{Group: &cachedGroup}, nil
}

userUUID, err := uuid.Parse(in.UserId)
if err != nil {
return nil, fmt.Errorf("invalid UUID format: %v", err)
}

// if not found cache, find group in database
group, err := s.repo.FindOne(userUUID)
if err != nil {
s.log.Error("Failed to find group", zap.String("user_id", in.UserId), zap.Error(err))
return nil, err
}

userInfo := make([]*proto.UserInfo, 0, len(group.Members))
for _, m := range group.Members {
user := proto.UserInfo{
Id: m.ID.String(),
Firstname: m.Firstname,
Lastname: m.Lastname,
ImageUrl: m.PhotoUrl,
}
userInfo = append(userInfo, &user)
}

groupRPC := proto.Group{
Id: group.ID.String(),
LeaderID: group.LeaderID,
Token: group.Token,
Members: userInfo,
IsConfirmed: group.IsConfirmed,
}

// set cache
if err := s.cache.SetValue(cacheKey, &groupRPC, 3600); err != nil { // cache นาน 1 ชั่วโมง
s.log.Warn("Failed to set group in cache", zap.String("user_id", in.UserId), zap.Error(err))
}

res := proto.FindOneGroupResponse{
Group: &groupRPC,
}

s.log.Info("FindOne group service completed",
zap.String("group_id", group.ID.String()),
zap.String("user_id", in.UserId),
zap.Int("member_count", len(userInfo)),
zap.Bool("from_cache", false))

return &res, nil
}
80 changes: 70 additions & 10 deletions internal/selection/selection.repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
type Repository interface {
Create(user *model.Selection) error
FindByGroupId(groupId string, selections *[]model.Selection) error
Delete(id string) error
CountGroupByBaanId() (map[string]int, error)
Delete(groupId string, baanId string) error
CountByBaanId() (map[string]int, error)
UpdateNewBaanExistOrder(updateSelection *model.Selection) error
UpdateExistBaanExistOrder(updateSelection *model.Selection) error
UpdateExistBaanNewOrder(updateSelection *model.Selection) error
}

type repositoryImpl struct {
Expand All @@ -27,26 +30,83 @@ func (r *repositoryImpl) Create(user *model.Selection) error {
}

func (r *repositoryImpl) FindByGroupId(groupId string, selections *[]model.Selection) error {
return r.Db.Find(selections, "groupId = ?", groupId).Error
return r.Db.Find(selections, "group_id = ?", groupId).Error
}

func (r *repositoryImpl) Delete(id string) error {
return r.Db.Delete(&model.Selection{}, "id = ?", id).Error
func (r *repositoryImpl) Delete(groupId string, baanId string) error {
return r.Db.Delete(&model.Selection{}, "group_id = ? AND baan = ?", groupId, baanId).Error
}

func (r *repositoryImpl) CountGroupByBaanId() (map[string]int, error) {
func (r *repositoryImpl) CountByBaanId() (map[string]int, error) {
var result []struct {
BaanId string
Count int
Baan string
Count int
}
if err := r.Db.Model(&model.Selection{}).Select("baan_id, count(*) as count").Group("baan_id").Scan(&result).Error; err != nil {
if err := r.Db.Model(&model.Selection{}).Select("baan, count(*) as count").Group("baan").Scan(&result).Error; err != nil {
return nil, err
}

count := make(map[string]int)
for _, v := range result {
count[v.BaanId] = v.Count
count[v.Baan] = v.Count
}

return count, nil
}

func (r *repositoryImpl) UpdateNewBaanExistOrder(updateSelection *model.Selection) error {
return r.Db.Transaction(func(tx *gorm.DB) error {
var existingSelection model.Selection
if err := tx.Where(`group_id = ? AND "order" = ?`, updateSelection.GroupID, updateSelection.Order).First(&existingSelection).Error; err != nil {
return err
}

if err := tx.Where(`"order" = ? AND group_id = ?`, updateSelection.Order, updateSelection.GroupID).Model(&existingSelection).Update("baan", updateSelection.Baan).Error; err != nil {
return err
}

return nil
})
}

func (r *repositoryImpl) UpdateExistBaanExistOrder(updateSelection *model.Selection) error {
return r.Db.Transaction(func(tx *gorm.DB) error {
var existingBaanSelection model.Selection
if err := tx.Where("group_id = ? AND baan = ?", updateSelection.GroupID, updateSelection.Baan).First(&existingBaanSelection).Error; err != nil {
return err
}

var existingOrderSelection model.Selection
if err := tx.Where(`group_id = ? AND "order" = ?`, updateSelection.GroupID, updateSelection.Order).First(&existingOrderSelection).Error; err != nil {
return err
}

if existingBaanSelection.Order == updateSelection.Order {
return nil
}

if err := tx.Where(`"order" = ? AND group_id = ?`, existingBaanSelection.Order, updateSelection.GroupID).Model(&existingBaanSelection).Update("baan", existingOrderSelection.Baan).Error; err != nil {
return err
}
if err := tx.Where(`"order" = ? AND group_id = ?`, existingOrderSelection.Order, updateSelection.GroupID).Model(&existingOrderSelection).Update("baan", updateSelection.Baan).Error; err != nil {
return err
}

return nil
})
}

func (r *repositoryImpl) UpdateExistBaanNewOrder(updateSelection *model.Selection) error {
return r.Db.Transaction(func(tx *gorm.DB) error {
var existingSelection model.Selection
if err := tx.Where("group_id = ? AND baan = ?", updateSelection.GroupID, updateSelection.Baan).First(&existingSelection).Error; err != nil {
return err
}

if err := tx.Where("baan = ? AND group_id = ?", updateSelection.Baan, updateSelection.GroupID).Model(&existingSelection).Update("order", updateSelection.Order).Error; err != nil {
return err
}

return nil
})
}
Loading

0 comments on commit 8393e41

Please sign in to comment.