Compare commits
No commits in common. "8ad58ca09dc5abbf1fdcbf52f9e1bce1633e314e" and "a33947b5a63a5adfb18c406f72d0adcd9e212dce" have entirely different histories.
8ad58ca09d
...
a33947b5a6
@ -15,7 +15,6 @@ 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")
|
||||||
@ -23,7 +22,8 @@ func main() {
|
|||||||
router.HandleFunc("/vehicle/service", controllers.GetvehiclesInService).Methods("GET")
|
router.HandleFunc("/vehicle/service", controllers.GetvehiclesInService).Methods("GET")
|
||||||
|
|
||||||
|
|
||||||
router.HandleFunc("/work-report/find", controllers.GetEmployeesReport).Methods("POST")
|
router.HandleFunc("/contacts/new", controllers.CreateContact).Methods("POST")
|
||||||
|
router.HandleFunc("/me/contacts", controllers.GetContacts).Methods("GET")
|
||||||
|
|
||||||
port := os.Getenv("PORT")
|
port := os.Getenv("PORT")
|
||||||
if port == "" {
|
if port == "" {
|
||||||
|
@ -1,5 +0,0 @@
|
|||||||
package api
|
|
||||||
|
|
||||||
type IdRequest struct {
|
|
||||||
Ids []int `json:"ids"`
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
package api
|
|
||||||
|
|
||||||
import "time"
|
|
||||||
|
|
||||||
type WorkReportRequest struct {
|
|
||||||
Start time.Time `json:"start"`
|
|
||||||
Finish time.Time `json:"finish"`
|
|
||||||
EmployeeId int `json:"employee_id"`
|
|
||||||
}
|
|
33
internal/controllers/contactController.go
Normal file
33
internal/controllers/contactController.go
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
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)
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
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"
|
||||||
@ -36,21 +35,3 @@ 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)
|
|
||||||
}
|
|
||||||
|
@ -19,8 +19,15 @@ 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
|
||||||
@ -30,6 +37,12 @@ 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
|
||||||
|
@ -1,29 +0,0 @@
|
|||||||
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)
|
|
||||||
}
|
|
66
internal/models/contact.go
Normal file
66
internal/models/contact.go
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
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
|
||||||
|
}
|
@ -31,7 +31,7 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
db = conn
|
db = conn
|
||||||
db.Debug().AutoMigrate(&Service{}, &Bonuse{}, &Client{}, &Vehicle{}, &WorkHistory{}, &Employee{}) //Миграция базы данных
|
db.Debug().AutoMigrate(&Service{}, &Bonuse{}, &Client{}, &Contact{}, &Vehicle{}) //Миграция базы данных
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDB() *gorm.DB {
|
func GetDB() *gorm.DB {
|
||||||
|
@ -8,7 +8,6 @@ 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"`
|
||||||
@ -35,24 +34,3 @@ 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
|
|
||||||
}
|
|
||||||
|
@ -8,7 +8,6 @@ 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"`
|
||||||
|
@ -10,9 +10,8 @@ import (
|
|||||||
type WorkHistory struct {
|
type WorkHistory struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
ClientId int `json:"client_id"`
|
ClientId int `json:"client_id"`
|
||||||
Vehicle Vehicle `json:"vehicle"`
|
VehicleId int `json:"vehicle_id"`
|
||||||
Service Service `json:"service"`
|
ServiceId int `json:"service_id"`
|
||||||
EmployeeId int `json:"employee_id"`
|
|
||||||
IsFinished bool `json:"is_finished"`
|
IsFinished bool `json:"is_finished"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -26,15 +25,10 @@ func (workHistory *WorkHistory) CreateWorkHistory() map[string]interface{} {
|
|||||||
return resp
|
return resp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (workHistory *WorkHistory) GetHistoryByDateAndEmployeeId(start time.Time, finish time.Time, employeeId int) []*WorkHistory {
|
func (workHistory *WorkHistory) GetHistoryByDate(start time.Time, finish time.Time) []*WorkHistory {
|
||||||
|
|
||||||
workHistorySlice := make([]*WorkHistory, 0)
|
workHistorySlice := make([]*WorkHistory, 0)
|
||||||
err := GetDB().Table("work_histories").
|
err := GetDB().Table("work_history").Where("created_at <= ? and updated_at <= ?", start, finish).Find(&workHistorySlice).Error
|
||||||
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
|
||||||
|
Loading…
Reference in New Issue
Block a user