@@ -134,29 +134,30 @@ export async function returnRequestedShow (req: Request, res: Response): Promise
134134 showQuery = db . collection ( "shows" ) . doc ( showId ) ,
135135 showDocument = await showQuery . get ( ) ;
136136
137- if ( showDocument . exists ) {
138- if ( req . originalUrl . match ( / \/ r a w $ / i) ) {
137+ if ( ! showDocument . exists ) {
138+ res . status ( 404 ) . json ( { type : "error" , message : "Show not found" } ) ;
139+ return ;
140+ }
139141
140- const showData = showDocument . data ( ) as StoredShow | undefined ;
142+ if ( req . originalUrl . match ( / \/ r a w $ / i ) ) {
141143
142- if ( showData ) {
143- res . status ( 200 ) . json ( { type : "success" , data : { ...showData , id : showDocument . id } } ) ;
144- } else {
145- res . status ( 400 ) . json ( { type : "error" , message : "Something went wrong" } ) ;
146- }
144+ const showData = showDocument . data ( ) as StoredShow | undefined ;
147145
146+ if ( showData ) {
147+ res . status ( 200 ) . json ( { type : "success" , data : { ...showData , id : showDocument . id } } ) ;
148148 } else {
149+ res . status ( 400 ) . json ( { type : "error" , message : "Something went wrong" } ) ;
150+ }
149151
150- const show = constructShowFromDocument ( showDocument , true ) ;
152+ } else {
153+
154+ const show = constructShowFromDocument ( showDocument , true ) ;
151155
152- if ( show ) {
153- res . status ( 200 ) . json ( { type : "success" , data : show } ) ;
154- } else {
155- res . status ( 404 ) . json ( { type : "error" , message : "Show not found" } ) ;
156- }
156+ if ( show ) {
157+ res . status ( 200 ) . json ( { type : "success" , data : show } ) ;
158+ } else {
159+ res . status ( 404 ) . json ( { type : "error" , message : "Show not found" } ) ;
157160 }
158- } else {
159- res . status ( 404 ) . json ( { type : "error" , message : "Show not found" } ) ;
160161 }
161162
162163 } catch ( err : unknown ) {
@@ -182,36 +183,33 @@ export async function returnRequestedEpisodeChapters (req: Request, res: Respons
182183 showQuery = db . collection ( "shows" ) . doc ( showId ) ,
183184 showDocument = await showQuery . get ( ) ;
184185
185- if ( showDocument . exists ) {
186+ if ( ! showDocument . exists ) {
187+ res . status ( 404 ) . json ( { type : "error" , message : "Show not found" } ) ;
188+ return ;
189+ }
186190
187- const show = showDocument . data ( ) as StoredShow | undefined ;
191+ const show = showDocument . data ( ) as StoredShow | undefined ;
188192
189- if ( show ) {
190-
191- const
192- season = getSeasonFromEpisodeId ( show , parseInt ( episodeId ) ) ,
193- episode = getEpisodeById ( show , parseInt ( episodeId ) ) ;
194-
195- if ( season && episode ) {
196-
197- const episodeChapters = await fetchEpisodeChapters ( season . mal_id , episode . id , episode . duration ) ;
193+ if ( ! show ) {
194+ res . status ( 500 ) . json ( { type : "error" , message : "Something went wrong" } ) ;
195+ return ;
196+ }
198197
199- if ( episodeChapters ) {
200- res . status ( 200 ) . json ( { type : "success" , data : episodeChapters } ) ;
201- } else {
202- res . status ( 404 ) . json ( { type : "error" , message : "Chapters not found" } ) ;
203- }
198+ const
199+ season = getSeasonFromEpisodeId ( show , parseInt ( episodeId ) ) ,
200+ episode = getEpisodeById ( show , parseInt ( episodeId ) ) ;
204201
205- } else {
206- res . status ( 404 ) . json ( { type : "error" , message : "Episode not found" } ) ;
207- }
202+ if ( ! season || ! episode ) {
203+ res . status ( 404 ) . json ( { type : "error" , message : "Episode not found" } ) ;
204+ return ;
205+ }
208206
209- } else {
210- res . status ( 500 ) . json ( { type : "error" , message : "Something went wrong" } ) ;
211- }
207+ const episodeChapters = await fetchEpisodeChapters ( season . mal_id , episode . id , episode . duration ) ;
212208
209+ if ( episodeChapters ) {
210+ res . status ( 200 ) . json ( { type : "success" , data : episodeChapters } ) ;
213211 } else {
214- res . status ( 404 ) . json ( { type : "error" , message : "Show not found" } ) ;
212+ res . status ( 404 ) . json ( { type : "error" , message : "Chapters not found" } ) ;
215213 }
216214
217215 } catch ( err : unknown ) {
0 commit comments