2016-07-06 22:20:36 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2017-01-14 17:06:16 +00:00
|
|
|
"zxq.co/ripple/rippleapi/common"
|
2016-07-06 22:20:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type blogPost struct {
|
|
|
|
ID int `json:"id"`
|
|
|
|
Title string `json:"title"`
|
|
|
|
Slug string `json:"slug"`
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Author userData `json:"author"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type blogPostsResponse struct {
|
|
|
|
common.ResponseBase
|
|
|
|
Posts []blogPost `json:"posts"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlogPostsGET retrieves the latest blog posts on the Ripple blog.
|
|
|
|
func BlogPostsGET(md common.MethodData) common.CodeMessager {
|
|
|
|
var and string
|
|
|
|
var params []interface{}
|
2016-08-15 17:59:46 +00:00
|
|
|
if md.Query("id") != "" {
|
2016-07-06 22:20:36 +00:00
|
|
|
and = "b.id = ?"
|
2016-08-15 17:59:46 +00:00
|
|
|
params = append(params, md.Query("id"))
|
2016-07-06 22:20:36 +00:00
|
|
|
}
|
|
|
|
rows, err := md.DB.Query(`
|
|
|
|
SELECT
|
|
|
|
b.id, b.title, b.slug, b.created,
|
|
|
|
|
|
|
|
u.id, u.username, s.username_aka, u.register_datetime,
|
|
|
|
u.privileges, u.latest_activity, s.country
|
|
|
|
FROM anchor_posts b
|
2016-08-19 21:48:44 +00:00
|
|
|
INNER JOIN users u ON b.author = u.id
|
|
|
|
INNER JOIN users_stats s ON b.author = s.id
|
2016-07-06 22:20:36 +00:00
|
|
|
WHERE status = "published" `+and+`
|
2016-08-15 17:59:46 +00:00
|
|
|
ORDER BY b.id DESC `+common.Paginate(md.Query("p"), md.Query("l"), 50), params...)
|
2016-07-06 22:20:36 +00:00
|
|
|
if err != nil {
|
|
|
|
md.Err(err)
|
|
|
|
return Err500
|
|
|
|
}
|
|
|
|
|
|
|
|
var r blogPostsResponse
|
|
|
|
for rows.Next() {
|
|
|
|
var post blogPost
|
|
|
|
err := rows.Scan(
|
|
|
|
&post.ID, &post.Title, &post.Slug, &post.Created,
|
|
|
|
|
|
|
|
&post.Author.ID, &post.Author.Username, &post.Author.UsernameAKA, &post.Author.RegisteredOn,
|
|
|
|
&post.Author.Privileges, &post.Author.LatestActivity, &post.Author.Country,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
md.Err(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r.Posts = append(r.Posts, post)
|
|
|
|
}
|
|
|
|
r.Code = 200
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
type blogPostContent struct {
|
|
|
|
common.ResponseBase
|
|
|
|
Content string `json:"content"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// BlogPostsContentGET retrieves the content of a specific blog post.
|
|
|
|
func BlogPostsContentGET(md common.MethodData) common.CodeMessager {
|
|
|
|
field := "markdown"
|
2016-08-27 10:04:12 +00:00
|
|
|
if md.HasQuery("html") {
|
2016-07-06 22:20:36 +00:00
|
|
|
field = "html"
|
|
|
|
}
|
2016-08-10 07:45:01 +00:00
|
|
|
var (
|
|
|
|
by string
|
|
|
|
val string
|
|
|
|
)
|
|
|
|
switch {
|
2016-08-15 17:59:46 +00:00
|
|
|
case md.Query("slug") != "":
|
2016-08-10 07:45:01 +00:00
|
|
|
by = "slug"
|
2016-08-15 17:59:46 +00:00
|
|
|
val = md.Query("slug")
|
|
|
|
case md.Query("id") != "":
|
2016-08-10 07:45:01 +00:00
|
|
|
by = "id"
|
2016-08-15 17:59:46 +00:00
|
|
|
val = md.Query("id")
|
2016-08-10 07:45:01 +00:00
|
|
|
default:
|
|
|
|
return ErrMissingField("id|slug")
|
2016-07-06 22:20:36 +00:00
|
|
|
}
|
|
|
|
var r blogPostContent
|
2016-08-10 07:45:01 +00:00
|
|
|
err := md.DB.QueryRow("SELECT "+field+" FROM anchor_posts WHERE "+by+" = ? AND status = 'published'", val).Scan(&r.Content)
|
2016-07-06 22:20:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return common.SimpleResponse(404, "no blog post found")
|
|
|
|
}
|
|
|
|
r.Code = 200
|
|
|
|
return r
|
|
|
|
}
|