67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
"truck-company/internal/usecase"
|
|
|
|
"github.com/gorilla/mux"
|
|
)
|
|
|
|
type TruckCompanyHandler struct {
|
|
uc *usecase.UseCase
|
|
}
|
|
|
|
func NewTruckCompanyHandler(u *usecase.UseCase) *TruckCompanyHandler {
|
|
return &TruckCompanyHandler{
|
|
uc: u,
|
|
}
|
|
}
|
|
|
|
func (h *TruckCompanyHandler) RegisterRoutes(r *mux.Router) {
|
|
|
|
r.HandleFunc("/routes", h.GetRoutes).Methods("GET")
|
|
r.HandleFunc("/routes/{route_id}/transport", h.GetTransportByRoute).Methods("GET")
|
|
// r.HandleFunc("/routes/{route_id}/transport-types", h.GetTransportTypesByRoute).Methods("GET")
|
|
// r.HandleFunc("/shipments/cost", h.GetShipmentCost).Methods("GET")
|
|
// r.HandleFunc("/routes/{route_id}/cargo-turnover", h.GetCargoTurnoverByRoute).Methods("GET")
|
|
|
|
//404 для всех остальных
|
|
r.HandleFunc("/", http.NotFound)
|
|
}
|
|
|
|
func (h *TruckCompanyHandler) GetRoutes(w http.ResponseWriter, r *http.Request) {
|
|
|
|
routes, err := h.uc.GetAllRoutes()
|
|
if err != nil {
|
|
sendErrorResponse(w, http.StatusInternalServerError, "", err.Error())
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(routes)
|
|
}
|
|
|
|
func (h *TruckCompanyHandler) GetTransportByRoute(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
routeID, err := strconv.ParseInt(vars["route_id"], 10, 64)
|
|
|
|
if err != nil {
|
|
sendErrorResponse(w, http.StatusBadRequest, "Invalid route ID", err.Error())
|
|
|
|
return
|
|
}
|
|
|
|
transport, err := h.uc.GetTransportByRoute(routeID)
|
|
if err != nil {
|
|
|
|
sendErrorResponse(w, http.StatusInternalServerError, "", err.Error())
|
|
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
json.NewEncoder(w).Encode(transport)
|
|
}
|