|
@ -1,5 +1,6 @@ |
|
|
import { IpcClient as ManagerIpc } from "@autoplex-api/manager"; |
|
|
|
|
|
import { MovieInfo, MovieTicket } from "@autoplex/database"; |
|
|
|
|
|
|
|
|
import { ErrorType, IpcClient as ManagerIpc } from "@autoplex-api/manager"; |
|
|
|
|
|
import { MovieTicket } from "@autoplex/database"; |
|
|
|
|
|
import { IpcResponseError } from "@autoplex/ipc"; |
|
|
import { handleRequest, RouteRegisterFactory, MiddlewareMethod, Status, respond } from "@autoplex/webserver"; |
|
|
import { handleRequest, RouteRegisterFactory, MiddlewareMethod, Status, respond } from "@autoplex/webserver"; |
|
|
import Application from "../../../Application"; |
|
|
import Application from "../../../Application"; |
|
|
import MovieSearch from "../../MovieSearch"; |
|
|
import MovieSearch from "../../MovieSearch"; |
|
@ -88,39 +89,26 @@ export default function register(factory: RouteRegisterFactory<MiddlewareMethod< |
|
|
* Request a movie to download |
|
|
* Request a movie to download |
|
|
*/ |
|
|
*/ |
|
|
factory.post("/create/:tmdb_id", handleRequest([RequestTmdbMovieRequest], async (request, reply) => { |
|
|
factory.post("/create/:tmdb_id", handleRequest([RequestTmdbMovieRequest], async (request, reply) => { |
|
|
// Verify that the ID has not yet been requested
|
|
|
|
|
|
let tmdbId = (<any>request.params)["tmdb_id"]; |
|
|
|
|
|
if (0 != await MovieTicket.count({ where: { tmdbId, isCanceled: false } })) { |
|
|
|
|
|
return respond(reply, Status.Conflict); |
|
|
|
|
|
|
|
|
let ticketId: any; |
|
|
|
|
|
try { |
|
|
|
|
|
let userId = request.middlewareParams.auth.user.id; |
|
|
|
|
|
let tmdbId = (<any>request.params)["tmdb_id"].parseInt(); |
|
|
|
|
|
ticketId = app.service<ManagerIpc>("Manager").createMovieRequest(userId, tmdbId); |
|
|
|
|
|
} catch(e) { |
|
|
|
|
|
if (e instanceof IpcResponseError) { |
|
|
|
|
|
switch(e.message) { |
|
|
|
|
|
case ErrorType.MovieNotFound: |
|
|
|
|
|
respond(reply, Status.NotFound); |
|
|
|
|
|
return; |
|
|
|
|
|
case ErrorType.MovieTicketConflict: |
|
|
|
|
|
respond(reply, Status.Conflict); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
respond(reply, Status.InternalServerError); |
|
|
|
|
|
return; |
|
|
} |
|
|
} |
|
|
let movie = await app.service<MovieSearch>("Movie Search").details(tmdbId); |
|
|
|
|
|
if (!movie) { |
|
|
|
|
|
return respond(reply, Status.NotFound); |
|
|
|
|
|
} |
|
|
|
|
|
// Create the movie request ticket
|
|
|
|
|
|
let user = request.middlewareParams.auth.user; |
|
|
|
|
|
|
|
|
|
|
|
let info = new MovieInfo(); |
|
|
|
|
|
info.originalLanguage = movie.originalLanguage; |
|
|
|
|
|
info.originalTitle = movie.originalTitle; |
|
|
|
|
|
info.overview = movie.overview; |
|
|
|
|
|
info.posterPath = movie.posterPath; |
|
|
|
|
|
info.backdropPath = movie.backdropPath; |
|
|
|
|
|
info.releaseDate = movie.releaseDate; |
|
|
|
|
|
info.runtime = movie.runtime; |
|
|
|
|
|
await info.save(); |
|
|
|
|
|
|
|
|
|
|
|
let ticket = new MovieTicket(); |
|
|
|
|
|
ticket.tmdbId = movie.tmdbId; |
|
|
|
|
|
ticket.imdbId = movie.imdbId; |
|
|
|
|
|
ticket.title = movie.title; |
|
|
|
|
|
ticket.year = movie.releaseDate ? parseInt(movie.releaseDate.slice(0, 4)) : null; |
|
|
|
|
|
ticket.user = user; |
|
|
|
|
|
ticket.info = info; |
|
|
|
|
|
await ticket.save(); |
|
|
|
|
|
|
|
|
|
|
|
app.service<ManagerIpc>("Manager").notifyMovieRequested(ticket.id); |
|
|
|
|
|
respond(reply, Status.Ok, { ticketId: ticket.id }); |
|
|
|
|
|
|
|
|
respond(reply, Status.Ok, { ticketId }); |
|
|
})); |
|
|
})); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|