forked from pclubiitk/puppy-love
-
Notifications
You must be signed in to change notification settings - Fork 0
/
declare.go
48 lines (39 loc) · 967 Bytes
/
declare.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
package controllers
import (
"log"
"net/http"
"github.com/pclubiitk/puppy-love/models"
"github.com/gin-gonic/gin"
"gopkg.in/mgo.v2/bson"
)
// @AUTH @Admin Create the entries in the declare table
// ----------------------------------------------------
func DeclarePrepare(c *gin.Context) {
id, err := SessionId(c)
if err != nil || id != "admin" {
c.AbortWithStatus(http.StatusForbidden)
return
}
type typeIds struct {
Id string `json:"_id" bson:"_id"`
}
var people []typeIds
if err := Db.GetCollection("user").
Find(bson.M{"dirty": false}).
All(&people); err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
return
}
bulk := Db.GetCollection("declare").Bulk()
for _, pe := range people {
res := models.NewDeclareTable(pe.Id)
bulk.Upsert(res.Selector, res.Change)
}
r, err := bulk.Run()
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
log.Println(err)
return
}
c.JSON(http.StatusOK, r)
}