acd70d319f
- FinanceEntry model (income/expense x fixed/extra) - CRUD API endpoints for finance entries - FinancePage with summary cards and drill-down - Updated navigation with Finance tab - Updated todos default filter to active
124 lines
2.6 KiB
Go
124 lines
2.6 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"lifelog/internal/model"
|
|
"lifelog/internal/repository"
|
|
"time"
|
|
)
|
|
|
|
type TodoService struct {
|
|
repo *repository.TodoRepository
|
|
}
|
|
|
|
func NewTodoService(repo *repository.TodoRepository) *TodoService {
|
|
return &TodoService{repo: repo}
|
|
}
|
|
|
|
func (s *TodoService) GetAll() ([]model.Todo, error) {
|
|
return s.repo.GetAll()
|
|
}
|
|
|
|
func (s *TodoService) Create(title string, priority int, dueDate *time.Time) (*model.Todo, error) {
|
|
todo := &model.Todo{
|
|
Title: title,
|
|
Done: false,
|
|
Priority: priority,
|
|
DueDate: dueDate,
|
|
}
|
|
err := s.repo.Create(todo)
|
|
return todo, err
|
|
}
|
|
|
|
func (s *TodoService) Update(id uint64, title string, priority int, done bool, dueDate *time.Time) (*model.Todo, error) {
|
|
todo, err := s.repo.GetByID(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
todo.Title = title
|
|
todo.Priority = priority
|
|
todo.Done = done
|
|
todo.DueDate = dueDate
|
|
err = s.repo.Update(todo)
|
|
return todo, err
|
|
}
|
|
|
|
func (s *TodoService) ToggleDone(id uint64) (*model.Todo, error) {
|
|
todo, err := s.repo.GetByID(id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
todo.Done = !todo.Done
|
|
err = s.repo.Update(todo)
|
|
return todo, err
|
|
}
|
|
|
|
func (s *TodoService) Delete(id uint64) error {
|
|
return s.repo.Delete(id)
|
|
}
|
|
|
|
type DailyLogService struct {
|
|
repo *repository.DailyLogRepository
|
|
}
|
|
|
|
func NewDailyLogService(repo *repository.DailyLogRepository) *DailyLogService {
|
|
return &DailyLogService{repo: repo}
|
|
}
|
|
|
|
func (s *DailyLogService) GetByDate(date string) (*model.DailyLog, error) {
|
|
log, err := s.repo.GetByDate(date)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if log == nil {
|
|
// 返回空记录
|
|
return &model.DailyLog{Date: date}, nil
|
|
}
|
|
return log, nil
|
|
}
|
|
|
|
func (s *DailyLogService) GetRecent(limit int) ([]model.DailyLog, error) {
|
|
if limit <= 0 {
|
|
limit = 30
|
|
}
|
|
return s.repo.GetRecent(limit)
|
|
}
|
|
|
|
func (s *DailyLogService) Save(date, morning, afternoon, evening string) (*model.DailyLog, error) {
|
|
if date == "" {
|
|
return nil, errors.New("date is required")
|
|
}
|
|
log := &model.DailyLog{
|
|
Date: date,
|
|
Morning: morning,
|
|
Afternoon: afternoon,
|
|
Evening: evening,
|
|
}
|
|
err := s.repo.Upsert(log)
|
|
return log, err
|
|
}
|
|
|
|
type FinanceService struct {
|
|
repo *repository.FinanceRepository
|
|
}
|
|
|
|
func NewFinanceService(repo *repository.FinanceRepository) *FinanceService {
|
|
return &FinanceService{repo: repo}
|
|
}
|
|
|
|
func (s *FinanceService) GetByYear(year int) ([]model.FinanceEntry, error) {
|
|
return s.repo.GetByYear(year)
|
|
}
|
|
|
|
func (s *FinanceService) Create(entry *model.FinanceEntry) error {
|
|
return s.repo.Create(entry)
|
|
}
|
|
|
|
func (s *FinanceService) Update(entry *model.FinanceEntry) error {
|
|
return s.repo.Update(entry)
|
|
}
|
|
|
|
func (s *FinanceService) Delete(id uint64) error {
|
|
return s.repo.Delete(id)
|
|
}
|