Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.