From fcb4a837879ce1f90f40bf3d4e06db3d5a9cbbe2 Mon Sep 17 00:00:00 2001 From: CookieDasora Date: Fri, 28 Jul 2023 10:18:22 -0300 Subject: [PATCH] Implemented comments likes and refactored some routes --- .../migration.sql | 5 ++ .../migration.sql | 36 +++++++++++ .../migration.sql | 59 +++++++++++++++++++ prisma/schema.prisma | 19 ++++-- src/controllers/comments-router.ts | 4 +- src/controllers/comments/create.ts | 2 +- src/controllers/comments/delete.ts | 4 +- .../comments/{fetch.ts => fetch-info.ts} | 4 +- src/controllers/comments/fetch-likes.ts | 21 +++++++ src/controllers/comments/update.ts | 2 +- src/controllers/posts/create.ts | 2 +- src/controllers/posts/delete.ts | 2 +- src/controllers/posts/fetch-info.ts | 2 +- src/controllers/posts/fetch-likes.ts | 2 +- src/controllers/posts/update.ts | 2 +- src/controllers/users-router.ts | 2 + src/controllers/users/auth.ts | 2 +- src/controllers/users/delete.ts | 2 +- src/controllers/users/fetch-info.ts | 2 +- src/controllers/users/fetch-posts.ts | 2 +- src/controllers/users/follow-user.ts | 2 +- src/controllers/users/like-comment.ts | 18 ++++++ src/controllers/users/like-post.ts | 2 +- src/controllers/users/search-user.ts | 2 +- src/controllers/users/signup.ts | 2 +- src/controllers/users/update.ts | 2 +- src/controllers/users/upload-picture.ts | 2 +- src/middlewares/upload-image.ts | 4 ++ .../comments/{fetch.ts => fetch-info.ts} | 0 src/services/comments/fetch-likes.ts | 26 ++++++++ src/services/comments/index.ts | 15 +++++ src/services/index.ts | 54 ----------------- src/services/posts/fetch-likes.ts | 2 +- src/services/posts/index.ts | 15 +++++ src/services/users/index.ts | 27 +++++++++ src/services/users/like-comment.ts | 55 +++++++++++++++++ src/services/users/like-post.ts | 6 +- 37 files changed, 326 insertions(+), 84 deletions(-) create mode 100644 prisma/migrations/20230727151408_create_like_field_comments/migration.sql create mode 100644 prisma/migrations/20230727154621_alter_like_model_fields/migration.sql create mode 100644 prisma/migrations/20230728124920_created_comment_like_model/migration.sql rename src/controllers/comments/{fetch.ts => fetch-info.ts} (81%) create mode 100644 src/controllers/comments/fetch-likes.ts create mode 100644 src/controllers/users/like-comment.ts rename src/services/comments/{fetch.ts => fetch-info.ts} (100%) create mode 100644 src/services/comments/fetch-likes.ts create mode 100644 src/services/comments/index.ts delete mode 100644 src/services/index.ts create mode 100644 src/services/posts/index.ts create mode 100644 src/services/users/index.ts create mode 100644 src/services/users/like-comment.ts diff --git a/prisma/migrations/20230727151408_create_like_field_comments/migration.sql b/prisma/migrations/20230727151408_create_like_field_comments/migration.sql new file mode 100644 index 0000000..8cd06df --- /dev/null +++ b/prisma/migrations/20230727151408_create_like_field_comments/migration.sql @@ -0,0 +1,5 @@ +-- AlterTable +ALTER TABLE "Like" ADD COLUMN "commentId" TEXT; + +-- AddForeignKey +ALTER TABLE "Like" ADD CONSTRAINT "Like_commentId_fkey" FOREIGN KEY ("commentId") REFERENCES "Comments"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/migrations/20230727154621_alter_like_model_fields/migration.sql b/prisma/migrations/20230727154621_alter_like_model_fields/migration.sql new file mode 100644 index 0000000..fcd80ff --- /dev/null +++ b/prisma/migrations/20230727154621_alter_like_model_fields/migration.sql @@ -0,0 +1,36 @@ +/* + Warnings: + + - You are about to drop the column `commentId` on the `Like` table. All the data in the column will be lost. + - You are about to drop the column `postId` on the `Like` table. All the data in the column will be lost. + - A unique constraint covering the columns `[userId,targetType,targetId]` on the table `Like` will be added. If there are existing duplicate values, this will fail. + - Added the required column `targetId` to the `Like` table without a default value. This is not possible if the table is not empty. + - Added the required column `targetType` to the `Like` table without a default value. This is not possible if the table is not empty. + +*/ +-- DropForeignKey +ALTER TABLE "Like" DROP CONSTRAINT "Like_commentId_fkey"; + +-- DropForeignKey +ALTER TABLE "Like" DROP CONSTRAINT "Like_postId_fkey"; + +-- DropForeignKey +ALTER TABLE "Like" DROP CONSTRAINT "Like_userId_fkey"; + +-- AlterTable +ALTER TABLE "Like" DROP COLUMN "commentId", +DROP COLUMN "postId", +ADD COLUMN "targetId" TEXT NOT NULL, +ADD COLUMN "targetType" TEXT NOT NULL; + +-- CreateIndex +CREATE UNIQUE INDEX "Like_userId_targetType_targetId_key" ON "Like"("userId", "targetType", "targetId"); + +-- AddForeignKey +ALTER TABLE "Like" ADD CONSTRAINT "Like_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Like" ADD CONSTRAINT "PostLikes" FOREIGN KEY ("targetId") REFERENCES "Post"("id") ON DELETE RESTRICT ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "Like" ADD CONSTRAINT "CommentLikes" FOREIGN KEY ("targetId") REFERENCES "Comments"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/prisma/migrations/20230728124920_created_comment_like_model/migration.sql b/prisma/migrations/20230728124920_created_comment_like_model/migration.sql new file mode 100644 index 0000000..ffa5818 --- /dev/null +++ b/prisma/migrations/20230728124920_created_comment_like_model/migration.sql @@ -0,0 +1,59 @@ +/* + Warnings: + + - The values [FRIEND] on the enum `NotificationType` will be removed. If these variants are still used in the database, this will fail. + - You are about to drop the `Like` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- AlterEnum +BEGIN; +CREATE TYPE "NotificationType_new" AS ENUM ('WARNING', 'INFO'); +ALTER TABLE "Notifications" ALTER COLUMN "type" TYPE "NotificationType_new" USING ("type"::text::"NotificationType_new"); +ALTER TYPE "NotificationType" RENAME TO "NotificationType_old"; +ALTER TYPE "NotificationType_new" RENAME TO "NotificationType"; +DROP TYPE "NotificationType_old"; +COMMIT; + +-- DropForeignKey +ALTER TABLE "Like" DROP CONSTRAINT "CommentLikes"; + +-- DropForeignKey +ALTER TABLE "Like" DROP CONSTRAINT "Like_userId_fkey"; + +-- DropForeignKey +ALTER TABLE "Like" DROP CONSTRAINT "PostLikes"; + +-- DropTable +DROP TABLE "Like"; + +-- CreateTable +CREATE TABLE "PostLike" ( + "id" TEXT NOT NULL, + "postId" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "PostLike_pkey" PRIMARY KEY ("id") +); + +-- CreateTable +CREATE TABLE "CommentLike" ( + "id" TEXT NOT NULL, + "commentId" TEXT NOT NULL, + "userId" TEXT NOT NULL, + "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, + + CONSTRAINT "CommentLike_pkey" PRIMARY KEY ("id") +); + +-- AddForeignKey +ALTER TABLE "PostLike" ADD CONSTRAINT "PostLike_postId_fkey" FOREIGN KEY ("postId") REFERENCES "Post"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "PostLike" ADD CONSTRAINT "PostLike_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "CommentLike" ADD CONSTRAINT "CommentLike_commentId_fkey" FOREIGN KEY ("commentId") REFERENCES "Comments"("id") ON DELETE CASCADE ON UPDATE CASCADE; + +-- AddForeignKey +ALTER TABLE "CommentLike" ADD CONSTRAINT "CommentLike_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 8b173e2..df8ae8e 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -15,7 +15,8 @@ model User { password String posts Post[] profileImage String? - likedPosts Like[] + likedPosts PostLike[] + likedComments CommentLike[] followers Follows[] @relation("following") following Follows[] @relation("follower") postComments Comments[] @@ -29,13 +30,13 @@ model Post { content String authorId String author User @relation(fields: [authorId], references: [id], onDelete: Cascade) - likes Like[] + likes PostLike[] comments Comments[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt } -model Like { +model PostLike { id String @id @default(uuid()) postId String post Post @relation(fields: [postId], references: [id], onDelete: Cascade) @@ -44,6 +45,15 @@ model Like { createdAt DateTime @default(now()) } +model CommentLike { + id String @id @default(uuid()) + commentId String + comment Comments @relation(fields: [commentId], references: [id], onDelete: Cascade) + userId String + user User @relation(fields: [userId], references: [id], onDelete: Cascade) + createdAt DateTime @default(now()) +} + model Follows { follower User @relation("follower", fields: [followerId], references: [id], onDelete: Cascade) followerId String @@ -60,6 +70,7 @@ model Comments { user User @relation(fields: [userId], references: [id], onDelete: Cascade) postId String post Post @relation(fields: [postId], references: [id], onDelete: Cascade) + likes CommentLike[] createdAt DateTime @default(now()) updatedAt DateTime @updatedAt @default(now()) } @@ -77,5 +88,5 @@ model Notifications { enum NotificationType { WARNING - FRIEND + INFO } \ No newline at end of file diff --git a/src/controllers/comments-router.ts b/src/controllers/comments-router.ts index 6f3395a..b225249 100644 --- a/src/controllers/comments-router.ts +++ b/src/controllers/comments-router.ts @@ -5,7 +5,8 @@ import { Router } from 'express' // Controllers import commentCreateController from './comments/create' import commentDeleteController from './comments/delete' -import commentFetchController from './comments/fetch' +import commentFetchController from './comments/fetch-info' +import commentFetchLikesController from './comments/fetch-likes' import commentUpdateController from './comments/update' // Middlewares @@ -18,5 +19,6 @@ commentsRouter.post('/create', ensureAuthenticated, commentCreateController) commentsRouter.post('/delete', ensureAuthenticated, commentDeleteController) commentsRouter.get('/info', commentFetchController) commentsRouter.put('/update', ensureAuthenticated, commentUpdateController) +commentsRouter.get('/fetch-likes', commentFetchLikesController) export default commentsRouter diff --git a/src/controllers/comments/create.ts b/src/controllers/comments/create.ts index a212e31..2d5ceca 100644 --- a/src/controllers/comments/create.ts +++ b/src/controllers/comments/create.ts @@ -1,4 +1,4 @@ -import { comment } from '../../services' +import comment from '../../services/comments/index' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/comments/delete.ts b/src/controllers/comments/delete.ts index 76a35fb..34463ef 100644 --- a/src/controllers/comments/delete.ts +++ b/src/controllers/comments/delete.ts @@ -1,4 +1,4 @@ -import { comment } from '../../services' +import comment from '../../services/comments/index' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' @@ -7,7 +7,7 @@ async function commentDeleteController (req: Request, res: Response): Promise { + const commentId = req.query.id as string + + if (commentId === undefined) { + return badRequest(res, 'Expected comment id') + } + + const result = await comment.fetchLikes(commentId) + + if (result instanceof Error) { + return badRequest(res, result.message) + } + + res.json(result) +} + +export default commentFetchLikesController diff --git a/src/controllers/comments/update.ts b/src/controllers/comments/update.ts index 5914b17..6801fa7 100644 --- a/src/controllers/comments/update.ts +++ b/src/controllers/comments/update.ts @@ -1,4 +1,4 @@ -import { comment } from '../../services' +import comment from '../../services/comments/index' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/posts/create.ts b/src/controllers/posts/create.ts index bcca1d1..83367f7 100644 --- a/src/controllers/posts/create.ts +++ b/src/controllers/posts/create.ts @@ -1,4 +1,4 @@ -import { post } from '../../services/index' +import post from '../../services/posts/index' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/posts/delete.ts b/src/controllers/posts/delete.ts index 8f3bedd..e3cd3fa 100644 --- a/src/controllers/posts/delete.ts +++ b/src/controllers/posts/delete.ts @@ -1,4 +1,4 @@ -import { post } from '../../services/index' +import post from '../../services/posts/index' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/posts/fetch-info.ts b/src/controllers/posts/fetch-info.ts index 47b7e25..6b3343b 100644 --- a/src/controllers/posts/fetch-info.ts +++ b/src/controllers/posts/fetch-info.ts @@ -1,4 +1,4 @@ -import { post } from '../../services/index' +import post from '../../services/posts/index' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/posts/fetch-likes.ts b/src/controllers/posts/fetch-likes.ts index 08b1957..13ac7d6 100644 --- a/src/controllers/posts/fetch-likes.ts +++ b/src/controllers/posts/fetch-likes.ts @@ -1,4 +1,4 @@ -import { post } from '../../services/index' +import post from '../../services/posts/index' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/posts/update.ts b/src/controllers/posts/update.ts index 8e356e6..58b7285 100644 --- a/src/controllers/posts/update.ts +++ b/src/controllers/posts/update.ts @@ -1,4 +1,4 @@ -import { post } from '../../services/index' +import post from '../../services/posts/index' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/users-router.ts b/src/controllers/users-router.ts index eecfacf..50b0833 100644 --- a/src/controllers/users-router.ts +++ b/src/controllers/users-router.ts @@ -8,6 +8,7 @@ import userDeleteController from './users/delete' import userFollowController from './users/follow-user' import userFetchInfoController from './users/fetch-info' import userFetchPostsController from './users/fetch-posts' +import userLikeCommentController from './users/like-comment' import userLikePostController from './users/like-post' import userSearchController from './users/search-user' import userSignupController from './users/signup' @@ -31,5 +32,6 @@ usersRouter.post('/like-post', ensureAuthenticated, userLikePostController) usersRouter.post('/follow-user', ensureAuthenticated, userFollowController) usersRouter.get('/fetch-posts', userFetchPostsController) usersRouter.get('/search', userSearchController) +usersRouter.post('/like-comment', ensureAuthenticated, userLikeCommentController) export default usersRouter diff --git a/src/controllers/users/auth.ts b/src/controllers/users/auth.ts index 261582b..8f6cda8 100644 --- a/src/controllers/users/auth.ts +++ b/src/controllers/users/auth.ts @@ -1,4 +1,4 @@ -import { user } from '../../services/index' +import user from '../../services/users' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/users/delete.ts b/src/controllers/users/delete.ts index 7611450..d2190ce 100644 --- a/src/controllers/users/delete.ts +++ b/src/controllers/users/delete.ts @@ -1,4 +1,4 @@ -import { user } from '../../services' +import user from '../../services/users' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/users/fetch-info.ts b/src/controllers/users/fetch-info.ts index 138f7b9..5c2515c 100644 --- a/src/controllers/users/fetch-info.ts +++ b/src/controllers/users/fetch-info.ts @@ -1,4 +1,4 @@ -import { user } from '../../services' +import user from '../../services/users' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/users/fetch-posts.ts b/src/controllers/users/fetch-posts.ts index b0c3f90..fee957c 100644 --- a/src/controllers/users/fetch-posts.ts +++ b/src/controllers/users/fetch-posts.ts @@ -1,4 +1,4 @@ -import { user } from '../../services' +import user from '../../services/users' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/users/follow-user.ts b/src/controllers/users/follow-user.ts index 16516ac..c61ed76 100644 --- a/src/controllers/users/follow-user.ts +++ b/src/controllers/users/follow-user.ts @@ -1,4 +1,4 @@ -import { user } from '../../services' +import user from '../../services/users' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/users/like-comment.ts b/src/controllers/users/like-comment.ts new file mode 100644 index 0000000..64d1922 --- /dev/null +++ b/src/controllers/users/like-comment.ts @@ -0,0 +1,18 @@ +import user from '../../services/users' +import type { Request, Response } from 'express' +import { badRequest } from '../../lib/http-errors' + +async function userLikeCommentController (req: Request, res: Response): Promise { + const userId = req.user?.id ?? '' + const { commentId, postId } = req.body + + const result = await user.likeComment(postId, commentId, userId) + + if (result instanceof Error) { + return badRequest(res, result.message) + } + + res.json(result) +} + +export default userLikeCommentController diff --git a/src/controllers/users/like-post.ts b/src/controllers/users/like-post.ts index e2df36c..9c8971e 100644 --- a/src/controllers/users/like-post.ts +++ b/src/controllers/users/like-post.ts @@ -1,4 +1,4 @@ -import { user } from '../../services' +import user from '../../services/users' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/users/search-user.ts b/src/controllers/users/search-user.ts index 6af6354..d52cfae 100644 --- a/src/controllers/users/search-user.ts +++ b/src/controllers/users/search-user.ts @@ -1,4 +1,4 @@ -import { user } from '../../services' +import user from '../../services/users' import { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/users/signup.ts b/src/controllers/users/signup.ts index 6f3b20f..90f0622 100644 --- a/src/controllers/users/signup.ts +++ b/src/controllers/users/signup.ts @@ -1,4 +1,4 @@ -import { user } from '../../services' +import user from '../../services/users' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/users/update.ts b/src/controllers/users/update.ts index 28fd09e..900f1f4 100644 --- a/src/controllers/users/update.ts +++ b/src/controllers/users/update.ts @@ -1,4 +1,4 @@ -import { user } from '../../services' +import user from '../../services/users' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/controllers/users/upload-picture.ts b/src/controllers/users/upload-picture.ts index e4820e9..ae51eca 100644 --- a/src/controllers/users/upload-picture.ts +++ b/src/controllers/users/upload-picture.ts @@ -1,5 +1,5 @@ /* eslint-disable @typescript-eslint/restrict-template-expressions */ -import { user } from '../../services/index' +import user from '../../services/users' import type { Request, Response } from 'express' import { badRequest } from '../../lib/http-errors' diff --git a/src/middlewares/upload-image.ts b/src/middlewares/upload-image.ts index 5f3e295..513ae22 100644 --- a/src/middlewares/upload-image.ts +++ b/src/middlewares/upload-image.ts @@ -18,6 +18,10 @@ function uploadImage (req: Request, res: Response, next: NextFunction) { return badRequest(res, cb.message) } + if (req.file === undefined) { + return badRequest(res, 'Expected file') + } + // @ts-expect-error property `key` does not exists in types await compressImage(req.file?.key) diff --git a/src/services/comments/fetch.ts b/src/services/comments/fetch-info.ts similarity index 100% rename from src/services/comments/fetch.ts rename to src/services/comments/fetch-info.ts diff --git a/src/services/comments/fetch-likes.ts b/src/services/comments/fetch-likes.ts new file mode 100644 index 0000000..80986fe --- /dev/null +++ b/src/services/comments/fetch-likes.ts @@ -0,0 +1,26 @@ +import prisma from '../../clients/prisma-client' + +async function commentFetchLikesService (id: string): Promise { + const post = await prisma.commentLike.findMany({ + where: { + commentId: id + }, + select: { + user: { + select: { + displayName: true, + username: true, + profileImage: true + } + } + } + }) + + if (post === null) { + return new Error('Comment not found') + } + + return post +} + +export default commentFetchLikesService diff --git a/src/services/comments/index.ts b/src/services/comments/index.ts new file mode 100644 index 0000000..3a74789 --- /dev/null +++ b/src/services/comments/index.ts @@ -0,0 +1,15 @@ +import commentCreateService from './create' +import commentDeleteService from './delete' +import commentFetchService from './fetch-info' +import commentFetchLikesService from './fetch-likes' +import commentUpdateService from './update' + +const comment = { + create: commentCreateService, + delete: commentDeleteService, + fetch: commentFetchService, + fetchLikes: commentFetchLikesService, + update: commentUpdateService +} + +export default comment diff --git a/src/services/index.ts b/src/services/index.ts deleted file mode 100644 index b1f152d..0000000 --- a/src/services/index.ts +++ /dev/null @@ -1,54 +0,0 @@ -import userAuthService from './users/auth' -import userDeleteService from './users/delete' -import userFollowService from './users/follow-user' -import userFetchPostsService from './users/fetch-posts' -import userFetchInfoService from './users/fetch-info' -import userLikePostService from './users/like-post' -import userSearchService from './users/search-user' -import userSignupService from './users/signup' -import userUpdateService from './users/update' -import userUploadPictureService from './users/upload-picture' - -import postCreateService from './posts/create' -import postDeleteService from './posts/delete' -import postFetchInfoService from './posts/fetch-info' -import postFetchLikesService from './posts/fetch-likes' -import postUpdateService from './posts/update' - -import commentCreateService from './comments/create' -import commentDeleteService from './comments/delete' -import commentFetchService from './comments/fetch' -import commentUpdateService from './comments/update' - -// User services -const user = { - auth: userAuthService, - delete: userDeleteService, - fetchInfo: userFetchInfoService, - signup: userSignupService, - update: userUpdateService, - uploadPicture: userUploadPictureService, - likePost: userLikePostService, - follow: userFollowService, - fetchPosts: userFetchPostsService, - searchUser: userSearchService -} - -// Post services -const post = { - create: postCreateService, - delete: postDeleteService, - info: postFetchInfoService, - update: postUpdateService, - fetchLikes: postFetchLikesService -} - -// Comment services -const comment = { - create: commentCreateService, - delete: commentDeleteService, - fetch: commentFetchService, - update: commentUpdateService -} - -export { user, post, comment } diff --git a/src/services/posts/fetch-likes.ts b/src/services/posts/fetch-likes.ts index e08930d..541a085 100644 --- a/src/services/posts/fetch-likes.ts +++ b/src/services/posts/fetch-likes.ts @@ -1,7 +1,7 @@ import prisma from '../../clients/prisma-client' async function postFetchLikesService (id: string): Promise { - const post = await prisma.like.findMany({ + const post = await prisma.postLike.findMany({ where: { postId: id }, diff --git a/src/services/posts/index.ts b/src/services/posts/index.ts new file mode 100644 index 0000000..cc99081 --- /dev/null +++ b/src/services/posts/index.ts @@ -0,0 +1,15 @@ +import postCreateService from './create' +import postDeleteService from './delete' +import postFetchInfoService from './fetch-info' +import postFetchLikesService from './fetch-likes' +import postUpdateService from './update' + +const post = { + create: postCreateService, + delete: postDeleteService, + fetchLikes: postFetchLikesService, + info: postFetchInfoService, + update: postUpdateService +} + +export default post diff --git a/src/services/users/index.ts b/src/services/users/index.ts new file mode 100644 index 0000000..8985cea --- /dev/null +++ b/src/services/users/index.ts @@ -0,0 +1,27 @@ +import userAuthService from './auth' +import userDeleteService from './delete' +import userFollowService from './follow-user' +import userFetchPostsService from './fetch-posts' +import userFetchInfoService from './fetch-info' +import userLikeCommentService from './like-comment' +import userLikePostService from './like-post' +import userSearchService from './search-user' +import userSignupService from './signup' +import userUpdateService from './update' +import userUploadPictureService from './upload-picture' + +const user = { + auth: userAuthService, + delete: userDeleteService, + fetchInfo: userFetchInfoService, + fetchPosts: userFetchPostsService, + follow: userFollowService, + likeComment: userLikeCommentService, + likePost: userLikePostService, + searchUser: userSearchService, + signup: userSignupService, + update: userUpdateService, + uploadPicture: userUploadPictureService +} + +export default user diff --git a/src/services/users/like-comment.ts b/src/services/users/like-comment.ts new file mode 100644 index 0000000..3a9d11a --- /dev/null +++ b/src/services/users/like-comment.ts @@ -0,0 +1,55 @@ +import prisma from '../../clients/prisma-client' + +async function userLikeCommentService (postId: string, commentId: string, userId: string): Promise { + if (commentId === undefined || userId === undefined) { + return new Error('Missing fields') + } + + const comment = await prisma.comments.findFirst({ + where: { + id: commentId + } + }) + + if (comment === null) { + return new Error('Comment not found') + } + + const user = await prisma.user.findFirst({ + where: { + id: userId + } + }) + + if (user === null) { + return new Error('User not found') + } + + const alreadyLiked = await prisma.commentLike.findFirst({ + where: { + commentId: comment.id, + userId: user.id + } + }) + + if (alreadyLiked !== null) { + await prisma.commentLike.deleteMany({ + where: { + commentId: comment.id, + userId: user.id + } + }) + return {} + } + + const like = await prisma.commentLike.create({ + data: { + commentId: comment.id, + userId: user.id + } + }) + + return like +} + +export default userLikeCommentService diff --git a/src/services/users/like-post.ts b/src/services/users/like-post.ts index e8116ac..c0fde8f 100644 --- a/src/services/users/like-post.ts +++ b/src/services/users/like-post.ts @@ -25,7 +25,7 @@ async function userLikePostService (postId: string, userId: string): Promise