/
remove.go
68 lines (57 loc) · 2.05 KB
/
remove.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package handler
import (
"context"
"github.com/aws/aws-lambda-go/events"
"github.com/ricardojonathanromero/lambda-serverless-example/api-gateway-example/todo-delete/internal/domain"
"github.com/ricardojonathanromero/lambda-serverless-example/api-gateway-example/todo-delete/internal/port"
"github.com/ricardojonathanromero/lambda-utilities/utils"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson/primitive"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"net/http"
)
type handle struct {
srv port.IService
}
var (
log = logrus.New()
commonHeaders = map[string]string{"Content-Type": "application/json"}
_ port.IHandler = (*handle)(nil)
)
// HandleRequest business logic/*
func (h *handle) HandleRequest(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
var id interface{}
res := events.APIGatewayProxyResponse{Headers: commonHeaders, IsBase64Encoded: true}
_, _ = tracer.StartSpanFromContext(ctx, "list todos")
log.Info("start HandleRequest()")
defer log.Info("end HandleRequest()")
idStr := req.PathParameters["id"]
log.Infof("id: %v", idStr)
if !primitive.IsValidObjectID(idStr) && utils.StringToInt(idStr) <= 0 {
log.Errorf("invalid id: %v", idStr)
res.StatusCode = http.StatusBadRequest
res.Body = utils.EncodeStr(utils.ToString(domain.NewErr("invalid_request", "the id sent is not valid")))
return res, nil
}
if primitive.IsValidObjectID(idStr) {
id, _ = primitive.ObjectIDFromHex(idStr)
} else {
id = utils.StringToInt(idStr)
}
log.Info("removing...")
err := h.srv.RemoveItem(id)
if err != nil {
log.Errorf("error retrieving item: %v", err)
res.StatusCode = http.StatusNotFound
res.Body = utils.EncodeStr(utils.ToString(domain.NewErr("not_found", err.Error())))
return res, nil
}
log.Info("returning response")
res.StatusCode = http.StatusAccepted
res.Body = utils.EncodeStr(`{"message":"removed"}`)
return res, nil
}
// New constructor/*
func New(srv port.IService) port.IHandler {
return &handle{srv: srv}
}