Next.js Discord

Discord Forum

API structure and route handlers

Unanswered
Yellow croaker posted this in #help-forum
Open in Discord
Yellow croakerOP
Hello! got question about API strcuture in nextjs route.
I have Resume model from prisma
model Resume {
  id              String              @id @default(cuid())
  userId          String
  user            User                @relation(fields: [userId], references: [id])
  title           String
  personalInfo    PersonalInfo?
  workexperiences WorkExperience[]
  templateStyle   ResumeTemplateStyle @default(BasicTemplate)
}

User will fill forms like personalInfo and workExperience. When i call patch to update my prisma db, should i have separate patch function for personalInfo and workExperience?

currently i have patch that only updates personalInfo
export async function PATCH(
...
    const json = await req.json();

    const body = personalInfoPatchSchema.parse(json);

    const updatedPersonalInfo = await db.personalInfo.update({
      where: {
        resumeId: body.resumeId,
      },
      data: body,
    });
)


So should i have something like PatchPersonal and PatchWork And then i call them from my client side?
But my fetch call to /api/resume/${id}
How i can separate the personalInfo and workExperience patches from each other?
export async function patchResume(
  id: string,
  values: ResumePersonalDetailValues,
) {
  console.log('values in patch', values);
  const response = await fetch(`/api/resume/${id}`, {
    method: 'PATCH',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify(values),
  });
  console.log('response', response);
  if (!response.ok) {
    throw new Error('Failed to update resume');
  }
  return await response.json();
}

4 Replies

Yellow croakerOP
export async function PATCH(
    ...
    const json = await req.json();
    console.log('Whole resume', json);
    const body = personalInfoPatchSchema.parse(json);
    console.log('Whole resume body', body);

    const updatedPersonalInfo = await db.personalInfo.update({
      where: {
        resumeId: body.resumeId,
      },
      data: body,
    });
...
)

Or should i just send the whole resume object everytime a change is made like this.
This approach feels some what heavy to always send the whole resume when a one change is made?

Whole resume body {
  personalInfo: {
    name: 'ffssf',
    email: 'fsfsfs@gmail.com',
    phone: 'fsasffsa',
    address: 'fsasffsa'
  },
  workExperience: [
    {
      position: 'ffff',
      company: 'fff',
      startDate: 'fff',
      endDate: 'ffff',
      description: 'fsfafasf'
    },
    {
      position: 'fasfa',
      company: 'fasfa',
      startDate: 'fafasf',
      endDate: 'fasfasf',
      description: 'fasfasf'
    }
  ],
  resumeId: 'clnli48gj000bk9fgvb4he9aw'
}
Yellow croakerOP
This is part of my schema.prisma
model Resume {
  id              String              @id @default(cuid())
  userId          String
  user            User                @relation(fields: [userId], references: [id])
  title           String
  personalInfo    PersonalInfo?
  workexperiences WorkExperience[]
  templateStyle   ResumeTemplateStyle @default(BasicTemplate)
}

model WorkExperience {
  id          String    @id @default(cuid())
  position    String
  company     String
  description String?
  startDate   DateTime
  endDate     DateTime?
  resumeId    String    @unique
  resume      Resume    @relation(fields: [resumeId], references: [id], onDelete: Cascade)
}

model PersonalInfo {
  id       String  @id @default(cuid())
  name     String?
  address  String?
  phone    String?
  email    String?
  resumeId String  @unique
  resume   Resume  @relation(fields: [resumeId], references: [id], onDelete: Cascade)
}
Yellow croakerOP
- Should i update the whole object with personalInfo also?
- I think the biggest problem here is, how i can update the array of workExperienes?
Yellow croakerOP
I tried to use server actions but cant get the prisma db to work

'use server';

import { db } from './lib/db';

export async function UpdateWorkExperience(
  resumeId: string,
  workExperienceData: any,
) {
  console.log('This is resumeID:', resumeId);
  console.log('WORKEXPERIENCEDATA', workExperienceData);

  const updatedResume = await db.resume.update({
    where: { id: resumeId },
    data: {
      workexperiences: {
        update: workExperienceData,
      },
    },
  });
  console.log('UPDATEDRESUME', updatedResume);
  return updatedResume;
}

here are the console.logs
This is resumeID: 1234
WORKEXPERIENCEDATA [
  {
    position: 'ffff',
    company: 'fff',
    startDate: '2023-10-21T15:30:45.123Z',
    endDate: '2023-10-21T15:30:45.123Z',
    description: 'fassff'
  }
]



I get this error when submit
- error PrismaClientValidationError: 
Invalid `prisma.resume.update()` invocation:

{
  where: {
    id: "1234"
  },
  data: {
    workexperiences: {
      update: [
        {
          position: "ffff",
          company: "fff",
          startDate: "2023-10-21T15:30:45.123Z",
          endDate: "2023-10-21T15:30:45.123Z",
          description: "fassff"
        }
      ]
    }
  }
}

Argument `where` is missing.
    at async UpdateWorkExperience (./app/actions.ts:13:27)