2023-08-16 13:17:42 +03:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"git.unistack.org/unistack-org/pkgdash/internal/storage"
|
|
|
|
pb "git.unistack.org/unistack-org/pkgdash/proto"
|
2023-08-18 23:59:15 +03:00
|
|
|
"github.com/google/uuid"
|
|
|
|
jsonpbcodec "go.unistack.org/micro-codec-jsonpb/v4"
|
|
|
|
"go.unistack.org/micro/v4/codec"
|
2023-08-16 13:17:42 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
2023-08-18 23:59:15 +03:00
|
|
|
store storage.Storage
|
|
|
|
codec codec.Codec
|
2023-08-16 13:17:42 +03:00
|
|
|
chanUrl chan *pb.PackagesCreateReq
|
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-18 23:59:15 +03:00
|
|
|
func NewHandler(store storage.Storage) (*Handler, error) {
|
2023-08-16 13:17:42 +03:00
|
|
|
h := &Handler{
|
2023-08-18 23:59:15 +03:00
|
|
|
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
|
|
|
}
|