Skip to content

Commit

Permalink
Refactor Swipe Strategies
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Aug 17, 2024
1 parent 0a047eb commit cadf45f
Show file tree
Hide file tree
Showing 16 changed files with 408 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ CREATE TABLE IF NOT EXISTS cards
interval_days INT NOT NULL DEFAULT 1,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
cardgroup_id INTEGER NOT NULL,
FOREIGN KEY (cardgroup_id) REFERENCES cardgroups (id)
cardgroup_id BIGINT NOT NULL,
FOREIGN KEY (cardgroup_id) REFERENCES cardgroups (id) ON DELETE CASCADE
);
CREATE INDEX idx_cards_id ON cards(id);
CREATE INDEX idx_cards_front ON cards(front);
Expand Down Expand Up @@ -80,11 +80,13 @@ CREATE TABLE IF NOT EXISTS swipe_records
id BIGSERIAL PRIMARY KEY,
user_id BIGINT NOT NULL,
card_id BIGINT NOT NULL,
cardgroup_id BIGINT NOT NULL,
direction TEXT NOT NULL,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
FOREIGN KEY (card_id) REFERENCES cards (id) ON DELETE CASCADE
FOREIGN KEY (card_id) REFERENCES cards (id) ON DELETE CASCADE,
FOREIGN KEY (cardgroup_id) REFERENCES cardgroups (id) ON DELETE CASCADE
);
CREATE INDEX idx_swipe_records_id ON swipe_records(id);
CREATE INDEX idx_swipe_records_user_id ON swipe_records(user_id);
Expand Down Expand Up @@ -174,8 +176,8 @@ DROP FUNCTION IF EXISTS trg_update_timestamp_current();

DROP TABLE IF EXISTS user_roles;
DROP TABLE IF EXISTS roles;
DROP TABLE IF EXISTS swipe_records;
DROP TABLE IF EXISTS cardgroup_users;
DROP TABLE IF EXISTS cards;
DROP TABLE IF EXISTS cardgroups;
DROP TABLE IF EXISTS swipe_records;
DROP TABLE IF EXISTS users;
13 changes: 7 additions & 6 deletions backend/graph/db/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ type Role struct {
}

type SwipeRecord struct {
ID int64 `gorm:"column:id;primaryKey" validate:"number"`
UserID int64 `gorm:"column:user_id" validate:"number"`
CardID int64 `gorm:"column:card_id" validate:"number"`
Direction string `gorm:"column:direction;not null" validate:"required"`
Created time.Time `gorm:"column:created;autoCreateTime"`
Updated time.Time `gorm:"column:updated;autoCreateTime"`
ID int64 `gorm:"column:id;primaryKey" validate:"number"`
UserID int64 `gorm:"column:user_id" validate:"number"`
CardID int64 `gorm:"column:card_id" validate:"number"`
CardGroupID int64 `gorm:"column:cardgroup_id" validate:"number"`
Direction string `gorm:"column:direction;not null" validate:"required"`
Created time.Time `gorm:"column:created;autoCreateTime"`
Updated time.Time `gorm:"column:updated;autoCreateTime"`
}
95 changes: 88 additions & 7 deletions backend/graph/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 13 additions & 10 deletions backend/graph/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions backend/graph/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ type SwipeRecord {
id: ID!
userId: ID!
cardId: ID!
cardGroupID: ID!
direction: String! @validation(format: "required")
created: Time!
updated: Time!
Expand Down Expand Up @@ -151,6 +152,8 @@ input NewRole {

input NewSwipeRecord {
userId: ID! @validation(format: "required")
cardId: ID! @validation(format: "required")
cardGroupID: ID! @validation(format: "required")
direction: String! @validation(format: "required")
created: Time!
updated: Time!
Expand Down
32 changes: 0 additions & 32 deletions backend/pkg/usecases/swipe_manager/def1_state_strategy.go

This file was deleted.

32 changes: 0 additions & 32 deletions backend/pkg/usecases/swipe_manager/def2_state_strategy.go

This file was deleted.

30 changes: 30 additions & 0 deletions backend/pkg/usecases/swipe_manager/default_state_strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package swipe_manager

import (
repository "backend/graph/db"
"golang.org/x/net/context"
)

type defaultStateStrategy struct {
swipeManagerUsecase SwipeManagerUsecase
}

type DefaultStateStrategy interface {
SwipeStrategy
}

// NewDefaultStateStrategy creates a new instance of Def2StateStrategy.
func NewDefaultStateStrategy(swipeManagerUsecase SwipeManagerUsecase) DefaultStateStrategy {
return &defaultStateStrategy{
swipeManagerUsecase: swipeManagerUsecase,
}
}

func (d *defaultStateStrategy) ChangeState(ctx context.Context, latestSwipeRecord repository.SwipeRecord) error {
// Assuming userID is somehow available in the context or swipeRecords
return d.swipeManagerUsecase.ChangeState(ctx, latestSwipeRecord.CardGroupID, latestSwipeRecord.UserID, DEFAULT)
}

func (d *defaultStateStrategy) IsApplicable(ctx context.Context, latestSwipeRecord repository.SwipeRecord) bool {
return true
}
Loading

0 comments on commit cadf45f

Please sign in to comment.