pkgdash/handler/handlers.go

163 lines
4.0 KiB
Go
Raw Normal View History

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"
"go.unistack.org/micro/v4"
2023-08-07 21:30:30 +03:00
"go.unistack.org/unistack-org/pkgdash/config"
pb "go.unistack.org/unistack-org/pkgdash/proto"
cligit "go.unistack.org/unistack-org/pkgdash/service/client_git"
"go.unistack.org/unistack-org/pkgdash/storage"
"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
writer writer
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 {
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)
2023-08-12 19:26:50 +03:00
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
return httpsrv.SetError(NewInternalError(err))
}
2023-08-12 19:26:50 +03:00
//rsp = new(pb.ListPackageRsp)
rsp.Packages = dbRsp.Decode()
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 {
logger := h.svc.Logger()
logger.Debug(ctx, "Start UpdatePackage")
2023-08-12 19:26:50 +03:00
if err := req.Validate(); 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-12 19:26:50 +03:00
if err := h.store.UpdatePackage(ctx, req); 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-12 19:26:50 +03:00
rsp.Id = req.Id
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 {
logger := h.svc.Logger()
logger.Debug(ctx, "Start AddComment")
2023-08-12 19:26:50 +03:00
err := req.Validate()
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-12 19:26:50 +03:00
if rsp.Id, err = h.store.AddComment(ctx, req); err != nil {
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))
}
logger.Debug(ctx, "Success finish addComment")
2023-08-12 19:26:50 +03:00
return nil
}
2023-08-12 19:26:50 +03:00
func (h *Handler) AddPackage(ctx context.Context, req *pb.AddPackageReq, rsp *pb.AddPackageRsp) error {
logger := h.svc.Logger()
logger.Debug(ctx, "Start AddPackage")
2023-08-12 19:26:50 +03:00
err := req.Validate()
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))
}
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"
logger.Debug(ctx, "Success finish addPackage")
2023-08-12 19:26:50 +03:00
return nil
}
2023-08-12 19:26:50 +03:00
func (h *Handler) GetModule(ctx context.Context, req *pb.GetModuleReq, rsp *pb.GetModuleRsp) error {
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-12 19:26:50 +03:00
rsp.Modules = modules.Decode()
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 {
h := &Handler{
2023-08-12 19:26:50 +03:00
svc: svc,
git: client,
}
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)
}
h.chanUrl = h.git.Run(ctx, st)
2023-08-07 21:30:30 +03:00
h.store = st
return nil
}