Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2023-08-16 13:17:42 +03:00
parent 0678e72908
commit 78f0ae14d7
70 changed files with 4159 additions and 2355 deletions

View File

@@ -0,0 +1,40 @@
package handler
import (
"context"
"database/sql"
"errors"
"net/http"
httpsrv "go.unistack.org/micro-server-http/v4"
"git.unistack.org/unistack-org/pkgdash/internal/models"
pb "git.unistack.org/unistack-org/pkgdash/proto"
)
func (h *Handler) CommentsCreate(ctx context.Context, req *pb.CommentsCreateReq, rsp *pb.CommentsCreateRsp) 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))
}
var com *models.Comment
if com, err = h.store.CommentsCreate(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))
}
rsp.Comment = models.NewComment(com)
logger.Debug(ctx, "Success finish addComment")
return nil
}

View File

@@ -0,0 +1,36 @@
package handler
import (
"context"
"database/sql"
"errors"
"net/http"
httpsrv "go.unistack.org/micro-server-http/v4"
pb "git.unistack.org/unistack-org/pkgdash/proto"
)
func (h *Handler) CommentsDelete(ctx context.Context, req *pb.CommentsDeleteReq, rsp *pb.CommentsDeleteRsp) 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 err = h.store.CommentsDelete(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
}

View File

@@ -0,0 +1,36 @@
package handler
import (
"context"
"net/http"
httpsrv "go.unistack.org/micro-server-http/v4"
"git.unistack.org/unistack-org/pkgdash/internal/models"
pb "git.unistack.org/unistack-org/pkgdash/proto"
)
func (h *Handler) CommentsList(ctx context.Context, req *pb.CommentsListReq, rsp *pb.CommentsListRsp) error {
logger := h.svc.Logger()
logger.Debug(ctx, "Start GetModule")
err := req.Validate()
if err != nil {
logger.Error(ctx, err)
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
return httpsrv.SetError(NewValidationError(err))
}
comments, err := h.store.CommentsList(ctx, req)
if err != nil {
logger.Error(ctx, err)
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
return httpsrv.SetError(NewInternalError(err))
}
for _, com := range comments {
rsp.Comments = append(rsp.Comments, models.NewComment(com))
}
logger.Debug(ctx, "Success finish getModule")
return nil
}

View File

@@ -0,0 +1,11 @@
package handler
import (
"context"
pb "git.unistack.org/unistack-org/pkgdash/proto"
)
func (h *Handler) CommentsLookup(ctx context.Context, req *pb.CommentsLookupReq, rsp *pb.CommentsLookupRsp) error {
return nil
}

View File

@@ -0,0 +1,76 @@
package handler
import (
"context"
"errors"
"net/http"
"strconv"
"github.com/google/uuid"
"go.unistack.org/micro/v4"
cligit "git.unistack.org/unistack-org/pkgdash/internal/service/client_git"
"git.unistack.org/unistack-org/pkgdash/internal/storage"
pb "git.unistack.org/unistack-org/pkgdash/proto"
"google.golang.org/protobuf/encoding/protojson"
)
type Handler struct {
svc micro.Service
store storage.Storage
protojson.MarshalOptions
protojson.UnmarshalOptions
git cligit.Client
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(),
}
}
func NewHandler(svc micro.Service, client cligit.Client) *Handler {
h := &Handler{
svc: svc,
git: client,
}
h.EmitUnpopulated = true
h.UseProtoNames = false
return h
}
func (h *Handler) Init(ctx context.Context) error {
store, err := storage.FromContext(h.svc.Options().Context)
if err != nil {
return errors.New("missing storage")
}
h.chanUrl = h.git.Run(ctx, store)
h.store = store
return nil
}

View File

@@ -0,0 +1,35 @@
package handler
import (
"context"
"net/http"
httpsrv "go.unistack.org/micro-server-http/v4"
"git.unistack.org/unistack-org/pkgdash/internal/models"
pb "git.unistack.org/unistack-org/pkgdash/proto"
)
func (h *Handler) ModulesList(ctx context.Context, req *pb.ModulesListReq, rsp *pb.ModulesListRsp) error {
logger := h.svc.Logger()
logger.Debug(ctx, "Start GetModule")
err := req.Validate()
if err != nil {
logger.Error(ctx, err)
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
return httpsrv.SetError(NewValidationError(err))
}
modules, err := h.store.ModulesList(ctx, req)
if err != nil {
logger.Error(ctx, err)
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
return httpsrv.SetError(NewInternalError(err))
}
for _, mod := range modules {
rsp.Modules = append(rsp.Modules, models.NewModule(mod))
}
logger.Debug(ctx, "Success finish getModule")
return nil
}

View File

@@ -0,0 +1,32 @@
package handler
import (
"context"
"net/http"
httpsrv "go.unistack.org/micro-server-http/v4"
pb "git.unistack.org/unistack-org/pkgdash/proto"
)
func (h *Handler) PackagesCreate(ctx context.Context, req *pb.PackagesCreateReq, rsp *pb.PackagesCreateRsp) 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
}

View File

@@ -0,0 +1,29 @@
package handler
import (
"context"
"net/http"
httpsrv "go.unistack.org/micro-server-http/v4"
pb "git.unistack.org/unistack-org/pkgdash/proto"
)
func (h *Handler) PackagesDelete(ctx context.Context, req *pb.PackagesDeleteReq, rsp *pb.PackagesDeleteRsp) 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.PackagesDelete(ctx, req); err != nil {
logger.Error(ctx, err)
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
return httpsrv.SetError(NewInternalError(err))
}
logger.Debug(ctx, "Success finish UpdatePackage")
return nil
}

View File

@@ -0,0 +1,28 @@
package handler
import (
"context"
"net/http"
httpsrv "go.unistack.org/micro-server-http/v4"
"git.unistack.org/unistack-org/pkgdash/internal/models"
pb "git.unistack.org/unistack-org/pkgdash/proto"
)
func (h *Handler) PackagesList(ctx context.Context, req *pb.PackagesListReq, rsp *pb.PackagesListRsp) error {
logger := h.svc.Logger()
logger.Debug(ctx, "Start getListPackage")
packages, err := h.store.PackagesList(ctx, req)
if err != nil {
logger.Errorf(ctx, "error db response: %v", err)
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
return httpsrv.SetError(NewInternalError(err))
}
for _, pkg := range packages {
rsp.Packages = append(rsp.Packages, models.NewPackage(pkg))
}
logger.Debug(ctx, "Success finish getListPackage")
return nil
}

View File

@@ -0,0 +1,31 @@
package handler
import (
"context"
"net/http"
httpsrv "go.unistack.org/micro-server-http/v4"
pb "git.unistack.org/unistack-org/pkgdash/proto"
)
func (h *Handler) PackagesUpdate(ctx context.Context, req *pb.PackagesUpdateReq, rsp *pb.PackagesUpdateRsp) 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.PackagesUpdate(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
}