Ejemplo n.º 1
0
func queryPosts(res *wcg.Response, req *wcg.Request, per_page int) (*postQueryResult, error) {
	var list []blog.Post
	var basePath = req.HttpRequest().URL.Path
	is_admin := (lib.GetUserKind(req) == lib.Admin) && (req.Query("is_admin") == "true")
	if per_page == 0 {
		per_page = wcg.ParseInt(req.Query("n"), 5, 0, 20) // default 5, max 20
	}
	page := wcg.ParseInt(req.Query("page"), 0, 0, wcg.ParseIntMax)
	q := NewPostDriver(lib.NewAppContextFromRequest(req)).NewQuery()
	q = q.Order("-PublishAt").Order("-CreatedAt")
	if !is_admin {
		q = q.Filter("PublishAt <=", time.Now()).Filter("IsDraft =", false).Filter("IsHidden =", false)
	}

	_, err := q.Offset(page * per_page).Limit(per_page).GetAll(&list)
	if err != nil {
		return nil, err
	}
	if list == nil {
		list = make([]blog.Post, 0)
	}

	p := &postQueryResult{
		Posts: list,
	}

	// prev url
	if page > 0 {
		p.Previous = fmt.Sprintf("%s?page=%d&n=%d", basePath, page-1, per_page)
		if is_admin {
			p.Previous = fmt.Sprintf("%s&is_admin=true", p.Previous)
		}
	}

	// next url
	if len(list) == per_page {
		p.Next = fmt.Sprintf("%s?page=%d&n=%d", basePath, page+1, per_page)
		if is_admin {
			p.Next = fmt.Sprintf("%s&is_admin=true", p.Next)
		}
	}
	return p, nil
}
Ejemplo n.º 2
0
func setupApi(app *App) {
	app.Api.Get("/posts/", func(res *wcg.Response, req *wcg.Request) {
		if result, err := queryPosts(res, req, 0); err != nil {
			lib.Error(res, req, err)
		} else {
			res.WriteJson(result)
		}
	})

	app.Api.Get("/posts/:id.json", func(res *wcg.Response, req *wcg.Request) {
		var p blog.Post
		driver := NewPostDriver(lib.NewAppContextFromRequest(req))
		key := driver.NewKey(req.Param("id"), 0, nil)
		if err := driver.Get(key, &p); err != nil {
			lib.Error(res, req, err)
			return
		}
		if p.IsDraft && lib.GetUserKind(req) != lib.Admin {
			app.Api.NotFound(res, req)
		} else {
			res.WriteJson(p)
		}
	})

	app.Api.Post("/posts/", lib.Admin.Required(
		func(res *wcg.Response, req *wcg.Request) {
			driver := NewPostDriver(lib.NewAppContextFromRequest(req))
			p := &blog.Post{}
			if err := updateAttributesFromForm(res, req, p); err != nil {
				lib.Error(res, req, err)
				return
			}
			if _, err := driver.Save(p); err != nil {
				lib.Error(res, req, err)
			}
			app.Api.Created(res, req, p.Id)
		},
	))

	app.Api.Put("/posts/:id.json", lib.Admin.Required(
		func(res *wcg.Response, req *wcg.Request) {
			var p blog.Post
			driver := NewPostDriver(lib.NewAppContextFromRequest(req))
			key := driver.NewKey(req.Param("id"), 0, nil)
			if err := driver.Get(key, &p); err != nil {
				lib.Error(res, req, err)
				return
			}
			if err := updateAttributesFromForm(res, req, &p); err != nil {
				lib.Error(res, req, err)
				return
			}
			if _, err := driver.Save(&p); err != nil {
				lib.Error(res, req, err)
				return
			} else {
				app.Api.Ok(res, req)
			}
		},
	))

	app.Api.Delete("/posts/:id.json", lib.Admin.Required(
		func(res *wcg.Response, req *wcg.Request) {
			driver := NewPostDriver(lib.NewAppContextFromRequest(req))
			key := driver.NewKey(req.Param("id"), 0, nil)
			if err := driver.Delete(key); err != nil {
				lib.Error(res, req, err)
				return
			} else {
				app.Api.Ok(res, req)
			}
		},
	))
}