2017-02-16 21:47:43 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2017-02-16 23:25:19 -05:00
|
|
|
|
|
|
|
"github.com/google/jsonapi"
|
2017-02-16 21:47:43 -05:00
|
|
|
)
|
|
|
|
|
2017-09-13 15:56:01 -04:00
|
|
|
// Blog is a model representing a blog site
|
2017-02-16 21:47:43 -05:00
|
|
|
type Blog struct {
|
|
|
|
ID int `jsonapi:"primary,blogs"`
|
|
|
|
Title string `jsonapi:"attr,title"`
|
|
|
|
Posts []*Post `jsonapi:"relation,posts"`
|
|
|
|
CurrentPost *Post `jsonapi:"relation,current_post"`
|
|
|
|
CurrentPostID int `jsonapi:"attr,current_post_id"`
|
|
|
|
CreatedAt time.Time `jsonapi:"attr,created_at"`
|
|
|
|
ViewCount int `jsonapi:"attr,view_count"`
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:56:01 -04:00
|
|
|
// Post is a model representing a post on a blog
|
2017-02-16 21:47:43 -05:00
|
|
|
type Post struct {
|
|
|
|
ID int `jsonapi:"primary,posts"`
|
|
|
|
BlogID int `jsonapi:"attr,blog_id"`
|
|
|
|
Title string `jsonapi:"attr,title"`
|
|
|
|
Body string `jsonapi:"attr,body"`
|
|
|
|
Comments []*Comment `jsonapi:"relation,comments"`
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:56:01 -04:00
|
|
|
// Comment is a model representing a user submitted comment
|
2017-02-16 21:47:43 -05:00
|
|
|
type Comment struct {
|
|
|
|
ID int `jsonapi:"primary,comments"`
|
|
|
|
PostID int `jsonapi:"attr,post_id"`
|
|
|
|
Body string `jsonapi:"attr,body"`
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:56:01 -04:00
|
|
|
// JSONAPILinks implements the Linkable interface for a blog
|
2017-02-16 23:25:19 -05:00
|
|
|
func (blog Blog) JSONAPILinks() *jsonapi.Links {
|
|
|
|
return &jsonapi.Links{
|
2017-02-16 21:47:43 -05:00
|
|
|
"self": fmt.Sprintf("https://example.com/blogs/%d", blog.ID),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:56:01 -04:00
|
|
|
// JSONAPIRelationshipLinks implements the RelationshipLinkable interface for a blog
|
2017-02-16 23:25:19 -05:00
|
|
|
func (blog Blog) JSONAPIRelationshipLinks(relation string) *jsonapi.Links {
|
2017-02-16 21:47:43 -05:00
|
|
|
if relation == "posts" {
|
2017-02-16 23:25:19 -05:00
|
|
|
return &jsonapi.Links{
|
2017-02-16 21:47:43 -05:00
|
|
|
"related": fmt.Sprintf("https://example.com/blogs/%d/posts", blog.ID),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if relation == "current_post" {
|
2017-02-16 23:25:19 -05:00
|
|
|
return &jsonapi.Links{
|
2017-02-16 21:47:43 -05:00
|
|
|
"related": fmt.Sprintf("https://example.com/blogs/%d/current_post", blog.ID),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-02-16 23:25:19 -05:00
|
|
|
|
2017-09-13 15:56:01 -04:00
|
|
|
// JSONAPIMeta implements the Metable interface for a blog
|
2017-02-16 23:25:19 -05:00
|
|
|
func (blog Blog) JSONAPIMeta() *jsonapi.Meta {
|
|
|
|
return &jsonapi.Meta{
|
|
|
|
"detail": "extra details regarding the blog",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:56:47 -04:00
|
|
|
// JSONAPIRelationshipMeta implements the RelationshipMetable interface for a blog
|
2017-02-16 23:25:19 -05:00
|
|
|
func (blog Blog) JSONAPIRelationshipMeta(relation string) *jsonapi.Meta {
|
|
|
|
if relation == "posts" {
|
|
|
|
return &jsonapi.Meta{
|
|
|
|
"detail": "posts meta information",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if relation == "current_post" {
|
|
|
|
return &jsonapi.Meta{
|
|
|
|
"detail": "current post meta information",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|