Files
cardverse-be/internal/modules/cardmaster/model.go
2026-07-30 09:51:25 +07:00

162 lines
5.3 KiB
Go

package cardmaster
import (
"database/sql/driver"
"encoding/json"
"errors"
"time"
)
// JSONB merepresentasikan kolom JSONB di PostgreSQL
type JSONB json.RawMessage
func (j JSONB) Value() (driver.Value, error) {
if len(j) == 0 {
return nil, nil
}
return string(j), nil
}
func (j *JSONB) Scan(value interface{}) error {
if value == nil {
*j = nil
return nil
}
switch v := value.(type) {
case []byte:
*j = append((*j)[0:0], v...)
case string:
*j = JSONB(v)
default:
return errors.New("type assertion ke []byte atau string gagal")
}
return nil
}
func (j JSONB) MarshalJSON() ([]byte, error) {
if len(j) == 0 {
return []byte("null"), nil
}
return json.RawMessage(j).MarshalJSON()
}
func (j *JSONB) UnmarshalJSON(data []byte) error {
if j == nil {
return errors.New("JSONB: UnmarshalJSON pada nil pointer")
}
*j = append((*j)[0:0], data...)
return nil
}
// CardMaster merepresentasikan tabel "card_masters" di database
type CardMaster struct {
ID uint `json:"id" gorm:"primaryKey;autoIncrement"`
IDSet uint `json:"id_set" gorm:"column:id_set;not null;index"`
Name string `json:"name" gorm:"type:varchar(150);not null;index"`
Number string `json:"number" gorm:"type:varchar(50)"`
Image string `json:"image" gorm:"type:varchar(255)"`
Stage string `json:"stage,omitempty" gorm:"type:varchar(50)"`
Element string `json:"element,omitempty" gorm:"type:varchar(50);index"`
ElementImage string `json:"element_image,omitempty" gorm:"type:varchar(255)"`
EvolvesFrom string `json:"evolves_from,omitempty" gorm:"type:varchar(150)"`
Illustrator string `json:"illustrator,omitempty" gorm:"type:varchar(150)"`
Regulation string `json:"regulation,omitempty" gorm:"type:varchar(10)"`
Rarity string `json:"rarity,omitempty" gorm:"type:varchar(50);index"`
IDPriceChartingEng string `json:"id_price_charting_eng,omitempty" gorm:"type:varchar(50)"`
LinkPriceChartingEng string `json:"link_price_charting_eng,omitempty" gorm:"type:text"`
IDPriceChartingJpn string `json:"id_price_charting_jpn,omitempty" gorm:"type:varchar(50)"`
LinkPriceChartingJpn string `json:"link_price_charting_jpn,omitempty" gorm:"type:text"`
HP JSONB `json:"hp,omitempty" gorm:"type:jsonb"`
Attacks JSONB `json:"attacks,omitempty" gorm:"type:jsonb"`
Abilities JSONB `json:"abilities,omitempty" gorm:"type:jsonb"`
Battle JSONB `json:"battle,omitempty" gorm:"type:jsonb"`
EvolutionLine JSONB `json:"evolution_line,omitempty" gorm:"type:jsonb"`
PokedexInfo JSONB `json:"pokedex_info,omitempty" gorm:"type:jsonb"`
Effects JSONB `json:"effects,omitempty" gorm:"type:jsonb"`
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime"`
CreatedBy *uint `json:"created_by" gorm:"default:null"`
UpdatedBy *uint `json:"updated_by" gorm:"default:null"`
}
func (CardMaster) TableName() string {
return "card_masters"
}
// Struct di bawah ini digunakan untuk parsing data JSON ke tipe Go yang terstruktur
type HP struct {
Value string `json:"value"`
Element string `json:"element"`
ElementImage string `json:"element_image,omitempty"`
}
type AttackCost struct {
Element string `json:"element"`
Image string `json:"image,omitempty"`
}
type Attack struct {
Cost []AttackCost `json:"cost"`
Name string `json:"name"`
Damage string `json:"damage,omitempty"`
Description string `json:"description,omitempty"`
}
type BattleStat struct {
Element string `json:"element,omitempty"`
ElementImage string `json:"element_image,omitempty"`
Value string `json:"value,omitempty"`
}
type Battle struct {
Weakness []BattleStat `json:"weakness"`
Resistance []BattleStat `json:"resistance"`
Retreat []BattleStat `json:"retreat"`
}
type EvolutionStage struct {
Stage string `json:"stage"`
Name string `json:"name"`
}
type PokedexInfo struct {
Number string `json:"number"`
Height string `json:"height"`
Weight string `json:"weight"`
}
type Ability struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
}
type Effect struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
}
// ElementImageMap memetakan nama element (Bahasa Indonesia) ke path gambar di /images/elements/
var ElementImageMap = map[string]string{
"Daun": "/images/elements/Grass.png",
"Api": "/images/elements/Fire.png",
"Air": "/images/elements/Water.png",
"Listrik": "/images/elements/Lightning.png",
"Psikis": "/images/elements/Psychic.png",
"Petarung": "/images/elements/Fighting.png",
"Kegelapan": "/images/elements/Darkness.png",
"Logam": "/images/elements/Metal.png",
"Naga": "/images/elements/Dragon.png",
"Peri": "/images/elements/Fairy.png",
"Bening": "/images/elements/Colorless.png",
}
// GetElementImage mengembalikan path gambar element berdasarkan nama element
func GetElementImage(element string) string {
if path, exists := ElementImageMap[element]; exists {
return path
}
return ""
}