mirror of
https://github.com/hazemKrimi/crimson-vault.git
synced 2026-05-01 18:20:27 +00:00
57 lines
1.2 KiB
Go
57 lines
1.2 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Client struct {
|
|
ID uint32 `json:"id"`
|
|
CreatedAt time.Time `json:"createAt"`
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
DeletedAt gorm.DeletedAt `json:"deletedAt" gorm:"index"`
|
|
Name string `json:"name"`
|
|
Country string `json:"country"`
|
|
Phone string `json:"phone"`
|
|
}
|
|
|
|
type CreateClientBody struct {
|
|
Name string `json:"name"`
|
|
Country string `json:"country"`
|
|
Phone string `json:"phone"`
|
|
}
|
|
|
|
func (wrapper *DBWrapper) MigrateClients() {
|
|
wrapper.db.AutoMigrate(&Client{})
|
|
}
|
|
|
|
func (wrapper *DBWrapper) CreateClient(body CreateClientBody) Client {
|
|
client := Client{Name: body.Name, Country: body.Country, Phone: body.Phone}
|
|
|
|
wrapper.db.Create(&client)
|
|
return client
|
|
}
|
|
|
|
func (wrapper *DBWrapper) GetClients() ([]Client, error) {
|
|
var clients []Client
|
|
|
|
result := wrapper.db.Find(&clients)
|
|
|
|
if result.Error != nil {
|
|
return nil, result.Error
|
|
}
|
|
|
|
return clients, nil
|
|
}
|
|
|
|
func (wrapper *DBWrapper) GetClient(id int, client *Client) error {
|
|
result := wrapper.db.Where("id = ?", id).First(&client, id)
|
|
|
|
if result.Error != nil {
|
|
return result.Error
|
|
}
|
|
|
|
return nil
|
|
}
|