-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.go
62 lines (52 loc) · 1.37 KB
/
example.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
package main
import (
"encoding/json"
"log"
"truewallet-gift-voucher-with-golang/functions"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
)
type Voucher struct {
Mobile string `json:"mobile"`
Code string `json:"code"`
}
type Handler struct {
Status struct {
Code string `json:"code"`
} `json:"status"`
}
func main() {
perform := fiber.New(fiber.Config{StrictRouting: true})
perform.Use(logger.New())
perform.Post("/transactions", func(c *fiber.Ctx) error {
voucher := new(Voucher)
if err := c.BodyParser(voucher); err != nil {
log.Println(err)
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"Code": 400,
"Message": "Bad Request",
})
}
redeemer, err := functions.NewRequestCampaign(voucher.Mobile, voucher.Code)
if err != nil {
log.Println(err)
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"Code": 400,
"Message": "Bad Request",
})
}
handler := new(Handler)
if err := json.Unmarshal([]byte(redeemer), handler); err != nil {
log.Println(err)
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
"Code": 422,
"Message": "Unprocessable Entity",
})
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"Code": handler.Status.Code,
"Message": "<You can edit the message for your service.>",
})
})
perform.Listen("127.0.0.1:8080")
}