mirror of
https://github.com/hazemKrimi/crimson-vault.git
synced 2026-05-01 18:20:27 +00:00
wip: client api with net/http poc
This commit is contained in:
+43
-11
@@ -1,24 +1,56 @@
|
||||
package models
|
||||
|
||||
import "gorm.io/gorm"
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
gorm.Model
|
||||
Name string
|
||||
Country string
|
||||
Phone string
|
||||
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(name string, country string, phone string) {
|
||||
wrapper.db.Create(&Client{Name: name, Country: country, Phone: phone})
|
||||
}
|
||||
func (wrapper *DBWrapper) CreateClient(body CreateClientBody) Client {
|
||||
client := Client{Name: body.Name, Country: body.Country, Phone: body.Phone}
|
||||
|
||||
func (wrapper *DBWrapper) GetClient(id int) (Client) {
|
||||
var client Client
|
||||
wrapper.db.First(&client, id)
|
||||
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
|
||||
}
|
||||
|
||||
@@ -11,10 +11,12 @@ type DBWrapper struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func (wrapper *DBWrapper) Initialize() {
|
||||
func (wrapper *DBWrapper) Connect() {
|
||||
db, err := gorm.Open(sqlite.Open("crimson_vault.db"), &gorm.Config{})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
wrapper.db = db
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user