diff --git a/src/db/repo/collections.repo.ts b/src/db/repo/collections.repo.ts index 3f6d010aaadde1401e50da04c3e7da60a1a81d1e..15b4a8640ef01213aff4b2a195980eeeab8fe682 100644 --- a/src/db/repo/collections.repo.ts +++ b/src/db/repo/collections.repo.ts @@ -6,7 +6,7 @@ import type { } from '../schema/collection.schema' import db from ".."; import collectionTable, { collectionSchemas } from "../schema/collection.schema"; -import { eq } from 'drizzle-orm' +import { and, eq, isNull } from 'drizzle-orm' @Service() export class CollectionRepo { @@ -78,4 +78,40 @@ export class CollectionRepo { .array() .parse(await db.query.collectionTable.findMany()) } + + async getActiveCollectionsByUsers(id: CollectionModel['user_id']): Promise <CollectionModel[]>{ + return collectionSchemas.collectionModelSchema.array().parse( + await db + .select() + .from(collectionTable) + .where(and(eq(collectionTable.user_id, id), eq(collectionTable.is_active, true))) + ) + } + + async getAllCollectionsByUsers(id: CollectionModel['user_id']): Promise <CollectionModel []>{ + return collectionSchemas.collectionModelSchema.array().parse( + await db + .select() + .from(collectionTable) + .where(eq(collectionTable.user_id, id)) + ) + } + + async getPublicCollectionsByUser(id: CollectionModel['user_id']): Promise <CollectionModel []>{ + return collectionSchemas.collectionModelSchema.array().parse( + await db + .select() + .from(collectionTable) + .where(and(eq(collectionTable.user_id, id), eq(collectionTable.is_private, false))) + ) + } + + async getPrivateCollectionsByUser(id: CollectionModel['user_id']): Promise <CollectionModel []>{ + return collectionSchemas.collectionModelSchema.array().parse( + await db + .select() + .from(collectionTable) + .where(and(eq(collectionTable.user_id, id), eq(collectionTable.is_private, true))) + ) + } } \ No newline at end of file diff --git a/src/routes/collections.route.ts b/src/routes/collections.route.ts index a0a964e2d5cffc59ed67276fb9764d149e35e561..e63c53fb2e5d8347a17392855dabce246d5a755c 100644 --- a/src/routes/collections.route.ts +++ b/src/routes/collections.route.ts @@ -43,7 +43,7 @@ export const collectionsRouter = honoWithJwt() } } ) - .post ( + .post( '/update', zValidator('json', collectionSchemas.collectionUpdateSchema), async (c) => { @@ -80,18 +80,18 @@ export const collectionsRouter = honoWithJwt() ) return c.json({ collection }) - } catch (e) { - return c.json( - createApexError({ - status: 'error', - message: 'could not delete collection', - code: HttpStatus.BAD_REQUEST, - path: c.req.routePath, - suggestion: 'check the input and try again', - }), - HttpStatus.BAD_REQUEST - ) - } + } catch (e) { + return c.json( + createApexError({ + status: 'error', + message: 'could not delete collection', + code: HttpStatus.BAD_REQUEST, + path: c.req.routePath, + suggestion: 'check the input and try again', + }), + HttpStatus.BAD_REQUEST + ) + } }) .post('/delete-permanently/:id', async (c) => { try { @@ -135,9 +135,30 @@ export const collectionsRouter = honoWithJwt() }), HttpStatus.BAD_REQUEST ) - }}) + } + }) + +export const getCollections = new Hono() + .get('/all', async (c) => { + try { + const collections = collectionSchemas.collectionDtoSchema.array().parse( + await service.findMany() + ) - export const getCollections = new Hono () + return c.json({ collections }) + } catch (e) { + return c.json( + createApexError({ + status: 'error', + message: 'could not find collections', + code: HttpStatus.BAD_REQUEST, + path: c.req.routePath, + suggestion: 'check the input and try again', + }), + HttpStatus.NOT_FOUND + ) + } + }) .get('/:id', async (c) => { try { const id = +c.req.param('id') @@ -160,18 +181,93 @@ export const collectionsRouter = honoWithJwt() ) } }) - .get('/', async (c) => { + + + + + .get('getActiveCollectionsByUsers/:id_user', + async (c) => { + try { + const id_user = +c.req.param('id_user') + const collection = collectionSchemas.collectionDtoSchema.array().parse( + await service.getActiveCollectionsByUsers(id_user) + ) + + return c.json({ collection }) + } catch (e) { + return c.json( + createApexError({ + status: 'error', + message: 'could not find collections by user', + code: HttpStatus.BAD_REQUEST, + path: c.req.routePath, + suggestion: 'check the input and try again', + }), + HttpStatus.NOT_FOUND + ) + } + }) + + .get('getAllCollectionsByUsers/:id_user', async (c) => { + try { + const id = +c.req.param('id_user') + + const collection = collectionSchemas.collectionDtoSchema.array().parse( + await service.getAllCollectionsByUsers(id) + ) + + return c.json({ collection }) + } catch (e) { + return c.json( + createApexError({ + status: 'error', + message: 'could not find collection', + code: HttpStatus.BAD_REQUEST, + path: c.req.routePath, + suggestion: 'check the input and try again', + }), + HttpStatus.NOT_FOUND + ) + } + }) + + .get('getPublicCollectionsByUser/:id_user', async (c) => { try { - const collections = collectionSchemas.collectionDtoSchema.array().parse( - await service.findMany() + const id = +c.req.param('id_user') + + const collection = collectionSchemas.collectionDtoSchema.array().parse( + await service.getPublicCollectionsByUser(id) ) - return c.json({ collections }) + return c.json({ collection }) } catch (e) { return c.json( createApexError({ status: 'error', - message: 'could not find collections', + message: 'could not find collection', + code: HttpStatus.BAD_REQUEST, + path: c.req.routePath, + suggestion: 'check the input and try again', + }), + HttpStatus.NOT_FOUND + ) + } + }) + + .get('getPrivateCollectionsByUser/:id_user', async (c) => { + try { + const id = +c.req.param('id_user') + + const collection = collectionSchemas.collectionDtoSchema.array().parse( + await service.getPrivateCollectionsByUser(id) + ) + + return c.json({ collection }) + } catch (e) { + return c.json( + createApexError({ + status: 'error', + message: 'could not find collection', code: HttpStatus.BAD_REQUEST, path: c.req.routePath, suggestion: 'check the input and try again', diff --git a/src/routes/user.route.ts b/src/routes/user.route.ts index c817ccd3e7a22d3be09233b66b4282612f83898f..333803ee97e9839cd07a599e5355b14966d5a19a 100644 --- a/src/routes/user.route.ts +++ b/src/routes/user.route.ts @@ -14,123 +14,123 @@ const followService = Container.get(FollowRelationService) const userStatsService = Container.get(UserStatsService) export const userRouter = honoWithJwt() -.post('/follow', - zValidator('json', followRelationSchemas.input), - async (c) => { - try { - const input = await c.req.valid('json') - const follower = await userStatsService.findById(input.follower_id) - const following = await userStatsService.findById(input.user_id) - const alreadyFollow = await followService.findByFollow(input) != 0 - if(follower == null || following == null || input.follower_id == input.user_id || alreadyFollow){ - throw new Error() + .post('/follow', + zValidator('json', followRelationSchemas.input), + async (c) => { + try { + const input = await c.req.valid('json') + const follower = await userStatsService.getById(input.follower_id) + const following = await userStatsService.getById(input.user_id) + const alreadyFollow = await followService.findByFollow(input) != 0 + if (follower == null || following == null || input.follower_id == input.user_id || alreadyFollow) { + throw new Error() + } + + + + follower.follows++ // incremento do numero de pessoas que o seguidor segue + following.followers++ // incremento do numero de seguidores que a pessoa seguida tem + + await userStatsService.update(follower) + await userStatsService.update(following) + + const followRelation = followRelationSchemas.dto.parse( + await followService.create(input) + ) + + return c.json({ followRelation }) + } catch (e) { + return c.json( + createApexError({ + status: 'error', + message: 'could not update user', + code: HttpStatus.BAD_REQUEST, + path: c.req.routePath, + suggestion: 'check the input and try again', + }), + HttpStatus.BAD_REQUEST + ) } - - - - follower.follows++ // incremento do numero de pessoas que o seguidor segue - following.followers++ // incremento do numero de seguidores que a pessoa seguida tem - - await userStatsService.update(follower) - await userStatsService.update(following) - - const followRelation = followRelationSchemas.dto.parse( - await followService.create(input) - ) - - return c.json({ followRelation }) - } catch (e) { - return c.json( - createApexError({ - status: 'error', - message: 'could not update user', - code: HttpStatus.BAD_REQUEST, - path: c.req.routePath, - suggestion: 'check the input and try again', - }), - HttpStatus.BAD_REQUEST - ) - } - } -) -.post('/unfollow', - zValidator('json', followRelationSchemas.input), - async (c) => { - try { - const input = await c.req.valid('json') - - - const follow = await followService.findByFollow(input) - - - const follower = await userStatsService.findById(input.follower_id) - const following = await userStatsService.findById(input.user_id) - - - - - follower!.follows-- // decremento do numero de pessoas que o seguidor segue - following!.followers-- // decremento do numero de seguidores que a pessoa seguida tem - - await userStatsService.update(follower!) - await userStatsService.update(following!) - - const followRelation = followRelationSchemas.dto.parse( - await followService.delete(follow) - ) - - return c.json({ followRelation }) - } catch (e) { - return c.json( - createApexError({ - status: 'error', - message: 'could not update user', - code: HttpStatus.BAD_REQUEST, - path: c.req.routePath, - suggestion: 'check the input and try again', - }), - HttpStatus.BAD_REQUEST - ) } - } -) -.get('/follows/:id', // check who this id follows - async (c) => { - try{ - const id = +c.req.param('id') - const follows_id = z.array(followRelationSchemas.model).parse(await followService.findByUserId(id)) - const follows: UserProfile[] = [] - - for(const element of follows_id){ - const follow = await service.findById(element.follower_id) - if(follow != null) - follows.push(userSchemas.userProfileSchema.parse(follow)) + ) + .post('/unfollow', + zValidator('json', followRelationSchemas.input), + async (c) => { + try { + const input = await c.req.valid('json') + + + const follow = await followService.findByFollow(input) + + + const follower = await userStatsService.getById(input.follower_id) + const following = await userStatsService.getById(input.user_id) + + + + + follower!.follows-- // decremento do numero de pessoas que o seguidor segue + following!.followers-- // decremento do numero de seguidores que a pessoa seguida tem + + await userStatsService.update(follower!) + await userStatsService.update(following!) + + const followRelation = followRelationSchemas.dto.parse( + await followService.delete(follow) + ) + + return c.json({ followRelation }) + } catch (e) { + return c.json( + createApexError({ + status: 'error', + message: 'could not update user', + code: HttpStatus.BAD_REQUEST, + path: c.req.routePath, + suggestion: 'check the input and try again', + }), + HttpStatus.BAD_REQUEST + ) } - - return c.json({ follows }) - } catch (e) { - return c.notFound() } -}) -.get('/followers/:id', //check who follows this id - async (c) => { - try{ - const id = +c.req.param('id') - const followers_id = z.array(followRelationSchemas.model).parse(await followService.findByFollowerId(id)) - const followers: UserProfile[] = [] - - for(const element of followers_id){ - const follow = await service.findById(element.follower_id) - if(follow != null) - followers.push(userSchemas.userProfileSchema.parse(follow)) + ) + .get('/follows/:id', // check who this id follows + async (c) => { + try { + const id = +c.req.param('id') + const follows_id = z.array(followRelationSchemas.model).parse(await followService.findByUserId(id)) + const follows: UserProfile[] = [] + + for (const element of follows_id) { + const follow = await service.findById(element.follower_id) + if (follow != null) + follows.push(userSchemas.userProfileSchema.parse(follow)) + } + + return c.json({ follows }) + } catch (e) { + return c.notFound() } - - return c.json({ followers }) + }) + .get('/followers/:id', //check who follows this id + async (c) => { + try { + const id = +c.req.param('id') + const followers_id = z.array(followRelationSchemas.model).parse(await followService.findByFollowerId(id)) + const followers: UserProfile[] = [] - } catch (e) { - return c.notFound() - } -}) + for (const element of followers_id) { + const follow = await service.findById(element.follower_id) + if (follow != null) + followers.push(userSchemas.userProfileSchema.parse(follow)) + } + + return c.json({ followers }) + + } catch (e) { + return c.notFound() + } + }) .get('/users', async (c) => { const p = c.get('jwtPayload') @@ -141,7 +141,7 @@ export const userRouter = honoWithJwt() const ret = z.array(userSchemas.userDtoSchema).parse(users) return c.json({ users: ret }) - }) + }) .get('/:username', async (c) => { try { const username = c.req.param('username') @@ -152,7 +152,7 @@ export const userRouter = honoWithJwt() } catch { return c.notFound() } - }) + }) .post('/update', zValidator('json', userSchemas.userUpdateSchema), async (c) => { @@ -162,8 +162,8 @@ export const userRouter = honoWithJwt() const user = userSchemas.userDtoSchema.parse( await service.update(input) ) - - return c.json({user}) + + return c.json({ user }) } catch (e) { return c.json( @@ -178,22 +178,22 @@ export const userRouter = honoWithJwt() ) } - }) + }) .post('/confirmation/:email', async (c) => { try { const email: string = c.req.param('email') const user = await service.findByEmail(email) - if(user == null) + if (user == null) throw new Error() user.confirmed_at = service.getUpdateTime() - + const confirmed_user = userSchemas.userDtoSchema.parse( await service.update(user) ) - return c.json( confirmed_user ) + return c.json(confirmed_user) } catch (e) { return c.json( createApexError({ @@ -206,24 +206,24 @@ export const userRouter = honoWithJwt() HttpStatus.BAD_REQUEST ) } - }) + }) .post('/reactivate/:email', async (c) => { try { const email: string = c.req.param('email') const user = await service.findByEmail(email) - if(user == null) + if (user == null) throw new Error() - + user.active = true - + user.reactivated_at = service.getUpdateTime() - + const reactivated_user = userSchemas.userDtoSchema.parse( await service.update(user) ) - return c.json( reactivated_user ) + return c.json(reactivated_user) } catch (e) { return c.json( createApexError({ @@ -236,24 +236,24 @@ export const userRouter = honoWithJwt() HttpStatus.BAD_REQUEST ) } - }) + }) .post('/delete/:id', async (c) => { try { const id: number = +c.req.param('id') const user = await service.findById(id) - if(user == null) + if (user == null) throw new Error() - + user.active = false - + user.deleted_at = service.getUpdateTime() - + const deleted_user = userSchemas.userDtoSchema.parse( await service.update(user) ) - return c.json( deleted_user ) + return c.json(deleted_user) } catch (e) { return c.json( createApexError({ @@ -266,7 +266,7 @@ export const userRouter = honoWithJwt() HttpStatus.BAD_REQUEST ) } - }) + }) .post('/delete/system/:id', async (c) => { try { @@ -288,4 +288,4 @@ export const userRouter = honoWithJwt() HttpStatus.BAD_REQUEST ) } - }) + }) diff --git a/src/services/collections.service.ts b/src/services/collections.service.ts index 024b3903a450f017329f36bcb1f7fa3b23a714b7..8bfeae954d08cccbcee672a24f94a17ae3859980 100644 --- a/src/services/collections.service.ts +++ b/src/services/collections.service.ts @@ -40,4 +40,21 @@ export class CollectionsService { async findMany(): Promise<CollectionModel[]> { return this.repo.findMany() } + + async getActiveCollectionsByUsers(id: CollectionModel['user_id']): Promise <CollectionModel[]>{ + return this.repo.getActiveCollectionsByUsers(id) + } + + async getAllCollectionsByUsers(id: CollectionModel['user_id']): Promise <CollectionModel[]>{ + return this.repo.getAllCollectionsByUsers(id) + } + + async getPublicCollectionsByUser(id: CollectionModel['user_id']): Promise <CollectionModel[]>{ + return this.repo.getPublicCollectionsByUser(id) + } + + async getPrivateCollectionsByUser(id: CollectionModel['user_id']): Promise <CollectionModel[]>{ + return this.repo.getPrivateCollectionsByUser(id) + } + } \ No newline at end of file