docs.go
| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | // Doc ist eine eingescannte oder fotografierte Unterlage eines Patienten. |
| 9 | type Doc struct { |
| 10 | ID, PatientID int64 |
| 11 | Filename, Original, Title, Category string |
| 12 | Date string // YYYY-MM-DD |
| 13 | Size int64 |
| 14 | Created time.Time |
| 15 | } |
| 16 | |
| 17 | // DocCategories für die Auswahlliste; freie Eingabe bleibt möglich. |
| 18 | var DocCategories = []string{ |
| 19 | "Ärztliche Verordnung", "Arztbrief / Befund", "OP-Bericht", "Bildgebung (Röntgen, MRT …)", |
| 20 | "Entlassungsbericht", "Einwilligung / Formular", "Foto (Verlauf, Wunde, Haltung)", "Sonstiges", |
| 21 | } |
| 22 | |
| 23 | func (s *Store) Docs(ctx context.Context, pid int64) ([]Doc, error) { |
| 24 | rows, err := s.db.QueryContext(ctx, `SELECT id, patient_id, filename, original, title, category, date, size, created_at |
| 25 | FROM patient_docs WHERE patient_id = ? ORDER BY date DESC, id DESC`, pid) |
| 26 | if err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | defer rows.Close() |
| 30 | var out []Doc |
| 31 | for rows.Next() { |
| 32 | var d Doc |
| 33 | var c int64 |
| 34 | if err := rows.Scan(&d.ID, &d.PatientID, &d.Filename, &d.Original, &d.Title, &d.Category, &d.Date, &d.Size, &c); err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | d.Created = time.Unix(c, 0) |
| 38 | out = append(out, d) |
| 39 | } |
| 40 | return out, rows.Err() |
| 41 | } |
| 42 | |
| 43 | func (s *Store) AddDoc(ctx context.Context, d Doc) error { |
| 44 | _, err := s.db.ExecContext(ctx, `INSERT INTO patient_docs (patient_id, filename, original, title, category, date, size) |
| 45 | VALUES (?, ?, ?, ?, ?, ?, ?)`, d.PatientID, d.Filename, d.Original, d.Title, d.Category, d.Date, d.Size) |
| 46 | if err == nil { |
| 47 | s.touchPatient(ctx, d.PatientID) |
| 48 | } |
| 49 | return err |
| 50 | } |
| 51 | |
| 52 | func (s *Store) DeleteDoc(ctx context.Context, id int64) (Doc, error) { |
| 53 | var d Doc |
| 54 | err := s.db.QueryRowContext(ctx, `DELETE FROM patient_docs WHERE id = ? RETURNING id, patient_id, filename`, id). |
| 55 | Scan(&d.ID, &d.PatientID, &d.Filename) |
| 56 | return d, notFound(err) |
| 57 | } |
| 58 | |
| 59 | // DocByFile liefert die Unterlage zu einem gespeicherten Dateinamen (nur bekannte Dateien werden ausgeliefert). |
| 60 | func (s *Store) DocByFile(ctx context.Context, filename string) (Doc, error) { |
| 61 | var d Doc |
| 62 | err := s.db.QueryRowContext(ctx, `SELECT id, patient_id, filename, original, title FROM patient_docs WHERE filename = ?`, filename). |
| 63 | Scan(&d.ID, &d.PatientID, &d.Filename, &d.Original, &d.Title) |
| 64 | return d, notFound(err) |
| 65 | } |
| 66 | |
| 67 | // DocFiles: alle Dateinamen eines Patienten, damit sie beim Löschen des Patienten mit entfernt werden. |
| 68 | func (s *Store) DocFiles(ctx context.Context, pid int64) ([]string, error) { |
| 69 | docs, err := s.Docs(ctx, pid) |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | files := make([]string, len(docs)) |
| 74 | for i, d := range docs { |
| 75 | files[i] = d.Filename |
| 76 | } |
| 77 | return files, nil |
| 78 | } |
| 79 |