2020-01-03 08:39:59 +01:00
|
|
|
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<Array<Movie>> {
|
|
|
|
return (await DatabaseHelper.Query({
|
|
|
|
table: MOVIES_TABLE,
|
|
|
|
where: {
|
|
|
|
ID_user: userID
|
|
|
|
},
|
|
|
|
order: "ID DESC"
|
|
|
|
})).map((row) => this.DBToMovie(row));
|
|
|
|
}
|
|
|
|
|
2020-01-03 10:24:05 +01:00
|
|
|
/**
|
|
|
|
* Get information about a single movie
|
|
|
|
*
|
|
|
|
* @param movieID The movie to get
|
|
|
|
*/
|
|
|
|
public static async GetInfo(movieID: number) : Promise<Movie> {
|
|
|
|
const row = await DatabaseHelper.QueryRow({
|
|
|
|
table: MOVIES_TABLE,
|
|
|
|
where: {
|
|
|
|
ID: movieID
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(row == null)
|
|
|
|
throw Error("Movie " + movieID + " could not be found!");
|
|
|
|
|
|
|
|
return this.DBToMovie(row);
|
|
|
|
}
|
|
|
|
|
2020-03-20 11:42:45 +01:00
|
|
|
/**
|
|
|
|
* Check out whether a movie belongs to a user or not
|
|
|
|
*
|
|
|
|
* @param userID Target user ID
|
|
|
|
* @param movieID The ID of the movie to check
|
|
|
|
*/
|
|
|
|
public static async DoesUserHas(userID: number, movieID: number): Promise<boolean> {
|
|
|
|
try {
|
|
|
|
return (await this.GetInfo(movieID)).userID == userID;
|
|
|
|
} catch (error) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-03 08:39:59 +01:00
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|