-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservices.go
45 lines (35 loc) · 876 Bytes
/
services.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
package main
import (
"net/http"
"time"
"github.com/labstack/echo/v4"
// import Price from ../models.go
)
func SubmitPrice(c echo.Context) error {
var price Price
if err := c.Bind(&price); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "Invalid request payload",
})
}
if err := c.Validate(&price); err != nil {
return c.JSON(http.StatusBadRequest, map[string]string{
"message": "Validation failed",
"error": err.Error(),
})
}
now := time.Now().UnixMilli()
price.Time = now
if err := db.Create(&price).Error; err != nil {
return c.JSON(http.StatusInternalServerError, map[string]string{
"message": "Failed to save price",
"error": err.Error(),
})
}
return c.JSON(200, map[string]interface{}{
"ok": true,
"price": price.Price,
"time": now,
"details": price.Details,
})
}