This commit is contained in:
parent
9677e0d7ab
commit
8ad58ca09d
@ -15,6 +15,7 @@ func main() {
|
|||||||
router.HandleFunc("/service/new", controllers.CreateService).Methods("POST")
|
router.HandleFunc("/service/new", controllers.CreateService).Methods("POST")
|
||||||
router.HandleFunc("/service/accept", controllers.AcceptVehicle).Methods("POST")
|
router.HandleFunc("/service/accept", controllers.AcceptVehicle).Methods("POST")
|
||||||
router.HandleFunc("/service/all", controllers.GetServices).Methods("GET")
|
router.HandleFunc("/service/all", controllers.GetServices).Methods("GET")
|
||||||
|
router.HandleFunc("/service/calculate", controllers.Calculate).Methods("POST")
|
||||||
|
|
||||||
//Vehicle
|
//Vehicle
|
||||||
router.HandleFunc("/vehicle/new", controllers.CreateVehicle).Methods("POST")
|
router.HandleFunc("/vehicle/new", controllers.CreateVehicle).Methods("POST")
|
||||||
|
5
internal/api/idRequest.go
Normal file
5
internal/api/idRequest.go
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
type IdRequest struct {
|
||||||
|
Ids []int `json:"ids"`
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"gocommunity.ru/workshop/internal/api"
|
||||||
"gocommunity.ru/workshop/internal/models"
|
"gocommunity.ru/workshop/internal/models"
|
||||||
u "gocommunity.ru/workshop/internal/utils"
|
u "gocommunity.ru/workshop/internal/utils"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
@ -35,3 +36,21 @@ var AcceptVehicle = func(w http.ResponseWriter, r *http.Request) {
|
|||||||
resp := serviceVehicle.AcceptVehicle()
|
resp := serviceVehicle.AcceptVehicle()
|
||||||
u.Respond(w, resp)
|
u.Respond(w, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var Calculate = func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
|
ids := &api.IdRequest{}
|
||||||
|
|
||||||
|
service := &models.Service{}
|
||||||
|
|
||||||
|
err := json.NewDecoder(r.Body).Decode(ids)
|
||||||
|
if err != nil {
|
||||||
|
u.Respond(w, u.Message(false, "Invalid request!"))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
data := service.Calculate(ids.Ids)
|
||||||
|
resp := u.Message(true, "success")
|
||||||
|
resp["data"] = data
|
||||||
|
u.Respond(w, resp)
|
||||||
|
}
|
||||||
|
@ -34,4 +34,25 @@ func GetServices() []*Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return servicesSlice
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user