diff --git a/server/controllers/mediatheque.ts b/server/controllers/mediatheque.ts index 046a6ab66..5d1aa3c6c 100644 --- a/server/controllers/mediatheque.ts +++ b/server/controllers/mediatheque.ts @@ -7,66 +7,66 @@ import { Controller } from './controller'; const mediathequeController = new Controller('/mediatheque'); -mediathequeController.post({ path: '' }, async (req, res) => { - const filters: Array = req?.body?.filters || []; - const offset = req?.query?.offset || 0; - - let subQueryBuilder = AppDataSource.getRepository(Activity).createQueryBuilder('activity').innerJoin('activity.user', 'user'); - - filters.map((filter, index) => { - subQueryBuilder = subQueryBuilder[index === 0 ? 'where' : 'orWhere']( - new Brackets((qb) => { - filter.map(({ table, column, values }, subQueryIndex) => { - let condition = ''; - values.map((_value, valueIndex) => { - condition += valueIndex > 0 ? ' or ' : '('; - condition += `${table}.${column} = ${values[valueIndex]}`; - }); - condition += ')'; - qb[subQueryIndex === 0 ? 'where' : 'andWhere'](condition); - }); - }), - ); - }); - const activities = await subQueryBuilder - .limit(6) - .offset(offset as number) +const getMedias = async (result: any[], queryBuilder, offset, limit) => { + const activities: Array = await queryBuilder + .limit(limit ? parseInt(limit) : undefined) + .offset(offset ? parseInt(offset) : undefined) .getMany(); - res.send(activities); -}); - -mediathequeController.post({ path: '/count' }, async (req, res) => { - try { - const filters: Array = req?.body?.filters || []; - - let subQueryBuilder = AppDataSource.getRepository(Activity).createQueryBuilder('activity').innerJoin('activity.user', 'user'); - - filters.map((filter, index) => { - subQueryBuilder = subQueryBuilder[index === 0 ? 'where' : 'orWhere']( - new Brackets((qb) => { - filter.map(({ table, column, values }, subQueryIndex) => { - let condition = ''; - values.map((_value, valueIndex) => { - condition += valueIndex > 0 ? ' or ' : '('; - condition += `${table}.${column} = ${values[valueIndex]}`; - }); - condition += ')'; - qb[subQueryIndex === 0 ? 'where' : 'andWhere'](condition); - }); + console.log('offset'); + console.log(offset); + console.log('limit'); + console.log(limit); + console.log('activities'); + console.log(activities); + const activitiesMediaFinder = activities.map(({ id, content, subType, type, villageId, userId }) => { + const result = { id, subType, type, villageId, userId, medias: [] }; + if (content.game) { + content.game.map(({ inputs }) => + inputs.map((input) => { + if (input.type === 3 || input.type === 4) { + result.medias.push({ type: input.type === 3 ? 'image' : 'video', value: input.selectedValue }); + } }), ); - }); - const count = await subQueryBuilder.getMany(); - res.status(200).json({ count }); - } catch (error) { - console.error('Error fetching count:', error); - res.status(500).json({ error: 'Internal Server Error' }); + } else { + content.map(({ type, value }) => { + const wantedTypes = ['image', 'video', 'sound']; + if (wantedTypes.includes(type)) { + result.medias.push({ type, value }); + } + }); + } + return result; + }); + + const activitiesWithMediaOnly = activitiesMediaFinder.filter((a) => a.medias.length > 0); + result = [...result, ...activitiesWithMediaOnly]; + + if (!limit) { + return { activities: result, offset: offset + limit }; } -}); + const oldLength = result.length; + result = result.slice(0, limit); + console.log('resultPOPOPO'); + console.log(result); + const newLength = result.length; + const hasReduced = oldLength !== newLength; -mediathequeController.post({ path: '/all' }, async (req, res) => { - const filters: Array = req?.body?.filters || []; + if (result.length < limit) { + if (activities.length < limit) { + return { activities: result, offset: offset + limit }; + } + const lastActivityInResultIndex = activities.findIndex((a) => a.id === result[result.length - 1]); + const newOffset = hasReduced ? offset + lastActivityInResultIndex + 1 : offset + limit; + return await getMedias(result, queryBuilder, newOffset, limit); + } + return { activities: result, offset: offset + limit }; +}; +mediathequeController.post({ path: '' }, async (req, res) => { + const filters: Array = req?.body?.filters || []; + const offset: string | undefined = req?.query?.offset as string; + const limit: string | undefined = req?.query?.limit as string; let subQueryBuilder = AppDataSource.getRepository(Activity).createQueryBuilder('activity').innerJoin('activity.user', 'user'); filters.map((filter, index) => { @@ -84,12 +84,68 @@ mediathequeController.post({ path: '/all' }, async (req, res) => { }), ); }); - const activities = await subQueryBuilder.getMany(); - const filterActivities = activities.map((activities) => activities); - console.log(filterActivities); + // if (limit) { + // subQueryBuilder = subQueryBuilder.limit(parseInt(limit)); + // } + const activitiesWithMediaOnly = await getMedias([], subQueryBuilder, offset ? parseInt(offset) : undefined, limit ? parseInt(limit) : undefined); + console.log('activitiesWithMediaOnly'); + console.log(activitiesWithMediaOnly); - res.send(activities); + res.send(activitiesWithMediaOnly); }); +// mediathequeController.post({ path: '/count' }, async (req, res) => { +// try { +// const filters: Array = req?.body?.filters || []; + +// let subQueryBuilder = AppDataSource.getRepository(Activity).createQueryBuilder('activity').innerJoin('activity.user', 'user'); + +// filters.map((filter, index) => { +// subQueryBuilder = subQueryBuilder[index === 0 ? 'where' : 'orWhere']( +// new Brackets((qb) => { +// filter.map(({ table, column, values }, subQueryIndex) => { +// let condition = ''; +// values.map((_value, valueIndex) => { +// condition += valueIndex > 0 ? ' or ' : '('; +// condition += `${table}.${column} = ${values[valueIndex]}`; +// }); +// condition += ')'; +// qb[subQueryIndex === 0 ? 'where' : 'andWhere'](condition); +// }); +// }), +// ); +// }); +// const count = await subQueryBuilder.getMany(); +// res.status(200).json({ count }); +// } catch (error) { +// console.error('Error fetching count:', error); +// res.status(500).json({ error: 'Internal Server Error' }); +// } +// }); + +// mediathequeController.post({ path: '/all' }, async (req, res) => { +// const filters: Array = req?.body?.filters || []; + +// let subQueryBuilder = AppDataSource.getRepository(Activity).createQueryBuilder('activity').innerJoin('activity.user', 'user'); + +// filters.map((filter, index) => { +// subQueryBuilder = subQueryBuilder[index === 0 ? 'where' : 'orWhere']( +// new Brackets((qb) => { +// filter.map(({ table, column, values }, subQueryIndex) => { +// let condition = ''; +// values.map((_value, valueIndex) => { +// condition += valueIndex > 0 ? ' or ' : '('; +// condition += `${table}.${column} = ${values[valueIndex]}`; +// }); +// condition += ')'; +// qb[subQueryIndex === 0 ? 'where' : 'andWhere'](condition); +// }); +// }), +// ); +// }); +// const activities = await subQueryBuilder.getMany(); + +// }); + export { mediathequeController }; diff --git a/src/api/mediatheque/mediatheque.get.ts b/src/api/mediatheque/mediatheque.get.ts index 2685cc012..4c6390fc3 100644 --- a/src/api/mediatheque/mediatheque.get.ts +++ b/src/api/mediatheque/mediatheque.get.ts @@ -3,8 +3,8 @@ import { useQuery } from 'react-query'; import { axiosRequest } from 'src/utils/axiosRequest'; import type { Filter } from 'types/mediatheque.type'; -async function getMediatheque(params: { offset: number | null; filters: Array }) { - const { offset, filters } = params; +async function getMediatheque(params: { filters: Array; offset?: number; limit?: number }) { + const { offset, filters, limit } = params; return ( await axiosRequest({ @@ -13,6 +13,7 @@ async function getMediatheque(params: { offset: number | null; filters: Array) => { - return useQuery(['Mediatheque', offset, filters], () => getMediatheque({ filters, offset })); +export const useGetMediatheque = (filters: Array, offset?: number, limit?: number) => { + return useQuery(['Mediatheque', filters, offset, limit], () => getMediatheque({ filters, offset, limit })); }; diff --git a/src/contexts/mediathequeContext.tsx b/src/contexts/mediathequeContext.tsx index 0397f49a6..d18630edb 100644 --- a/src/contexts/mediathequeContext.tsx +++ b/src/contexts/mediathequeContext.tsx @@ -13,6 +13,7 @@ type MediathequeProviderProps = { type MediathequeContextType = { filters: Array; setFilters: Dispatch>>; + offset: number; filtered: []; setOffset: Dispatch>; count: number; @@ -23,6 +24,7 @@ const MediathequeContext: React.Context = createContext< filters: [], setFilters: () => {}, filtered: [], + offset: 0, setOffset: () => {}, count: 0, allFiltered: [], @@ -31,13 +33,39 @@ const MediathequeContext: React.Context = createContext< export const MediathequeProvider: FC = ({ children }) => { const [filters, setFilters] = useState>([[]]); const [offset, setOffset] = useState(0); - const { data: filtered } = useGetMediatheque(offset, filters); - const { data: count } = useGetMediathequeCount(filters); - const { data: allFiltered } = useGetMediathequeAll(filters); - console.log(allFiltered); + // const r = useGetMediatheque(filters, offset, 2); + // console.log(r); + const { data } = useGetMediatheque(filters, offset, 6); + console.log(data); + const { data: allData } = useGetMediatheque(filters); + + if (!data) { + return null; + } + // const { + // data: { activities: filtered, offset: newOffset }, + // } = useGetMediatheque(filters, offset, 6); + // const { data: count } = useGetMediathequeCount(filters); + + // const { + // data: { activities: allFiltered }, + // } = useGetMediatheque(filters); return ( - {children} + + {/* */} + {children} + ); };