chore: use the echo framework instead of plain go net/http

This commit is contained in:
2025-06-02 17:35:07 +01:00
parent 4e6d939fd5
commit 9bd04f843a
8 changed files with 199 additions and 71 deletions
+40 -8
View File
@@ -22,21 +22,27 @@ type CreateClientBody struct {
Phone string `json:"phone"`
}
func (wrapper *DBWrapper) MigrateClients() {
wrapper.db.AutoMigrate(&Client{})
type UpdateClientBody struct {
Name string `json:"name"`
Country string `json:"country"`
Phone string `json:"phone"`
}
func (wrapper *DBWrapper) CreateClient(body CreateClientBody) Client {
func (db *DB) MigrateClients() {
db.instance.AutoMigrate(&Client{})
}
func (db *DB) CreateClient(body CreateClientBody) Client {
client := Client{Name: body.Name, Country: body.Country, Phone: body.Phone}
wrapper.db.Create(&client)
db.instance.Create(&client)
return client
}
func (wrapper *DBWrapper) GetClients() ([]Client, error) {
func (db *DB) GetClients() ([]Client, error) {
var clients []Client
result := wrapper.db.Find(&clients)
result := db.instance.Find(&clients)
if result.Error != nil {
return nil, result.Error
@@ -45,8 +51,34 @@ func (wrapper *DBWrapper) GetClients() ([]Client, error) {
return clients, nil
}
func (wrapper *DBWrapper) GetClient(id int, client *Client) error {
result := wrapper.db.Where("id = ?", id).First(&client, id)
func (db *DB) GetClient(id int, client *Client) error {
result := db.instance.Where("id = ?", id).First(&client, id)
if result.Error != nil {
return result.Error
}
return nil
}
func (db *DB) UpdateClient(id int, body UpdateClientBody, client *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, Country: body.Country, Phone: body.Phone})
if result.Error != nil {
return result.Error
}
return nil
}
func (db *DB) DeleteClient(id int) error {
result := db.instance.Delete(&Client{}, id)
if result.Error != nil {
return result.Error