wip: client api with net/http poc

This commit is contained in:
2025-05-29 21:22:28 +01:00
parent 5c189046d3
commit 3add628087
7 changed files with 148 additions and 19 deletions
+43 -11
View File
@@ -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
}
+3 -1
View File
@@ -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
}