Feat/import export (#136)

* added import/export functionality
This commit is contained in:
Daniel
2023-08-10 12:16:44 -04:00
committed by GitHub
parent 159075b38b
commit d008c441b7
16 changed files with 352 additions and 93 deletions
@@ -40,14 +40,6 @@ export default async function postCollection(
name: collection.name.trim(),
description: collection.description,
color: collection.color,
members: {
create: collection.members.map((e) => ({
user: { connect: { id: e.user.id } },
canCreate: e.canCreate,
canUpdate: e.canUpdate,
canDelete: e.canDelete,
})),
},
},
include: {
_count: {
+24
View File
@@ -0,0 +1,24 @@
import { prisma } from "@/lib/api/db";
export default async function getData(userId: number) {
const user = await prisma.user.findUnique({
where: { id: userId },
include: {
collections: {
include: {
links: {
include: {
tags: true,
},
},
},
},
},
});
if (!user) return { response: "User not found.", status: 404 };
const { password, id, image, ...userData } = user;
return { response: userData, status: 200 };
}
+86
View File
@@ -0,0 +1,86 @@
import { prisma } from "@/lib/api/db";
import { Backup } from "@/types/global";
import createFolder from "@/lib/api/storage/createFolder";
export default async function getData(userId: number, data: Backup) {
// Import collections
try {
data.collections.forEach(async (e) => {
e.name = e.name.trim();
const findCollection = await prisma.user.findUnique({
where: {
id: userId,
},
select: {
collections: {
where: {
name: e.name,
},
},
},
});
const checkIfCollectionExists = findCollection?.collections[0];
let collectionId = findCollection?.collections[0]?.id;
if (!checkIfCollectionExists) {
const newCollection = await prisma.collection.create({
data: {
owner: {
connect: {
id: userId,
},
},
name: e.name,
description: e.description,
color: e.color,
},
});
createFolder({ filePath: `archives/${newCollection.id}` });
collectionId = newCollection.id;
}
// Import Links
e.links.forEach(async (e) => {
const newLink = await prisma.link.create({
data: {
url: e.url,
name: e.name,
description: e.description,
collection: {
connect: {
id: collectionId,
},
},
tags: {
connectOrCreate: e.tags.map((tag) => ({
where: {
name_ownerId: {
name: tag.name.trim(),
ownerId: userId,
},
},
create: {
name: tag.name.trim(),
owner: {
connect: {
id: userId,
},
},
},
})),
},
},
});
});
});
} catch (err) {
console.log(err);
}
return { response: "Success.", status: 200 };
}
+1 -4
View File
@@ -20,9 +20,6 @@ export default async function postLink(
};
}
// This has to move above we assign link.collection.name
// Because if the link is null (write then delete text on collection)
// It will try to do trim on empty string and will throw and error, this prevents it.
if (!link.collection.name) {
link.collection.name = "Unnamed Collection";
}
@@ -54,7 +51,7 @@ export default async function postLink(
? link.description
: await getTitle(link.url);
const newLink: Link = await prisma.link.create({
const newLink = await prisma.link.create({
data: {
url: link.url,
name: link.name,