diff --git a/src/controllers/MoviesController.ts b/src/controllers/MoviesController.ts new file mode 100644 index 0000000..fc045f4 --- /dev/null +++ b/src/controllers/MoviesController.ts @@ -0,0 +1,40 @@ +import { RequestHandler } from "../entities/RequestHandler"; +import { MoviesHelper } from "../helpers/MoviesHelper"; +import { Movie } from "../entities/Movies"; + +/** + * Movies controller + * + * @author Pierre HUBERT + */ + +export class MoviesController { + + /** + * Get the list of movies of the user + * + * @param h Request handler + */ + public static async GetList(h: RequestHandler) { + const list = await MoviesHelper.GetListUser(h.getUserId()); + + h.send(list.map((m) => this.MovieToAPI(m))); + } + + /** + * Turn a movie into an API entry + * + * @param m The movie to convert + */ + private static MovieToAPI(m: Movie): any { + return { + id: m.id, + uri: m.uri, + url: m.url, + userID: m.userID, + name: m.name, + file_type: m.fileType, + size: m.size + } + } +} \ No newline at end of file diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index db2968d..acfbee9 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -10,6 +10,7 @@ import { VirtualDirectoryController } from "./VirtualDirectoryController"; import { WebAppControllers } from "./WebAppController"; import { CallsController } from "./CallsController"; import { FriendsController } from "./FriendsController"; +import { MoviesController } from "./MoviesController"; /** * Controllers routes @@ -190,6 +191,11 @@ export const Routes : Route[] = [ + // Movies controller + {path: "/movies/get_list", cb: (h) => MoviesController.GetList(h)}, + + + // Virtual directory controller {path: "/user/findbyfolder", cb: (h) => VirtualDirectoryController.FindUser(h)}, diff --git a/src/entities/Movies.ts b/src/entities/Movies.ts new file mode 100644 index 0000000..15cbf98 --- /dev/null +++ b/src/entities/Movies.ts @@ -0,0 +1,35 @@ +import { pathUserData } from "../utils/UserDataUtils"; + +/** + * Movie entity + * + * @author Pierre HUBERT + */ + +export interface MovieBuilder { + id: number, + userID: number, + name: string, + uri: string, + fileType: string, + size: number +} + +export class Movie implements MovieBuilder { + id: number; userID: number; + name: string; + uri: string; + fileType: string; + size: number; + + public constructor(info: MovieBuilder) { + for (const key in info) { + if (info.hasOwnProperty(key)) + this[key] = info[key]; + } + } + + get url() : string { + return pathUserData(this.uri); + } +} \ No newline at end of file diff --git a/src/helpers/MoviesHelper.ts b/src/helpers/MoviesHelper.ts new file mode 100644 index 0000000..7e23e48 --- /dev/null +++ b/src/helpers/MoviesHelper.ts @@ -0,0 +1,45 @@ +import { RequestHandler } from "../entities/RequestHandler"; +import { Movie } from "../entities/Movies"; +import { DatabaseHelper } from "./DatabaseHelper"; + +/** + * Legacy movies helper + * + * @author Pierre HUBERT + */ + +const MOVIES_TABLE = "galerie_video"; + +export class MoviesHelper { + + /** + * Get the list of movies of the user + * + * @param userID The ID of the target user + */ + public static async GetListUser(userID: number) : Promise> { + return (await DatabaseHelper.Query({ + table: MOVIES_TABLE, + where: { + ID_user: userID + }, + order: "ID DESC" + })).map((row) => this.DBToMovie(row)); + } + + /** + * Turn a database entry into a movie object + * + * @param row Database entry + */ + private static DBToMovie(row: any) : Movie { + return new Movie({ + id: row.ID, + uri: row.URL, + userID: row.ID_user, + name: row.nom_video, + fileType: row.file_type, + size: Number(row.size) + }); + } +} \ No newline at end of file