1 2
Some checks failed
Deploy to Server (Docker) / deploy (push) Failing after 1m2s

This commit is contained in:
Dmitry Sirotkin 2025-03-27 20:53:29 +03:00
parent 2534bd9e7f
commit a33947b5a6
7 changed files with 116 additions and 18 deletions

View File

@ -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")

3
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=

View File

@ -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)
}

View File

@ -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
}

View File

@ -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

View File

@ -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
}