mirror of
https://github.com/hazemKrimi/crimson-vault.git
synced 2026-05-01 18:20:27 +00:00
chore: add user schema
This commit is contained in:
+26
-54
@@ -1,58 +1,30 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"github.com/hazemKrimi/crimson-vault/internal/types"
|
||||
)
|
||||
|
||||
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"`
|
||||
FiscalCode string `json:"fiscalCode"`
|
||||
Address string `json:"address"`
|
||||
Zip string `json:"zip"`
|
||||
Country string `json:"country"`
|
||||
Phone string `json:"phone"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
type CreateClientRequestBody struct {
|
||||
Name string `json:"name" validate:"required"`
|
||||
FiscalCode string `json:"fiscalCode"`
|
||||
Address string `json:"address" validate:"required"`
|
||||
Zip string `json:"zip" validate:"required"`
|
||||
Country string `json:"country" validate:"required"`
|
||||
Phone string `json:"phone" validate:"required,e164"`
|
||||
Email string `json:"email" validate:"required,email"`
|
||||
}
|
||||
|
||||
type UpdateClientRequestBody struct {
|
||||
Name string `json:"name"`
|
||||
FiscalCode string `json:"fiscalCode"`
|
||||
Address string `json:"address"`
|
||||
Zip string `json:"zip"`
|
||||
Country string `json:"country"`
|
||||
Phone string `json:"phone" validate:"omitempty,e164"`
|
||||
Email string `json:"email" validate:"omitempty,email"`
|
||||
}
|
||||
|
||||
func (db *DB) MigrateClients() {
|
||||
db.instance.AutoMigrate(&Client{})
|
||||
db.instance.AutoMigrate(&types.Client{})
|
||||
}
|
||||
|
||||
func (db *DB) CreateClient(body CreateClientRequestBody) Client {
|
||||
client := Client{Name: body.Name, Country: body.Country, Phone: body.Phone}
|
||||
func (db *DB) CreateClient(body types.CreateClientRequestBody) types.Client {
|
||||
client := types.Client{
|
||||
Name: body.Name,
|
||||
FiscalCode: body.FiscalCode,
|
||||
Address: body.Address,
|
||||
Zip: body.Zip,
|
||||
Country: body.Country,
|
||||
Phone: body.Phone,
|
||||
Email: body.Email,
|
||||
}
|
||||
|
||||
db.instance.Create(&client)
|
||||
return client
|
||||
}
|
||||
|
||||
func (db *DB) GetClients() ([]Client, error) {
|
||||
var clients []Client
|
||||
func (db *DB) GetClients() ([]types.Client, error) {
|
||||
var clients []types.Client
|
||||
|
||||
result := db.instance.Find(&clients)
|
||||
|
||||
@@ -63,8 +35,8 @@ func (db *DB) GetClients() ([]Client, error) {
|
||||
return clients, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetClient(id int, client *Client) error {
|
||||
result := db.instance.Where("id = ?", id).First(&client, id)
|
||||
func (db *DB) GetClient(id int, client *types.Client) error {
|
||||
result := db.instance.Where("id = ?", id).First(client, id)
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
@@ -73,21 +45,21 @@ func (db *DB) GetClient(id int, client *Client) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) UpdateClient(id int, body UpdateClientRequestBody, client *Client) error {
|
||||
result := db.instance.Where("id = ?", id).First(&client, id)
|
||||
func (db *DB) UpdateClient(id int, body types.UpdateClientRequestBody, client *types.Client) error {
|
||||
result := db.instance.Where("id = ?", id).First(client, id)
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
result = db.instance.Model(&client).Updates(Client{
|
||||
Name: body.Name,
|
||||
result = db.instance.Model(client).Updates(types.Client{
|
||||
Name: body.Name,
|
||||
FiscalCode: body.FiscalCode,
|
||||
Address: body.Address,
|
||||
Zip: body.Zip,
|
||||
Country: body.Country,
|
||||
Phone: body.Phone,
|
||||
Email: body.Email,
|
||||
Address: body.Address,
|
||||
Zip: body.Zip,
|
||||
Country: body.Country,
|
||||
Phone: body.Phone,
|
||||
Email: body.Email,
|
||||
})
|
||||
|
||||
if result.Error != nil {
|
||||
@@ -98,7 +70,7 @@ func (db *DB) UpdateClient(id int, body UpdateClientRequestBody, client *Client)
|
||||
}
|
||||
|
||||
func (db *DB) DeleteClient(id int) error {
|
||||
result := db.instance.Delete(&Client{}, id)
|
||||
result := db.instance.Delete(&types.Client{}, id)
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"github.com/hazemKrimi/crimson-vault/internal/types"
|
||||
)
|
||||
|
||||
func (db *DB) MigrateUsers() {
|
||||
db.instance.AutoMigrate(&types.User{})
|
||||
}
|
||||
|
||||
func (db *DB) CreateUser(body types.CreateUserRequestBody) types.User {
|
||||
user := types.User{
|
||||
Name: body.Name,
|
||||
FiscalCode: body.FiscalCode,
|
||||
Address: body.Address,
|
||||
Zip: body.Zip,
|
||||
Country: body.Country,
|
||||
Phone: body.Phone,
|
||||
Email: body.Email,
|
||||
}
|
||||
|
||||
db.instance.Create(&user)
|
||||
return user
|
||||
}
|
||||
|
||||
func (db *DB) GetUsers() ([]types.User, error) {
|
||||
var users []types.User
|
||||
|
||||
result := db.instance.Find(&users)
|
||||
|
||||
if result.Error != nil {
|
||||
return nil, result.Error
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (db *DB) GetUser(id int, user *types.User) error {
|
||||
result := db.instance.Where("id = ?", id).First(user, id)
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) UpdateUser(id int, body types.UpdateUserRequestBody, user *types.User) error {
|
||||
result := db.instance.Where("id = ?", id).First(user, id)
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
result = db.instance.Model(user).Updates(types.User{
|
||||
Name: body.Name,
|
||||
FiscalCode: body.FiscalCode,
|
||||
Address: body.Address,
|
||||
Zip: body.Zip,
|
||||
Country: body.Country,
|
||||
Phone: body.Phone,
|
||||
Email: body.Email,
|
||||
})
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) UpdateUserSecurityDetails(id int, body types.UpdateUserSecurityDetailsBody, user *types.User) error {
|
||||
result := db.instance.Where("id = ?", id).First(user, id)
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
result = db.instance.Model(user).Updates(types.User{
|
||||
Username: body.Username,
|
||||
Password: body.Password,
|
||||
})
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) UpdateUserLogo(path string, user *types.User) error {
|
||||
result := db.instance.Model(user).Updates(types.User{
|
||||
Logo: path,
|
||||
})
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) DeleteUser(id int) error {
|
||||
result := db.instance.Delete(&types.User{}, id)
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *DB) DeleteUserLogo(user *types.User) error {
|
||||
result := db.instance.Model(user).Updates(&types.User{
|
||||
Logo: "",
|
||||
})
|
||||
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user