Files
digital-psychology/apps/api/internal/service/companion/service.go
T
jackyu66gitandCursor bd22d9dddd feat: P1 合盘/星座/问答与测测完整设计包及模拟器取证工具
落地 synastry/star/ask API 与 H5 页面,补齐 cece-frontend-re complete-design 证据文档,并加入 Android 模拟器截图抓取脚本。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-03 11:37:53 +08:00

64 lines
1.5 KiB
Go

package companion
import (
"context"
"errors"
"strings"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
core "github.com/yuxingu/digital-psychology/apps/api/internal/companion"
"github.com/yuxingu/digital-psychology/apps/api/internal/model"
"github.com/yuxingu/digital-psychology/apps/api/internal/repository"
)
// Service exposes solar terms and moods.
type Service struct {
Moods *repository.MoodRepo
}
// TodaySolar returns today's tip.
func (s *Service) TodaySolar() core.SolarTerm {
return core.TodaySolar(time.Now())
}
// SaveMoodInput for POST /moods.
type SaveMoodInput struct {
Score *int
Note *string
Day *time.Time
}
// SaveMood upserts mood for a day (default today).
func (s *Service) SaveMood(ctx context.Context, userID uuid.UUID, in SaveMoodInput) (*model.Mood, error) {
if in.Score != nil && (*in.Score < 1 || *in.Score > 5) {
return nil, errors.New("score must be 1-5")
}
if in.Note != nil {
n := strings.TrimSpace(*in.Note)
if len([]rune(n)) > 200 {
return nil, errors.New("note too long")
}
in.Note = &n
}
day := time.Now()
if in.Day != nil {
day = *in.Day
}
return s.Moods.UpsertToday(ctx, userID, day, in.Score, in.Note)
}
// GetTodayMood returns today's mood or nil when none.
func (s *Service) GetTodayMood(ctx context.Context, userID uuid.UUID) (*model.Mood, error) {
m, err := s.Moods.GetToday(ctx, userID, time.Now())
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
return nil, err
}
return m, nil
}