/
webservice.go
72 lines (56 loc) · 1.78 KB
/
webservice.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
69
70
71
72
package service
import (
"fmt"
"net/http"
"github.com/dimcz/tgpollbot/storage"
"github.com/go-redis/redis/v8"
"github.com/labstack/echo/v4"
"github.com/rs/xid"
"github.com/sirupsen/logrus"
)
type WebService struct {
cache *Cache
}
func (srv *WebService) Post(ctx echo.Context) error {
var task storage.Task
if err := ctx.Bind(&task); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, errorWrap(err, "could not decode user data"))
}
if err := ctx.Validate(&task); err != nil {
return echo.NewHTTPError(http.StatusUnprocessableEntity, errorWrap(err, "could not validate user data"))
}
id := xid.New().String()
r := storage.Request{
Status: storage.RecordProcessing,
Task: task,
}
if err := srv.cache.InitRequest(ctx.Request().Context(), id, r); err != nil {
logrus.Error("could not set record to cache with error: ", err)
return echo.NewHTTPError(http.StatusInternalServerError, errorWrap(err, "failed to set new request"))
}
return ctx.JSON(http.StatusCreated, map[string]string{
"request_id": id,
})
}
func (srv *WebService) Get(ctx echo.Context) error {
id := ctx.Param("request_id")
r, err := srv.cache.Get(ctx.Request().Context(), id)
if err != nil {
if err == redis.Nil {
return echo.NewHTTPError(http.StatusNotFound,
errorWrap(fmt.Errorf("could not found request with id %s", id), "request not found"))
}
logrus.Error("failed to get request from cache with error: ", err)
return echo.NewHTTPError(http.StatusInternalServerError, errorWrap(err, "failed to get request"))
}
return ctx.JSON(http.StatusOK, r.DTO())
}
func NewWebService(cache *Cache) *WebService {
return &WebService{cache}
}
func errorWrap(err error, message string) map[string]string {
return map[string]string{
"error": message,
"details": err.Error(),
}
}