Skip to content
Snippets Groups Projects
likeService.ts 3.33 KiB
import { db } from "@/lib/db"
import { Prisma } from "@prisma/client"

type likeType = Prisma.LikeUncheckedCreateInput

/**
 * Creates like if user has not liked this post.
 * Deletes like if user has liked post already.
 */
export async function putLike(like: likeType): Promise<likeType | undefined> {
    // check if like exists by this user and for this post
    // if exists delete
    // if not create
    try {
        const actualLike = await db.like.findFirst({
            where: {
                // id: like.id,
                postId: like.postId,
                userId: like.userId
            }
        })
        console.log("found like: ", actualLike?.id)
        if (actualLike == null) {
            console.log("like is null", "postid:", like.postId, "so create it")
            throw Error("Message was not liked by this user")
        }
        console.log("delete like", like.postId, "likeid: ", actualLike?.id)
        await db.like.delete({
            where: {
                id: actualLike.id
            }
        })

        /*         const msg = await db.post.update({
                    where: {
                        id: like.postId
                    },
                    data: {
                        likeCount: { increment: -1 }
                    }
                }) */

        return undefined;

    } catch {

        const createdLike = await db.like.create({
            data: {
                postId: like.postId,
                userId: like.userId
            }
        })

        const updatedMessage = await db.post.update({
            where: {
                id: like.postId
            },
            data: {
                likeCount: { increment: 1 }
            }
        })
    }
}

export async function putLikeComment(like: likeType) {
    // check if like exists by this user and for this post
    // if exists delete
    // if not create
    try {
        const actualLike = await db.like.findFirst({
            where: {
                // id: like.id,
                postId: like.postId,
                commentId: like.commentId,
                userId: like.userId
            }
        })
        console.log("found like: ", actualLike?.id)
        if (actualLike == null) {
            console.log("like is null", like.commentId, "so create it")
            const createdLike = await db.like.create({
                data: {
                    postId: like.postId,
                    userId: like.userId,
                    commentId: like.commentId
                }
            })
        } else {
            console.log("delete like", like.commentId, "postid:", like.postId, "likeid: ", actualLike?.id)
            await db.like.delete({
                where: {
                    id: actualLike.id
                }
            })
        }

        /*         const msg = await db.comment.update({
                    where: {
                        id: like.postId
                    },
                    data: {
                        likeCount: { increment: -1 }
                    }
                }) */

    } catch {

        /*         const updatedMessage = await db.comment.update({
                    where: {
                        id: like.postId
                    },
                    data: {
                        likeCount: { increment: 1 }
                    }
                }) */
    }
}