Compare commits

..

No commits in common. "8ad58ca09dc5abbf1fdcbf52f9e1bce1633e314e" and "a33947b5a63a5adfb18c406f72d0adcd9e212dce" have entirely different histories.

12 changed files with 120 additions and 99 deletions

View File

@ -15,7 +15,6 @@ func main() {
router.HandleFunc("/service/new", controllers.CreateService).Methods("POST")
router.HandleFunc("/service/accept", controllers.AcceptVehicle).Methods("POST")
router.HandleFunc("/service/all", controllers.GetServices).Methods("GET")
router.HandleFunc("/service/calculate", controllers.Calculate).Methods("POST")
//Vehicle
router.HandleFunc("/vehicle/new", controllers.CreateVehicle).Methods("POST")
@ -23,7 +22,8 @@ func main() {
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")
if port == "" {

View File

@ -1,5 +0,0 @@
package api
type IdRequest struct {
Ids []int `json:"ids"`
}

View File

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

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

View File

@ -1,7 +1,6 @@
package controllers
import (
"gocommunity.ru/workshop/internal/api"
"gocommunity.ru/workshop/internal/models"
u "gocommunity.ru/workshop/internal/utils"
"encoding/json"
@ -36,21 +35,3 @@ var AcceptVehicle = func(w http.ResponseWriter, r *http.Request) {
resp := serviceVehicle.AcceptVehicle()
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)
}

View File

@ -19,8 +19,15 @@ var CreateVehicle = func(w http.ResponseWriter, r *http.Request) {
}
var GetvehiclesWithServices = 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.Getvehicles()
resp := u.Message(true, "success")
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) {
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

View File

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

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

View File

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

View File

@ -8,7 +8,6 @@ import (
type Service struct {
gorm.Model
WorkHistoryId int `json:"work_history_id"`
Price int32 `json:"price"`
Name string `json:"name"`
Description string `json:"description"`
@ -34,25 +33,4 @@ func GetServices() []*Service {
}
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
}
}

View File

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

View File

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