2023-08-07 21:30:30 +03:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-08-12 19:26:50 +03:00
|
|
|
"database/sql"
|
|
|
|
"errors"
|
2023-08-07 21:30:30 +03:00
|
|
|
cmsstorage "go.unistack.org/cms-service/storage"
|
2023-08-12 19:26:50 +03:00
|
|
|
httpsrv "go.unistack.org/micro-server-http/v4"
|
2023-08-11 21:27:38 +03:00
|
|
|
"go.unistack.org/micro/v4"
|
2023-08-07 21:30:30 +03:00
|
|
|
"go.unistack.org/unistack-org/pkgdash/config"
|
2023-08-11 21:27:38 +03:00
|
|
|
pb "go.unistack.org/unistack-org/pkgdash/proto"
|
2023-08-11 20:12:15 +03:00
|
|
|
cligit "go.unistack.org/unistack-org/pkgdash/service/client_git"
|
2023-08-09 14:31:23 +03:00
|
|
|
"go.unistack.org/unistack-org/pkgdash/storage"
|
2023-08-11 20:12:15 +03:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
2023-08-12 19:26:50 +03:00
|
|
|
"google.golang.org/protobuf/types/known/emptypb"
|
|
|
|
"net/http"
|
2023-08-07 21:30:30 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Handler struct {
|
|
|
|
svc micro.Service
|
|
|
|
store storage.Storage
|
2023-08-09 14:31:23 +03:00
|
|
|
|
|
|
|
writer writer
|
2023-08-11 20:12:15 +03:00
|
|
|
protojson.MarshalOptions
|
|
|
|
protojson.UnmarshalOptions
|
|
|
|
|
|
|
|
git cligit.Client
|
|
|
|
chanUrl chan *pb.AddPackageReq
|
2023-08-07 21:30:30 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
func (h *Handler) ListPackage(ctx context.Context, _ *emptypb.Empty, rsp *pb.ListPackageRsp) error {
|
2023-08-09 14:31:23 +03:00
|
|
|
logger := h.svc.Logger()
|
|
|
|
logger.Debug(ctx, "Start getListPackage")
|
|
|
|
|
2023-08-11 20:12:15 +03:00
|
|
|
dbRsp, err := h.store.ListPackage(ctx)
|
2023-08-09 14:31:23 +03:00
|
|
|
if err != nil {
|
|
|
|
logger.Errorf(ctx, "error db response: %v", err)
|
2023-08-12 19:26:50 +03:00
|
|
|
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
|
|
|
|
return httpsrv.SetError(NewInternalError(err))
|
2023-08-09 14:31:23 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
//rsp = new(pb.ListPackageRsp)
|
2023-08-11 20:12:15 +03:00
|
|
|
rsp.Packages = dbRsp.Decode()
|
2023-08-09 14:31:23 +03:00
|
|
|
|
|
|
|
logger.Debug(ctx, "Success finish getListPackage")
|
2023-08-12 19:26:50 +03:00
|
|
|
return nil
|
2023-08-07 21:30:30 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
func (h *Handler) UpdatePackage(ctx context.Context, req *pb.UpdatePackageReq, rsp *pb.UpdatePackageRsp) error {
|
2023-08-09 14:31:23 +03:00
|
|
|
logger := h.svc.Logger()
|
2023-08-11 20:12:15 +03:00
|
|
|
logger.Debug(ctx, "Start UpdatePackage")
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
if err := req.Validate(); err != nil {
|
2023-08-11 20:12:15 +03:00
|
|
|
logger.Error(ctx, err)
|
2023-08-12 19:26:50 +03:00
|
|
|
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
|
|
|
return httpsrv.SetError(NewValidationError(err))
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
if err := h.store.UpdatePackage(ctx, req); err != nil {
|
2023-08-11 20:12:15 +03:00
|
|
|
logger.Error(ctx, err)
|
2023-08-12 19:26:50 +03:00
|
|
|
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
|
|
|
|
return httpsrv.SetError(NewInternalError(err))
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
rsp.Id = req.Id
|
2023-08-11 20:12:15 +03:00
|
|
|
|
|
|
|
logger.Debug(ctx, "Success finish UpdatePackage")
|
2023-08-12 19:26:50 +03:00
|
|
|
return nil
|
2023-08-07 21:30:30 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
func (h *Handler) AddComment(ctx context.Context, req *pb.AddCommentReq, rsp *pb.AddCommentRsp) error {
|
2023-08-09 14:31:23 +03:00
|
|
|
logger := h.svc.Logger()
|
|
|
|
logger.Debug(ctx, "Start AddComment")
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
err := req.Validate()
|
2023-08-11 20:12:15 +03:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(ctx, err)
|
2023-08-12 19:26:50 +03:00
|
|
|
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
|
|
|
return httpsrv.SetError(NewValidationError(err))
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
if rsp.Id, err = h.store.AddComment(ctx, req); err != nil {
|
2023-08-11 20:12:15 +03:00
|
|
|
logger.Error(ctx, err)
|
2023-08-12 19:26:50 +03:00
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
httpsrv.SetRspCode(ctx, http.StatusNotFound)
|
|
|
|
return httpsrv.SetError(NewNotFoundError(err))
|
|
|
|
}
|
|
|
|
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
|
|
|
|
return httpsrv.SetError(NewInternalError(err))
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.Debug(ctx, "Success finish addComment")
|
2023-08-12 19:26:50 +03:00
|
|
|
return nil
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
func (h *Handler) AddPackage(ctx context.Context, req *pb.AddPackageReq, rsp *pb.AddPackageRsp) error {
|
2023-08-11 20:12:15 +03:00
|
|
|
logger := h.svc.Logger()
|
|
|
|
logger.Debug(ctx, "Start AddPackage")
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
err := req.Validate()
|
2023-08-11 20:12:15 +03:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(ctx, err)
|
2023-08-12 19:26:50 +03:00
|
|
|
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
|
|
|
return httpsrv.SetError(NewValidationError(err))
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if h.git.IsClose() {
|
|
|
|
logger.Error(ctx, "chan is closed")
|
|
|
|
} else {
|
|
|
|
h.chanUrl <- req
|
|
|
|
}
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
rsp.Status = "Sent"
|
|
|
|
|
2023-08-11 20:12:15 +03:00
|
|
|
logger.Debug(ctx, "Success finish addPackage")
|
2023-08-12 19:26:50 +03:00
|
|
|
return nil
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
func (h *Handler) GetModule(ctx context.Context, req *pb.GetModuleReq, rsp *pb.GetModuleRsp) error {
|
2023-08-11 20:12:15 +03:00
|
|
|
logger := h.svc.Logger()
|
|
|
|
logger.Debug(ctx, "Start GetModule")
|
|
|
|
|
|
|
|
modules, err := h.store.GetModule(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(ctx, err)
|
2023-08-12 19:26:50 +03:00
|
|
|
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
|
|
|
|
return httpsrv.SetError(NewInternalError(err))
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
rsp.Modules = modules.Decode()
|
2023-08-11 20:12:15 +03:00
|
|
|
|
|
|
|
logger.Debug(ctx, "Success finish getModule")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
func NewHandler(svc micro.Service, client cligit.Client) *Handler {
|
2023-08-11 20:12:15 +03:00
|
|
|
h := &Handler{
|
2023-08-12 19:26:50 +03:00
|
|
|
svc: svc,
|
|
|
|
git: client,
|
2023-08-11 20:12:15 +03:00
|
|
|
}
|
|
|
|
h.EmitUnpopulated = true
|
|
|
|
h.UseProtoNames = false
|
|
|
|
return h
|
2023-08-07 21:30:30 +03:00
|
|
|
}
|
|
|
|
|
2023-08-12 19:26:50 +03:00
|
|
|
// TODO add conn db
|
2023-08-07 21:30:30 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-08-11 20:12:15 +03:00
|
|
|
h.chanUrl = h.git.Run(ctx, st)
|
|
|
|
|
2023-08-07 21:30:30 +03:00
|
|
|
h.store = st
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|