type CreateParams struct {
Title string
Body string
}
type Article struct {
AuthorID auth.UID
Title string
Body string
}
// Create publishes a new article.
//encore:api auth method=POST path=/article
func Create(ctx context.Context, p
*CreateParams) (
*Article, error) {
authorID :=
auth.UserID() // get the logged in user's ID
_, err := sqldb.Exec(ctx, `
INSERT INTO articles (author, title, body) VALUES ($1, $2, $3)
`, authorID, p.Title, p.Body)
if err != nil {
return nil, err
}
article
:= &Article{AuthorID: authorID, Title: p.Title, Body: p.Body}
followers, err
:= user.GetFollowers(ctx, authorID)
for _, follower
:= range followers {
notificationQueue.Push(&NewArticleEvent{
Article: article,
FollowerID: follower,
})
}
return article, err
}
type NewArticleEvent struct {
Article
*Article
FollowerID auth.UID
}
// notificationQueue emits events for all followers
// when a new article is published.
var notificationQueue = queue.New[NewArticleEvent]("new-articles")
// Send a daily summary of today's most viewed posts.
var _ = cron.NewJob("daily-summary", cron.JobConfig{
Every: 24 * cron.Hour,
Endpoint: SendDailySummary,
})
2
Cloud primitives at your fingertips
Instead of endless boilerplate and configuration, Encore lets you use primitives like databases, queues, and scheduled tasks by simply writing code.
And with Encore you get the same smooth workflow for local development, testing, and production.
No configuration. No setup. Just float into the flow.