diff --git a/cmd/workshop.go b/cmd/workshop.go index f56c248..b7dfe9c 100644 --- a/cmd/workshop.go +++ b/cmd/workshop.go @@ -18,7 +18,8 @@ func main() { //Vehicle router.HandleFunc("/vehicle/new", controllers.CreateVehicle).Methods("POST") - router.HandleFunc("/vehicle/all", controllers.GetvehiclesInService).Methods("GET") + router.HandleFunc("/vehicle/all", controllers.GetvehiclesWithServices).Methods("GET") + router.HandleFunc("/vehicle/service", controllers.GetvehiclesInService).Methods("GET") router.HandleFunc("/contacts/new", controllers.CreateContact).Methods("POST") diff --git a/go.mod b/go.mod index a3109a8..1e1b407 100644 --- a/go.mod +++ b/go.mod @@ -3,14 +3,13 @@ module gocommunity.ru/workshop go 1.23.6 require ( - github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/gorilla/mux v1.8.1 github.com/jinzhu/gorm v1.9.16 github.com/joho/godotenv v1.5.1 - golang.org/x/crypto v0.36.0 ) require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/lib/pq v1.1.1 // indirect + golang.org/x/crypto v0.36.0 // indirect ) diff --git a/go.sum b/go.sum index bd7f3db..a1b89b1 100644 --- a/go.sum +++ b/go.sum @@ -2,8 +2,6 @@ github.com/PuerkitoBio/goquery v1.5.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBK github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y= github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd h1:83Wprp6ROGeiHFAP8WJdI2RoxALQYgdllERc3N5N2DM= github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU= -github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= -github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 h1:Yzb9+7DPaBjB8zlTR87/ElzFsnQfuHnVUVqpZZIcV5Y= github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5/go.mod h1:a2zkGnVExMxdzMo3M0Hi/3sEU+cWnZpSni0O6/Yb/P0= github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= diff --git a/internal/controllers/vehicleController.go b/internal/controllers/vehicleController.go index 69fabdd..a98a188 100644 --- a/internal/controllers/vehicleController.go +++ b/internal/controllers/vehicleController.go @@ -18,8 +18,8 @@ var CreateVehicle = func(w http.ResponseWriter, r *http.Request) { u.Respond(w, resp) } -var GetvehiclesInService = func(w http.ResponseWriter, r *http.Request) { - VehicleId := r.Context().Value("id").(uint) +var GetvehiclesWithServices = func(w http.ResponseWriter, r *http.Request) { + vehicle := &models.Vehicle{} err := json.NewDecoder(r.Body).Decode(vehicle) @@ -28,7 +28,23 @@ var GetvehiclesInService = func(w http.ResponseWriter, r *http.Request) { return } - vehicle.ID = VehicleId - resp := vehicle.CreateVehicle() + data := vehicle.Getvehicles() + resp := u.Message(true, "success") + resp["data"] = data + u.Respond(w, resp) +} + +var GetvehiclesInService = func(w http.ResponseWriter, r *http.Request) { + vehicle := &models.Vehicle{} + + err := json.NewDecoder(r.Body).Decode(vehicle) + if err != nil { + u.Respond(w, u.Message(false, "Error!")) + return + } + + data := vehicle.GetvehiclesInService() + resp := u.Message(true, "success") + resp["data"] = data u.Respond(w, resp) } diff --git a/internal/models/employee.go b/internal/models/employee.go new file mode 100644 index 0000000..4109d3e --- /dev/null +++ b/internal/models/employee.go @@ -0,0 +1,46 @@ +package models + +import ( + //"fmt" + "github.com/jinzhu/gorm" + u "gocommunity.ru/workshop/internal/utils" +) + +type Employee struct { + gorm.Model + + Vehicles []Vehicle `gorm:"many2many:vehilce_employee;"` + Name string `json:"name"` + Specialization string `json:"specialization"` + Capacity int32 `json:"capacity"` +} + +func (employee *Employee) ValidateEmployee() (map[string]interface{}, bool) { + + if employee.Name == "" { + return u.Message(false, "cannot be empty!"), false + } + + if employee.Specialization == "" { + return u.Message(false, "cannot be empty!"), false + } + + if employee.Capacity <= 0 { + return u.Message(false, " < 0"), false + } + + return u.Message(true, "success"), true +} + +func (employee *Employee) CreateEmployee() map[string]interface{} { + + if response, ok := employee.ValidateEmployee(); !ok { + return response + } + + GetDB().Create(employee) + + resp := u.Message(true, "success") + resp["employee"] = employee + return resp +} \ No newline at end of file diff --git a/internal/models/vehicle.go b/internal/models/vehicle.go index de4dbd6..ba154ce 100644 --- a/internal/models/vehicle.go +++ b/internal/models/vehicle.go @@ -1,17 +1,17 @@ package models import ( - u "gocommunity.ru/workshop/internal/utils" "fmt" "github.com/jinzhu/gorm" + u "gocommunity.ru/workshop/internal/utils" ) type Vehicle struct { gorm.Model - ClientId int `json:"client_id"` - Brand string `json:"brand"` - Color string `json:"color"` - Services []Service `gorm:"many2many:service_vehicle;foreignkey:vehicle_id;references:Id"` + ClientId int `json:"client_id"` + Brand string `json:"brand"` + Color string `json:"color"` + Services []Service `gorm:"many2many:service_vehicle;"` } func (vehicle *Vehicle) ValidateVehicle() (map[string]interface{}, bool) { @@ -54,10 +54,10 @@ func Getvehicle(id uint) *Vehicle { return vehicle } -func Getvehicles(user uint) []*Vehicle { +func (vehicle *Vehicle) Getvehicles() []*Vehicle { vehiclesSlice := make([]*Vehicle, 0) - err := GetDB().Table("vehicles").Where("user_id = ?", user).Find(&vehiclesSlice).Error + err := GetDB().Table("vehicles").Preload("Services").Find(&vehiclesSlice).Error if err != nil { fmt.Println(err) return nil @@ -66,10 +66,10 @@ func Getvehicles(user uint) []*Vehicle { return vehiclesSlice } -func GetvehiclesInService() []*Vehicle { +func (vehicle *Vehicle) GetvehiclesInService() []*Vehicle { vehiclesSlice := make([]*Vehicle, 0) - err := GetDB().Table("vehicles").Joins("join service_vehicle sv on ").Where("user_id = ?").Find(&vehiclesSlice).Error + err := GetDB().Table("vehicles").Joins("inner join service_vehicle on id = vehicle_id").Find(&vehiclesSlice).Error if err != nil { fmt.Println(err) return nil diff --git a/internal/models/workHistory.go b/internal/models/workHistory.go new file mode 100644 index 0000000..7e0beb0 --- /dev/null +++ b/internal/models/workHistory.go @@ -0,0 +1,38 @@ +package models + +import ( + "time" + "fmt" + "github.com/jinzhu/gorm" + u "gocommunity.ru/workshop/internal/utils" +) + +type WorkHistory struct { + gorm.Model + ClientId int `json:"client_id"` + VehicleId int `json:"vehicle_id"` + ServiceId int `json:"service_id"` + IsFinished bool `json:"is_finished"` +} + + +func (workHistory *WorkHistory) CreateWorkHistory() map[string]interface{} { + + GetDB().Create(workHistory) + + resp := u.Message(true, "success") + resp["workHistory"] = workHistory + return resp +} + +func (workHistory *WorkHistory) GetHistoryByDate(start time.Time, finish time.Time) []*WorkHistory { + + workHistorySlice := make([]*WorkHistory, 0) + err := GetDB().Table("work_history").Where("created_at <= ? and updated_at <= ?", start, finish).Find(&workHistorySlice).Error + if err != nil { + fmt.Println(err) + return nil + } + + return workHistorySlice +} \ No newline at end of file