fixing style
This commit is contained in:
parent
e454940dfa
commit
f40485842d
35 changed files with 16000 additions and 286 deletions
|
|
@ -7,6 +7,7 @@ import (
|
|||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
|
|
@ -209,14 +210,11 @@ func (s *ContentService) ListBadges(context.Context) ([]Badge, error) {
|
|||
// Get returns one article by slug.
|
||||
func (s *ContentService) Get(_ context.Context, slug string) (Content, error) {
|
||||
slug = strings.ToLower(strings.TrimSpace(slug))
|
||||
if !validSlug(slug) {
|
||||
return Content{}, errors.New("invalid slug")
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
for _, item := range s.items {
|
||||
if item.Slug == slug {
|
||||
if item.Slug == slug || fmt.Sprint(item.ID) == slug {
|
||||
return cloneContent(item), nil
|
||||
}
|
||||
}
|
||||
|
|
@ -235,7 +233,7 @@ func (s *ContentService) UpdateAs(_ context.Context, c Content, actor string) (C
|
|||
|
||||
func (s *ContentService) update(c Content, actor string) (Content, error) {
|
||||
c.Slug = strings.ToLower(strings.TrimSpace(c.Slug))
|
||||
if !validSlug(c.Slug) || strings.TrimSpace(c.Title) == "" || strings.TrimSpace(c.Body) == "" {
|
||||
if strings.TrimSpace(c.Title) == "" || strings.TrimSpace(c.Body) == "" {
|
||||
return Content{}, errors.New("invalid content")
|
||||
}
|
||||
c.Badges = normalizeBadges(c.Badges)
|
||||
|
|
@ -247,7 +245,7 @@ func (s *ContentService) update(c Content, actor string) (Content, error) {
|
|||
|
||||
index := -1
|
||||
for i := range s.items {
|
||||
if s.items[i].Slug == c.Slug {
|
||||
if s.items[i].Slug == c.Slug || fmt.Sprint(s.items[i].ID) == c.Slug {
|
||||
index = i
|
||||
break
|
||||
}
|
||||
|
|
@ -262,8 +260,14 @@ func (s *ContentService) update(c Content, actor string) (Content, error) {
|
|||
c.ID = current.ID
|
||||
c.CreatedAt = current.CreatedAt
|
||||
c.UpdatedAt = time.Now().Truncate(time.Second)
|
||||
c.Category = normalizeCategory(current.Category)
|
||||
c.Author = current.Author
|
||||
if c.Category == "" {
|
||||
c.Category = normalizeCategory(current.Category)
|
||||
} else {
|
||||
c.Category = normalizeCategory(c.Category)
|
||||
}
|
||||
if c.Author == "" {
|
||||
c.Author = current.Author
|
||||
}
|
||||
if c.Status == "published" && c.PublishedAt == nil {
|
||||
now := time.Now().Truncate(time.Second)
|
||||
c.PublishedAt = &now
|
||||
|
|
@ -294,7 +298,10 @@ func (s *ContentService) Create(_ context.Context, c Content) (Content, error) {
|
|||
}
|
||||
c.Slug = strings.ToLower(strings.TrimSpace(c.Slug))
|
||||
if !validSlug(c.Slug) {
|
||||
return Content{}, errors.New("invalid slug")
|
||||
c.Slug = generateSlug(c.Title)
|
||||
}
|
||||
if !validSlug(c.Slug) {
|
||||
c.Slug = "post"
|
||||
}
|
||||
c.Category = normalizeCategory(c.Category)
|
||||
c.Badges = normalizeBadges(c.Badges)
|
||||
|
|
@ -327,8 +334,8 @@ func (s *ContentService) Create(_ context.Context, c Content) (Content, error) {
|
|||
s.nextID++
|
||||
for _, it := range s.items {
|
||||
if it.Slug == c.Slug {
|
||||
s.mu.Unlock()
|
||||
return Content{}, errors.New("slug already exists")
|
||||
c.Slug = fmt.Sprintf("%s-%d", c.Slug, c.ID)
|
||||
break
|
||||
}
|
||||
}
|
||||
created := cloneContent(c)
|
||||
|
|
@ -344,13 +351,10 @@ func (s *ContentService) Create(_ context.Context, c Content) (Content, error) {
|
|||
|
||||
func (s *ContentService) Delete(_ context.Context, slug string) error {
|
||||
slug = strings.ToLower(strings.TrimSpace(slug))
|
||||
if !validSlug(slug) {
|
||||
return errors.New("invalid slug")
|
||||
}
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
for i, it := range s.items {
|
||||
if it.Slug == slug {
|
||||
if it.Slug == slug || fmt.Sprint(it.ID) == slug {
|
||||
s.items = append(s.items[:i], s.items[i+1:]...)
|
||||
snapshot, _ := json.Marshal(it)
|
||||
s.history = append(s.history, contentRevision{ContentID: it.ID, Operation: "deleted", CreatedAt: time.Now(), Snapshot: string(snapshot)})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue