145 lines
3.2 KiB
Go
145 lines
3.2 KiB
Go
package handler
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
|
|
cmsstorage "go.unistack.org/cms-service/storage"
|
|
"go.unistack.org/micro/v3"
|
|
"go.unistack.org/micro/v3/errors"
|
|
"go.unistack.org/unistack-org/pkgdash/config"
|
|
pb "go.unistack.org/unistack-org/pkgdash/proto/go_generate"
|
|
"go.unistack.org/unistack-org/pkgdash/storage"
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
)
|
|
|
|
type Handler struct {
|
|
svc micro.Service
|
|
store storage.Storage
|
|
|
|
writer writer
|
|
protojson.MarshalOptions
|
|
protojson.UnmarshalOptions
|
|
}
|
|
|
|
func (h *Handler) ListPackage(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
logger := h.svc.Logger()
|
|
logger.Debug(ctx, "Start getListPackage")
|
|
|
|
dbRsp, err := h.store.ListPackage(ctx)
|
|
if err != nil {
|
|
logger.Errorf(ctx, "error db response: %v", err)
|
|
h.writer.Response(ctx, w, err)
|
|
return
|
|
}
|
|
|
|
rsp := new(pb.ListPackageRsp)
|
|
rsp.Packages = dbRsp.Mapping()
|
|
|
|
logger.Debug(ctx, "Success finish getListPackage")
|
|
h.writer.Response(ctx, w, rsp)
|
|
}
|
|
|
|
func (h *Handler) UpdateInfo(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
logger := h.svc.Logger()
|
|
logger.Debug(ctx, "Start UpdateInfo")
|
|
|
|
// TODO
|
|
}
|
|
|
|
func (h *Handler) AddComment(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
logger := h.svc.Logger()
|
|
logger.Debug(ctx, "Start AddComment")
|
|
|
|
defer r.Body.Close()
|
|
all, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
logger.Error(ctx, err)
|
|
h.writer.Response(ctx, w, NewInternalError(err))
|
|
return
|
|
}
|
|
|
|
rsp := new(pb.AddCommentRsp)
|
|
if err = h.Unmarshal(all, rsp); err != nil {
|
|
logger.Error(ctx, err)
|
|
h.writer.Response(ctx, w, NewUnmarshalError(err))
|
|
return
|
|
}
|
|
|
|
if err = rsp.Validate(); err != nil {
|
|
logger.Error(ctx, err)
|
|
h.writer.Response(ctx, w, NewValidationError(err))
|
|
return
|
|
}
|
|
|
|
if err = h.store.AddComment(ctx, rsp); err != nil {
|
|
logger.Error(ctx, err)
|
|
h.writer.Response(ctx, w, NewInternalError(err))
|
|
return
|
|
}
|
|
|
|
logger.Debug(ctx, "Success finish addComment")
|
|
}
|
|
|
|
func (h *Handler) AddPackage(w http.ResponseWriter, r *http.Request) {
|
|
ctx := r.Context()
|
|
logger := h.svc.Logger()
|
|
logger.Debug(ctx, "Start AddPackage")
|
|
|
|
defer r.Body.Close()
|
|
all, err := io.ReadAll(r.Body)
|
|
if err != nil {
|
|
logger.Error(ctx, err)
|
|
h.writer.Response(ctx, w, NewInternalError(err))
|
|
return
|
|
}
|
|
|
|
req := new(pb.AddPackageRsp)
|
|
if err = h.Unmarshal(all, req); err != nil {
|
|
logger.Error(ctx, err)
|
|
h.writer.Response(ctx, w, NewUnmarshalError(err))
|
|
return
|
|
}
|
|
|
|
if err = req.Validate(); err != nil {
|
|
logger.Error(ctx, err)
|
|
h.writer.Response(ctx, w, NewValidationError(err))
|
|
return
|
|
}
|
|
|
|
// TODO:
|
|
// need logic for add module from go.mod
|
|
// err := setModules(req)
|
|
|
|
if err = h.store.AddPackage(ctx, req); err != nil {
|
|
logger.Error(ctx, err)
|
|
h.writer.Response(ctx, w, NewInternalError(err))
|
|
return
|
|
}
|
|
|
|
logger.Debug(ctx, "Success finish addPackage")
|
|
}
|
|
|
|
func NewHandler(svc micro.Service, w writer) *Handler {
|
|
return &Handler{svc: svc, writer: w}
|
|
}
|
|
|
|
func (h *Handler) Init(ctx context.Context) error {
|
|
store := cmsstorage.InterfaceFromContext(h.svc.Options().Context)
|
|
if store == nil {
|
|
return cmsstorage.ErrMissingStorage
|
|
}
|
|
st, ok := store.(storage.Storage)
|
|
if !ok {
|
|
return errors.New(config.ServiceName, "error init storage", 1)
|
|
}
|
|
|
|
h.store = st
|
|
|
|
return nil
|
|
}
|