22. Comment on Photo

김종민·2022년 4월 26일
0

insta-backend

목록 보기
22/37

1. schema.prisma

model User {
  id        Int       @id @default(autoincrement())
  username  String    @unique
  email     String    @unique
  password  String
  bio       String?
  avatar    String?
  followers User[]    @relation("FollowRelation", references: [id])
  following User[]    @relation("FollowRelation", references: [id])
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  photos    Photo[]
  likes     Like[]
  comments  Comment[]
}

model Photo {
  id        Int       @id @default(autoincrement())
  user      User      @relation(fields: [userId], references: [id])
  userId    Int
  file      String
  caption   String?
  hashtags  Hashtag[]
  likes     Like[]
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  comments  Comment[]
}

model Hashtag {
  id        Int      @id @default(autoincrement())
  hashtag   String   @unique
  photos    Photo[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Like {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  photo     Photo    @relation(fields: [photoId], references: [id])
  user      User     @relation(fields: [userId], references: [id])
  photoId   Int
  userId    Int

  @@unique([photoId, userId])
}

model Comment {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  user      User     @relation(fields: [userId], references: [id])
  photo     Photo    @relation(fields: [photoId], references: [id])
  payload   String
  userId    Int
  photoId   Int
}

check!!
1. model Comment 확인!!
2. model User와 model Photo에서 Comment와의 relation확인할 것

2. comment.typeDefs.js

import { gql } from 'apollo-server'

export default gql`
  type Comment {
    id: Int!
    createdAt: String!
    updatedAt: String!
    user: User!
    phoho: Photo!
    payload: String!
    isMine: Boolean!
  }
`

3. comment.resolvers.js

import prisma from "../client"

export default {
  Comment: {
    isMine: ({ userId }, _, { loggedInUser }) => {
      if (!loggedInUser) {
        return false
      }
      return userId === loggedInUser.id
    },
    user: ({ userId }) => prisma.user.findUnique({ where: { id: userId } }),
  },
}

4. createComment.typeDefs.js

import { gql } from 'apollo-server'

export default gql`
  type CreateCommentResult {
    ok: Boolean!
    error: String
  }
  type Mutation {
    createComment(photoId: Int!, payload: String!): CreateCommentResult!
  }
`

check!! photoId를 arg로 받는 거 확인할 것!!!
payload는 댓글 내용!!

5. createComment.resolvers.js

import prisma from '../../client'
import { protectedResolver } from '../../users/users.util'

export default {
  Mutation: {
    createComment: protectedResolver(
      async (_, { photoId, payload }, { loggedInUser }) => {
        const ok = await prisma.photo.findUnique({
          where: { id: photoId },
          select: { id: true },
        })
        if (!ok) {
          return {
            ok: false,
            error: 'Photo not found',
          }
        }
        await prisma.comment.create({
          data: {
            payload,
            photo: {
              connect: { id: photoId },
            },
            user: {
              connect: { id: loggedInUser.id },
            },
          },
        })
        return {
          ok: true,
        }
      }
    ),
  },
}

check!!
await prisma.comment.create에서 photo와 user를 connect 해 주는 부분 확인할 것!!

profile
코딩하는초딩쌤

14개의 댓글

comment-user-thumbnail
2022년 10월 26일

The information you share is excellent and exciting, and thanks to that, I know more valuable things. Keep posting interesting things and I will keep an eye on your posts. krunker

답글 달기
comment-user-thumbnail
2023년 4월 11일

Drift Boss is a famous drifting game. In the game, you will have the opportunity to control your car on endless roads full of difficult turns and collisions. The game sounds simple but it takes a lot of practice to be able to drift well around corners. Join the game now to prove that you are the best racer and score the most points.

답글 달기
comment-user-thumbnail
2023년 4월 22일

Welcome to the obvious escorts benefit in your goliath city Delhi. We have gotten as Delhi Call Young women. A call young woman is known essentially to meet one's sexual necessities or basics.
https://www.sanakhan.in/katwaria-sarai-call-girls.html
https://www.sanakhan.in/karkardooma-call-girls.html
https://www.sanakhan.in/hari-nagar-call-girls.html
https://www.sanakhan.in/jorhipur-call-girls.html
https://www.sanakhan.in/hauz-khas-call-girl.html

답글 달기
comment-user-thumbnail
2023년 5월 3일

I can't post pictures uno online in comments

답글 달기
comment-user-thumbnail
2023년 5월 19일

Whether you are looking to ship goods across the state or across the country, there are plenty of freight transport providers in Michigan that can help. Here are some of the key players in the industry and the services they offer: https://www.youtube.com/watch?v=kvWcrsP1iQ0

답글 달기
comment-user-thumbnail
2023년 7월 21일

The information you share is excellent and exciting, and thanks to that, I know more valuable things. Keep posting interesting things and I will keep an eye on your posts https://busd-store.com/

1개의 답글
comment-user-thumbnail
2023년 8월 23일

The largest call girls cash payment are standing by to welcome you with amiable conversations and attractive looks. There will be many girls for you to pick from. There are many different types of nearby escort girls, including young, aged, MILF, breasts, skinny, curvy, Indian, Russian, and more.
https://sheetaldubay.com/jalandhar-call-girls4/
https://sheetaldubay.com/call-girl-in-amritsar4/
https://sheetaldubay.com/escort-navi-mumbai4/
https://sheetaldubay.com/chennai-call-girls4/
https://sheetaldubay.com/call-girls-hyderabad4/
https://sheetaldubay.com/kolkata-call-girls4/
https://sheetaldubay.com/call-girl-bangalore4/

답글 달기
comment-user-thumbnail
2023년 8월 28일
답글 달기