2023-08-16 13:17:42 +03:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
"github.com/google/uuid"
|
2024-12-07 02:35:30 +03:00
|
|
|
jsonpbcodec "go.unistack.org/micro-codec-jsonpb/v3"
|
|
|
|
"go.unistack.org/micro/v3/codec"
|
|
|
|
"go.unistack.org/micro/v3/logger"
|
|
|
|
"go.unistack.org/pkgdash/internal/storage"
|
|
|
|
pb "go.unistack.org/pkgdash/proto"
|
2023-08-16 13:17:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
2024-12-07 02:35:30 +03:00
|
|
|
logger logger.Logger
|
|
|
|
store storage.Storage
|
|
|
|
codec codec.Codec
|
2023-08-16 13:17:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNotFoundError(err error) *pb.ErrorRsp {
|
|
|
|
return &pb.ErrorRsp{
|
|
|
|
Code: strconv.Itoa(http.StatusBadRequest),
|
|
|
|
Title: "NotFound",
|
|
|
|
Uuid: uuid.New().String(),
|
|
|
|
Details: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInternalError(err error) *pb.ErrorRsp {
|
|
|
|
return &pb.ErrorRsp{
|
|
|
|
Code: strconv.Itoa(http.StatusInternalServerError),
|
|
|
|
Title: "InternalServerError",
|
|
|
|
Uuid: uuid.New().String(),
|
|
|
|
Details: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewValidationError(err error) *pb.ErrorRsp {
|
|
|
|
return &pb.ErrorRsp{
|
|
|
|
Code: strconv.Itoa(http.StatusBadRequest),
|
|
|
|
Title: "BadRequest",
|
|
|
|
Uuid: uuid.New().String(),
|
|
|
|
Details: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-07 02:35:30 +03:00
|
|
|
func NewHandler(log logger.Logger, store storage.Storage) (*Handler, error) {
|
2023-08-16 13:17:42 +03:00
|
|
|
h := &Handler{
|
2024-12-07 02:35:30 +03:00
|
|
|
logger: log,
|
|
|
|
codec: jsonpbcodec.NewCodec(),
|
|
|
|
store: store,
|
2023-08-16 13:17:42 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
return h, nil
|
2023-08-16 13:17:42 +03:00
|
|
|
}
|