Format SQL style
[brackets.git] / routes / artist / artist.go
CommitLineData
2400eea4
JC
1package artist
2
3import (
d7efcbb7 4 "database/sql"
2353fc58 5 "encoding/json"
2400eea4
JC
6 "git.jacobcasper.com/brackets/env"
7 "git.jacobcasper.com/brackets/routes"
2353fc58 8 "git.jacobcasper.com/brackets/types"
2400eea4
JC
9 "github.com/zmb3/spotify"
10 "log"
11 "net/http"
12)
13
2353fc58
JC
14func Index(env *env.Env) routes.Handler {
15 return func(w http.ResponseWriter, r *http.Request) {
16 if r.Method != "GET" {
17 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
18 return
19 }
20
21 w.Header().Set("Content-Type", "application/json")
22
23 artistId := r.FormValue("id")
24 if artistId != "" {
25 artist := types.Artist{}
9a3c373c
JC
26 row := env.Db.Db.QueryRow(`
27SELECT ID, NAME
28FROM ARTIST
29WHERE ID = ?`,
30 artistId,
31 )
2353fc58
JC
32 if err := row.Scan(&artist.ID, &artist.Name); err != nil {
33 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
34 return
35 }
36 b, err := json.Marshal(artist)
37 if err != nil {
38 log.Print(err)
39 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
40 return
41 }
42 w.Write(b)
43 return
44 }
45
9a3c373c
JC
46 rows, err := env.Db.Db.Query(`
47SELECT ID, NAME
48FROM ARTIST
49LIMIT 20`,
50 )
2353fc58
JC
51 if err != nil {
52 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
53 return
54 }
55 defer rows.Close()
56
57 artists := make([]types.Artist, 0)
58 for rows.Next() {
59 artist := types.Artist{}
60 if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
61 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
62 return
63 }
64 artists = append(artists, artist)
65 }
66 if err = rows.Err(); err != nil {
67 log.Print(err)
68 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
69 return
70 }
71 b, err := json.Marshal(artists)
72 if err != nil {
73 log.Print(err)
74 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
75 return
76 }
77 w.Write(b)
78 }
79}
80
2400eea4
JC
81func Add(env *env.Env) routes.Handler {
82 return func(w http.ResponseWriter, r *http.Request) {
83 if r.Method != "POST" {
84 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
85 return
86 }
87
88 r.ParseForm()
89 artistId := r.PostForm.Get("id")
90
91 if artistId == "" {
92 http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
93 return
94 }
95
96 artist, err := env.C.GetArtist(spotify.ID(artistId))
97 if err != nil {
98 log.Printf("Failed to retrieve artist %s: %s", artistId, err.Error())
99 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
100 return
101 }
102
103 env.Db.Mu.Lock()
104 defer env.Db.Mu.Unlock()
9a3c373c
JC
105 env.Db.Db.Exec(`
106INSERT INTO ARTIST
107(ID, NAME)
108VALUES (?, ?)`,
109 artist.ID,
110 artist.Name,
111 )
2400eea4
JC
112
113 for _, genre := range artist.Genres {
d7efcbb7
JC
114 var genreId int64
115 row := env.Db.Db.QueryRow(`
116SELECT ID
117FROM GENRE
118WHERE NAME = lower(?)
119`,
9a3c373c
JC
120 genre,
121 )
d7efcbb7
JC
122
123 err := row.Scan(&genreId)
124 if err == sql.ErrNoRows {
9a3c373c
JC
125 result, err := env.Db.Db.Exec(`
126INSERT INTO GENRE
127(NAME)
128VALUES (?)`,
129 genre,
130 )
d7efcbb7
JC
131 if err != nil {
132 log.Printf("Failed to insert genre %s: %s", genre, err.Error())
133 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
134 return
135 }
136
137 genreId, err = result.LastInsertId()
138 if err != nil {
139 log.Print("Failed to retrieve last insert id: ", err.Error())
140 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
141 return
142 }
2400eea4
JC
143 }
144
9a3c373c
JC
145 env.Db.Db.Exec(`
146INSERT INTO ARTIST_GENRE_XREF
147(ARTIST_ID, GENRE_ID)
148VALUES (?, ?)`,
149 artist.ID,
150 genreId,
151 )
2400eea4
JC
152 }
153 w.WriteHeader(http.StatusCreated)
154 }
155}
eac0dfd6
JC
156
157func ByGenre(env *env.Env) routes.Handler {
158 return func(w http.ResponseWriter, r *http.Request) {
159 if r.Method != "GET" {
160 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
161 return
162 }
163
164 w.Header().Set("Content-Type", "application/json")
165 genreName := r.FormValue("genre_name")
166 if genreName != "" {
9a3c373c
JC
167 rows, err := env.Db.Db.Query(`
168SELECT a.ID, a.NAME
169FROM ARTIST a
170JOIN ARTIST_GENRE_XREF x ON a.ID = x.ARTIST_ID
171JOIN GENRE g ON g.ID = x.GENRE_ID
172WHERE g.NAME = lower(?)
173`,
174 genreName,
175 )
eac0dfd6
JC
176 if err != nil {
177 log.Print(err)
178 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
179 return
180 }
181 defer rows.Close()
182
183 artists := make([]types.Artist, 0)
184 for rows.Next() {
185 artist := types.Artist{}
186 if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
187 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
188 return
189 }
190 artists = append(artists, artist)
191 }
192 if err = rows.Err(); err != nil {
193 log.Print(err)
194 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
195 return
196 }
197 b, err := json.Marshal(artists)
198 if err != nil {
199 log.Print(err)
200 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
201 return
202 }
203 w.Write(b)
204 }
205 }
206}