BlueGit

timfa bluegit

Files

bluegit / internal / store / notes.go

notes.go

feat: nieuw-formulier, groepsoverzicht, logo's en talen
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
Failed d1203d3b History
notes.go 2.0 KiB Download Raw
1package store
2
3import (
4 "context"
5 "strconv"
6
7 "gitlab.com/Timfa/bluegit/internal/i18n"
8 "strings"
9 "time"
10)
11
12// Note is een reactie op een merge request.
13type Note struct {
14 ID int64
15 RepoID int64
16 MRID int64
17 AuthorID int64
18 Author string
19 Body string
20 CreatedAt time.Time
21}
22
23// AddNote bewaart een reactie en telt hem als bijdrage.
24func (s *Store) AddNote(ctx context.Context, repoID, mrID, authorID int64, body string) (Note, error) {
25 body = strings.TrimSpace(body)
26 if body == "" {
27 return Note{}, i18n.E("err.noteEmpty")
28 }
29 body = trim(body, 4000)
30 tx, err := s.db.BeginTx(ctx, nil)
31 if err != nil {
32 return Note{}, err
33 }
34 defer func() { _ = tx.Rollback() }()
35 ts := now()
36 res, err := tx.ExecContext(ctx, `INSERT INTO notes (repo_id, mr_id, author_id, body, created_at) VALUES (?, ?, ?, ?, ?)`,
37 repoID, mrID, authorID, body, ts)
38 if err != nil {
39 return Note{}, err
40 }
41 id, _ := res.LastInsertId()
42 if _, err := tx.ExecContext(ctx, `INSERT OR IGNORE INTO contributions (user_id, repo_id, kind, ref, occurred_at) VALUES (?, ?, 'comment', ?, ?)`,
43 authorID, repoID, "note:"+strconv.FormatInt(id, 10), ts); err != nil {
44 return Note{}, err
45 }
46 if err := tx.Commit(); err != nil {
47 return Note{}, err
48 }
49 return Note{ID: id, RepoID: repoID, MRID: mrID, AuthorID: authorID, Body: body, CreatedAt: parseTime(ts)}, nil
50}
51
52// ListNotes geeft de reacties van één merge request, oudste eerst.
53func (s *Store) ListNotes(ctx context.Context, mrID int64) ([]Note, error) {
54 rows, err := s.db.QueryContext(ctx, `SELECT n.id, n.repo_id, n.mr_id, n.author_id, u.username, n.body, n.created_at
55 FROM notes n JOIN users u ON u.id = n.author_id WHERE n.mr_id = ? ORDER BY n.id`, mrID)
56 if err != nil {
57 return nil, err
58 }
59 defer rows.Close()
60 var out []Note
61 for rows.Next() {
62 var n Note
63 var created string
64 if err := rows.Scan(&n.ID, &n.RepoID, &n.MRID, &n.AuthorID, &n.Author, &n.Body, &created); err != nil {
65 return nil, err
66 }
67 n.CreatedAt = parseTime(created)
68 out = append(out, n)
69 }
70 return out, rows.Err()
71}