-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
93 lines (77 loc) · 2.23 KB
/
main.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
86
87
88
89
90
91
92
93
package main
import (
"context"
"fmt"
"log"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"gopkg.in/mgo.v2/bson"
)
type InvitationData struct {
GuestName string
EventDate string
EventTime string
EventLocation string
RSVPlink string
ContactName string
ContactEmail string
ContactPhone string
}
type User struct {
Name string `json:"name" bson:"name"`
Index int `json:"index" bson:"index"`
}
func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*")
client, err := mongo.Connect(context.Background(), options.Client().ApplyURI("mongodb+srv://root:[email protected]/?retryWrites=true&w=majority"))
if err != nil {
log.Fatal(err)
}
defer client.Disconnect(context.Background())
db := client.Database("welcome")
collection := db.Collection("users")
router.GET("/:index", func(c *gin.Context) {
indexStr := c.Param("index")
num, _ := strconv.Atoi(indexStr)
filter := bson.M{"index": int(num)}
var user User
collection.FindOne(context.Background(), filter).Decode(&user)
data := InvitationData{
GuestName: user.Name,
EventDate: "July 11, 2023",
EventTime: "9:00 PM",
EventLocation: "Mahadev mandir, krishnakunj soc.",
RSVPlink: "http://example.com/rsvp",
ContactName: "Jane Smith",
ContactEmail: "[email protected]",
ContactPhone: "123-456-7890",
}
c.HTML(http.StatusOK, "invitation.html", gin.H{
"data": data,
})
})
router.GET("/add", func(c *gin.Context) {
c.HTML(http.StatusOK, "add.html", gin.H{})
})
router.POST("/users", func(c *gin.Context) {
var user User
if err := c.ShouldBindJSON(&user); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
filter := bson.M{"index": user.Index}
update := bson.M{"$set": bson.M{"name": user.Name}}
resp, err := collection.UpdateOne(context.Background(), filter, update, options.Update().SetUpsert(true))
fmt.Printf("resp: %v\n", resp)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "User name added successfully"})
})
router.Run(":80")
}