feat: added edit + delete collection functionality

This commit is contained in:
Daniel
2023-04-29 00:40:29 +03:30
parent ca30e42f0c
commit 8b7a660766
14 changed files with 285 additions and 52 deletions
@@ -0,0 +1,48 @@
// Copyright (C) 2022-present Daniel31x13 <daniel31x13@gmail.com>
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
import { prisma } from "@/lib/api/db";
import getPermission from "@/lib/api/getPermission";
import fs from "fs";
export default async function (collection: { id: number }, userId: number) {
console.log(collection.id);
if (!collection.id)
return { response: "Please choose a valid collection.", status: 401 };
const collectionIsAccessible = await getPermission(userId, collection.id);
if (!(collectionIsAccessible?.ownerId === userId))
return { response: "Collection is not accessible.", status: 401 };
const deletedCollection = await prisma.$transaction(async () => {
await prisma.usersAndCollections.deleteMany({
where: {
collection: {
id: collection.id,
},
},
});
await prisma.link.deleteMany({
where: {
collection: {
id: collection.id,
},
},
});
fs.rmdirSync(`data/archives/${collection.id}`, { recursive: true });
return await prisma.collection.delete({
where: {
id: collection.id,
},
});
});
return { response: deletedCollection, status: 200 };
}
@@ -0,0 +1,48 @@
// Copyright (C) 2022-present Daniel31x13 <daniel31x13@gmail.com>
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
import { prisma } from "@/lib/api/db";
import { ExtendedCollection } from "@/types/global";
import getPermission from "@/lib/api/getPermission";
export default async function (collection: ExtendedCollection, userId: number) {
if (!collection)
return { response: "Please choose a valid collection.", status: 401 };
const collectionIsAccessible = await getPermission(userId, collection.id);
if (!(collectionIsAccessible?.ownerId === userId))
return { response: "Collection is not accessible.", status: 401 };
const updatedCollection = await prisma.$transaction(async () => {
await prisma.usersAndCollections.deleteMany({
where: {
collection: {
id: collection.id,
},
},
});
return await prisma.collection.update({
where: {
id: collection.id,
},
data: {
name: collection.name,
description: collection.description,
members: {
create: collection.members.map((e) => ({
user: { connect: { email: e.user.email } },
canCreate: e.canCreate,
canUpdate: e.canUpdate,
canDelete: e.canDelete,
})),
},
},
});
});
return { response: updatedCollection, status: 200 };
}
+1 -1
View File
@@ -6,7 +6,7 @@
import { prisma } from "@/lib/api/db";
export default async function (userId: number) {
// tag cleanup
// remove empty tags
await prisma.tag.deleteMany({
where: {
links: {
+3 -1
View File
@@ -19,5 +19,7 @@ export default async function (email: string) {
createdAt: user?.createdAt,
};
return { response: unsensitiveUserInfo || null, status: 200 };
const statusCode = user?.id ? 200 : 404;
return { response: unsensitiveUserInfo || null, status: statusCode };
}