owl-blogs/app/entry_service.go

55 lines
1.5 KiB
Go
Raw Permalink Normal View History

2023-06-25 19:32:36 +00:00
package app
import (
"owl-blogs/app/repository"
"owl-blogs/domain/model"
)
type EntryService struct {
EntryRepository repository.EntryRepository
}
func NewEntryService(entryRepository repository.EntryRepository) *EntryService {
return &EntryService{EntryRepository: entryRepository}
}
2023-07-09 20:12:06 +00:00
func (s *EntryService) Create(entry model.Entry) error {
return s.EntryRepository.Create(entry)
2023-06-25 19:32:36 +00:00
}
func (s *EntryService) Update(entry model.Entry) error {
return s.EntryRepository.Update(entry)
}
func (s *EntryService) Delete(entry model.Entry) error {
return s.EntryRepository.Delete(entry)
}
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
}