/
geojson.go
183 lines (174 loc) · 5.32 KB
/
geojson.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package gisbackend
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
pasproj "github.com/HRMonitorr/PasetoprojectBackend"
"github.com/petapedia/peda"
"github.com/whatsauth/watoken"
)
func GCHandlerFunc(publickey, Mongostring, dbname, colname string, r *http.Request) string {
resp := new(pasproj.Credential)
tokenlogin := r.Header.Get("Login")
if tokenlogin == "" {
resp.Status = false
resp.Message = "Header Login Not Exist"
} else {
existing := IsExist(tokenlogin, os.Getenv(publickey))
if !existing {
resp.Status = false
resp.Message = "Kamu kayaknya belum punya akun"
} else {
koneksyen := GetConnectionMongo(Mongostring, dbname)
datageo := GetAllGeoData(koneksyen, colname)
jsoncihuy, _ := json.Marshal(datageo)
resp.Status = true
resp.Message = "Data Berhasil diambil"
resp.Token = string(jsoncihuy)
}
}
return ReturnStringStruct(resp)
}
func GCFPostCoordinate(Mongostring, Publickey, dbname, colname string, r *http.Request) string {
req := new(Credents)
conn := GetConnectionMongo(Mongostring, dbname)
resp := new(LonLatProperties)
err := json.NewDecoder(r.Body).Decode(&resp)
tokenlogin := r.Header.Get("Login")
if tokenlogin == "" {
req.Status = strconv.Itoa(http.StatusNotFound)
req.Message = "Header Login Not Exist"
} else {
existing := IsExist(tokenlogin, os.Getenv(Publickey))
if !existing {
req.Status = strconv.Itoa(http.StatusNotFound)
req.Message = "Kamu kayaknya belum punya akun"
} else {
if err != nil {
req.Status = strconv.Itoa(http.StatusNotFound)
req.Message = "error parsing application/json: " + err.Error()
} else {
req.Status = strconv.Itoa(http.StatusOK)
Ins := InsertDataLonlat(conn, colname,
resp.Coordinates,
resp.Name,
resp.Volume,
resp.Type)
req.Message = fmt.Sprintf("%v:%v", "Berhasil Input data", Ins)
}
}
}
return ReturnStringStruct(req)
}
func GCFUpdateName(publickey, Mongostring, dbname string, r *http.Request) string {
req := new(Credents)
resp := new(LonLatProperties)
err := json.NewDecoder(r.Body).Decode(&resp)
tokenlogin := r.Header.Get("Login")
if tokenlogin == "" {
req.Status = strconv.Itoa(http.StatusNotFound)
req.Message = "Header Login Not Exist"
} else {
existing := IsExist(tokenlogin, os.Getenv(publickey))
if !existing {
req.Status = strconv.Itoa(http.StatusNotFound)
req.Message = "Kamu kayaknya belum punya akun"
} else {
if err != nil {
req.Status = strconv.Itoa(http.StatusNotFound)
req.Message = "error parsing application/json: " + err.Error()
} else {
req.Status = strconv.Itoa(http.StatusOK)
Ins := UpdateNameGeo(Mongostring, dbname, context.Background(),
LonLatProperties{
Type: resp.Type,
Name: resp.Name,
Volume: resp.Volume,
})
req.Message = fmt.Sprintf("%v:%v", "Berhasil Update data", Ins)
}
}
}
return ReturnStringStruct(req)
}
func GCFDeleteLon(publickey, Mongostring, dbname string, r *http.Request) string {
req := new(Credents)
resp := new(LonLatProperties)
err := json.NewDecoder(r.Body).Decode(&resp)
tokenlogin := r.Header.Get("Login")
if tokenlogin == "" {
req.Status = strconv.Itoa(http.StatusNotFound)
req.Message = "Header Login Not Exist"
} else {
existing := IsExist(tokenlogin, os.Getenv(publickey))
if !existing {
req.Status = strconv.Itoa(http.StatusNotFound)
req.Message = "Kamu kayaknya belum punya akun"
} else {
if err != nil {
req.Status = strconv.Itoa(http.StatusNotFound)
req.Message = "error parsing application/json: " + err.Error()
} else {
req.Status = strconv.Itoa(http.StatusOK)
Ins := DeleteDataGeo(Mongostring, dbname, context.Background(),
LonLatProperties{
Type: resp.Type,
Name: resp.Name,
Volume: resp.Volume,
})
req.Message = fmt.Sprintf("%v:%v", "Berhasil Hapus data", Ins)
}
}
}
return ReturnStringStruct(req)
}
func ReturnStringStruct(Data any) string {
jsonee, _ := json.Marshal(Data)
return string(jsonee)
}
func Register(Mongoenv, dbname string, r *http.Request) string {
resp := new(pasproj.Credential)
userdata := new(RegisterStruct)
resp.Status = false
conn := GetConnectionMongo(Mongoenv, dbname)
err := json.NewDecoder(r.Body).Decode(&userdata)
if err != nil {
resp.Message = "error parsing application/json: " + err.Error()
} else {
resp.Status = true
hash, err := pasproj.HashPass(userdata.Password)
if err != nil {
resp.Message = "Gagal Hash Password" + err.Error()
}
InsertUserdata(conn, userdata.Username, hash)
resp.Message = "Berhasil Input data"
}
response := pasproj.ReturnStringStruct(resp)
return response
}
func Login(Privatekey, MongoEnv, dbname, Colname string, r *http.Request) string {
var resp pasproj.Credential
mconn := pasproj.MongoCreateConnection(MongoEnv, dbname)
var datauser peda.User
err := json.NewDecoder(r.Body).Decode(&datauser)
if err != nil {
resp.Message = "error parsing application/json: " + err.Error()
} else {
if peda.IsPasswordValid(mconn, Colname, datauser) {
tokenstring, err := watoken.Encode(datauser.Username, os.Getenv(Privatekey))
if err != nil {
resp.Message = "Gagal Encode Token : " + err.Error()
} else {
resp.Status = true
resp.Message = "Selamat Datang"
resp.Token = tokenstring
}
} else {
resp.Message = "Password Salah"
}
}
return pasproj.ReturnStringStruct(resp)
}