package handler import ( "context" "database/sql" "errors" cmsstorage "go.unistack.org/cms-service/storage" httpsrv "go.unistack.org/micro-server-http/v4" "go.unistack.org/micro/v4" "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" "google.golang.org/protobuf/types/known/emptypb" "net/http" ) type Handler struct { svc micro.Service store storage.Storage writer writer protojson.MarshalOptions protojson.UnmarshalOptions git cligit.Client chanUrl chan *pb.AddPackageReq } 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) httpsrv.SetRspCode(ctx, http.StatusInternalServerError) return httpsrv.SetError(NewInternalError(err)) } //rsp = new(pb.ListPackageRsp) rsp.Packages = dbRsp.Decode() logger.Debug(ctx, "Success finish getListPackage") return nil } func (h *Handler) UpdatePackage(ctx context.Context, req *pb.UpdatePackageReq, rsp *pb.UpdatePackageRsp) error { logger := h.svc.Logger() logger.Debug(ctx, "Start UpdatePackage") if err := req.Validate(); err != nil { logger.Error(ctx, err) httpsrv.SetRspCode(ctx, http.StatusBadRequest) return httpsrv.SetError(NewValidationError(err)) } if err := h.store.UpdatePackage(ctx, req); err != nil { logger.Error(ctx, err) httpsrv.SetRspCode(ctx, http.StatusInternalServerError) return httpsrv.SetError(NewInternalError(err)) } rsp.Id = req.Id logger.Debug(ctx, "Success finish UpdatePackage") return nil } func (h *Handler) AddComment(ctx context.Context, req *pb.AddCommentReq, rsp *pb.AddCommentRsp) error { logger := h.svc.Logger() logger.Debug(ctx, "Start AddComment") err := req.Validate() if err != nil { logger.Error(ctx, err) httpsrv.SetRspCode(ctx, http.StatusBadRequest) return httpsrv.SetError(NewValidationError(err)) } if rsp.Id, err = h.store.AddComment(ctx, req); err != nil { logger.Error(ctx, err) 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") return nil } func (h *Handler) AddPackage(ctx context.Context, req *pb.AddPackageReq, rsp *pb.AddPackageRsp) error { logger := h.svc.Logger() logger.Debug(ctx, "Start AddPackage") err := req.Validate() if err != nil { logger.Error(ctx, err) httpsrv.SetRspCode(ctx, http.StatusBadRequest) return httpsrv.SetError(NewValidationError(err)) } if h.git.IsClose() { logger.Error(ctx, "chan is closed") } else { h.chanUrl <- req } rsp.Status = "Sent" logger.Debug(ctx, "Success finish addPackage") return nil } 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) httpsrv.SetRspCode(ctx, http.StatusInternalServerError) return httpsrv.SetError(NewInternalError(err)) } rsp.Modules = modules.Decode() logger.Debug(ctx, "Success finish getModule") return nil } func NewHandler(svc micro.Service, client cligit.Client) *Handler { h := &Handler{ svc: svc, git: client, } h.EmitUnpopulated = true h.UseProtoNames = false return h } // TODO add conn db 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) h.store = st return nil }