(app *App, w http.ResponseWriter, r *http.Request)
| 425 | } |
| 426 | |
| 427 | func newCollection(app *App, w http.ResponseWriter, r *http.Request) error { |
| 428 | reqJSON := IsJSON(r) |
| 429 | alias := r.FormValue("alias") |
| 430 | title := r.FormValue("title") |
| 431 | |
| 432 | var missingParams, accessToken string |
| 433 | var u *User |
| 434 | c := struct { |
| 435 | Alias string `json:"alias" schema:"alias"` |
| 436 | Title string `json:"title" schema:"title"` |
| 437 | Web bool `json:"web" schema:"web"` |
| 438 | }{} |
| 439 | if reqJSON { |
| 440 | // Decode JSON request |
| 441 | decoder := json.NewDecoder(r.Body) |
| 442 | err := decoder.Decode(&c) |
| 443 | if err != nil { |
| 444 | log.Error("Couldn't parse post update JSON request: %v\n", err) |
| 445 | return ErrBadJSON |
| 446 | } |
| 447 | } else { |
| 448 | // TODO: move form parsing to formDecoder |
| 449 | c.Alias = alias |
| 450 | c.Title = title |
| 451 | } |
| 452 | |
| 453 | if c.Alias == "" { |
| 454 | if c.Title != "" { |
| 455 | // If only a title was given, just use it to generate the alias. |
| 456 | c.Alias = getSlug(c.Title, "") |
| 457 | } else { |
| 458 | missingParams += "`alias` " |
| 459 | } |
| 460 | } |
| 461 | if c.Title == "" { |
| 462 | missingParams += "`title` " |
| 463 | } |
| 464 | if missingParams != "" { |
| 465 | return impart.HTTPError{http.StatusBadRequest, fmt.Sprintf("Parameter(s) %srequired.", missingParams)} |
| 466 | } |
| 467 | |
| 468 | var userID int64 |
| 469 | var err error |
| 470 | if reqJSON && !c.Web { |
| 471 | accessToken = r.Header.Get("Authorization") |
| 472 | if accessToken == "" { |
| 473 | return ErrNoAccessToken |
| 474 | } |
| 475 | userID = app.db.GetUserID(accessToken) |
| 476 | if userID == -1 { |
| 477 | return ErrBadAccessToken |
| 478 | } |
| 479 | } else { |
| 480 | u = getUserSession(app, r) |
| 481 | if u == nil { |
| 482 | return ErrNotLoggedIn |
| 483 | } |
| 484 | userID = u.ID |
nothing calls this directly
no test coverage detected