#19 reset HEAD
This commit is contained in:
39
internal/handler/comment_create.go
Normal file
39
internal/handler/comment_create.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
httpsrv "go.unistack.org/micro-server-http/v3"
|
||||
"go.unistack.org/pkgdash/internal/models"
|
||||
pb "go.unistack.org/pkgdash/proto"
|
||||
)
|
||||
|
||||
func (h *Handler) CommentCreate(ctx context.Context, req *pb.CommentCreateReq, rsp *pb.CommentCreateRsp) error {
|
||||
h.logger.Debug(ctx, "Start AddComment")
|
||||
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
h.logger.Error(ctx, "validation error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
||||
return httpsrv.SetError(NewValidationError(err))
|
||||
}
|
||||
|
||||
var com *models.Comment
|
||||
if com, err = h.store.CommentCreate(ctx, req); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
httpsrv.SetRspCode(ctx, http.StatusNotFound)
|
||||
return httpsrv.SetError(NewNotFoundError(err))
|
||||
}
|
||||
h.logger.Error(ctx, "comment create error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
|
||||
return httpsrv.SetError(NewInternalError(err))
|
||||
}
|
||||
|
||||
rsp.Comment = models.NewComment(com)
|
||||
|
||||
h.logger.Debug(ctx, "Success finish addComment")
|
||||
return nil
|
||||
}
|
36
internal/handler/comment_delete.go
Normal file
36
internal/handler/comment_delete.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
httpsrv "go.unistack.org/micro-server-http/v3"
|
||||
pb "go.unistack.org/pkgdash/proto"
|
||||
)
|
||||
|
||||
func (h *Handler) CommentDelete(ctx context.Context, req *pb.CommentDeleteReq, rsp *pb.CommentDeleteRsp) error {
|
||||
h.logger.Debug(ctx, "Start AddComment")
|
||||
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
h.logger.Error(ctx, "validate error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
||||
return httpsrv.SetError(NewValidationError(err))
|
||||
}
|
||||
|
||||
if err = h.store.CommentDelete(ctx, req); err != nil {
|
||||
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
httpsrv.SetRspCode(ctx, http.StatusNotFound)
|
||||
return httpsrv.SetError(NewNotFoundError(err))
|
||||
}
|
||||
h.logger.Error(ctx, "comment delete error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
|
||||
return httpsrv.SetError(NewInternalError(err))
|
||||
}
|
||||
|
||||
h.logger.Debug(ctx, "Success finish addComment")
|
||||
return nil
|
||||
}
|
35
internal/handler/comment_list.go
Normal file
35
internal/handler/comment_list.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
httpsrv "go.unistack.org/micro-server-http/v3"
|
||||
"go.unistack.org/pkgdash/internal/models"
|
||||
pb "go.unistack.org/pkgdash/proto"
|
||||
)
|
||||
|
||||
func (h *Handler) CommentList(ctx context.Context, req *pb.CommentListReq, rsp *pb.CommentListRsp) error {
|
||||
h.logger.Debug(ctx, "Start GetModule")
|
||||
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
h.logger.Error(ctx, "validate error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
||||
return httpsrv.SetError(NewValidationError(err))
|
||||
}
|
||||
|
||||
comments, err := h.store.CommentList(ctx, req)
|
||||
if err != nil {
|
||||
h.logger.Error(ctx, "comment list error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
|
||||
return httpsrv.SetError(NewInternalError(err))
|
||||
}
|
||||
|
||||
for _, com := range comments {
|
||||
rsp.Comments = append(rsp.Comments, models.NewComment(com))
|
||||
}
|
||||
|
||||
h.logger.Debug(ctx, "Success finish getModule")
|
||||
return nil
|
||||
}
|
11
internal/handler/comment_lookup.go
Normal file
11
internal/handler/comment_lookup.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
pb "go.unistack.org/pkgdash/proto"
|
||||
)
|
||||
|
||||
func (h *Handler) CommentLookup(ctx context.Context, req *pb.CommentLookupReq, rsp *pb.CommentLookupRsp) error {
|
||||
return nil
|
||||
}
|
56
internal/handler/handler.go
Normal file
56
internal/handler/handler.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
jsonpbcodec "go.unistack.org/micro-codec-jsonpb/v3"
|
||||
"go.unistack.org/micro/v3/codec"
|
||||
"go.unistack.org/micro/v3/logger"
|
||||
"go.unistack.org/pkgdash/internal/storage"
|
||||
pb "go.unistack.org/pkgdash/proto"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
logger logger.Logger
|
||||
store storage.Storage
|
||||
codec codec.Codec
|
||||
}
|
||||
|
||||
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(log logger.Logger, store storage.Storage) (*Handler, error) {
|
||||
h := &Handler{
|
||||
logger: log,
|
||||
codec: jsonpbcodec.NewCodec(),
|
||||
store: store,
|
||||
}
|
||||
|
||||
return h, nil
|
||||
}
|
27
internal/handler/handler_list.go
Normal file
27
internal/handler/handler_list.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
httpsrv "go.unistack.org/micro-server-http/v3"
|
||||
"go.unistack.org/pkgdash/internal/models"
|
||||
pb "go.unistack.org/pkgdash/proto"
|
||||
)
|
||||
|
||||
func (h *Handler) HandlerList(ctx context.Context, req *pb.HandlerListReq, rsp *pb.HandlerListRsp) error {
|
||||
h.logger.Debug(ctx, "HandlerList handler start")
|
||||
|
||||
packages, err := h.store.HandlerList(ctx, req)
|
||||
if err != nil {
|
||||
h.logger.Error(ctx, "error db response: %v", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
|
||||
return httpsrv.SetError(NewInternalError(err))
|
||||
}
|
||||
|
||||
for _, hdlr := range packages {
|
||||
rsp.Handlers = append(rsp.Handlers, models.NewHandler(hdlr))
|
||||
}
|
||||
h.logger.Debug(ctx, "HandlerList handler stop")
|
||||
return nil
|
||||
}
|
34
internal/handler/modules_list.go
Normal file
34
internal/handler/modules_list.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
httpsrv "go.unistack.org/micro-server-http/v3"
|
||||
"go.unistack.org/pkgdash/internal/models"
|
||||
pb "go.unistack.org/pkgdash/proto"
|
||||
)
|
||||
|
||||
func (h *Handler) ModuleList(ctx context.Context, req *pb.ModuleListReq, rsp *pb.ModuleListRsp) error {
|
||||
h.logger.Debug(ctx, "Start GetModule")
|
||||
|
||||
err := req.Validate()
|
||||
if err != nil {
|
||||
h.logger.Error(ctx, "validate error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
||||
return httpsrv.SetError(NewValidationError(err))
|
||||
}
|
||||
|
||||
modules, err := h.store.ModuleList(ctx, req)
|
||||
if err != nil {
|
||||
h.logger.Error(ctx, "module list error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
|
||||
return httpsrv.SetError(NewInternalError(err))
|
||||
}
|
||||
|
||||
for _, mod := range modules {
|
||||
rsp.Modules = append(rsp.Modules, models.NewModule(mod))
|
||||
}
|
||||
h.logger.Debug(ctx, "Success finish getModule")
|
||||
return nil
|
||||
}
|
32
internal/handler/package_create.go
Normal file
32
internal/handler/package_create.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
httpsrv "go.unistack.org/micro-server-http/v3"
|
||||
"go.unistack.org/pkgdash/internal/models"
|
||||
pb "go.unistack.org/pkgdash/proto"
|
||||
)
|
||||
|
||||
func (h *Handler) PackageCreate(ctx context.Context, req *pb.PackageCreateReq, rsp *pb.PackageCreateRsp) error {
|
||||
h.logger.Debug(ctx, "PackagesCreate handler start")
|
||||
|
||||
if err := req.Validate(); err != nil {
|
||||
h.logger.Error(ctx, "validate error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
||||
return httpsrv.SetError(NewValidationError(err))
|
||||
}
|
||||
|
||||
pkg, err := h.store.PackageCreate(ctx, req)
|
||||
if err != nil {
|
||||
h.logger.Error(ctx, "package create error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
||||
return httpsrv.SetError(NewValidationError(err))
|
||||
}
|
||||
|
||||
rsp.Package = models.NewPackage(pkg)
|
||||
|
||||
h.logger.Debug(ctx, "PackagesCreate handler stop")
|
||||
return nil
|
||||
}
|
28
internal/handler/package_delete.go
Normal file
28
internal/handler/package_delete.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
httpsrv "go.unistack.org/micro-server-http/v3"
|
||||
pb "go.unistack.org/pkgdash/proto"
|
||||
)
|
||||
|
||||
func (h *Handler) PackageDelete(ctx context.Context, req *pb.PackageDeleteReq, rsp *pb.PackageDeleteRsp) error {
|
||||
h.logger.Debug(ctx, "Start UpdatePackage")
|
||||
|
||||
if err := req.Validate(); err != nil {
|
||||
h.logger.Error(ctx, "validate error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
||||
return httpsrv.SetError(NewValidationError(err))
|
||||
}
|
||||
|
||||
if err := h.store.PackageDelete(ctx, req); err != nil {
|
||||
h.logger.Error(ctx, "package delete error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
|
||||
return httpsrv.SetError(NewInternalError(err))
|
||||
}
|
||||
|
||||
h.logger.Debug(ctx, "Success finish UpdatePackage")
|
||||
return nil
|
||||
}
|
27
internal/handler/package_list.go
Normal file
27
internal/handler/package_list.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
httpsrv "go.unistack.org/micro-server-http/v3"
|
||||
"go.unistack.org/pkgdash/internal/models"
|
||||
pb "go.unistack.org/pkgdash/proto"
|
||||
)
|
||||
|
||||
func (h *Handler) PackageList(ctx context.Context, req *pb.PackageListReq, rsp *pb.PackageListRsp) error {
|
||||
h.logger.Debug(ctx, "PackagesList handler start")
|
||||
|
||||
packages, err := h.store.PackageList(ctx, req)
|
||||
if err != nil {
|
||||
h.logger.Error(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))
|
||||
}
|
||||
h.logger.Debug(ctx, "PackagesList handler stop")
|
||||
return nil
|
||||
}
|
32
internal/handler/package_lookup.go
Normal file
32
internal/handler/package_lookup.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
httpsrv "go.unistack.org/micro-server-http/v3"
|
||||
"go.unistack.org/pkgdash/internal/models"
|
||||
pb "go.unistack.org/pkgdash/proto"
|
||||
)
|
||||
|
||||
func (h *Handler) PackageLookup(ctx context.Context, req *pb.PackageLookupReq, rsp *pb.PackageLookupRsp) error {
|
||||
h.logger.Debug(ctx, "Start PackagesLookup")
|
||||
|
||||
if err := req.Validate(); err != nil {
|
||||
h.logger.Error(ctx, "validate error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
||||
return httpsrv.SetError(NewValidationError(err))
|
||||
}
|
||||
|
||||
pkg, err := h.store.PackageLookup(ctx, req)
|
||||
if err != nil {
|
||||
h.logger.Error(ctx, "package lookup", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
|
||||
return httpsrv.SetError(NewInternalError(err))
|
||||
}
|
||||
|
||||
rsp.Package = models.NewPackage(pkg)
|
||||
|
||||
h.logger.Debug(ctx, "Success finish PackagesLookup")
|
||||
return nil
|
||||
}
|
27
internal/handler/package_modules.go
Normal file
27
internal/handler/package_modules.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
httpsrv "go.unistack.org/micro-server-http/v3"
|
||||
"go.unistack.org/pkgdash/internal/models"
|
||||
pb "go.unistack.org/pkgdash/proto"
|
||||
)
|
||||
|
||||
func (h *Handler) PackageModules(ctx context.Context, req *pb.PackageModulesReq, rsp *pb.PackageModulesRsp) error {
|
||||
h.logger.Debug(ctx, "PackageModules handler start")
|
||||
|
||||
modules, err := h.store.PackageModules(ctx, req)
|
||||
if err != nil {
|
||||
h.logger.Error(ctx, "error db response: %v", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
|
||||
return httpsrv.SetError(NewInternalError(err))
|
||||
}
|
||||
|
||||
for _, mod := range modules {
|
||||
rsp.Modules = append(rsp.Modules, models.NewModule(mod))
|
||||
}
|
||||
h.logger.Debug(ctx, "PackagesModules handler stop")
|
||||
return nil
|
||||
}
|
32
internal/handler/package_update.go
Normal file
32
internal/handler/package_update.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
httpsrv "go.unistack.org/micro-server-http/v3"
|
||||
"go.unistack.org/pkgdash/internal/models"
|
||||
pb "go.unistack.org/pkgdash/proto"
|
||||
)
|
||||
|
||||
func (h *Handler) PackageUpdate(ctx context.Context, req *pb.PackageUpdateReq, rsp *pb.PackageUpdateRsp) error {
|
||||
h.logger.Debug(ctx, "Start UpdatePackage")
|
||||
|
||||
if err := req.Validate(); err != nil {
|
||||
h.logger.Error(ctx, "validate error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusBadRequest)
|
||||
return httpsrv.SetError(NewValidationError(err))
|
||||
}
|
||||
|
||||
pkg, err := h.store.PackageUpdate(ctx, req)
|
||||
if err != nil {
|
||||
h.logger.Error(ctx, "package update error", err)
|
||||
httpsrv.SetRspCode(ctx, http.StatusInternalServerError)
|
||||
return httpsrv.SetError(NewInternalError(err))
|
||||
}
|
||||
|
||||
rsp.Package = models.NewPackage(pkg)
|
||||
|
||||
h.logger.Debug(ctx, "Success finish UpdatePackage")
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user