improvements + better error handling

This commit is contained in:
Daniel
2023-07-24 09:39:51 -04:00
parent 6708be2dfd
commit cbf53f6e30
4 changed files with 51 additions and 28 deletions
+14 -6
View File
@@ -10,11 +10,19 @@ export default async function postLink(
link: LinkIncludingShortenedCollectionAndTags,
userId: number
) {
try {
new URL(link.url);
} catch (error) {
return {
response:
"Please enter a valid Address for the Link. (It should start with http/https)",
status: 400,
};
}
link.collection.name = link.collection.name.trim();
if (!link.name) {
return { response: "Please enter a valid name for the link.", status: 400 };
} else if (!link.collection.name) {
if (!link.collection.name) {
link.collection.name = "Unnamed Collection";
}
@@ -45,8 +53,8 @@ export default async function postLink(
const newLink: Link = await prisma.link.create({
data: {
name: link.name,
url: link.url,
name: link.name,
description,
collection: {
connectOrCreate: {
@@ -66,12 +74,12 @@ export default async function postLink(
connectOrCreate: link.tags.map((tag) => ({
where: {
name_ownerId: {
name: tag.name,
name: tag.name.trim(),
ownerId: link.collection.ownerId,
},
},
create: {
name: tag.name,
name: tag.name.trim(),
owner: {
connect: {
id: link.collection.ownerId,