Last active
May 23, 2018 13:30
-
-
Save indrasaputra/6ec6897be1d313fd17dfb77b45a0a696 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package blog | |
import "time" | |
// Article is our beloved article struct. | |
// This struct can be moved to another package, | |
// such as `package entity`. | |
// But, I choose to put it here because | |
// of readability reason. | |
type Article struct { | |
// ID is article's unique identifier. | |
ID int | |
// Body is the content of the article. | |
Body string | |
// CreatedAt is the time when the article was created. | |
CreatedAt time.Time | |
} | |
// Blog is the main business process(es). | |
type Blog interface { | |
// CreateArticle persists an article then publish it. | |
CreateArticle(article Article) error | |
// GetArticles retrieves an article based on its id. | |
GetArticle(id int) (Article, error) | |
} | |
// ArticleProvider provides data interchange between | |
// application and provider. | |
// The provider could be any persistent data storage, | |
// such as MySQL, Postgres, Cassandra or anything | |
// as long as it implements this interface. | |
type ArticleProvider interface { | |
// SaveArticle persists an article. | |
SaveArticle(article Article) error | |
// GetArticle retrieves an article. | |
GetArticle(id int) (Article, error) | |
} | |
// ArticlePublisher publishes article. | |
// Publisher could be Kafka, Rabbitmq, or anything | |
// as long as it implements this interface. | |
type ArticlePublisher interface { | |
// PublishArticle publishes an article. | |
PublishArticle(article Article) error | |
} | |
// GoBlog implements Blog interface. | |
type GoBlog struct { | |
ArticlePvd ArticleProvider | |
ArticlePub ArticlePublisher | |
} | |
// CreateArticle creates and publishes article. | |
func (g GoBlog) CreateArticle(article Article) error { | |
if err := g.ArticlePvd.SaveArticle(article); err != nil { | |
return err | |
} | |
return g.ArticlePub.PublishArticle(article) | |
} | |
// GetArticle gets a certain article. | |
func (g GoBlog) GetArticle(id int) (Article, error) { | |
return g.ArticlePvd.GetArticle(id) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment