-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoWithGinService.go
85 lines (70 loc) · 1.8 KB
/
GoWithGinService.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
73
74
75
76
77
78
79
80
81
82
83
84
85
// main.go
package main
import (
"log"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
// Customer struct represents customer details
type Customer struct {
Name string `json:"name"`
Code string `json:"code"`
}
// Order struct represents order details
type Order struct {
Item string `json:"item"`
Amount float64 `json:"amount"`
Time time.Time `json:"time"`
}
// Dummy data storage
var customers []Customer
var orders []Order
func main() {
router := gin.Default()
// Routes for customers
router.GET("/customers", getCustomers)
router.POST("/customers", createCustomer)
// Routes for orders
router.GET("/orders", getOrders)
router.POST("/orders", createOrder)
// Run the server
if err := router.Run(":8080"); err != nil {
log.Fatal(err)
}
}
// Handler to get all customers
func getCustomers(c *gin.Context) {
c.JSON(http.StatusOK, customers)
}
// Handler to create a new customer
func createCustomer(c *gin.Context) {
var customer Customer
if err := c.BindJSON(&customer); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
customers = append(customers, customer)
c.JSON(http.StatusCreated, customer)
}
// Handler to get all orders
func getOrders(c *gin.Context) {
c.JSON(http.StatusOK, orders)
}
// Handler to create a new order
func createOrder(c *gin.Context) {
var order Order
if err := c.BindJSON(&order); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
order.Time = time.Now()
orders = append(orders, order)
// Send SMS alert here
sendSMSAlert(order)
c.JSON(http.StatusCreated, order)
}
// Dummy function to simulate sending SMS alert
func sendSMSAlert(order Order) {
log.Printf("SMS Alert: New order added - Item: %s, Amount: %.2f, Time: %s", order.Item, order.Amount, order.Time.Format(time.RFC3339))
}