-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaccount_delete.go
68 lines (61 loc) · 1.77 KB
/
account_delete.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
package basicserver
import (
"errors"
"github.com/globalsign/mgo/bson"
"github.com/kataras/iris"
)
type fileItem struct {
ID bson.ObjectId `bson:"_id"`
Filename string `bson:"filename"`
}
// ServeRemoveAccountDelete serves
// Method: DELETE
// Resource: http://localhost/account
//
// This resource requires `Authorization` header, e.g.:
//
// Authorization: Bearer {token}
//
// If everything goes well, then this will return status code `200` and no response body.
//
// In case of error, this will return status code `400` or `500` and `text/plain` error
// message as a response.
//
// In case of invalid/expired token, this will return status code `401` and `text/plain`
// error message as a response.
//
func (app *BasicApp) ServeRemoveAccountDelete() iris.Handler {
return func(ctx iris.Context) {
uid := ctx.Values().Get("uid").(string)
objectUID := bson.ObjectIdHex(uid)
// remove all user files
filenamePrefix := "^" + uid + ":"
var item fileItem
iter := app.Coll.Files.Find(bson.M{"filename": bson.RegEx{Pattern: filenamePrefix}}).Iter()
for iter.Next(&item) {
err := app.Coll.Files.RemoveId(item.ID)
if err != nil {
app.HandleError(err, ctx, iris.StatusInternalServerError)
return
}
}
// then remove user state
err := app.Coll.States.RemoveId(objectUID)
if err != nil {
if err.Error() != "not found" { // state might not be existing yet
app.HandleError(err, ctx, iris.StatusInternalServerError)
}
}
// to finally remove the user
err = app.Coll.Users.RemoveId(objectUID)
if err != nil {
if err.Error() == "not found" {
err := errors.New("Account Not Found")
app.HandleError(err, ctx, iris.StatusUnauthorized)
return
}
app.HandleError(err, ctx, iris.StatusInternalServerError)
return
}
}
}