2026-07-28 16:22:46 +08:00
|
|
|
package repository
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"lifelog/internal/model"
|
|
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type TodoRepository struct {
|
|
|
|
|
db *gorm.DB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewTodoRepository(db *gorm.DB) *TodoRepository {
|
|
|
|
|
return &TodoRepository{db: db}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *TodoRepository) GetAll() ([]model.Todo, error) {
|
|
|
|
|
var todos []model.Todo
|
|
|
|
|
err := r.db.Order("priority asc, created_at desc").Find(&todos).Error
|
|
|
|
|
return todos, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *TodoRepository) Create(todo *model.Todo) error {
|
|
|
|
|
return r.db.Create(todo).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *TodoRepository) Update(todo *model.Todo) error {
|
|
|
|
|
return r.db.Save(todo).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *TodoRepository) Delete(id uint64) error {
|
|
|
|
|
return r.db.Delete(&model.Todo{}, id).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *TodoRepository) GetByID(id uint64) (*model.Todo, error) {
|
|
|
|
|
var todo model.Todo
|
|
|
|
|
err := r.db.First(&todo, id).Error
|
|
|
|
|
return &todo, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type DailyLogRepository struct {
|
|
|
|
|
db *gorm.DB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewDailyLogRepository(db *gorm.DB) *DailyLogRepository {
|
|
|
|
|
return &DailyLogRepository{db: db}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *DailyLogRepository) GetByDate(date string) (*model.DailyLog, error) {
|
|
|
|
|
var log model.DailyLog
|
|
|
|
|
err := r.db.Where("date = ?", date).First(&log).Error
|
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
return &log, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *DailyLogRepository) GetRecent(limit int) ([]model.DailyLog, error) {
|
|
|
|
|
var logs []model.DailyLog
|
|
|
|
|
err := r.db.Order("date desc").Limit(limit).Find(&logs).Error
|
|
|
|
|
return logs, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *DailyLogRepository) Upsert(log *model.DailyLog) error {
|
|
|
|
|
var existing model.DailyLog
|
|
|
|
|
err := r.db.Where("date = ?", log.Date).First(&existing).Error
|
|
|
|
|
if err == gorm.ErrRecordNotFound {
|
|
|
|
|
return r.db.Create(log).Error
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
// 更新已有记录
|
|
|
|
|
existing.Morning = log.Morning
|
|
|
|
|
existing.Afternoon = log.Afternoon
|
|
|
|
|
existing.Evening = log.Evening
|
|
|
|
|
return r.db.Save(&existing).Error
|
|
|
|
|
}
|
2026-07-29 00:15:41 +08:00
|
|
|
|
|
|
|
|
type FinanceRepository struct {
|
|
|
|
|
db *gorm.DB
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewFinanceRepository(db *gorm.DB) *FinanceRepository {
|
|
|
|
|
return &FinanceRepository{db: db}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *FinanceRepository) GetByYear(year int) ([]model.FinanceEntry, error) {
|
|
|
|
|
var entries []model.FinanceEntry
|
|
|
|
|
err := r.db.Where("year = ?", year).Order("month ASC, id ASC").Find(&entries).Error
|
|
|
|
|
return entries, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *FinanceRepository) Create(entry *model.FinanceEntry) error {
|
|
|
|
|
return r.db.Create(entry).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *FinanceRepository) Update(entry *model.FinanceEntry) error {
|
|
|
|
|
return r.db.Save(entry).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *FinanceRepository) Delete(id uint64) error {
|
|
|
|
|
return r.db.Delete(&model.FinanceEntry{}, id).Error
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *FinanceRepository) GetByID(id uint64) (*model.FinanceEntry, error) {
|
|
|
|
|
var entry model.FinanceEntry
|
|
|
|
|
err := r.db.First(&entry, id).Error
|
|
|
|
|
return &entry, err
|
|
|
|
|
}
|