This commit is contained in:
Dmitry Sirotkin 2025-03-28 19:03:56 +03:00
parent a33947b5a6
commit 9677e0d7ab
10 changed files with 52 additions and 119 deletions

View File

@ -22,8 +22,7 @@ func main() {
router.HandleFunc("/vehicle/service", controllers.GetvehiclesInService).Methods("GET") router.HandleFunc("/vehicle/service", controllers.GetvehiclesInService).Methods("GET")
router.HandleFunc("/contacts/new", controllers.CreateContact).Methods("POST") router.HandleFunc("/work-report/find", controllers.GetEmployeesReport).Methods("POST")
router.HandleFunc("/me/contacts", controllers.GetContacts).Methods("GET")
port := os.Getenv("PORT") port := os.Getenv("PORT")
if port == "" { if port == "" {

View File

@ -0,0 +1,9 @@
package api
import "time"
type WorkReportRequest struct {
Start time.Time `json:"start"`
Finish time.Time `json:"finish"`
EmployeeId int `json:"employee_id"`
}

View File

@ -1,33 +0,0 @@
package controllers
import (
"gocommunity.ru/workshop/internal/models"
u "gocommunity.ru/workshop/internal/utils"
"encoding/json"
"net/http"
)
var CreateContact = func(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value("user").(uint)
contact := &models.Contact{}
err := json.NewDecoder(r.Body).Decode(contact)
if err != nil {
u.Respond(w, u.Message(false, "Error!"))
return
}
contact.UserId = user
resp := contact.CreateContact()
u.Respond(w, resp)
}
var GetContacts = func(w http.ResponseWriter, r *http.Request) {
id := r.Context().Value("user").(uint)
data := models.GetContacts(id)
resp := u.Message(true, "success")
resp["data"] = data
u.Respond(w, resp)
}

View File

@ -19,15 +19,8 @@ var CreateVehicle = func(w http.ResponseWriter, r *http.Request) {
} }
var GetvehiclesWithServices = func(w http.ResponseWriter, r *http.Request) { var GetvehiclesWithServices = func(w http.ResponseWriter, r *http.Request) {
vehicle := &models.Vehicle{} vehicle := &models.Vehicle{}
err := json.NewDecoder(r.Body).Decode(vehicle)
if err != nil {
u.Respond(w, u.Message(false, "Error!"))
return
}
data := vehicle.Getvehicles() data := vehicle.Getvehicles()
resp := u.Message(true, "success") resp := u.Message(true, "success")
resp["data"] = data resp["data"] = data
@ -37,12 +30,6 @@ var GetvehiclesWithServices = func(w http.ResponseWriter, r *http.Request) {
var GetvehiclesInService = func(w http.ResponseWriter, r *http.Request) { var GetvehiclesInService = func(w http.ResponseWriter, r *http.Request) {
vehicle := &models.Vehicle{} vehicle := &models.Vehicle{}
err := json.NewDecoder(r.Body).Decode(vehicle)
if err != nil {
u.Respond(w, u.Message(false, "Error!"))
return
}
data := vehicle.GetvehiclesInService() data := vehicle.GetvehiclesInService()
resp := u.Message(true, "success") resp := u.Message(true, "success")
resp["data"] = data resp["data"] = data

View File

@ -0,0 +1,29 @@
package controllers
import (
"gocommunity.ru/workshop/internal/api"
"gocommunity.ru/workshop/internal/models"
u "gocommunity.ru/workshop/internal/utils"
"encoding/json"
"net/http"
)
var GetEmployeesReport = func(w http.ResponseWriter, r *http.Request) {
request := &api.WorkReportRequest{}
workHistory := &models.WorkHistory{}
err := json.NewDecoder(r.Body).Decode(request)
if err != nil {
u.Respond(w, u.Message(false, "Errorz!"))
return
}
data := workHistory.GetHistoryByDateAndEmployeeId(request.Start, request.Finish, request.EmployeeId)
resp := u.Message(true, "success")
resp["data"] = data
u.Respond(w, resp)
}

View File

@ -1,66 +0,0 @@
package models
import (
u "gocommunity.ru/workshop/internal/utils"
"fmt"
"github.com/jinzhu/gorm"
)
type Contact struct {
gorm.Model
Name string `json:"name"`
Phone string `json:"phone"`
UserId uint `json:"user_id"`
}
func (contact *Contact) ValidateContact() (map[string]interface{}, bool) {
if contact.Name == "" {
return u.Message(false, "Name cannot be empty!"), false
}
if contact.Phone == "" {
return u.Message(false, "Phone number cannot be empty!"), false
}
if contact.UserId <= 0 {
return u.Message(false, "User not found!"), false
}
return u.Message(true, "success"), true
}
func (contact *Contact) CreateContact() map[string]interface{} {
if response, ok := contact.ValidateContact(); !ok {
return response
}
GetDB().Create(contact)
resp := u.Message(true, "success")
resp["contact"] = contact
return resp
}
func GetContact(id uint) *Contact {
contact := &Contact{}
err := GetDB().Table("contacts").Where("id = ?", id).First(contact).Error
if err != nil {
return nil
}
return contact
}
func GetContacts(user uint) []*Contact {
contactsSlice := make([]*Contact, 0)
err := GetDB().Table("contacts").Where("user_id = ?", user).Find(&contactsSlice).Error
if err != nil {
fmt.Println(err)
return nil
}
return contactsSlice
}

View File

@ -31,7 +31,7 @@ func init() {
} }
db = conn db = conn
db.Debug().AutoMigrate(&Service{}, &Bonuse{}, &Client{}, &Contact{}, &Vehicle{}) //Миграция базы данных db.Debug().AutoMigrate(&Service{}, &Bonuse{}, &Client{}, &Vehicle{}, &WorkHistory{}, &Employee{}) //Миграция базы данных
} }
func GetDB() *gorm.DB { func GetDB() *gorm.DB {

View File

@ -8,6 +8,7 @@ import (
type Service struct { type Service struct {
gorm.Model gorm.Model
WorkHistoryId int `json:"work_history_id"`
Price int32 `json:"price"` Price int32 `json:"price"`
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description"` Description string `json:"description"`

View File

@ -8,6 +8,7 @@ import (
type Vehicle struct { type Vehicle struct {
gorm.Model gorm.Model
WorkHistoryId int `json:"work_history_id"`
ClientId int `json:"client_id"` ClientId int `json:"client_id"`
Brand string `json:"brand"` Brand string `json:"brand"`
Color string `json:"color"` Color string `json:"color"`

View File

@ -10,8 +10,9 @@ import (
type WorkHistory struct { type WorkHistory struct {
gorm.Model gorm.Model
ClientId int `json:"client_id"` ClientId int `json:"client_id"`
VehicleId int `json:"vehicle_id"` Vehicle Vehicle `json:"vehicle"`
ServiceId int `json:"service_id"` Service Service `json:"service"`
EmployeeId int `json:"employee_id"`
IsFinished bool `json:"is_finished"` IsFinished bool `json:"is_finished"`
} }
@ -25,10 +26,15 @@ func (workHistory *WorkHistory) CreateWorkHistory() map[string]interface{} {
return resp return resp
} }
func (workHistory *WorkHistory) GetHistoryByDate(start time.Time, finish time.Time) []*WorkHistory { func (workHistory *WorkHistory) GetHistoryByDateAndEmployeeId(start time.Time, finish time.Time, employeeId int) []*WorkHistory {
workHistorySlice := make([]*WorkHistory, 0) workHistorySlice := make([]*WorkHistory, 0)
err := GetDB().Table("work_history").Where("created_at <= ? and updated_at <= ?", start, finish).Find(&workHistorySlice).Error err := GetDB().Table("work_histories").
Where("employee_id = ? and created_at <= ? and updated_at <= ?", employeeId, start, finish).
Preload("client").
Preload("vehicle").
Preload("service").
Find(&workHistorySlice).Error
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
return nil return nil