2023-06-25 19:32:36 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2023-08-08 17:41:07 +00:00
|
|
|
"fmt"
|
2023-06-25 19:32:36 +00:00
|
|
|
"owl-blogs/app/repository"
|
|
|
|
"owl-blogs/domain/model"
|
2023-08-08 17:41:07 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2023-06-25 19:32:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type EntryService struct {
|
|
|
|
EntryRepository repository.EntryRepository
|
2023-08-11 13:14:38 +00:00
|
|
|
Bus *EventBus
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
2023-08-09 19:56:56 +00:00
|
|
|
func NewEntryService(
|
|
|
|
entryRepository repository.EntryRepository,
|
2023-08-11 13:14:38 +00:00
|
|
|
bus *EventBus,
|
2023-08-09 19:56:56 +00:00
|
|
|
) *EntryService {
|
|
|
|
return &EntryService{
|
|
|
|
EntryRepository: entryRepository,
|
2023-08-11 13:14:38 +00:00
|
|
|
Bus: bus,
|
2023-08-09 19:56:56 +00:00
|
|
|
}
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
2023-07-09 20:12:06 +00:00
|
|
|
func (s *EntryService) Create(entry model.Entry) error {
|
2023-08-08 17:41:07 +00:00
|
|
|
// try to find a good ID
|
|
|
|
m := regexp.MustCompile(`[^a-z0-9-]`)
|
|
|
|
prefix := m.ReplaceAllString(strings.ToLower(entry.Title()), "-")
|
|
|
|
title := prefix
|
|
|
|
counter := 0
|
|
|
|
for {
|
|
|
|
_, err := s.EntryRepository.FindById(title)
|
|
|
|
if err == nil {
|
|
|
|
counter += 1
|
|
|
|
title = prefix + "-" + fmt.Sprintf("%s-%d", prefix, counter)
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
entry.SetID(title)
|
|
|
|
|
2023-08-09 19:56:56 +00:00
|
|
|
err := s.EntryRepository.Create(entry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-11 13:14:38 +00:00
|
|
|
s.Bus.NotifyCreated(entry)
|
2023-08-09 19:56:56 +00:00
|
|
|
return nil
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EntryService) Update(entry model.Entry) error {
|
2023-08-11 13:14:38 +00:00
|
|
|
err := s.EntryRepository.Update(entry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Bus.NotifyUpdated(entry)
|
|
|
|
return nil
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EntryService) Delete(entry model.Entry) error {
|
2023-08-11 13:14:38 +00:00
|
|
|
err := s.EntryRepository.Delete(entry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.Bus.NotifyDeleted(entry)
|
|
|
|
return nil
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EntryService) FindById(id string) (model.Entry, error) {
|
|
|
|
return s.EntryRepository.FindById(id)
|
|
|
|
}
|
|
|
|
|
2023-07-19 18:45:42 +00:00
|
|
|
func (s *EntryService) filterEntries(entries []model.Entry, published bool, drafts bool) []model.Entry {
|
|
|
|
filteredEntries := make([]model.Entry, 0)
|
|
|
|
for _, entry := range entries {
|
|
|
|
if published && entry.PublishedAt() != nil && !entry.PublishedAt().IsZero() {
|
|
|
|
filteredEntries = append(filteredEntries, entry)
|
|
|
|
}
|
|
|
|
if drafts && (entry.PublishedAt() == nil || entry.PublishedAt().IsZero()) {
|
|
|
|
filteredEntries = append(filteredEntries, entry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredEntries
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EntryService) FindAllByType(types *[]string, published bool, drafts bool) ([]model.Entry, error) {
|
|
|
|
entries, err := s.EntryRepository.FindAll(types)
|
|
|
|
return s.filterEntries(entries, published, drafts), err
|
|
|
|
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *EntryService) FindAll() ([]model.Entry, error) {
|
2023-07-13 19:20:00 +00:00
|
|
|
entries, err := s.EntryRepository.FindAll(nil)
|
2023-07-19 18:45:42 +00:00
|
|
|
return s.filterEntries(entries, true, true), err
|
2023-06-25 19:32:36 +00:00
|
|
|
}
|