Styled the post list and added updating of posts
This commit is contained in:
parent
e77cac2fd5
commit
aef3bb6a1e
31 changed files with 1230 additions and 118 deletions
65
services/posts/create.go
Normal file
65
services/posts/create.go
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
package posts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
"lmika.dev/lmika/weiro/providers/db"
|
||||
)
|
||||
|
||||
type CreatePostParams struct {
|
||||
GUID string `form:"guid" json:"guid"`
|
||||
Title string `form:"title" json:"title"`
|
||||
Body string `form:"body" json:"body"`
|
||||
}
|
||||
|
||||
func (s *Service) PublishPost(ctx context.Context, params CreatePostParams) (*models.Post, error) {
|
||||
site, ok := models.GetSite(ctx)
|
||||
if !ok {
|
||||
return nil, models.SiteRequiredError
|
||||
}
|
||||
|
||||
post, err := s.fetchOrCreatePost(ctx, site, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
post.Title = params.Title
|
||||
post.Body = params.Body
|
||||
post.PublishedAt = time.Now()
|
||||
post.Slug = post.BestSlug()
|
||||
|
||||
if err := s.db.SavePost(ctx, post); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: do on separate thread
|
||||
if err := s.publisher.Publish(ctx, site); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return post, nil
|
||||
}
|
||||
|
||||
func (s *Service) fetchOrCreatePost(ctx context.Context, site models.Site, params CreatePostParams) (*models.Post, error) {
|
||||
post, err := s.db.SelectPostByGUID(ctx, params.GUID)
|
||||
if err == nil {
|
||||
if post.SiteID != site.ID {
|
||||
return nil, models.NotFoundError
|
||||
}
|
||||
|
||||
return post, nil
|
||||
} else if !db.ErrorIsNoRows(err) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
post = &models.Post{
|
||||
SiteID: site.ID,
|
||||
GUID: params.GUID,
|
||||
Title: params.Title,
|
||||
Body: params.Body,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
return post, nil
|
||||
}
|
||||
37
services/posts/list.go
Normal file
37
services/posts/list.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package posts
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
)
|
||||
|
||||
func (s *Service) ListPosts(ctx context.Context) ([]*models.Post, error) {
|
||||
site, ok := models.GetSite(ctx)
|
||||
if !ok {
|
||||
return nil, models.SiteRequiredError
|
||||
}
|
||||
|
||||
posts, err := s.db.SelectPostsOfSite(ctx, site.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return posts, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetPost(ctx context.Context, pid int64) (*models.Post, error) {
|
||||
site, ok := models.GetSite(ctx)
|
||||
if !ok {
|
||||
return nil, models.SiteRequiredError
|
||||
}
|
||||
|
||||
post, err := s.db.SelectPost(ctx, pid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if post.SiteID != site.ID {
|
||||
return nil, models.NotFoundError
|
||||
}
|
||||
|
||||
return post, nil
|
||||
}
|
||||
|
|
@ -1,10 +1,6 @@
|
|||
package posts
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"lmika.dev/lmika/weiro/models"
|
||||
"lmika.dev/lmika/weiro/providers/db"
|
||||
"lmika.dev/lmika/weiro/services/publisher"
|
||||
)
|
||||
|
|
@ -20,59 +16,3 @@ func New(db *db.Provider, publisher *publisher.Publisher) *Service {
|
|||
publisher: publisher,
|
||||
}
|
||||
}
|
||||
|
||||
type CreatePostParams struct {
|
||||
GUID string `form:"guid" json:"guid"`
|
||||
Title string `form:"title" json:"title"`
|
||||
Body string `form:"body" json:"body"`
|
||||
}
|
||||
|
||||
func (s *Service) PublishPost(ctx context.Context, params CreatePostParams) (*models.Post, error) {
|
||||
site, ok := models.GetSite(ctx)
|
||||
if !ok {
|
||||
return nil, models.SiteRequiredError
|
||||
}
|
||||
|
||||
post, err := s.fetchOrCreatePost(ctx, site, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
post.Title = params.Title
|
||||
post.Body = params.Body
|
||||
post.PublishedAt = time.Now()
|
||||
post.Slug = post.BestSlug()
|
||||
|
||||
if err := s.db.SavePost(ctx, post); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO: do on separate thread
|
||||
if err := s.publisher.Publish(ctx, site); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return post, nil
|
||||
}
|
||||
|
||||
func (s *Service) fetchOrCreatePost(ctx context.Context, site models.Site, params CreatePostParams) (*models.Post, error) {
|
||||
post, err := s.db.SelectPostByGUID(ctx, params.GUID)
|
||||
if err == nil {
|
||||
if post.SiteID != site.ID {
|
||||
return nil, models.NotFoundError
|
||||
}
|
||||
|
||||
return post, nil
|
||||
} else if !db.ErrorIsNoRows(err) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
post = &models.Post{
|
||||
SiteID: site.ID,
|
||||
GUID: params.GUID,
|
||||
Title: params.Title,
|
||||
Body: params.Body,
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
return post, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,10 @@ func (p *Publisher) Publish(ctx context.Context, site models.Site) error {
|
|||
}
|
||||
|
||||
for _, target := range targets {
|
||||
if !target.Enabled {
|
||||
continue
|
||||
}
|
||||
|
||||
pubSite := pubmodel.Site{
|
||||
Site: site,
|
||||
Posts: posts,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue