aboutsummaryrefslogtreecommitdiff
path: root/cmd/_old/sanguisuga/tv.go
blob: 73273a87dd94d8ae1c62fead3373739f67766fdd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main

import (
	"encoding/json"
	"log/slog"
	"net/http"
)

func (s *Sanguisuga) UntrackTV(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()

	var show Show
	err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096)).Decode(&show)
	if err != nil {
		slog.Error("can't read request body", "err", err)
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	s.dbLock.Lock()
	defer s.dbLock.Unlock()

	s.db.Data.TVWatch = remove(s.db.Data.TVWatch, func(s Show) bool {
		return s.Title == show.Title
	})

	slog.Info("no longer tracking TV show", "show", show)

	if err := s.db.Save(); err != nil {
		slog.Error("can't save database", "err", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.WriteHeader(http.StatusNoContent)
}

func (s *Sanguisuga) TrackTV(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()

	var show Show
	err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 4096)).Decode(&show)
	if err != nil {
		slog.Error("can't read request body", "err", err)
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	s.dbLock.Lock()
	defer s.dbLock.Unlock()

	s.db.Data.TVWatch = append(s.db.Data.TVWatch, show)
	if err := s.db.Save(); err != nil {
		slog.Error("can't save database", "err", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	json.NewEncoder(w).Encode(s.db.Data.TVWatch)
}

func (s *Sanguisuga) ListTVSnatches(w http.ResponseWriter, r *http.Request) {
	s.dbLock.Lock()
	defer s.dbLock.Unlock()

	json.NewEncoder(w).Encode(s.db.Data.TVSnatches)
}

func (s *Sanguisuga) ListTV(w http.ResponseWriter, r *http.Request) {
	s.dbLock.Lock()
	defer s.dbLock.Unlock()

	json.NewEncoder(w).Encode(s.db.Data.TVWatch)
}