2023-02-14 10:10:10 +01:00
|
|
|
import type { User, Project } from '@prisma/client';
|
2023-02-11 03:14:14 +01:00
|
|
|
|
2023-02-14 10:10:10 +01:00
|
|
|
import { prisma } from '~/db.server';
|
2023-02-11 03:14:14 +01:00
|
|
|
|
2023-02-14 10:10:10 +01:00
|
|
|
export type { Project } from '@prisma/client';
|
2023-02-11 03:14:14 +01:00
|
|
|
|
|
|
|
|
export function getProject({
|
|
|
|
|
id,
|
|
|
|
|
userId
|
|
|
|
|
}: Pick<Project, 'id'> & {
|
2023-02-14 10:10:10 +01:00
|
|
|
userId: User['id'];
|
2023-02-11 03:14:14 +01:00
|
|
|
}) {
|
|
|
|
|
return prisma.project.findFirst({
|
|
|
|
|
where: { id, userId }
|
2023-02-14 10:10:10 +01:00
|
|
|
});
|
2023-02-11 03:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-18 23:15:58 +01:00
|
|
|
export function getProjectByName({
|
|
|
|
|
name,
|
|
|
|
|
userId
|
|
|
|
|
}: Pick<Project, 'name'> & {
|
|
|
|
|
userId: User['id'];
|
|
|
|
|
}) {
|
|
|
|
|
return prisma.project.findFirst({
|
|
|
|
|
where: { name, userId }
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-14 10:10:10 +01:00
|
|
|
export async function getProjects({
|
2023-02-11 03:14:14 +01:00
|
|
|
userId,
|
|
|
|
|
page,
|
2023-02-14 10:10:10 +01:00
|
|
|
size,
|
|
|
|
|
orderBy,
|
|
|
|
|
order
|
2023-02-11 03:14:14 +01:00
|
|
|
}: {
|
2023-02-14 10:10:10 +01:00
|
|
|
userId: User['id'];
|
|
|
|
|
page?: number;
|
|
|
|
|
size?: number;
|
|
|
|
|
orderBy?: string;
|
|
|
|
|
order?: 'asc' | 'desc';
|
2023-02-11 03:14:14 +01:00
|
|
|
}) {
|
2023-02-14 10:10:10 +01:00
|
|
|
const totalProjects = await prisma.project.count({
|
|
|
|
|
where: { userId }
|
|
|
|
|
});
|
|
|
|
|
const paginatedProjects = await prisma.project.findMany({
|
2023-02-11 03:14:14 +01:00
|
|
|
where: { userId },
|
2023-02-14 10:10:10 +01:00
|
|
|
orderBy: {
|
|
|
|
|
[orderBy || 'createdAt']: order || 'desc'
|
|
|
|
|
},
|
|
|
|
|
skip: page && size ? (page - 1) * size : 0,
|
|
|
|
|
take: size
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const nextPage =
|
|
|
|
|
page && size && totalProjects > page * size ? page + 1 : null;
|
|
|
|
|
const previousPage = page && page > 2 ? page - 1 : null;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
total: totalProjects,
|
|
|
|
|
projects: paginatedProjects,
|
|
|
|
|
nextPage,
|
|
|
|
|
previousPage
|
|
|
|
|
};
|
2023-02-11 03:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createProject({
|
|
|
|
|
name,
|
|
|
|
|
description,
|
|
|
|
|
color,
|
|
|
|
|
userId
|
|
|
|
|
}: Pick<Project, 'name' | 'description' | 'color'> & {
|
2023-02-14 10:10:10 +01:00
|
|
|
userId: User['id'];
|
2023-02-11 03:14:14 +01:00
|
|
|
}) {
|
|
|
|
|
return prisma.project.create({
|
|
|
|
|
data: {
|
|
|
|
|
name,
|
|
|
|
|
description,
|
|
|
|
|
color,
|
|
|
|
|
userId
|
|
|
|
|
}
|
2023-02-14 10:10:10 +01:00
|
|
|
});
|
2023-02-11 03:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function updateProject({
|
|
|
|
|
projectId,
|
|
|
|
|
name,
|
|
|
|
|
description,
|
|
|
|
|
color
|
|
|
|
|
}: Partial<Pick<Project, 'name' | 'description' | 'color'>> & {
|
2023-02-14 10:10:10 +01:00
|
|
|
projectId: Project['id'];
|
2023-02-11 03:14:14 +01:00
|
|
|
}) {
|
|
|
|
|
return prisma.project.update({
|
|
|
|
|
data: {
|
|
|
|
|
name,
|
|
|
|
|
description,
|
|
|
|
|
color
|
|
|
|
|
},
|
|
|
|
|
where: {
|
|
|
|
|
id: projectId
|
|
|
|
|
}
|
2023-02-14 10:10:10 +01:00
|
|
|
});
|
2023-02-11 03:14:14 +01:00
|
|
|
}
|
|
|
|
|
|
2023-02-14 10:10:10 +01:00
|
|
|
export function deleteProject({
|
|
|
|
|
id,
|
|
|
|
|
userId
|
|
|
|
|
}: Pick<Project, 'id'> & { userId: User['id'] }) {
|
2023-02-11 03:14:14 +01:00
|
|
|
return prisma.project.deleteMany({
|
|
|
|
|
where: { id, userId }
|
2023-02-14 10:10:10 +01:00
|
|
|
});
|
2023-02-11 03:14:14 +01:00
|
|
|
}
|