Skip to content

Commit

Permalink
Merge pull request #64 from WildCodeSchool/remove-extra-files
Browse files Browse the repository at this point in the history
Remove extra files
  • Loading branch information
mahdimcheik authored Mar 9, 2024
2 parents a93f648 + 6c29108 commit 3a5bbf8
Show file tree
Hide file tree
Showing 24 changed files with 389 additions and 197 deletions.
2 changes: 1 addition & 1 deletion backend/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const migrate = async () => {

// this line is to be deleted later //////////////////////////////////////
// Drop the existing database if it exists
// await database.query(`drop database if exists ${DB_NAME}`);
await database.query(`drop database if exists ${DB_NAME}`);

// Create a new database with the specified name
// await database.query(`create database ${DB_NAME}`);
Expand Down
24 changes: 24 additions & 0 deletions backend/src/controllers/userControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,29 @@ async function updateDescription(req, res) {
}
}

async function updateRole(req, res) {
try {
const { email, role } = req.params;
const isAdmin = +role;
const { affectedRows } = await userManager.update({
isAdmin,
email,
});
if (affectedRows !== 0) {
res.status(200).json({
message: "Nom et prénom actualisés!!!",
result: true,
});
} else {
res
.status(404)
.json({ message: "Utilisateur non existant", result: false });
}
} catch (err) {
res.status(404).json({ message: err.message, result: false });
}
}

async function sendResetPassword(req, res) {
try {
const { email } = req.body;
Expand Down Expand Up @@ -277,4 +300,5 @@ module.exports = {
browseByEmail,
getProfile,
updateDescription,
updateRole,
};
46 changes: 46 additions & 0 deletions backend/src/controllers/videoControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ const browse = async (req, res, next) => {
}
};

// The B of BREAD - Browse (Read All) operation
async function browsePagination(req, res) {
try {
const { start, offset } = req.query;
const videos = await videoManager.browsePagination(start, offset);
res.status(200).json({
videos: videos.rows,
count: videos.length,
message: "all good",
});
} catch (err) {
res.sendStatus(404);
}
}

const browsePlaylists = async (req, res, next) => {
try {
// Fetch all items from the database
Expand Down Expand Up @@ -60,6 +75,35 @@ const readPlaylist = async (req, res) => {
res.status(404).json({ message: err.message, success: false });
}
};

const browseVideosFromPlaylist = async (req, res) => {
try {
const { playlistId } = req.params;
const { start, offset } = req.query;
// Fetch a specific item from the database based on the provided ID
const { videos, title, count } = await videoManager.readPlayListPagination(
playlistId,
start,
offset
);
if (videos == null) {
res
.status(200)
.json({ message: "Playliste non existante", success: false });
} else {
res.status(200).json({
videos,
title,
message: "play list récu",
success: true,
count,
});
}
} catch (err) {
// Pass any errors to the error-handling middleware
res.status(404).json({ message: err.message, success: false });
}
};
// The E of BREAD - Edit (Update) operation
// This operation is not yet implemented
async function edit(req, res) {
Expand Down Expand Up @@ -278,6 +322,7 @@ module.exports = {
check,
readPlaylist,
browsePlaylists,
browsePagination,
getPlaylists,
getPlaylistsByCategory,
addPlaylist,
Expand All @@ -286,4 +331,5 @@ module.exports = {
getPlaylistsPagination,
addPlaylistFromYoutube,
getPlaylistsByCategoryPagination,
browseVideosFromPlaylist,
};
37 changes: 37 additions & 0 deletions backend/src/models/videoManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,22 @@ class VideoManager {
return rows;
}

static async browsePagination(start, offset) {
try {
const [count] = await database.query(
`select count(*) as length from video`
);
// console.log(count[0]);
const [rows] = await database.query(
`select * from video limit ${start}, ${offset}`
);
// console.log(rows);
return { rows, ...count[0] };
} catch (error) {
throw new Error(error.message);
}
}

static async readAllById(ytId) {
const [rows] = await database.query(
`select * from video where ytId like '${ytId}%'`
Expand All @@ -80,6 +96,27 @@ class VideoManager {
return { rows, title: playlist[0].playlistTitle };
}

static async readPlayListPagination(playListId, start, offset) {
const [count] = await database.query(
`select count(*) as length from video where playlistId = ?`,
[playListId]
);
const [rows] = await database.query(
`select * from video where playlistId = ? limit ${start}, ${offset}`,
[playListId]
);
const [playlist] = await database.query(
`select playlistTitle from playlist where playlistId = ? `,
[playListId]
);

return {
videos: rows,
title: playlist[0].playlistTitle,
count: count[0].length,
};
}

static async update(ytId, video) {
let sql = "UPDATE video set";
const sqlValues = [];
Expand Down
8 changes: 8 additions & 0 deletions backend/src/routerAdmin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const express = require("express");
const adminController = require("./controllers/adminController");
const middleware = require("./middlewares/userMiddle");
const userControllers = require("./controllers/userControllers");

const routerAdmin = express.Router();

Expand All @@ -17,4 +18,11 @@ routerAdmin.delete(
adminController.destroy
);

// Route to change role
routerAdmin.patch(
"/users/:email/:role",
middleware.verifyAdminToken,
userControllers.updateRole
);

module.exports = routerAdmin;
9 changes: 8 additions & 1 deletion backend/src/routerVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ const videoControllers = require("./controllers/videoControllers");
routerVideo.get("/playlist", videoControllers.readPlaylist);

// Route to get a list of items
routerVideo.get("/videos/:max", videoControllers.browse);
// routerVideo.get("/videos/:max", videoControllers.browse);
routerVideo.get(
"/videos/:playlistId",
videoControllers.browseVideosFromPlaylist
);

// Route to get a list of items
routerVideo.get("/videos", videoControllers.browsePagination);

// Route to get a specific item by ID
routerVideo.get("/video/:ytId", videoControllers.read);
Expand Down
84 changes: 84 additions & 0 deletions frontend/src/admin/PlaylistVideos.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Center, Container, Grid, Pagination, Progress } from "@mantine/core";
import { useEffect, useRef, useState } from "react";
import axios from "axios";
import { useParams } from "react-router-dom";
import SingleLineVideo from "../components/SingleLineVideo";
import VideoBarPlaylist from "./VideoBarPlaylist";

export default function PlaylistVideos() {
const videoPerPage = useRef(10);
const [activePage, setPage] = useState(1);
const [videos, setVideos] = useState([]);
const [playlistTitle, setPlaylistTitle] = useState("");
const [countVideos, setCountVideos] = useState(1);

const { playlistId } = useParams();

async function getVideoListFromPlaylist(playlistid, start, offset) {
try {
const { data } = await axios.get(
`${
import.meta.env.VITE_BACKEND_URL
}/api/videos/${playlistid}?start=${start}&offset=${offset}`
);
setVideos([...data.videos]);
setCountVideos(data.count);
setPlaylistTitle(data.title);
} catch (err) {
setVideos([]);
}
}

useEffect(() => {
async function getData() {
await getVideoListFromPlaylist(
playlistId,
(activePage - 1) * videoPerPage.current,
videoPerPage.current
);
}
getData();
}, [activePage]);

return (
<Container size="fluid">
<VideoBarPlaylist p={0} playlistTitle={playlistTitle} />
<Progress h={4} className="yellow-3" mt={10} mb={5} />
<Grid>
<Grid.Col span={3}>Titre</Grid.Col>
<Grid.Col span={1}>Cachée</Grid.Col>
<Grid.Col span={1}>Pubique </Grid.Col>
<Grid.Col span={1}>Durée </Grid.Col>
<Grid.Col span={2}>Playliste </Grid.Col>
<Grid.Col span={4}>
<Center>Action</Center>
</Grid.Col>
</Grid>
<div
className="list-playlist"
style={{ height: "400px", overflowY: "auto", overflowX: "hidden" }}
>
{videos.map((ele) => (
<SingleLineVideo
key={ele.ytId}
ytId={ele.ytId}
titre={ele.title}
isHidden={ele.isHidden}
isPublic={ele.isPublic}
duration={ele.duration}
playList={ele.playlistTitle}
playListId={ele.playlistId}
/>
))}
</div>
{videoPerPage.current < countVideos && (
<Pagination
total={countVideos / videoPerPage.current + 1}
value={activePage}
onChange={setPage}
className="pages"
/>
)}
</Container>
);
}
4 changes: 3 additions & 1 deletion frontend/src/admin/Playlists.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export default function Playlists() {
<TaskBarPlaylist />
<PlaylistTopLine />
<div className="list-playlist">
<div>
<div
style={{ height: "300px", overflowY: "auto", overflowX: "hidden" }}
>
{playlists.map((ele, index) => (
<h2 key={ele.playlistId}>
<SingleLinePlaylist
Expand Down
19 changes: 1 addition & 18 deletions frontend/src/admin/VideoBar.jsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
import { Container, Input } from "@mantine/core";
import { FaSearch } from "react-icons/fa";
import { useVideoContext } from "../context/videoContext";
import { Container } from "@mantine/core";

export default function VideoBar() {
const { videoId, setVideoId } = useVideoContext();

const icon = (
<FaSearch style={{ width: "25px", height: "25px" }} stroke={1.5} />
);
return (
<Container h={40} size="fluid" className="taskbar" p={0}>
<h2 style={{ marginRight: `20px` }}>Vidéos</h2>

<Input
placeholder="Video"
type="text"
rightSection={icon}
value={videoId}
onChange={(e) => {
setVideoId(e.currentTarget.value);
}}
/>
</Container>
);
}
14 changes: 14 additions & 0 deletions frontend/src/admin/VideoBarPlaylist.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Container } from "@mantine/core";
import PropTypes from "prop-types";

export default function VideoBarPlaylist({ playlistTitle }) {
return (
<Container h={40} size="fluid" className="taskbar" p={0}>
<h2 style={{ marginRight: `20px` }}>{playlistTitle}</h2>
</Container>
);
}

VideoBarPlaylist.propTypes = {
playlistTitle: PropTypes.string.isRequired,
};
Loading

0 comments on commit 3a5bbf8

Please sign in to comment.