-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.go
102 lines (82 loc) · 1.95 KB
/
handler.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
94
95
96
97
98
99
100
101
102
package main
import (
"context"
"database/sql"
"log"
"net/http"
db "github.com/Grama-Check/Address-Check-Api/db/sqlc"
"github.com/Grama-Check/Address-Check-Api/util"
"github.com/Grama-Check/Address-Check-Api/models"
"github.com/gin-gonic/gin"
_ "github.com/lib/pq"
)
var config util.Config
var conn *sql.DB
func init() {
var err error
config, err = util.LoadConfig(".")
if err != nil {
log.Fatal("Cannot load config")
}
conn, err = sql.Open(config.DBDriver, config.DBSource)
//conn, err := sql.Open(config.DBDriver, config.DBSource)
if err != nil {
log.Println("HELP")
return
}
}
func IdentityCheck(c *gin.Context) {
ctx := context.Background()
user := models.UserData{}
err := c.BindJSON(&user)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, "Couldn't parse json request")
return
}
// Send a ping to make sure the database connection is alive.
if err := conn.Ping(); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, "Cannot connect to database")
return
}
queries := db.New(conn)
_, err = queries.GetPerson(ctx, user.NIC)
exists := err == nil
log.Println(user.NIC, ":", exists)
c.JSON(
http.StatusOK,
gin.H{
"nic": user.NIC,
"exists": exists,
},
)
}
func CreatePerson(c *gin.Context) {
person := models.PersonData{}
ctx := context.Background()
err := c.BindJSON(&person)
log.Println(person)
if err != nil {
log.Println("Couldn't bind input to person model")
}
// Send a ping to make sure the database connection is alive.
if err = conn.Ping(); err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, "Cannot connect to database")
return
}
queries := db.New(conn)
args := db.CreatePersonParams{
Name: person.Name,
Address: person.Address,
Nic: person.NIC,
}
person1, err := queries.CreatePerson(ctx, args)
if err != nil {
log.Println("Couldn't add person to database")
}
c.JSON(
http.StatusOK,
gin.H{
"nic": person1.Nic,
},
)
}