Skip to content

Commit

Permalink
Merge branch 'main' into Mariano
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatino12 committed Mar 18, 2022
2 parents 427bb0a + e953b8d commit 472d24a
Show file tree
Hide file tree
Showing 10 changed files with 287 additions and 105 deletions.
53 changes: 22 additions & 31 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"typescript": "^4.6.2"
},
"dependencies": {
"@prisma/client": "^3.10.0",
"@prisma/client": "^3.11.0",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.0.0",
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ model Users {
name String
name_user String
email String @unique
mailingList Boolean
direction String
rol Int // 0 = User / 1 = User with Shop / 2 = Admin
shopsId String[]
Expand Down
63 changes: 39 additions & 24 deletions src/controllers/categories.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,42 @@ const prisma: PrismaClient = new PrismaClient();
const getCategoriesByshopId = async (shopId: string) => {
try {
let productsCategories: any = await prisma.products.findMany({
where:{
shopId
where: {
shopId,
},
select:{
categoriesId: true
}
})
select: {
categoriesId: true,
},
});

let categories: any;
for (let i = 0; i < productsCategories.length; i++) {
productsCategories[i] = await prisma.categories.findMany({
where:{
id: {in: productsCategories[i].categoriesId}
},
select:{
name: true
}
})
where: {
id: { in: productsCategories[i].categoriesId },
},
select: {
name: true,
},
});
}
productsCategories = productsCategories.map((elem: any) => elem[0].name)
productsCategories = productsCategories.filter((valor: string, indice: number) => productsCategories.indexOf(valor) === indice)
productsCategories = productsCategories.map((elem: any) => elem[0].name);
productsCategories = productsCategories.filter(
(valor: string, indice: number) =>
productsCategories.indexOf(valor) === indice
);
//console.log(productsCategories);
return productsCategories

return productsCategories;
} catch (error) {
return null
return null;
}
}
};
export const getCategories = async (shopId?: string) => {
try {
let cat: any ;
if(shopId){
let cat: any;
if (shopId) {
cat = await getCategoriesByshopId(shopId);
}
else{
} else {
cat = await prisma.categories.findMany();
}
return cat;
Expand All @@ -57,3 +58,17 @@ export const saveNewCategory = async (data: any) => {
return null;
}
};

export const getCategoriesObject = async () => {
try {
const categories = prisma.categories.findMany({
select: {
id: true,
name: true,
},
});
return categories;
} catch (error) {
return null;
}
};
28 changes: 26 additions & 2 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
saveFavourite,
getFavouriteShops,
removeFavourite,
updateUserMailingState,
} from "./user.controller";
import {
getProducts,
Expand All @@ -19,7 +20,11 @@ import {
updateInfoProduct,
getProductsNames,
} from "./product.controller";
import { getCategories, saveNewCategory } from "./categories.controller";
import {
getCategories,
getCategoriesObject,
saveNewCategory,
} from "./categories.controller";
import {
getShops,
saveNewShop,
Expand Down Expand Up @@ -219,7 +224,7 @@ export const getAllProducts = async (req: Request, res: Response) => {
let { discount } = req.query;
let { shopId } = req.params;

//console.log(req.query);
console.log("hola1");
let pageBase: number = 0,
myPage: string = req.query.page as string;
const pageAsNumber: number = parseInt(myPage);
Expand Down Expand Up @@ -467,6 +472,17 @@ export const removeFavouriteShop = async (req: Request, res: Response) => {
}
};

export const updateMailingListState = async (req: Request, res: Response) => {
try {
const { userId, boolean } = req.params;
const updatedUser = await updateUserMailingState(userId, boolean);
res.status(201).send(updatedUser);
} catch (error) {
console.error(error);
res.status(401).json({ msg: "error", error: error });
}
};

/* -------------------------------------------------------------------------------------------- */

// CATEGORIES
Expand All @@ -493,6 +509,14 @@ export const postCategory = async (req: Request, res: Response) => {
}
};

export const getCategoriesId = async (req: Request, res: Response) => {
try {
const categories = await getCategoriesObject();
res.status(201).send(categories);
} catch (error) {
res.status(401).json({ msg: "error", error: error });
}
};
/* -------------------------------------------------------------------------------------------- */

// CART
Expand Down
Loading

0 comments on commit 472d24a

Please sign in to comment.