Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/basic card deck operations #27

Merged
merged 14 commits into from
May 19, 2023
23 changes: 12 additions & 11 deletions backend/pkg/helper/publicID.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import (
"gorm.io/gorm"
)

var (
random = *rand.New(rand.NewSource(time.Now().UnixNano()))
const (
charset = "abcdefghijklmnopqrstuvwxyz0123456789"
4KevR marked this conversation as resolved.
Show resolved Hide resolved
)

var (
random = rand.New(rand.NewSource(time.Now().UnixNano()))
)

var ErrRetryCountExceeded = errors.New("exceeded retry count")

type PublicID struct {
Expand All @@ -33,25 +36,23 @@ func (i PublicID) GetStringRepresentation() string {
return fmt.Sprintf("%c-%s", i.prefix, i.id)
}

func FindFreePublicID[T any](
func FindFreePublicID[T, C any](
db *gorm.DB,
retries int,
generator func(prefix rune) PublicID,
prefix rune,
where func(candidate string) *T,
) (res string, err error) {
where func() (C, *T),
) (res C, err error) {
var currentTry int
var t T
for {
currentTry++
if currentTry > retries {
err = errors.New("exceeded retry count")
err = ErrRetryCountExceeded
return
}
candidate := generator(prefix)
if err = db.Where(where(candidate.GetStringRepresentation())).First(&t).Error; err != nil {
candidate, val := where()
4KevR marked this conversation as resolved.
Show resolved Hide resolved
if err = db.Where(val).First(&t).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return candidate.GetStringRepresentation(), nil
return candidate, nil
}
return
}
Expand Down
6 changes: 4 additions & 2 deletions backend/pkg/model/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ type Card struct {
}

func (c *Card) BeforeCreate(db *gorm.DB) (err error) {
c.PublicID, err = helper.FindFreePublicID(db, 10, helper.GeneratePublicID, 'C', func(candidate string) *Card {
return &Card{PublicID: candidate}
newPublicID, err := helper.FindFreePublicID(db, 10, func() (helper.PublicID, *Card) {
id := helper.GeneratePublicID('C')
return id, &Card{PublicID: id.GetStringRepresentation()}
})
c.PublicID = newPublicID.GetStringRepresentation()
return
}
6 changes: 4 additions & 2 deletions backend/pkg/model/deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ type Deck struct {
}

func (d *Deck) BeforeCreate(db *gorm.DB) (err error) {
d.PublicID, err = helper.FindFreePublicID(db, 10, helper.GeneratePublicID, 'D', func(candidate string) *Deck {
return &Deck{PublicID: candidate}
newPublicID, err := helper.FindFreePublicID(db, 10, func() (helper.PublicID, *Deck) {
id := helper.GeneratePublicID('D')
return id, &Deck{PublicID: id.GetStringRepresentation()}
})
d.PublicID = newPublicID.GetStringRepresentation()
return
}
6 changes: 4 additions & 2 deletions backend/pkg/model/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ type Group struct {
}

func (g *Group) BeforeCreate(db *gorm.DB) (err error) {
g.PublicID, err = helper.FindFreePublicID(db, 10, helper.GeneratePublicID, 'G', func(candidate string) *Group {
return &Group{PublicID: candidate}
newPublicID, err := helper.FindFreePublicID(db, 10, func() (helper.PublicID, *Group) {
id := helper.GeneratePublicID('G')
return id, &Group{PublicID: id.GetStringRepresentation()}
})
g.PublicID = newPublicID.GetStringRepresentation()
return
}
12 changes: 11 additions & 1 deletion backend/services/carddeck/handler/carddeck.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,15 @@ func (e *CardDeck) CreateCard(ctx context.Context, req *pb.CreateCardRequest, rs
if err != nil {
TomRomeo marked this conversation as resolved.
Show resolved Hide resolved
return err
}
roleRsp, err := e.collaborationService.GetGroupUserRole(context.TODO(), &pbcollab.GroupRequest{UserID: req.UserID, GroupPublicID: deck.Group.PublicID})
if err != nil {
return err
}
if roleRsp.GroupRole != pbcollab.GroupRole_ADMIN && roleRsp.GroupRole != pbcollab.GroupRole_WRITE {
return err
4KevR marked this conversation as resolved.
Show resolved Hide resolved
}
newCard := model.Card{
DeckID: deck.GroupID,
DeckID: deck.ID,
Frontside: req.Frontside,
Backside: req.Backside,
}
Expand All @@ -46,6 +53,9 @@ func (e *CardDeck) CreateDeck(ctx context.Context, req *pb.CreateDeckRequest, rs
if err != nil {
return err
}
TomRomeo marked this conversation as resolved.
Show resolved Hide resolved
if roleRsp.GroupRole != pbcollab.GroupRole_ADMIN && roleRsp.GroupRole != pbcollab.GroupRole_WRITE {
return err
4KevR marked this conversation as resolved.
Show resolved Hide resolved
}
newDeck := model.Deck{
Name: req.DeckName,
CreatedAt: time.Now(),
Expand Down
2 changes: 1 addition & 1 deletion backend/services/carddeck/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {

// Create service
srv := micro.NewService(
micro.Server(grpcs.NewServer(server.Address(serviceAddress))),
micro.Server(grpcs.NewServer(server.Address(serviceAddress), server.Wait(nil))),
micro.Client(grpcc.NewClient()),
)
srv.Init(
Expand Down
8 changes: 4 additions & 4 deletions backend/services/collaboration/handler/collaboration.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ func (e *Collaboration) GetGroupUserRole(ctx context.Context, req *pb.GroupReque
return err
}
rsp.GroupID = uint64(group.ID)
if *role == model.RoleRead {
if role == model.RoleRead {
rsp.GroupRole = pb.GroupRole_READ
} else if role == model.RoleWrite {
rsp.GroupRole = pb.GroupRole_WRITE
TomRomeo marked this conversation as resolved.
Show resolved Hide resolved
} else if *role == model.RoleWrite {
rsp.GroupRole = pb.GroupRole_WRITE
} else if *role == model.RoleAdmin {
} else if role == model.RoleAdmin {
rsp.GroupRole = pb.GroupRole_ADMIN
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion backend/services/collaboration/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func main() {

// Create service
srv := micro.NewService(
micro.Server(grpcs.NewServer(server.Address(serviceAddress))),
micro.Server(grpcs.NewServer(server.Address(serviceAddress), server.Wait(nil))),
micro.Client(grpcc.NewClient()),
)
srv.Init(
Expand Down
2 changes: 1 addition & 1 deletion backend/services/frontend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {

// Create service
srv := micro.NewService(
micro.Server(grpcs.NewServer(server.Address(serviceAddress))),
micro.Server(grpcs.NewServer(server.Address(serviceAddress), server.Wait(nil))),
micro.Client(grpcc.NewClient()),
)
srv.Init(
Expand Down
2 changes: 1 addition & 1 deletion backend/services/user/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {

// Create service
srv := micro.NewService(
micro.Server(grpcs.NewServer(server.Address(serviceAddress))),
micro.Server(grpcs.NewServer(server.Address(serviceAddress), server.Wait(nil))),
micro.Client(grpcc.NewClient()),
)
srv.Init(
Expand Down
28 changes: 11 additions & 17 deletions backend/store/postgres.go
4KevR marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,15 @@ import (
"github.com/kioku-project/kioku/pkg/model"
)

type PostgresStore struct {
type GormStore struct {
db *gorm.DB
}

type UserStoreImpl struct {
PostgresStore
}
type UserStoreImpl GormStore

type CardDeckStoreImpl struct {
PostgresStore
}
type CardDeckStoreImpl GormStore

type CollaborationStoreImpl struct {
PostgresStore
}
type CollaborationStoreImpl GormStore

func NewPostgresStore() (*gorm.DB, error) {
_ = godotenv.Load("../.env", "../.env.example")
4KevR marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -48,7 +42,7 @@ func NewUserStore() (UserStore, error) {
if err != nil {
return nil, err
}
return &UserStoreImpl{PostgresStore: PostgresStore{db: db}}, nil
return &UserStoreImpl{db: db}, nil
}

func NewCardDeckStore() (CardDeckStore, error) {
Expand All @@ -60,7 +54,7 @@ func NewCardDeckStore() (CardDeckStore, error) {
if err != nil {
return nil, err
}
return &CardDeckStoreImpl{PostgresStore: PostgresStore{db: db}}, nil
return &CardDeckStoreImpl{db: db}, nil
}

func NewCollaborationStore() (CollaborationStore, error) {
Expand All @@ -80,7 +74,7 @@ func NewCollaborationStore() (CollaborationStore, error) {
if err != nil {
return nil, err
}
return &CollaborationStoreImpl{PostgresStore: PostgresStore{db: db}}, nil
return &CollaborationStoreImpl{db: db}, nil
}

func (s *UserStoreImpl) FindUserByEmail(email string) (user *model.User, err error) {
Expand Down Expand Up @@ -122,18 +116,18 @@ func (s *CollaborationStoreImpl) CreateNewGroupWithAdmin(adminUserID uint, newGr
return rsp.Error
}

func (s *PostgresStore) FindGroupByPublicID(publicID string) (group *model.Group, err error) {
func (s *CollaborationStoreImpl) FindGroupByPublicID(publicID string) (group *model.Group, err error) {
err = s.db.Where(model.Group{PublicID: publicID}).First(&group).Error
return
}

func (s *CollaborationStoreImpl) GetGroupUserRole(userID uint, groupID uint) (*model.RoleType, error) {
func (s *CollaborationStoreImpl) GetGroupUserRole(userID uint, groupID uint) (model.RoleType, error) {
var groupUser model.GroupUserRole
err := s.db.Where(model.GroupUserRole{GroupID: groupID, UserID: userID}).First(&groupUser).Error
if err != nil {
return nil, err
return "", err
}
return &groupUser.RoleType, nil
return groupUser.RoleType, nil
}

func (s *CollaborationStoreImpl) FindGroupsByUserID(userID uint) (groups []model.Group, err error) {
Expand Down
2 changes: 1 addition & 1 deletion backend/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ type CardDeckStore interface {
type CollaborationStore interface {
CreateNewGroupWithAdmin(adminUserID uint, newGroup *model.Group) error
FindGroupByPublicID(publicID string) (*model.Group, error)
GetGroupUserRole(userID uint, groupID uint) (*model.RoleType, error)
GetGroupUserRole(userID uint, groupID uint) (model.RoleType, error)
FindGroupsByUserID(userID uint) ([]model.Group, error)
}