mirror of
https://github.com/hazemKrimi/crimson-vault.git
synced 2026-05-01 18:20:27 +00:00
chore: use the echo framework instead of plain go net/http
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -7,16 +7,16 @@ import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type DBWrapper struct {
|
||||
db *gorm.DB
|
||||
type DB struct {
|
||||
instance *gorm.DB
|
||||
}
|
||||
|
||||
func (wrapper *DBWrapper) Connect() {
|
||||
func (wrapper *DB) Connect() {
|
||||
db, err := gorm.Open(sqlite.Open("crimson_vault.db"), &gorm.Config{})
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
wrapper.db = db
|
||||
wrapper.instance = db
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user