2024-08-01 23:02:55 +02:00
|
|
|
package db
|
|
|
|
|
2024-08-03 09:44:06 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
2024-08-01 23:02:55 +02:00
|
|
|
|
2024-08-03 07:05:46 +02:00
|
|
|
// Card can be either card or wallet
|
|
|
|
type Card struct {
|
2024-08-01 23:02:55 +02:00
|
|
|
gorm.Model
|
|
|
|
Name string
|
|
|
|
Value uint64
|
|
|
|
HaveCreditLine bool
|
|
|
|
CreditLine uint64
|
2024-08-03 07:06:46 +02:00
|
|
|
UserID uint
|
|
|
|
User *User
|
2024-08-01 23:02:55 +02:00
|
|
|
}
|
2024-08-03 09:44:06 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
ERROR_CARD_NAME_EMPTY = errors.New("Card's name can't be empty")
|
|
|
|
ERROR_CARD_NAME_NOT_UNIQUE = errors.New("Card's name have to be unique for user")
|
|
|
|
)
|
|
|
|
|
|
|
|
func (c *Card) BeforeSave(tx *gorm.DB) error {
|
|
|
|
if c.Name == "" {
|
|
|
|
return ERROR_CARD_NAME_EMPTY
|
|
|
|
}
|
|
|
|
|
|
|
|
var dup Card
|
2024-08-03 09:45:09 +02:00
|
|
|
if err := tx.Find(&dup, Card{Name: c.Name, UserID: c.UserID}).Error; err != nil {
|
2024-08-03 09:44:06 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if dup.ID != 0 {
|
|
|
|
return ERROR_CARD_NAME_NOT_UNIQUE
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|