示例#1
0
文件: boards.go 项目: fortytw2/eden
// CreateBoard creates a new board
func CreateBoard(ds *datastore.Datastore) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		user, err := util.Authenticate(r, ds)
		if err != nil {
			util.JSONError(w, err, http.StatusUnauthorized)
			return
		}

		body, err := ioutil.ReadAll(r.Body)
		if err != nil {
			util.JSONError(w, err, http.StatusBadRequest)
			return
		}

		var nbd newBoardData
		err = json.Unmarshal(body, &nbd)
		if err != nil {
			util.JSONError(w, err, http.StatusBadRequest)
			return
		}

		b := &model.Board{
			Name:    nbd.Name,
			Summary: nbd.Summary,
			Creator: user.Username,
		}

		err = ds.CreateBoard(b)
		if err != nil {
			util.JSONError(w, err, http.StatusBadRequest)
			return
		}

		w.Header().Set("Content-Type", "application/json")
		json.NewEncoder(w).Encode(map[string]string{"success": "board submitted for approval"})
	}
}