-
Notifications
You must be signed in to change notification settings - Fork 1
/
lostDevicesPage.go
55 lines (51 loc) · 1.45 KB
/
lostDevicesPage.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
package main
import (
"encoding/json"
"net/http"
"github.com/globalsign/mgo/bson"
)
func lostDevicesPage(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
if r.FormValue("userId") == "" {
writeResponse(w, requiredInputError("Kullanıcı numarası"))
} else {
var devices, control = getLostDeviceList(r.FormValue("userId"))
if devices == nil {
if control == "nil" {
writeResponse(w, notFindRecordError())
} else if control == "ID" {
writeResponse(w, objectIDError())
} else {
writeResponse(w, someThingWentWrong())
}
} else {
writeResponse(w, string(devices))
}
}
} else {
writeResponse(w, notValidRequestError(r.Method))
}
}
func getLostDeviceList(id string) ([]byte, string) {
var data []byte
var beaconInfo *LostBeaconInApp
controlID, errID := checkObjID(id)
if errID == true {
var l []*LostBeaconInApp
beacon := &LostBeacon{}
lostBeacon := connection.Collection("lost_beacons").Find(bson.M{"user_infos.user_id": bson.ObjectIdHex(controlID)})
for lostBeacon.Next(beacon) {
beaconInfo = &LostBeaconInApp{beacon.BeaconInfos.BeaconID, beacon.UserInfos.UserPhone, beacon.UserInfos.UserMail, beacon.Created, beacon.LostLat, beacon.LostLong, beacon.LostDesc, beacon.LostStatus}
l = append(l, beaconInfo)
}
if l == nil {
return nil, "nil"
}
if l != nil {
response := &LostDevices{l}
data, _ = json.Marshal(response)
return addError(data), ""
}
}
return data, "ID"
}