This commit is contained in:
2023-05-20 10:48:15 -05:00
parent 89648fc631
commit 231f7ec29c
9 changed files with 97 additions and 283 deletions

View File

@@ -1,112 +0,0 @@
package model
import (
"errors"
"fmt"
uuid "github.com/gofrs/uuid"
)
// Account example
type Account struct {
ID int `json:"id" example:"1" format:"int64"`
Name string `json:"name" example:"account name"`
UUID uuid.UUID `json:"uuid" example:"550e8400-e29b-41d4-a716-446655440000" format:"uuid"`
}
// example
var (
ErrNameInvalid = errors.New("name is empty")
)
// AddAccount example
type AddAccount struct {
Name string `json:"name" example:"account name"`
}
// Validation example
func (a AddAccount) Validation() error {
switch {
case len(a.Name) == 0:
return ErrNameInvalid
default:
return nil
}
}
// UpdateAccount example
type UpdateAccount struct {
Name string `json:"name" example:"account name"`
}
// Validation example
func (a UpdateAccount) Validation() error {
switch {
case len(a.Name) == 0:
return ErrNameInvalid
default:
return nil
}
}
// AccountsAll example
func AccountsAll(q string) ([]Account, error) {
if q == "" {
return accounts, nil
}
as := []Account{}
for k, v := range accounts {
if q == v.Name {
as = append(as, accounts[k])
}
}
return as, nil
}
// AccountOne example
func AccountOne(id int) (Account, error) {
for _, v := range accounts {
if id == v.ID {
return v, nil
}
}
return Account{}, ErrNoRow
}
// Insert example
func (a Account) Insert() (int, error) {
accountMaxID++
a.ID = accountMaxID
a.Name = fmt.Sprintf("account_%d", accountMaxID)
accounts = append(accounts, a)
return accountMaxID, nil
}
// Delete example
func Delete(id int) error {
for k, v := range accounts {
if id == v.ID {
accounts = append(accounts[:k], accounts[k+1:]...)
return nil
}
}
return fmt.Errorf("account id=%d is not found", id)
}
// Update example
func (a Account) Update() error {
for k, v := range accounts {
if a.ID == v.ID {
accounts[k].Name = a.Name
return nil
}
}
return fmt.Errorf("account id=%d is not found", a.ID)
}
var accountMaxID = 3
var accounts = []Account{
{ID: 1, Name: "account_1"},
{ID: 2, Name: "account_2"},
{ID: 3, Name: "account_3"},
}

View File

@@ -1,29 +0,0 @@
package model
// Bottle example
type Bottle struct {
ID int `json:"id" example:"1"`
Name string `json:"name" example:"bottle_name"`
Account Account `json:"account"`
}
// BottlesAll example
func BottlesAll() ([]Bottle, error) {
return bottles, nil
}
// BottleOne example
func BottleOne(id int) (*Bottle, error) {
for _, v := range bottles {
if id == v.ID {
return &v, nil
}
}
return nil, ErrNoRow
}
var bottles = []Bottle{
{ID: 1, Name: "bottle_1", Account: Account{ID: 1, Name: "accout_1"}},
{ID: 2, Name: "bottle_2", Account: Account{ID: 2, Name: "accout_2"}},
{ID: 3, Name: "bottle_3", Account: Account{ID: 3, Name: "accout_3"}},
}