Files
digital-psychology/apps/api/internal/repository/mood_repo.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

56 lines
1.5 KiB
Go

package repository
import (
"context"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/yuxingu/digital-psychology/apps/api/internal/model"
)
// MoodRepo persists daily moods.
type MoodRepo struct {
Pool *pgxpool.Pool
}
// UpsertToday inserts or updates today's mood for user.
func (r *MoodRepo) UpsertToday(ctx context.Context, userID uuid.UUID, day time.Time, score *int, note *string) (*model.Mood, error) {
m := &model.Mood{}
var dayOut time.Time
err := r.Pool.QueryRow(ctx, `
INSERT INTO moods(user_id, day, score, note)
VALUES ($1, $2::date, $3, $4)
ON CONFLICT (user_id, day) DO UPDATE
SET score = EXCLUDED.score,
note = EXCLUDED.note,
updated_at = now(),
deleted_at = NULL
RETURNING id, user_id, day, score, note, created_at`,
userID, day.Format("2006-01-02"), score, note,
).Scan(&m.ID, &m.UserID, &dayOut, &m.Score, &m.Note, &m.CreatedAt)
if err != nil {
return nil, err
}
m.Day = dayOut.Format("2006-01-02")
return m, nil
}
// GetToday returns today's mood if any.
func (r *MoodRepo) GetToday(ctx context.Context, userID uuid.UUID, day time.Time) (*model.Mood, error) {
m := &model.Mood{}
var dayOut time.Time
err := r.Pool.QueryRow(ctx, `
SELECT id, user_id, day, score, note, created_at
FROM moods
WHERE user_id=$1 AND day=$2::date AND deleted_at IS NULL`,
userID, day.Format("2006-01-02"),
).Scan(&m.ID, &m.UserID, &dayOut, &m.Score, &m.Note, &m.CreatedAt)
if err != nil {
return nil, err
}
m.Day = dayOut.Format("2006-01-02")
return m, nil
}