Fix genre_xrefs not being created if genre already existed
[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{}
26 row := env.Db.Db.QueryRow("SELECT ID, NAME FROM ARTIST WHERE ID = ?", artistId)
27 if err := row.Scan(&artist.ID, &artist.Name); err != nil {
28 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
29 return
30 }
31 b, err := json.Marshal(artist)
32 if err != nil {
33 log.Print(err)
34 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
35 return
36 }
37 w.Write(b)
38 return
39 }
40
41 rows, err := env.Db.Db.Query("SELECT ID, NAME FROM ARTIST LIMIT 20")
42 if err != nil {
43 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
44 return
45 }
46 defer rows.Close()
47
48 artists := make([]types.Artist, 0)
49 for rows.Next() {
50 artist := types.Artist{}
51 if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
52 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
53 return
54 }
55 artists = append(artists, artist)
56 }
57 if err = rows.Err(); err != nil {
58 log.Print(err)
59 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
60 return
61 }
62 b, err := json.Marshal(artists)
63 if err != nil {
64 log.Print(err)
65 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
66 return
67 }
68 w.Write(b)
69 }
70}
71
2400eea4
JC
72func Add(env *env.Env) routes.Handler {
73 return func(w http.ResponseWriter, r *http.Request) {
74 if r.Method != "POST" {
75 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
76 return
77 }
78
79 r.ParseForm()
80 artistId := r.PostForm.Get("id")
81
82 if artistId == "" {
83 http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
84 return
85 }
86
87 artist, err := env.C.GetArtist(spotify.ID(artistId))
88 if err != nil {
89 log.Printf("Failed to retrieve artist %s: %s", artistId, err.Error())
90 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
91 return
92 }
93
94 env.Db.Mu.Lock()
95 defer env.Db.Mu.Unlock()
96 env.Db.Db.Exec("INSERT INTO ARTIST (ID, NAME) VALUES (?, ?)", artist.ID, artist.Name)
97
98 for _, genre := range artist.Genres {
d7efcbb7
JC
99 var genreId int64
100 row := env.Db.Db.QueryRow(`
101SELECT ID
102FROM GENRE
103WHERE NAME = lower(?)
104`,
105 genre)
106
107 err := row.Scan(&genreId)
108 if err == sql.ErrNoRows {
109 result, err := env.Db.Db.Exec("INSERT INTO GENRE (NAME) VALUES (?)", genre)
110 if err != nil {
111 log.Printf("Failed to insert genre %s: %s", genre, err.Error())
112 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
113 return
114 }
115
116 genreId, err = result.LastInsertId()
117 if err != nil {
118 log.Print("Failed to retrieve last insert id: ", err.Error())
119 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
120 return
121 }
2400eea4
JC
122 }
123
124 env.Db.Db.Exec("INSERT INTO ARTIST_GENRE_XREF (ARTIST_ID, GENRE_ID) VALUES (?, ?)", artist.ID, genreId)
125 }
126 w.WriteHeader(http.StatusCreated)
127 }
128}
eac0dfd6
JC
129
130func ByGenre(env *env.Env) routes.Handler {
131 return func(w http.ResponseWriter, r *http.Request) {
132 if r.Method != "GET" {
133 http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
134 return
135 }
136
137 w.Header().Set("Content-Type", "application/json")
138 genreName := r.FormValue("genre_name")
139 if genreName != "" {
140 rows, err := env.Db.Db.Query("SELECT ID, NAME FROM ARTIST WHERE ID IN (SELECT ARTIST_ID FROM ARTIST_GENRE_XREF WHERE GENRE_ID IN (SELECT ID FROM GENRE WHERE NAME = lower(?))) LIMIT 20", genreName)
141 if err != nil {
142 log.Print(err)
143 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
144 return
145 }
146 defer rows.Close()
147
148 artists := make([]types.Artist, 0)
149 for rows.Next() {
150 artist := types.Artist{}
151 if err := rows.Scan(&artist.ID, &artist.Name); err != nil {
152 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
153 return
154 }
155 artists = append(artists, artist)
156 }
157 if err = rows.Err(); err != nil {
158 log.Print(err)
159 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
160 return
161 }
162 b, err := json.Marshal(artists)
163 if err != nil {
164 log.Print(err)
165 http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
166 return
167 }
168 w.Write(b)
169 }
170 }
171}