41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"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 {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
json.NewEncoder(w).Encode(routes)
|
|
}
|