diff --git a/app/Controllers/Http/DashbordsController.ts b/app/Controllers/Http/DashbordsController.ts index 5d08d73..db53f98 100644 --- a/app/Controllers/Http/DashbordsController.ts +++ b/app/Controllers/Http/DashbordsController.ts @@ -5,102 +5,147 @@ import User from 'App/Models/User' import Categorie from 'App/Models/Categorie' import AssetService from 'App/Services/AssetService' import CategorieValidator from 'App/Validators/CategorieValidator' +import Profile from 'App/Models/Profile' export default class DashbordsController { async showDashbord({ inertia, auth, response }: HttpContextContract) { - if(auth.user?.roleId != 2) { - return response.redirect('/login') + if (auth.user?.roleId != 2) { + return response.redirect("/login"); } - const products = await Product.all() - const productUrl = await Drive.getUrl('./products') + const products = await Product.all(); + const productUrl = await Drive.getUrl("./products"); - const users = await User.all() + const users = await User.all(); - const categories = await Categorie.all() - const categorieUrl = await Drive.getUrl('./Categories') + const categories = await Categorie.all(); + const categorieUrl = await Drive.getUrl("./Categories"); - return inertia.render('Dashbord/Dashbord', { + return inertia.render("Dashbord/Dashbord", { products, productUrl, users, categories, categorieUrl, - auth - }) + auth, + }); } - public async statisticOfActivity({inertia, auth, response}: HttpContextContract) { - if(auth.user?.roleId != 2) { - return response.redirect('/login') + public async statisticOfActivity({ + inertia, + auth, + response, + }: HttpContextContract) { + if (auth.user?.roleId != 2) { + return response.redirect("/login"); } const products = await Product.all(); - - return inertia.render('Dashbord/Statistic', { - products, auth - }) + + return inertia.render("Dashbord/Statistic", { + products, + auth, + }); } - public async collections({ inertia, auth, response }:HttpContextContract) { - if(auth.user?.roleId != 2) { - return response.redirect('/login') + public async collections({ inertia, auth, response }: HttpContextContract) { + if (auth.user?.roleId != 2) { + return response.redirect("/login"); } - const products = await Product.all() - const productUrl = await Drive.getUrl('./products') + const products = await Product.all(); + const productUrl = await Drive.getUrl("./products"); - const users = await User.all() + const users = await User.all(); - const categories = await Categorie.all() - const categorieUrl = await Drive.getUrl('./Categories') + const categories = await Categorie.all(); + const categorieUrl = await Drive.getUrl("./Categories"); - return inertia.render('Dashbord/DashbordCollection', { + return inertia.render("Dashbord/DashbordCollection", { products, productUrl, users, categories, categorieUrl, - auth - }) + auth, + }); } - public createCategorie({ inertia,auth, response }: HttpContextContract) { - if(auth.user?.roleId != 2) { - return response.redirect('/login') + public createCategorie({ inertia, auth, response }: HttpContextContract) { + if (auth.user?.roleId != 2) { + return response.redirect("/login"); } - return inertia.render('Dashbord/CategorieForm') + return inertia.render("Dashbord/CategorieForm"); } - public async saveCategorie({ request, response, session, }: HttpContextContract) { - const categorie = new Categorie() + public async saveCategorie({ + request, + response, + session, + }: HttpContextContract) { + const categorie = new Categorie(); categorie.asset = await AssetService.uploadFile( request, - './Categories/', - 'asset', - ) - - const data = await request.validate(CategorieValidator) - await categorie.merge({ ...data }).save() - session.flash('success', 'Your Categorie is saved') - return response.redirect('/dashbord') + "./Categories/", + "asset" + ); + + const data = await request.validate(CategorieValidator); + await categorie.merge({ ...data }).save(); + session.flash("success", "Your Categorie is saved"); + return response.redirect("/dashbord"); } public async deleteCategorie({ response, params }: HttpContextContract) { - const categorie = await Categorie.findOrFail(params.id) - await categorie?.delete() - return response.redirect('/dashbord/collections') + const categorie = await Categorie.findOrFail(params.id); + await categorie?.delete(); + return response.redirect("/dashbord/collections"); } public async editProduct({ inertia, auth, response }: HttpContextContract) { - if(auth.user?.roleId != 2) { - return response.redirect('/login') + if (auth.user?.roleId != 2) { + return response.redirect("/login"); } - return inertia.render('Dashbord/EditProduct', {auth}) + return inertia.render("Dashbord/EditProduct", { auth }); } public async deleteProduct({ params, response }: HttpContextContract) { - const product = await Product.findBy('id',params.id) + const product = await Product.findBy("id", params.id); + + try { + await product?.delete(); + return response.redirect().toRoute("dashbord.collections"); + } catch (error) { + console.error('There are an error when the product deleted', error) + return response.redirect().toRoute("dashbord.collections"); + } + } + + public async getAllUsers({ inertia, auth, response }: HttpContextContract) { + if (auth.user?.roleId != 2) { + return response.redirect().toRoute("user.login"); + } + const usersList = await User.all() + const allUserProfiles = await Profile.all() + + const userUrl = await Drive.getUrl("./avatar"); + return inertia.render("Dashbord/UsersList", { auth, usersList, allUserProfiles, userUrl }); + } + + public async deleteUserById({auth, params, response}: HttpContextContract) { + if (auth.user?.roleId != 2) { + return response.redirect().toRoute("user.login"); + } + + try{ + const userToDeleted = await User.findBy('id', params.id) + const profileOfUserToDeleted = await Profile.findBy('user_id', userToDeleted?.id) + await profileOfUserToDeleted?.delete() + await userToDeleted?.delete() + return response.redirect().toRoute("dashbord.user"); + } catch(error) { + console.error('There are an error when delete user: ', error) + return response.redirect().toRoute("dashbord.user"); - await product?.delete() - return response.redirect('/dashbord/collections') + } + } } diff --git a/app/Controllers/Http/ProductsController.ts b/app/Controllers/Http/ProductsController.ts index 46773cc..bbddfd1 100644 --- a/app/Controllers/Http/ProductsController.ts +++ b/app/Controllers/Http/ProductsController.ts @@ -54,61 +54,72 @@ export default class ProductsController { } public async show({ inertia, params, auth }: HttpContextContract) { - const productName = params.name.split('_').join(' ') - const product = await Product.findBy('name', productName) - const assetUrl = await Drive.getUrl('./products') - - const AllComments = await Comment.all() - const allProfiles = await Profile.all() - const profileComments = Array() - const comments = Array() - AllComments.filter(comment => { - if(comment.productId === product?.id) { - comments.push(comment); - - allProfiles.filter(prof => { - if(comment.userId === prof.userId) { - profileComments.push(prof) - } - }) - } - }) - - const artist = await User.findBy('id', product?.userId) - const profile = await Profile.findBy('user_id', artist?.id) - - const categorie = await Categorie.findBy('id', product?.categorieId) - - const otherProducts = Array() - const allProducts = await Product.all() - allProducts.map((prod) => { - if (prod.name != product?.name) { - if (prod.categorieId == product?.categorieId) { - otherProducts.push(prod) + try { + const productName = params.name.split("_").join(" "); + const product = await Product.findBy("name", productName); + const assetUrl = await Drive.getUrl("./products"); + + const AllComments = await Comment.all(); + const allProfiles = await Profile.all(); + const profileComments = Array(); + const comments = Array(); + AllComments.filter((comment) => { + if (comment.productId === product?.id) { + comments.push(comment); + + allProfiles.filter((prof) => { + if (comment.userId === prof.userId) { + profileComments.push(prof); + } + }); } - } - }) + }); - let liked: boolean = false - const hasBeenLiked = await Like.findBy('product_id', product?.id) - if(hasBeenLiked && hasBeenLiked?.userId == auth.user?.id){ - liked = hasBeenLiked!.isLiked - } + const artist = await User.findBy("id", product?.userId); + const profile = await Profile.findBy("user_id", artist?.id); - const { - avatarUrl, - authenticateProfile, - } = await ProfileService.getAthenticateProfile(auth) + const categorie = await Categorie.findBy("id", product?.categorieId); - const users = await User.all() + const otherProducts = Array(); + const allProducts = await Product.all(); + allProducts.map((prod) => { + if (prod.name != product?.name) { + if (prod.categorieId == product?.categorieId) { + otherProducts.push(prod); + } + } + }); - return inertia.render('Product/ProductShow', { - product, assetUrl, liked, - artist, profile, avatarUrl, - categorie, otherProducts, - comments, profileComments, - auth, users, authenticateProfile - }) + let liked: boolean = false; + const hasBeenLiked = await Like.findBy("product_id", product?.id); + if (hasBeenLiked && hasBeenLiked?.userId == auth.user?.id) { + liked = hasBeenLiked!.isLiked; + } + + const { avatarUrl, authenticateProfile } = + await ProfileService.getAthenticateProfile(auth); + + const users = await User.all(); + + return inertia.render("Product/ProductShow", { + product, + assetUrl, + liked, + artist, + profile, + avatarUrl, + categorie, + otherProducts, + comments, + profileComments, + auth, + users, + authenticateProfile, + }); + } catch (error) { + console.log('une erreur est survenue'+ error) + return inertia.redirectBack() + } } public async download({ response, params }: HttpContextContract) { @@ -146,28 +157,33 @@ export default class ProductsController { } public async handleIsLiked({params, response, auth}: HttpContextContract) { - const product = await Product.findBy('id', params.id) - if(!auth) { - return response.redirect().toRoute('user.login') - } - const hasBeenLiked = await Like.findBy('product_id', product?.id) - if(hasBeenLiked?.userId == auth.user?.id) { - hasBeenLiked!.isLiked = !hasBeenLiked!.isLiked - hasBeenLiked?.save() - product!.nomberLike = hasBeenLiked!.isLiked ? product!.nomberLike + 1: product!.nomberLike - 1 - product?.save() - return response.redirect(`/product/show/${product?.id}`) - } - - const isLiked = new Like() - isLiked.merge({ - userId: auth.user?.id, - productId: product?.id, - isLiked: true - }).save() - - product!.nomberLike += 1 - product?.save() - return response.redirect(`/product/show/${product?.id}`) + try{ + const product = await Product.findBy('id', params.id) + if(!auth) { + return response.redirect().toRoute('user.login') + } + const hasBeenLiked = await Like.findBy('product_id', product?.id) + if(hasBeenLiked?.userId == auth.user?.id) { + hasBeenLiked!.isLiked = !hasBeenLiked!.isLiked + hasBeenLiked?.save() + product!.nomberLike = hasBeenLiked!.isLiked ? product!.nomberLike + 1: product!.nomberLike - 1 + product?.save() + return response.redirect(`/product/show/${product?.id}`) + } + + const isLiked = new Like() + isLiked.merge({ + userId: auth.user?.id, + productId: product?.id, + isLiked: true + }).save() + + product!.nomberLike += 1 + product?.save() + return response.redirect(`/product/show/${product?.id}`) + } catch(error) { + console.log('Une erreur est survenue:' + error) + return response.redirect().back() + } } } diff --git a/app/Controllers/Http/UsersController.ts b/app/Controllers/Http/UsersController.ts index 4896079..ecfa968 100644 --- a/app/Controllers/Http/UsersController.ts +++ b/app/Controllers/Http/UsersController.ts @@ -21,7 +21,7 @@ export default class UsersController { }).save() await auth.login(user) session.flash('success', 'You are register in it. Good navigation!') - return response.redirect().toRoute('profile.edit') + return response.redirect(`/profile/edit/${profile.id + 1}`) } public async loginShow({inertia}: HttpContextContract) { diff --git a/resources/js/Components/MenuBar/Sidebar.js b/resources/js/Components/MenuBar/Sidebar.js index 5b59414..85ce3f6 100644 --- a/resources/js/Components/MenuBar/Sidebar.js +++ b/resources/js/Components/MenuBar/Sidebar.js @@ -24,12 +24,18 @@ const route = [ }, { id: 4, + link: '/dashbord/users', + title: 'Users List', + icon: + }, + { + id: 5, link: '/dashbord/categorie/create', title: 'Create Categorie', icon: }, { - id: 5, + id: 6, link: '/dashbord/post/create', title: 'Create Post', icon: diff --git a/resources/js/Pages/Dashbord/UsersList.js b/resources/js/Pages/Dashbord/UsersList.js new file mode 100644 index 0000000..e2496c2 --- /dev/null +++ b/resources/js/Pages/Dashbord/UsersList.js @@ -0,0 +1,71 @@ +import React from 'react' +import DashbordLayout from './DashbordLayout' +import { Container, Typography, TableContainer, Table, TableBody, TableCell, TableHead, TableRow, Button } from '@mui/material' +import { Delete } from '@mui/icons-material' +import { Inertia } from '@inertiajs/inertia' + + +const UsersList = (props) => { + const { auth, usersList, allUserProfiles, userUrl } = props + + const tableHeaderTexts = ['id', 'Image', 'Name', 'Lasname', 'Firstname', 'Action'] + + const findUserProfileById = (id) => { + let profile = new Object() + allUserProfiles.map((data) => { + if(data.user_id == id){ + profile = data + } + }) + return profile + } + return ( + + + Liste des Produits + + + + + {tableHeaderTexts.map(item => {item})} + + + + {usersList.map(user => { + const profile = findUserProfileById(user.id) + return ( + + {user.id} + + + + {user.name} + {profile.lastname} + {profile.firstname} + + + + + ) + })} + +
+
+
+
+ ) +} + +UsersList.layout = page => +export default UsersList \ No newline at end of file diff --git a/start/routes.ts b/start/routes.ts index aea2963..942449d 100644 --- a/start/routes.ts +++ b/start/routes.ts @@ -34,7 +34,7 @@ Route.group(() => { Route.group(() => { Route.get('/', 'DashbordsController.showDashbord') Route.get('/statistic', 'DashbordsController.statisticOfActivity') - Route.get('/collections', 'DashbordsController.collections') + Route.get('/collections', 'DashbordsController.collections').as('dashbord.collections') Route.delete('/collections/:id', 'DashbordsController.deleteProduct').as('dash.product.delete') Route.group(() => { Route.get('/create', 'DashbordsController.createCategorie') @@ -45,6 +45,10 @@ Route.group(() => { Route.get('/create', 'PostsController.create').as('post.create') Route.post('/create', 'PostsController.store').as('post.store') }).prefix('/post') + Route.group(() => { + Route.get('/', 'DashbordsController.getAllUsers').as('dashbord.user') + Route.delete('/:id', 'DashbordsController.deleteUserById') + }).prefix('/users') }).prefix('/dashbord').middleware('auth') Route.group(() => {