Skip to content

Commit

Permalink
Change field for Swiperecord
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuflatland-lf committed Aug 18, 2024
1 parent c458c5e commit 217603f
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ CREATE TABLE IF NOT EXISTS swipe_records
user_id BIGINT NOT NULL,
card_id BIGINT NOT NULL,
cardgroup_id BIGINT NOT NULL,
direction TEXT NOT NULL,
mode INT DEFAULT 1,
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE,
Expand All @@ -91,6 +91,7 @@ CREATE TABLE IF NOT EXISTS swipe_records
CREATE INDEX idx_swipe_records_id ON swipe_records(id);
CREATE INDEX idx_swipe_records_user_id ON swipe_records(user_id);
CREATE INDEX idx_swipe_records_card_id ON swipe_records(card_id);
CREATE INDEX idx_swipe_records_cardgroup_id ON swipe_records(cardgroup_id);

-- +goose statementbegin
-- When the updated column is not changed, assign NULL
Expand Down Expand Up @@ -174,9 +175,9 @@ DROP FUNCTION IF EXISTS trg_update_timestamp_none();
DROP FUNCTION IF EXISTS trg_update_timestamp_same();
DROP FUNCTION IF EXISTS trg_update_timestamp_current();

DROP TABLE IF EXISTS swipe_records;
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;
Expand Down
9 changes: 0 additions & 9 deletions backend/graph/db/card.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/go-playground/validator/v10"
"github.com/m-mizutani/goerr"
"gorm.io/gorm"
"time"
)

// BeforeCreate hook to validate the front and back fields
Expand Down Expand Up @@ -45,11 +44,3 @@ func (c *Card) validateAtCreate(card *Card) error {

return nil
}

// AfterUpdate Updating data in same transaction
func (c *Card) AfterUpdate(tx *gorm.DB) (err error) {

tx.Model(&Card{}).Where("id = ?", c.ID).Update("updated", time.Now().UTC())

return nil
}
7 changes: 0 additions & 7 deletions backend/graph/db/cardgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/go-playground/validator/v10"
"github.com/m-mizutani/goerr"
"gorm.io/gorm"
"time"
)

// BeforeCreate hook to validate the Cardgroup before creating
Expand Down Expand Up @@ -42,9 +41,3 @@ func (cg *Cardgroup) validateAtCreate(cardgroup *Cardgroup) error {
}
return nil
}

// Updating data in the same transaction
func (cg *Cardgroup) AfterUpdate(tx *gorm.DB) (err error) {
tx.Model(&Cardgroup{}).Where("id = ?", cg.ID).Update("updated", time.Now().UTC())
return nil
}
2 changes: 1 addition & 1 deletion backend/graph/db/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type SwipeRecord struct {
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"`
Mode int `gorm:"column:mode;default:1;not null" validate:"gte=0"`
Created time.Time `gorm:"column:created;autoCreateTime"`
Updated time.Time `gorm:"column:updated;autoCreateTime"`
}
7 changes: 0 additions & 7 deletions backend/graph/db/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/go-playground/validator/v10"
"github.com/m-mizutani/goerr"
"gorm.io/gorm"
"time"
)

// BeforeCreate hook to validate the Role before creating
Expand Down Expand Up @@ -40,9 +39,3 @@ func (r *Role) validateAtCreate(role *Role) error {
}
return nil
}

// Updating data in same transaction
func (r *Role) AfterUpdate(tx *gorm.DB) (err error) {
tx.Model(&Role{}).Where("id = ?", r.ID).Update("updated", time.Now().UTC())
return nil
}
14 changes: 6 additions & 8 deletions backend/graph/db/swiperecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/go-playground/validator/v10"
"github.com/m-mizutani/goerr"
"gorm.io/gorm"
"time"
)

// BeforeCreate hook to validate the SwipeRecord fields
Expand Down Expand Up @@ -40,16 +39,15 @@ func (s *SwipeRecord) validateAtCreate(swipeRecord *SwipeRecord) error {
return goerr.Wrap(err, fmt.Sprintf("Field validation for 'user_id' failed %+v", err))
}

err = v.Validator().Var(swipeRecord.Direction, "required,oneof=left right up down") // Assuming 'Direction' has specific allowed values
err = v.Validator().Var(swipeRecord.CardID, "required")
if err != nil {
return goerr.Wrap(err, fmt.Sprintf("Field validation for 'direction' failed %+v", err))
return goerr.Wrap(err, fmt.Sprintf("Field validation for 'card_id' failed %+v", err))
}

return nil
}
err = v.Validator().Var(swipeRecord.CardGroupID, "required")
if err != nil {
return goerr.Wrap(err, fmt.Sprintf("Field validation for 'cardgroup_id' failed %+v", err))
}

// AfterUpdate hook to update the timestamp in the same transaction
func (s *SwipeRecord) AfterUpdate(tx *gorm.DB) (err error) {
tx.Model(&SwipeRecord{}).Where("id = ?", s.ID).Update("updated", time.Now().UTC())
return nil
}
7 changes: 0 additions & 7 deletions backend/graph/db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/go-playground/validator/v10"
"github.com/m-mizutani/goerr"
"gorm.io/gorm"
"time"
)

func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
Expand Down Expand Up @@ -39,9 +38,3 @@ func (u *User) validateAtCreate(user *User) error {
}
return nil
}

// AfterUpdate Updating data in same transaction
func (u *User) AfterUpdate(tx *gorm.DB) (err error) {
tx.Model(&User{}).Where("id = ?", u.ID).Update("updated", time.Now().UTC())
return nil
}
62 changes: 31 additions & 31 deletions backend/graph/generated.go

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

4 changes: 2 additions & 2 deletions backend/graph/model/models_gen.go

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

4 changes: 2 additions & 2 deletions backend/graph/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type SwipeRecord {
userId: ID!
cardId: ID!
cardGroupID: ID!
direction: String! @validation(format: "required")
mode: Int! @validation(format: "gte=0")
created: Time!
updated: Time!
}
Expand Down Expand Up @@ -154,7 +154,7 @@ input NewSwipeRecord {
userId: ID! @validation(format: "required")
cardId: ID! @validation(format: "required")
cardGroupID: ID! @validation(format: "required")
direction: String! @validation(format: "required")
mode: Int! @validation(format: "gte=0")
created: Time!
updated: Time!
}
Expand Down
12 changes: 3 additions & 9 deletions backend/graph/services/swiperecord.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ type swipeRecordService struct {
defaultLimit int
}

const (
KNOWN = "known"
DONTKNOW = "dontknow"
MAYBE = "maybe"
)

type SwipeRecordService interface {
GetSwipeRecordByID(ctx context.Context, id int64) (*model.SwipeRecord, error)
CreateSwipeRecord(ctx context.Context, input model.NewSwipeRecord) (*model.SwipeRecord, error)
Expand All @@ -45,7 +39,7 @@ func ConvertToGormSwipeRecord(input model.NewSwipeRecord) *repository.SwipeRecor
UserID: input.UserID,
CardID: input.CardID,
CardGroupID: input.CardGroupID,
Direction: input.Direction,
Mode: input.Mode,
Created: input.Created,
Updated: input.Updated,
}
Expand All @@ -57,7 +51,7 @@ func ConvertToSwipeRecord(swipeRecord repository.SwipeRecord) *model.SwipeRecord
UserID: swipeRecord.UserID,
CardID: swipeRecord.CardID,
CardGroupID: swipeRecord.CardGroupID,
Direction: swipeRecord.Direction,
Mode: swipeRecord.Mode,
Created: swipeRecord.Created,
Updated: swipeRecord.Updated,
}
Expand Down Expand Up @@ -91,7 +85,7 @@ func (s *swipeRecordService) UpdateSwipeRecord(ctx context.Context, id int64, in
if err := s.db.WithContext(ctx).First(&swipeRecord, id).Error; err != nil {
return nil, goerr.Wrap(fmt.Errorf("swipe record does not exist: id=%d", id), err)
}
swipeRecord.Direction = input.Direction
swipeRecord.Mode = input.Mode
swipeRecord.Updated = time.Now().UTC()

if err := s.db.WithContext(ctx).Save(&swipeRecord).Error; err != nil {
Expand Down
Loading

0 comments on commit 217603f

Please sign in to comment.