diff options
| author | Bobby <[email protected]> | 2026-03-29 22:52:46 +0530 |
|---|---|---|
| committer | Bobby <[email protected]> | 2026-03-29 22:52:46 +0530 |
| commit | 9eb9b7f4bd552a641235764f66483e1f940fcfd9 (patch) | |
| tree | da520b923b5e6758d5457b6233dd6671fc640914 /nexus/models | |
| parent | 65a143a0871c35989b7c7ea6723d39a0585c089e (diff) | |
| download | echoes-of-vaelun-main.tar.xz echoes-of-vaelun-main.zip | |
Diffstat (limited to 'nexus/models')
| -rw-r--r-- | nexus/models/account.go | 77 | ||||
| -rw-r--r-- | nexus/models/character.go | 51 | ||||
| -rw-r--r-- | nexus/models/defaults.go | 7 | ||||
| -rw-r--r-- | nexus/models/realm.go | 36 | ||||
| -rw-r--r-- | nexus/models/session.go | 36 |
5 files changed, 207 insertions, 0 deletions
diff --git a/nexus/models/account.go b/nexus/models/account.go new file mode 100644 index 0000000..d6e8b5a --- /dev/null +++ b/nexus/models/account.go @@ -0,0 +1,77 @@ +package models
+
+import (
+ "errors"
+ "nexus/types/account"
+ "nexus/utils/validate"
+ "strings"
+
+ "github.com/google/uuid"
+ "golang.org/x/crypto/bcrypt"
+ "gorm.io/gorm"
+)
+
+type Account struct {
+ gorm.Model
+ ID uuid.UUID `gorm:"type:uuid;primaryKey"`
+ Username string `gorm:"uniqueIndex;not null"`
+ Email string `gorm:"uniqueIndex;not null"`
+ PasswordHash string `gorm:"not null"`
+ IsActive bool `gorm:"default:true"`
+ IsVerified bool `gorm:"default:false"`
+ Characters []Character `gorm:"foreignKey:AccountID"`
+ Sessions []Session `gorm:"foreignKey:AccountID"`
+}
+
+func (self *Account) BeforeCreate(tx *gorm.DB) error {
+ if self.ID == uuid.Nil {
+ self.ID = uuid.New()
+ }
+
+ self.Email = strings.TrimSpace(strings.ToLower(self.Email))
+ self.Username = strings.TrimSpace(self.Username)
+
+ if !validate.Email(self.Email) {
+ return errors.New(validate.InvalidEmail)
+ }
+
+ if self.Username == "" {
+ return errors.New("username is required")
+ }
+
+ return nil
+}
+
+func (self *Account) BeforeUpdate(tx *gorm.DB) error {
+ self.Email = strings.TrimSpace(strings.ToLower(self.Email))
+ self.Username = strings.TrimSpace(self.Username)
+
+ if !validate.Email(self.Email) {
+ return errors.New(validate.InvalidEmail)
+ }
+
+ return nil
+}
+
+func (self *Account) SetPassword(password string) error {
+ hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
+ if err != nil {
+ return err
+ }
+ self.PasswordHash = string(hash)
+ return nil
+}
+
+func (self *Account) CheckPassword(password string) bool {
+ return bcrypt.CompareHashAndPassword([]byte(self.PasswordHash), []byte(password)) == nil
+}
+
+func (self *Account) ToResponse() account.Response {
+ return account.Response{
+ ID: self.ID,
+ Username: self.Username,
+ Email: self.Email,
+ IsVerified: self.IsVerified,
+ CreatedAt: self.CreatedAt,
+ }
+}
diff --git a/nexus/models/character.go b/nexus/models/character.go new file mode 100644 index 0000000..b44cf87 --- /dev/null +++ b/nexus/models/character.go @@ -0,0 +1,51 @@ +package models
+
+import (
+ "nexus/types/character"
+ "strings"
+
+ "github.com/google/uuid"
+ "gorm.io/gorm"
+)
+
+type Character struct {
+ gorm.Model
+ ID uuid.UUID `gorm:"type:uuid;primaryKey"`
+ AccountID uuid.UUID `gorm:"type:uuid;not null;index"`
+ RealmID uuid.UUID `gorm:"type:uuid;not null;index"`
+ Name string `gorm:"not null"`
+ Race string `gorm:"not null"`
+ StartingKingdom string `gorm:"not null"`
+ Account Account `gorm:"foreignKey:AccountID"`
+ Realm Realm `gorm:"foreignKey:RealmID"`
+}
+
+func (self *Character) BeforeCreate(tx *gorm.DB) error {
+ if self.ID == uuid.Nil {
+ self.ID = uuid.New()
+ }
+ self.Name = strings.TrimSpace(self.Name)
+ if self.Name == "" {
+ return ErrCharacterNameRequired
+ }
+ return nil
+}
+
+func (self *Character) BeforeUpdate(tx *gorm.DB) error {
+ self.Name = strings.TrimSpace(self.Name)
+ if self.Name == "" {
+ return ErrCharacterNameRequired
+ }
+ return nil
+}
+
+func (self *Character) ToResponse() character.Response {
+ return character.Response{
+ ID: self.ID,
+ Name: self.Name,
+ Race: self.Race,
+ StartingKingdom: self.StartingKingdom,
+ RealmID: self.RealmID,
+ CreatedAt: self.CreatedAt,
+ }
+}
diff --git a/nexus/models/defaults.go b/nexus/models/defaults.go new file mode 100644 index 0000000..9dd830e --- /dev/null +++ b/nexus/models/defaults.go @@ -0,0 +1,7 @@ +package models
+
+import "errors"
+
+var (
+ ErrCharacterNameRequired = errors.New("character name is required")
+)
diff --git a/nexus/models/realm.go b/nexus/models/realm.go new file mode 100644 index 0000000..044db71 --- /dev/null +++ b/nexus/models/realm.go @@ -0,0 +1,36 @@ +package models
+
+import (
+ "nexus/types/realm"
+
+ "github.com/google/uuid"
+ "gorm.io/gorm"
+)
+
+type Realm struct {
+ gorm.Model
+ ID uuid.UUID `gorm:"type:uuid;primaryKey"`
+ Name string `gorm:"uniqueIndex;not null"`
+ Region string `gorm:"not null"`
+ Host string `gorm:"not null"`
+ Port int `gorm:"not null"`
+ IsOnline bool `gorm:"default:true"`
+ ServerKey string `gorm:"not null"`
+ Characters []Character `gorm:"foreignKey:RealmID"`
+}
+
+func (self *Realm) BeforeCreate(tx *gorm.DB) error {
+ if self.ID == uuid.Nil {
+ self.ID = uuid.New()
+ }
+ return nil
+}
+
+func (self *Realm) ToResponse() realm.Response {
+ return realm.Response{
+ ID: self.ID,
+ Name: self.Name,
+ Region: self.Region,
+ IsOnline: self.IsOnline,
+ }
+}
diff --git a/nexus/models/session.go b/nexus/models/session.go new file mode 100644 index 0000000..0adbba2 --- /dev/null +++ b/nexus/models/session.go @@ -0,0 +1,36 @@ +package models
+
+import (
+ "time"
+
+ "github.com/google/uuid"
+ "gorm.io/gorm"
+)
+
+type Session struct {
+ gorm.Model
+ ID uuid.UUID `gorm:"type:uuid;primaryKey"`
+ AccountID uuid.UUID `gorm:"type:uuid;not null;index"`
+ AuthToken string `gorm:"uniqueIndex;not null"`
+ RefreshToken string `gorm:"uniqueIndex;not null"`
+ AuthExpiry time.Time `gorm:"not null"`
+ RefreshExpiry time.Time `gorm:"not null"`
+ IPAddress string
+ UserAgent string
+ Account Account `gorm:"foreignKey:AccountID"`
+}
+
+func (self *Session) BeforeCreate(tx *gorm.DB) error {
+ if self.ID == uuid.Nil {
+ self.ID = uuid.New()
+ }
+ return nil
+}
+
+func (self *Session) IsAuthExpired() bool {
+ return time.Now().After(self.AuthExpiry)
+}
+
+func (self *Session) IsRefreshExpired() bool {
+ return time.Now().After(self.RefreshExpiry)
+}
|
