Remove legacy question storage (#7)
Deletes obsolete question/answer/vote persistence and the compatibility answer endpoint. Existing databases drop the legacy tables through migration 009. Plumber replies now notify the root homeowner even when nested beneath another plumber reply. Post and reply forms prevent duplicate submissions and show progress while posting. Reviewed-on: #7 Co-authored-by: codegirl-007 <s.raide@gmail.com>
This commit was merged in pull request #7.
This commit is contained in:
+4
-199
@@ -10,20 +10,15 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"plumber/internal/pacific"
|
||||
)
|
||||
|
||||
// Memory is an in-process Store for tests.
|
||||
type Memory struct {
|
||||
mu sync.Mutex
|
||||
users map[string]*User // id -> user
|
||||
byName map[string]string // username -> id
|
||||
questions map[string]*RankedQuestion // id -> question
|
||||
answers map[string]*Answer // questionID -> answer
|
||||
votes map[string]map[string]int // questionID -> userID -> value
|
||||
posts map[string]*Post // id -> post
|
||||
postVotes map[string]map[string]int // postID -> userID -> value
|
||||
users map[string]*User // id -> user
|
||||
byName map[string]string // username -> id
|
||||
posts map[string]*Post // id -> post
|
||||
postVotes map[string]map[string]int // postID -> userID -> value
|
||||
}
|
||||
|
||||
// NewMemory returns an empty Memory store.
|
||||
@@ -31,9 +26,6 @@ func NewMemory() *Memory {
|
||||
return &Memory{
|
||||
users: map[string]*User{},
|
||||
byName: map[string]string{},
|
||||
questions: map[string]*RankedQuestion{},
|
||||
answers: map[string]*Answer{},
|
||||
votes: map[string]map[string]int{},
|
||||
posts: map[string]*Post{},
|
||||
postVotes: map[string]map[string]int{},
|
||||
}
|
||||
@@ -210,172 +202,6 @@ func (m *Memory) SaveUserProfile(_ context.Context, u *User) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memory) CreateQuestion(_ context.Context, q *RankedQuestion) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
q.Title = strings.TrimSpace(q.Title)
|
||||
q.Body = strings.TrimSpace(q.Body)
|
||||
q.City = strings.TrimSpace(q.City)
|
||||
if q.ID == "" {
|
||||
q.ID = uuid.NewString()
|
||||
}
|
||||
if q.HuntDate == "" {
|
||||
q.HuntDate = pacific.Today()
|
||||
}
|
||||
if q.CreatedAt == "" {
|
||||
q.CreatedAt = time.Now().UTC().Format(time.RFC3339)
|
||||
}
|
||||
author, ok := m.users[q.AuthorID]
|
||||
if !ok {
|
||||
return fmt.Errorf("unknown author")
|
||||
}
|
||||
cp := *q
|
||||
cp.AuthorName = author.Name
|
||||
cp.db = nil
|
||||
m.questions[cp.ID] = &cp
|
||||
*q = cp
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memory) annotate(q *RankedQuestion, viewerID string) RankedQuestion {
|
||||
out := *q
|
||||
score := 0
|
||||
userVote := 0
|
||||
if votes, ok := m.votes[q.ID]; ok {
|
||||
for uid, v := range votes {
|
||||
score += v
|
||||
if uid == viewerID {
|
||||
userVote = v
|
||||
}
|
||||
}
|
||||
}
|
||||
_, answered := m.answers[q.ID]
|
||||
out.Score = score
|
||||
out.Answered = answered
|
||||
out.UserVote = userVote
|
||||
out.db = nil
|
||||
return out
|
||||
}
|
||||
|
||||
func (m *Memory) GetQuestion(_ context.Context, id, viewerID string) (*RankedQuestion, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
q, ok := m.questions[id]
|
||||
if !ok {
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
out := m.annotate(q, viewerID)
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func (m *Memory) ListHunt(_ context.Context, huntDate, viewerID string) ([]RankedQuestion, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
out := make([]RankedQuestion, 0)
|
||||
for _, q := range m.questions {
|
||||
if q.HuntDate != huntDate || q.Hidden {
|
||||
continue
|
||||
}
|
||||
out = append(out, m.annotate(q, viewerID))
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool {
|
||||
if out[i].Score != out[j].Score {
|
||||
return out[i].Score > out[j].Score
|
||||
}
|
||||
return out[i].CreatedAt < out[j].CreatedAt
|
||||
})
|
||||
if len(out) > HuntListLimit {
|
||||
out = out[:HuntListLimit]
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *Memory) ListQuestionsByAuthor(_ context.Context, authorID string) ([]RankedQuestion, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
out := make([]RankedQuestion, 0)
|
||||
for _, q := range m.questions {
|
||||
if q.AuthorID != authorID || q.Hidden {
|
||||
continue
|
||||
}
|
||||
out = append(out, m.annotate(q, ""))
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].CreatedAt > out[j].CreatedAt })
|
||||
if len(out) > ProfileListLimit {
|
||||
out = out[:ProfileListLimit]
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *Memory) ListQuestionsAnsweredBy(_ context.Context, adminID string) ([]RankedQuestion, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
out := make([]RankedQuestion, 0)
|
||||
for qid, a := range m.answers {
|
||||
if a.AuthorID != adminID {
|
||||
continue
|
||||
}
|
||||
q, ok := m.questions[qid]
|
||||
if !ok || q.Hidden {
|
||||
continue
|
||||
}
|
||||
rq := m.annotate(q, "")
|
||||
rq.Answered = true
|
||||
out = append(out, rq)
|
||||
}
|
||||
sort.Slice(out, func(i, j int) bool { return out[i].CreatedAt > out[j].CreatedAt })
|
||||
if len(out) > ProfileListLimit {
|
||||
out = out[:ProfileListLimit]
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (m *Memory) HideQuestion(_ context.Context, id string) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
q, ok := m.questions[id]
|
||||
if !ok {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
q.Hidden = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memory) GetAnswer(_ context.Context, questionID string) (*Answer, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
a, ok := m.answers[questionID]
|
||||
if !ok {
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
cp := *a
|
||||
if u, ok := m.users[a.AuthorID]; ok {
|
||||
cp.AuthorName = u.Name
|
||||
}
|
||||
return &cp, nil
|
||||
}
|
||||
|
||||
func (m *Memory) UpsertAnswer(_ context.Context, a *Answer) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if _, ok := m.questions[a.QuestionID]; !ok {
|
||||
return fmt.Errorf("unknown question")
|
||||
}
|
||||
a.Body = strings.TrimSpace(a.Body)
|
||||
now := time.Now().UTC().Format(time.RFC3339)
|
||||
if existing, ok := m.answers[a.QuestionID]; ok {
|
||||
a.CreatedAt = existing.CreatedAt
|
||||
} else if a.CreatedAt == "" {
|
||||
a.CreatedAt = now
|
||||
}
|
||||
a.UpdatedAt = now
|
||||
cp := *a
|
||||
cp.db = nil
|
||||
m.answers[a.QuestionID] = &cp
|
||||
*a = cp
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Memory) CreatePost(_ context.Context, post *Post) error {
|
||||
if post == nil {
|
||||
return fmt.Errorf("%w: post is nil", ErrInvalidPost)
|
||||
@@ -637,24 +463,3 @@ func clonePostWithAuthor(post *Post, users map[string]*User) *Post {
|
||||
}
|
||||
return cp
|
||||
}
|
||||
|
||||
func (m *Memory) Vote(_ context.Context, userID, questionID string, value int) error {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
if value != 1 && value != -1 && value != 0 {
|
||||
return fmt.Errorf("invalid vote")
|
||||
}
|
||||
q, ok := m.questions[questionID]
|
||||
if !ok || q.Hidden {
|
||||
return ErrHiddenOrMissing
|
||||
}
|
||||
if m.votes[questionID] == nil {
|
||||
m.votes[questionID] = map[string]int{}
|
||||
}
|
||||
if value == 0 {
|
||||
delete(m.votes[questionID], userID)
|
||||
return nil
|
||||
}
|
||||
m.votes[questionID][userID] = value
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user