func HandleProjectCreate(w http.ResponseWriter, r *http.Request) { ctx := GetContext(r) if ctx.Account == nil { http.Redirect(w, r, "/login", http.StatusSeeOther) return } err := r.ParseForm() catch(r, err) vars := mux.Vars(r) idStr := vars["id"] if !bson.IsObjectIdHex(idStr) { ServeNotFound(w, r) return } id := bson.ObjectIdHex(idStr) org, err := data.GetOrganization(id) catch(r, err) if org == nil { ServeNotFound(w, r) return } if org.OwnerID != ctx.Account.ID { ServeForbidden(w, r) return } body := struct { Name string `schema:"name"` }{} err = schema.NewDecoder().Decode(&body, r.PostForm) catch(r, err) switch { case body.Name == "": RedirectBack(w, r) return } prj := data.Project{ Name: body.Name, OwnerID: ctx.Account.ID, OrganizationID: org.ID, } err = prj.Put() catch(r, err) mem := data.Member{ OrganizationID: prj.OrganizationID, ProjectID: prj.ID, AccountID: prj.OwnerID, InviterID: prj.OwnerID, InvitedAt: time.Now(), } err = mem.Put() catch(r, err) prj.MemberIDs = append(prj.MemberIDs, mem.ID) err = prj.Put() catch(r, err) http.Redirect(w, r, "/projects/"+prj.ID.Hex(), http.StatusSeeOther) }
func HandleMemberAdd(w http.ResponseWriter, r *http.Request) { ctx := GetContext(r) if ctx.Account == nil { http.Redirect(w, r, "/login", http.StatusSeeOther) return } err := r.ParseForm() catch(r, err) vars := mux.Vars(r) idStr := vars["id"] if !bson.IsObjectIdHex(idStr) { ServeNotFound(w, r) return } id := bson.ObjectIdHex(idStr) prj, err := data.GetProject(id) catch(r, err) if prj == nil { ServeNotFound(w, r) return } if prj.OwnerID != ctx.Account.ID { ServeForbidden(w, r) return } body := struct { Email string `schema:"email"` }{} err = schema.NewDecoder().Decode(&body, r.PostForm) catch(r, err) acc, err := data.GetAccountEmail(body.Email) catch(r, err) if acc == nil { RedirectBack(w, r) return } mem, err := data.GetMemberProjectAccount(prj.ID, acc.ID) catch(r, err) if mem != nil { RedirectBack(w, r) return } nM := data.Member{ OrganizationID: prj.OrganizationID, ProjectID: prj.ID, AccountID: acc.ID, InviterID: ctx.Account.ID, InvitedAt: time.Now(), } err = nM.Put() catch(r, err) mems, err := data.ListMembersProject(prj.ID, 0, math.MaxInt32) catch(r, err) prj.MemberIDs = []bson.ObjectId{} for _, mem := range mems { prj.MemberIDs = append(prj.MemberIDs, mem.ID) } err = prj.Put() catch(r, err) http.Redirect(w, r, "/projects/"+prj.ID.Hex(), http.StatusSeeOther) }