Skip to content

Commit

Permalink
add user manager on dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Herman-Riah19 committed Nov 20, 2023
1 parent bea4de9 commit bee69c0
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 127 deletions.
147 changes: 96 additions & 51 deletions app/Controllers/Http/DashbordsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

}
}
162 changes: 89 additions & 73 deletions app/Controllers/Http/ProductsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Profile>()
const comments = Array<Comment>()
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<Product>()
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<Profile>();
const comments = Array<Comment>();
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<Product>();
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) {
Expand Down Expand Up @@ -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()
}
}
}
2 changes: 1 addition & 1 deletion app/Controllers/Http/UsersController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading

0 comments on commit bee69c0

Please sign in to comment.