Skip to content

Commit

Permalink
Merge pull request #99 from Dreallers/name
Browse files Browse the repository at this point in the history
Name
  • Loading branch information
Aurelemeriau authored Feb 7, 2024
2 parents 6a19aa0 + f6a0a94 commit 94be6a4
Show file tree
Hide file tree
Showing 26 changed files with 433 additions and 128 deletions.
5 changes: 4 additions & 1 deletion backend/database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ CREATE TABLE
`avatar_filename` VARCHAR(255)
);

-- ! Add a videoFilename column to the Film table to store the video file name in case there is no video URL and the video is stored locally

DROP TABLE IF EXISTS `Film`;

CREATE TABLE
Expand All @@ -17,7 +19,8 @@ CREATE TABLE
`cover_url` VARCHAR(255) DEFAULT NULL,
`cover_filename` VARCHAR(255) DEFAULT NULL,
`title` VARCHAR(255) NOT NULL,
`videoUrl` VARCHAR(255) NOT NULL,
`videoUrl` VARCHAR(255) DEFAULT NULL,
`videoFilename` VARCHAR(255) DEFAULT NULL,
`duration` INT NOT NULL,
`year` VARCHAR(4) NOT NULL,
`description` VARCHAR(700) NOT NULL,
Expand Down
5 changes: 4 additions & 1 deletion backend/public/assets/icons/circle-arrow-left-solid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions backend/public/assets/icons/xmark-solid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions backend/src/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// const path = require("path");
// Load the express module to create a web application

const express = require("express");
Expand Down Expand Up @@ -145,4 +146,8 @@ app.use(logErrors);

/* ************************************************************************* */

// app.get("*", (req, res) => {
// res.sendFile(path.join(__dirname, "/../public/index.html"));
// });

module.exports = app;
1 change: 0 additions & 1 deletion backend/src/controllers/authControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const login = async (req, res, next) => {
user.hashed_password,
req.body.password
);
console.warn("verified =>", verified);

if (verified) {
// delete user.hashed_password;
Expand Down
1 change: 0 additions & 1 deletion backend/src/controllers/categorieControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const count = async (request, response, next) => {
try {
// Fetch all items from the database
const categories = await tables.Categorie.count();
console.warn(categories);

// Respond with the items in JSON format
response.json(categories);
Expand Down
43 changes: 25 additions & 18 deletions backend/src/controllers/filmControllers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const tables = require("../tables");

// % How to retrieve the uploaded videos from the request object in the edit method?

const browse = async (req, res, next) => {
try {
const films = await tables.Film.readAll();
Expand All @@ -25,6 +27,19 @@ const read = async (req, res, next) => {
const edit = async (req, res, next) => {
const { id } = req.params;
req.body.id = id;

if (req.files.cover && req.files.cover[0]) {
req.body.cover_filename = req.files.cover[0].filename;
}

if (req.files.miniature && req.files.miniature[0]) {
req.body.miniature_filename = req.files.miniature[0].filename;
}

if (req.files.videoFile && req.files.videoFile[0]) {
req.body.videoFilename = req.files.videoFile[0].filename;
}

try {
const result = await tables.Film.update(req.body);
if (result) {
Expand All @@ -39,13 +54,16 @@ const edit = async (req, res, next) => {
};

const add = async (req, res, next) => {
if (req.body.images.length === 2) {
const miniature = req.body.images[0];
const cover = req.body.images[1];
req.body.miniature_filename = miniature;
req.body.cover_filename = cover;
} else {
res.status(403).send({ message: "Missing file" });
if (req.files.cover && req.files.cover[0]) {
req.body.cover_filename = req.files.cover[0].filename;
}

if (req.files.miniature && req.files.miniature[0]) {
req.body.miniature_filename = req.files.miniature[0].filename;
}

if (req.files.videoFile && req.files.videoFile[0]) {
req.body.videoFilename = req.files.videoFile[0].filename;
}

const film = req.body;
Expand All @@ -67,17 +85,6 @@ const add = async (req, res, next) => {
categoriesIds,
});

// Utiliser Promise.all pour paralléliser les opérations asynchrones
// const response = await Promise.all(
// categories.map(async (category) => {
// // Appeler la fonction asynchrone pour la liaison catégorie-film
// await tables.categorie_par_film.create({
// filmId: insertId,
// categorieId: category.id,
// });
// })
// );

if (response) {
res.status(200).json({ insertId });
} else {
Expand Down
11 changes: 1 addition & 10 deletions backend/src/controllers/userControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,6 @@ const edit = async (req, res, next) => {
res.status(404).json({ error: "User not found" });
}

console.warn("currentUser =>", currentUser);
console.warn("currentUser.hashed_password =>", currentUser.hashed_password);

console.warn(
"currentPassword && newPassword =>",
current_password,
new_password
);

// Ensure currentUser has a hashed_password and it's not empty
if (
!currentUser.hashed_password ||
Expand All @@ -133,7 +124,7 @@ const edit = async (req, res, next) => {
currentUser.hashed_password,
current_password
);
console.warn("isPasswordCorrect =>", isPasswordCorrect);

if (!isPasswordCorrect) {
return res.status(400).json({ error: "Incorrect current password" });
}
Expand Down
2 changes: 0 additions & 2 deletions backend/src/models/CategorieParFilmManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ class CategorieParFilmManager extends AbstractManager {

arrDep.push(filmId, catId, unique_key);
});
// console.log("querySQL =>", querySQL);
// console.log("arrDep =>", arrDep);

// Execute the SQL INSERT query to add a new categorieParFilm to the "categorieParFilm" table
const result = await this.database.query(`${querySQL};`, arrDep);
Expand Down
12 changes: 10 additions & 2 deletions backend/src/models/FilmManager.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const AbstractManager = require("./AbstractManager");

// § Add videoFilename field in each method of the FilmManager class.

class FilmManager extends AbstractManager {
constructor() {
super({ table: "Film" });
Expand All @@ -10,18 +12,20 @@ class FilmManager extends AbstractManager {
cover_filename,
title,
videoUrl,
videoFilename,
duration,
year,
description,
isAvailable,
}) {
const [result] = await this.database.query(
`insert into ${this.table} (miniature_filename, cover_filename, title, videoUrl, duration, year, description, isAvailable) values (?, ?, ?, ?, ?, ?, ?, ?)`,
`insert into ${this.table} (miniature_filename, cover_filename, title, videoUrl, videoFilename, duration, year, description, isAvailable) values (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
miniature_filename,
cover_filename,
title,
videoUrl,
videoFilename,
duration,
year,
description,
Expand All @@ -47,19 +51,23 @@ class FilmManager extends AbstractManager {
async update({
id,
miniature_filename,
cover_filename,
title,
videoUrl,
videoFilename,
duration,
year,
description,
IsAvailable,
}) {
const [result] = await this.database.query(
`update ${this.table} SET miniature_filename=?, title=?, videoUrl=?, duration=?, year=?, description=?, IsAvailable=? where id=?`,
`update ${this.table} SET miniature_filename = COALESCE(?, miniature_filename), cover_filename = COALESCE(?, cover_filename), title = COALESCE(?, title), videoUrl = COALESCE(?, videoUrl), videoFilename = COALESCE(?, videoFilename), duration = COALESCE(?, duration), year = COALESCE(?, year), description = COALESCE(?, description), IsAvailable = COALESCE(?, IsAvailable) where id=?`,
[
miniature_filename,
cover_filename,
title,
videoUrl,
videoFilename,
duration,
year,
description,
Expand Down
24 changes: 21 additions & 3 deletions backend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const router = express.Router();

// Import itemControllers module for handling item-related operations

const { uploadImages } = require("./services/multer");
const { uploadImages, uploadImages2 } = require("./services/multer");
const { hashPassword, verifyToken } = require("./services/auth");

const userControllers = require("./controllers/userControllers");
Expand All @@ -22,6 +22,8 @@ const commentaireFilmControllers = require("./controllers/commentaireFilmControl

const authControllers = require("./controllers/authControllers");

// § I would to implement the upload of video files for adding or editing a film with the multer package.

// Route to get a list of items
router.get("/users", userControllers.browse);
router.get("/films", filmControllers.browse);
Expand Down Expand Up @@ -65,7 +67,15 @@ router.get("/category/:id", categorieControllers.read);
// Route to edit a specific item by ID
router.put("/user/:id", userControllers.edit);
router.put("/comments/:commentId", commentaireFilmControllers.updateComment);
router.put("/films/:id", filmControllers.edit);
router.put(
"/films/:id",
uploadImages2.fields([
{ name: "miniature", maxCount: 1 },
{ name: "cover", maxCount: 1 },
{ name: "videoFile", maxCount: 1 },
]),
filmControllers.edit
);
router.put("/category/:id", categorieControllers.edit);

// Route to add a new item
Expand All @@ -74,7 +84,15 @@ router.post("/users", hashPassword, userControllers.add);
router.post("/favorites/film", favoriFilmControllers.addMovieToFavorite);
router.post("/watchlist/film", watchlistControllers.addMovieToWatchlist);
router.post("/comments", commentaireFilmControllers.addComment);
router.post("/films", uploadImages.array("images", 2), filmControllers.add);
router.post(
"/films",
uploadImages.fields([
{ name: "miniature", maxCount: 1 },
{ name: "cover", maxCount: 1 },
{ name: "videoFile", maxCount: 1 },
]),
filmControllers.add
);
router.post(
"/film/:filmId/category/:categoryId",
categorieParFilmControllers.addFilmToCategory
Expand Down
38 changes: 31 additions & 7 deletions backend/src/services/multer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
const multer = require("multer");
const { v4 } = require("uuid");

// ! Implement the upload of video files for adding or editing a film with the multer package.

const imagesStorage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "./public/assets/images");
Expand All @@ -12,20 +14,42 @@ const imagesStorage = multer.diskStorage({
// eslint-disable-next-line prefer-template
const name = v4() + "." + extension;

if (req.body.images) {
req.body.images.push(name);
} else {
req.body.images = [name];
if (req.body.cover) {
req.body.cover = name;
}

if (req.body.miniature) {
req.body.miniature = name;
}
cb(null, name);
},
});

const imagesStorage2 = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "./public/assets/images");
},
filename: (req, file, cb) => {
const extArray = file.mimetype.split("/");
const extension = extArray[extArray.length - 1];
// eslint-disable-next-line prefer-template
const name = v4() + "." + extension;

if (req.body.cover) {
req.body.cover = name;
}

if (req.body.miniature) {
req.body.miniature = name;
}
cb(null, name);
},
// limits: {
// fieldSize: 1024 * 5,
// },
});

const uploadImages = multer({ storage: imagesStorage });
const uploadImages2 = multer({ storage: imagesStorage2 });

module.exports = {
uploadImages,
uploadImages2,
};
4 changes: 2 additions & 2 deletions frontend/src/components/CategoryDisplay.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ function CategoryDisplay({ categorie, getCategories }) {
toast.success("Category deleted");
getCategories();
} else {
toast.error("An error occurred");
toast.error("Error deleting category");
}
} catch (error) {
console.error(error);
toast.error("An error occurred");
toast.error("Error deleting category");
} finally {
setIsDeleting(false);
}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/FreeMovie.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ FreeMovie.propTypes = {
movie: PropTypes.shape({
id: PropTypes.number.isRequired,
cover_filename: PropTypes.string,
cover_url: PropTypes.string.isRequired,
cover_url: PropTypes.string,
title: PropTypes.string.isRequired,
year: PropTypes.string.isRequired,
duration: PropTypes.number.isRequired,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/MovieDescription.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ MovieDescription.propTypes = {
title: PropTypes.string.isRequired,
description: PropTypes.string.isRequired,
cover_filename: PropTypes.string,
cover_url: PropTypes.string.isRequired,
cover_url: PropTypes.string,
year: PropTypes.string.isRequired,
duration: PropTypes.number.isRequired,
IsAvailable: PropTypes.number.isRequired,
Expand Down
10 changes: 6 additions & 4 deletions frontend/src/components/MovieLoginRequired.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useParams } from "react-router-dom";
import { NavLink, useParams } from "react-router-dom";
import PropTypes from "prop-types";
import CommentsSection from "./CommentsSection";

Expand Down Expand Up @@ -51,9 +51,11 @@ function MovieLoginRequired({ movie }) {
</div>
<div className="upper-layer">
<div className="registration-invitation-container">
<p className="registration-invitation">
Log in to watch this movie.
</p>
<NavLink to="/connection" className="login-link">
<p className="registration-invitation">
Log in to watch this movie.
</p>
</NavLink>
</div>
</div>
</div>
Expand Down
Loading

0 comments on commit 94be6a4

Please sign in to comment.