683 lines
24 KiB
Go
683 lines
24 KiB
Go
![]() |
package server
|
||
|
|
||
|
import (
|
||
|
"encoding/json"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
"path"
|
||
|
"time"
|
||
|
|
||
|
"git.espin.casa/albert/cml04-plb018/internal/service"
|
||
|
"git.espin.casa/albert/cml04-plb018/internal/types"
|
||
|
"github.com/zc2638/swag"
|
||
|
"github.com/zc2638/swag/endpoint"
|
||
|
"github.com/zc2638/swag/option"
|
||
|
)
|
||
|
|
||
|
type Server struct {
|
||
|
svc service.IService
|
||
|
api *swag.API
|
||
|
url string
|
||
|
}
|
||
|
|
||
|
func NroMatriculaSiguienteEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/nromatricula/next",
|
||
|
endpoint.BodyR(types.NroPaqueteSiguienteReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.NroPaqueteSiguienteRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Obtiene el numero de matricula siguiente"),
|
||
|
endpoint.Description("Obtiene el numero de matricula siguiente"),
|
||
|
endpoint.Handler(NroMatriculaSiguienteHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func NroMatriculaSiguienteHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.NroPaqueteSiguienteReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
// call service
|
||
|
res, err := svc.NroPaqueteSiguiente(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, fmt.Sprintf("PaquetesHandler failed: %s", err.Error()), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func NroMatriculaActualEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/nromatricula/get",
|
||
|
endpoint.BodyR(types.NroPaqueteActualReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.NroPaqueteActualReq{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Obtiene el numero de matricula actual"),
|
||
|
endpoint.Description("Obtiene el numero de matricula actual"),
|
||
|
endpoint.Handler(NroMatriculaActualHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func NroMatriculaActualHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.NroPaqueteActualReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
// call service
|
||
|
res, err := svc.NroPaqueteActual(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, fmt.Sprintf("PaquetesHandler failed: %s", err.Error()), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func PaquetesEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/paquete/list",
|
||
|
endpoint.BodyR(types.PaquetesReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.PaquetesRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Obtiene el numero de matricula actual"),
|
||
|
endpoint.Description("Obtiene el numero de matricula actual"),
|
||
|
endpoint.Handler(PaquetesHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func PaquetesHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.PaquetesReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
// call service
|
||
|
res, err := svc.Paquetes(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, fmt.Sprintf("PaquetesHandler failed: %s", err.Error()), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func PaqueteEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/paquete/get",
|
||
|
endpoint.BodyR(types.PaqueteReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.PaqueteRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Obtiene los datos del paquete (bulto)"),
|
||
|
endpoint.Description("Obtiene los datos del paquete (bulto)"),
|
||
|
endpoint.Handler(PaqueteHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func PaqueteHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.PaqueteReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
// call service
|
||
|
res, err := svc.Paquete(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, fmt.Sprintf("PaqueteHandler failed: %s", err.Error()), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func EtiquetasEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/etiqueta/list",
|
||
|
endpoint.BodyR(types.EtiquetasReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.EtiquetasRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Obtiene los datos de las etiquetas"),
|
||
|
endpoint.Description("Obtiene los datos de las etiquetas"),
|
||
|
endpoint.Handler(EtiquetasHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func EtiquetasHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.EtiquetasReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
// call service
|
||
|
res, err := svc.Etiquetas(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, fmt.Sprintf("EtiquetasHandler failed: %s", err.Error()), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func EtiquetaEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/etiqueta/get",
|
||
|
endpoint.BodyR(types.EtiquetaReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.EtiquetaRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Obtiene los datos de la etiqueta"),
|
||
|
endpoint.Description("Obtiene los datos de la etiqueta"),
|
||
|
endpoint.Handler(EtiquetaHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func EtiquetaHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.EtiquetaReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
res, err := svc.Etiqueta(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, "etiqueta handler failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func SalvarEtiquetaEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/etiqueta/create",
|
||
|
endpoint.BodyR(types.SalvarEtiquetaReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.SalvarEtiquetaRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Salva los datos de la etiqueta"),
|
||
|
endpoint.Description("Salva los datos de la etiqueta"),
|
||
|
endpoint.Handler(SalvarEtiquetaHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func SalvarEtiquetaHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.SalvarEtiquetaReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
res, err := svc.SalvarEtiqueta(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, "salva datos etiqueta handler failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func SalvarImpresorasEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/impresora/create",
|
||
|
endpoint.BodyR(types.SalvarImpresoraReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.SalvarImpresoraRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Salva los datos de la impresora"),
|
||
|
endpoint.Description("Salva los datos de la impresora"),
|
||
|
endpoint.Handler(SalvarImpresoraHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func SalvarImpresoraHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.SalvarImpresoraReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
res, err := svc.SalvarImpresora(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, "salva datos impresora handler failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func ImpresorasEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/impresora/list",
|
||
|
endpoint.BodyR(types.ImpresorasReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.ImpresorasRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Obtiene los datos de las impresoras"),
|
||
|
endpoint.Description("Obtiene los datos de las impresoras"),
|
||
|
endpoint.Handler(ImpresorasHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func ImpresorasHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.ImpresorasReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
res, err := svc.Impresoras(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, "obtiene datos de las impresoras handler failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func ImpresoraEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/impresora/get",
|
||
|
endpoint.BodyR(types.ImpresoraReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.ImpresoraRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Obtiene los datos de la impresora"),
|
||
|
endpoint.Description("Obtiene los datos de la impresora"),
|
||
|
endpoint.Handler(ImpresoraHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func ImpresoraHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.ImpresoraReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
res, err := svc.Impresora(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, "obtiene datos impresora handler failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func OrdenesClientesEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/co/list",
|
||
|
endpoint.BodyR(types.OrdenesClienteReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.OrdenesClienteRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Obtener lista de Ordenes de cliente"),
|
||
|
endpoint.Description("Obtener lista de Ordenes de cliente"),
|
||
|
endpoint.Handler(OrdenesClienteHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func OrdenesClienteHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.OrdenesClienteReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
res, err := svc.OrdenesCliente(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, "obtiene ordenes de cliente handler failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func OrdenClienteEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/co/get",
|
||
|
endpoint.BodyR(types.OrdenClienteReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.OrdenClienteRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Obtener lista de Ordenes de cliente"),
|
||
|
endpoint.Description("Obtener lista de Ordenes de cliente"),
|
||
|
endpoint.Handler(OrdenClienteHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func OrdenClienteHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.OrdenClienteReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
res, err := svc.OrdenCliente(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, "obtiene ordenes de cliente handler failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func OrdenesProduccionEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/po/list",
|
||
|
endpoint.BodyR(types.OrdenesProduccionReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.OrdenesProduccionRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Obtener lista de Ordenes de producción"),
|
||
|
endpoint.Description("Obtener lista de Ordenes de producción"),
|
||
|
endpoint.Handler(OrdenesProduccionHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func OrdenesProduccionHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.OrdenesProduccionReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
res, err := svc.OrdenesProduccion(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func OrdenProduccionEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/po/get",
|
||
|
endpoint.BodyR(types.OrdenProduccionReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.OrdenProduccionRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Obtener datos de la Orden de producción basado en el identificador"),
|
||
|
endpoint.Description("Obtener datos de la Orden de producción basado en el identificador"),
|
||
|
endpoint.Handler(OrdenProduccionHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func OrdenProduccionHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.OrdenProduccionReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
res, err := svc.OrdenProduccion(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, "obtiene orden de produccion by id datos handler failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func ImprimirPaqueteRobotEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/robot/print",
|
||
|
endpoint.BodyR(types.ImprimirPaqueteReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.ImprimirPaqueteRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Imprimir paquete"),
|
||
|
endpoint.Description("Imprimir paquete"),
|
||
|
endpoint.Handler(ImprimirPaqueteRobotHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func ImprimirPaqueteRobotHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.ImprimirPaqueteReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
res, err := svc.ImprimirPaqueteRobot(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func ImprimirEtiquetaEndPoint(svc service.IService) *swag.Endpoint {
|
||
|
return endpoint.New(http.MethodPost, "/paquete/print",
|
||
|
endpoint.BodyR(types.ImprimirPaqueteReq{}),
|
||
|
endpoint.ResponseSuccess(endpoint.SchemaResponseOption(types.ImprimirPaqueteRes{})),
|
||
|
endpoint.Response(http.StatusInternalServerError, "process internal error", endpoint.SchemaResponseOption("")),
|
||
|
endpoint.Summary("Imprimir paquete"),
|
||
|
endpoint.Description("Imprimir paquete"),
|
||
|
endpoint.Handler(ImprimirEtiquetaHandler(svc)),
|
||
|
)
|
||
|
}
|
||
|
|
||
|
func ImprimirEtiquetaHandler(svc service.IService) http.Handler {
|
||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
// request body holder
|
||
|
req := &types.ImprimirPaqueteReq{}
|
||
|
// decode json body request
|
||
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||
|
http.Error(w, "body json decoding failed", http.StatusBadRequest)
|
||
|
return
|
||
|
}
|
||
|
res, err := svc.ImprimirPaquete(r.Context(), req)
|
||
|
if err != nil {
|
||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
// set content type header
|
||
|
w.Header().Set("Content-Type", "application/json")
|
||
|
// response
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
// write response
|
||
|
if err := json.NewEncoder(w).Encode(&res); err != nil {
|
||
|
http.Error(w, "encode response failed", http.StatusInternalServerError)
|
||
|
return
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func (s *Server) Start() error {
|
||
|
// set api handlers
|
||
|
for p, endpoints := range s.api.Paths {
|
||
|
http.DefaultServeMux.Handle(path.Join(s.api.BasePath, p), endpoints)
|
||
|
}
|
||
|
// set swagger handlers
|
||
|
http.DefaultServeMux.Handle("/swagger/json", s.api.Handler())
|
||
|
patterns := swag.UIPatterns("/swagger/ui")
|
||
|
for _, pattern := range patterns {
|
||
|
http.DefaultServeMux.Handle(pattern, swag.UIHandler("/swagger/ui", "/swagger/json", true))
|
||
|
}
|
||
|
// Crear un servidor HTTP
|
||
|
server := http.Server{
|
||
|
Addr: s.url,
|
||
|
ReadTimeout: 5 * time.Second, // time limit for reading
|
||
|
WriteTimeout: 5 * time.Second, // time limit for writting
|
||
|
IdleTimeout: 5 * time.Second,
|
||
|
}
|
||
|
// start api
|
||
|
go func() {
|
||
|
if err := server.ListenAndServe(); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}()
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func NewServer(url string, svc service.IService) *Server {
|
||
|
// create new swag
|
||
|
api := swag.New(
|
||
|
option.Title("CELSA 4 Plb018 API Server"),
|
||
|
option.Description("This is an implementation of a REST API server for Plb18"),
|
||
|
option.ContactEmail("aespin@gcelsa.com"),
|
||
|
option.Version("1.0"),
|
||
|
option.BasePath("/api/v1"),
|
||
|
option.License("MIT", "https://opensource.org/license/mit"),
|
||
|
)
|
||
|
// add end points
|
||
|
api.AddEndpoint(
|
||
|
ImprimirEtiquetaEndPoint(svc),
|
||
|
ImprimirPaqueteRobotEndPoint(svc),
|
||
|
EtiquetaEndPoint(svc),
|
||
|
EtiquetasEndPoint(svc),
|
||
|
SalvarEtiquetaEndPoint(svc),
|
||
|
OrdenProduccionEndPoint(svc),
|
||
|
OrdenesProduccionEndPoint(svc),
|
||
|
OrdenClienteEndPoint(svc),
|
||
|
OrdenesClientesEndPoint(svc),
|
||
|
ImpresoraEndPoint(svc),
|
||
|
ImpresorasEndPoint(svc),
|
||
|
SalvarImpresorasEndPoint(svc),
|
||
|
PaqueteEndPoint(svc),
|
||
|
PaquetesEndPoint(svc),
|
||
|
NroMatriculaActualEndPoint(svc),
|
||
|
NroMatriculaSiguienteEndPoint(svc),
|
||
|
)
|
||
|
// return new server
|
||
|
return &Server{
|
||
|
svc: svc,
|
||
|
api: api,
|
||
|
url: url,
|
||
|
}
|
||
|
}
|