Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit 1d2e2d9

Browse files
committed
added bulk insert and increased the default max upload to 150mb
1 parent 903dad3 commit 1d2e2d9

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

db.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ type Database struct {
2525

2626
func (database *Database) dbreq(w http.ResponseWriter, r *http.Request) {
2727
if r.Method == http.MethodPost {
28-
database.add(w, r)
28+
if len(r.URL.Query().Get("bulk")) > 0 {
29+
database.bulkAdd(w, r)
30+
} else {
31+
database.add(w, r)
32+
}
2933
} else if r.Method == http.MethodPut {
3034
database.update(w, r)
3135
} else if r.Method == http.MethodDelete {
@@ -81,6 +85,32 @@ func (database *Database) add(w http.ResponseWriter, r *http.Request) {
8185
respond(w, http.StatusCreated, doc)
8286
}
8387

88+
func (database *Database) bulkAdd(w http.ResponseWriter, r *http.Request) {
89+
conf, auth, err := middleware.Extract(r, true)
90+
if err != nil {
91+
http.Error(w, err.Error(), http.StatusBadRequest)
92+
return
93+
}
94+
95+
curDB := database.client.Database(conf.Name)
96+
97+
_, r.URL.Path = ShiftPath(r.URL.Path)
98+
col, _ := ShiftPath(r.URL.Path)
99+
100+
var v []interface{}
101+
if err := json.NewDecoder(r.Body).Decode(&v); err != nil {
102+
http.Error(w, err.Error(), http.StatusInternalServerError)
103+
return
104+
}
105+
106+
if err := database.base.BulkAdd(auth, curDB, col, v); err != nil {
107+
http.Error(w, err.Error(), http.StatusInternalServerError)
108+
return
109+
}
110+
111+
respond(w, http.StatusCreated, true)
112+
}
113+
84114
func (database *Database) list(w http.ResponseWriter, r *http.Request) {
85115
page, size := getPagination(r.URL)
86116

db/base.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,31 @@ func (b *Base) Add(auth internal.Auth, db *mongo.Database, col string, doc map[s
3939
return doc, nil
4040
}
4141

42+
func (b *Base) BulkAdd(auth internal.Auth, db *mongo.Database, col string, docs []interface{}) error {
43+
for _, item := range docs {
44+
doc, ok := item.(map[string]interface{})
45+
if !ok {
46+
return fmt.Errorf("unable to cast docs to map")
47+
}
48+
49+
delete(doc, "id")
50+
delete(doc, internal.FieldID)
51+
delete(doc, internal.FieldAccountID)
52+
delete(doc, internal.FieldOwnerID)
53+
54+
doc[internal.FieldID] = primitive.NewObjectID()
55+
doc[internal.FieldAccountID] = auth.AccountID
56+
doc[internal.FieldOwnerID] = auth.UserID
57+
}
58+
59+
ctx := context.Background()
60+
if _, err := db.Collection(col).InsertMany(ctx, docs); err != nil {
61+
return err
62+
}
63+
64+
return nil
65+
}
66+
4267
type PagedResult struct {
4368
Page int64 `json:"page"`
4469
Size int64 `json:"size"`

storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func upload(w http.ResponseWriter, r *http.Request) {
3535

3636
// check for file size
3737
// TODO: This should be based on current plan
38-
if h.Size/(1000*1000) > 64 {
38+
if h.Size/(1000*1000) > 150 {
3939
http.Error(w, "file size exeeded your limit", http.StatusBadRequest)
4040
return
4141
}

0 commit comments

Comments
 (0)