Skip to content

Commit

Permalink
Merge pull request #67 from data-students/ExistingApunts
Browse files Browse the repository at this point in the history
add: existing apunts toast
  • Loading branch information
puigde authored Mar 14, 2024
2 parents 2c4ecb8 + e60afbe commit dd3e1b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
10 changes: 10 additions & 0 deletions src/app/api/subject/post/create/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ export async function POST(req: Request) {
return new Response("Author not found", { status: 404 })
}
}
const existingPost = await db.post.findFirst({
where: {
title: title,
subjectId: subject.id,
authorId: authorId,
},
})
if (existingPost) {
return new Response("Post already exists", { status: 406 })
}
await db.post.create({
data: {
title: title,
Expand Down
24 changes: 18 additions & 6 deletions src/components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,24 @@ export function ProfileForm({
const { data } = await axios.post("/api/subject/post/create", payload)
return data
},
onError: () => {
toast({
title: "Alguna cosa no ha anat bé",
description: "No s'ha pogut crear el post. Torna-ho a provar més tard.",
variant: "destructive",
})
onError: (error) => {
if ((error as any).response && (error as any).response.status === 406) {
return toast({
title: "Ja tens un post amb aquest títol per aquesta assignatura",
description:
"Si creus que això és un error, contacta amb nosaltres a [email protected]",
})
} else {
const Errmessage = (error as Error).message
? (error as Error).message
: "No s'ha pogut crear el post."
toast({
title: Errmessage,
description:
"Alguna cosa ha anat malament, si creus que no hauria d'haver-hi cap problema, contacta amb nosaltres a [email protected]",
variant: "destructive",
})
}
},
onSuccess: (subjectAcronym) => {
router.push(`/${subjectAcronym}`)
Expand Down

0 comments on commit dd3e1b0

Please sign in to comment.