59 lines
1012 B
Go
59 lines
1012 B
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/jinzhu/gorm"
|
|
u "gocommunity.ru/workshop/internal/utils"
|
|
)
|
|
|
|
type Service struct {
|
|
gorm.Model
|
|
WorkHistoryId int `json:"work_history_id"`
|
|
Price int32 `json:"price"`
|
|
Name string `json:"name"`
|
|
Description string `json:"description"`
|
|
}
|
|
|
|
|
|
func (service *Service) CreateService() map[string]interface{} {
|
|
|
|
GetDB().Create(service)
|
|
|
|
resp := u.Message(true, "success")
|
|
resp["service"] = service
|
|
return resp
|
|
}
|
|
|
|
func GetServices() []*Service {
|
|
|
|
servicesSlice := make([]*Service, 0)
|
|
err := GetDB().Table("services").Find(&servicesSlice).Error
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return nil
|
|
}
|
|
|
|
return servicesSlice
|
|
}
|
|
|
|
type ServiceSum struct {
|
|
Sum int `json:"sum"`
|
|
}
|
|
|
|
func (service *Service) Calculate(ids []int) []*ServiceSum {
|
|
|
|
serviceSum := make([]*ServiceSum, 0)
|
|
|
|
err := GetDB().Table("services").
|
|
Select("sum(price) as sum").
|
|
Where("id in (?)", ids).
|
|
Find(&serviceSum).Error
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return nil
|
|
}
|
|
|
|
return serviceSum
|
|
}
|