func defaultPage(params martini.Params, u *user.User, c context.Context, w http.ResponseWriter, r *http.Request) {

	rpkey := model.NewRootPageKey(c)
	var rp model.RootPage
	if err := ds.Get(c, rpkey, &rp); err != nil {
		if errors.Root(err) == datastore.ErrNoSuchEntity {
			login(c, w, r)
		} else {
			handleError(c, w, err, http.StatusInternalServerError)
		}
		return
	}

	renderPage(strconv.FormatInt(rp.PageID, 10), u, c, w, r, false)
}
Exemple #2
0
func queryPages(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)

	rpkey := model.NewRootPageKey(c)

	var rp model.RootPage
	err := ds.Get(c, rpkey, &rp)
	if err != nil && errors.Root(err) != datastore.ErrNoSuchEntity {
		handleError(c, w, err, http.StatusInternalServerError)
		return
	}

	pages, err := getPages(c)
	if err != nil {
		log.Errorf(c, err.Error())
		handleError(c, w, err, http.StatusInternalServerError)
		return
	}

	jsonPages := make([]*model.JsonPage, len(pages))

	for i, p := range pages {
		jsonPages[i] = model.NewJsonPage(p, nil, p.Key.IntID() == rp.PageID)
	}

	b, err := json.Marshal(jsonPages)
	if err != nil {
		handleError(c, w, err, http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	_, err = w.Write(b)
	if err != nil {
		handleError(c, w, err, http.StatusInternalServerError)
		return
	}
	return
}
Exemple #3
0
func putRootPage(c context.Context, w http.ResponseWriter, r *http.Request) {
	var rp model.RootPage
	if err := getRequestJson(w, r, &rp); err != nil {
		handleError(c, w, err, http.StatusBadRequest)
		return
	}

	if rp.PageID == 0 {
		handleError(c, w, errors.New("pageID not found."), http.StatusBadRequest)
		return
	}

	rootPageKey := model.NewRootPageKey(c)

	err := datastore.RunInTransaction(c, func(c context.Context) error {
		var rp2put model.RootPage
		if err := ds.Get(c, rootPageKey, &rp2put); err != nil {
			if errors.Root(err) != datastore.ErrNoSuchEntity {
				return err
			} else {
				rp2put = model.RootPage{}
				rp2put.Key = rootPageKey
			}
		}

		if rp2put.PageID != rp.PageID {
			rp2put.PageID = rp.PageID
			ds.Put(c, &rp2put)
		}

		return nil
	}, nil)
	if err != nil {
		handleError(c, w, err, http.StatusInternalServerError)
		return
	}
}
Exemple #4
0
func getPage(params martini.Params, w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)

	if r.Method != "GET" {
		handleError(c, w, errors.New("error: illegal access."), http.StatusInternalServerError)
		return
	}

	keyIDStr := params["key"]
	if keyIDStr != "" {
		// get page from datastore
		intID, err := strconv.ParseInt(keyIDStr, 10, 64)
		if err != nil {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}

		key := model.NewPageKey(c, intID)

		var p model.Page
		if err := ds.Get(c, key, &p); err != nil {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}

		props, err := getPageProperties(c, key)
		if err != nil {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}

		rpkey := model.NewRootPageKey(c)

		var rp model.RootPage
		err = ds.Get(c, rpkey, &rp)
		if err != nil && errors.Root(err) != datastore.ErrNoSuchEntity {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}

		jsonPage := model.NewJsonPage(&p, props, intID == rp.PageID)

		b, err := json.Marshal(jsonPage)
		if err != nil {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}
		pageJson := string(b)

		w.Header().Set("Content-Type", "application/json; charset=utf-8")
		_, err = w.Write([]byte(pageJson))
		if err != nil {
			handleError(c, w, err, http.StatusInternalServerError)
			return
		}
	} else {
		handleError(c, w, errors.New("error: key string not found."), http.StatusInternalServerError)
		return
	}

	return
}