Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CRUD functions for models via Prisma #228

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions app/models/certificate.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import type { Certificate } from '@prisma/client';

import { prisma } from '~/db.server';

export type { Certificate } from '@prisma/client';

export async function getCertificateByUsername(username: Certificate['username']) {
Ririio marked this conversation as resolved.
Show resolved Hide resolved
return prisma.certificate.findUnique({ where: { username } });
}

export async function createCertificate(
username: Certificate['username'],
subject: Certificate['subject'],
certificate: Certificate['certificate'],
orderUrl: Certificate['orderUrl'],
privateKey: Certificate['privateKey']
) {
// Set expiration date 90 days from now
const validTo = new Date();
validTo.setDate(validTo.getDate() + 90);
cychu42 marked this conversation as resolved.
Show resolved Hide resolved

return prisma.certificate.create({
data: {
username,
cychu42 marked this conversation as resolved.
Show resolved Hide resolved
subject,
certificate,
orderUrl,
privateKey,
validTo,
},
});
}

export async function updateCertificateByUsername(
username: Certificate['username'],
subject?: Certificate['subject'],
certificate?: Certificate['certificate'],
orderUrl?: Certificate['orderUrl'],
privateKey?: Certificate['privateKey'],
validFrom?: Certificate['validFrom']
) {
// If validFrom is changed, set validTo to 90 days from validFrom
let validTo;
if (validFrom) {
validTo = validFrom;
validTo.setDate(validTo.getDate() + 90);
}
return prisma.certificate.update({
where: { username },
data: {
username,
subject,
certificate,
orderUrl,
privateKey,
validFrom,
validTo,
},
});
}
Ririio marked this conversation as resolved.
Show resolved Hide resolved

export async function deleteCertificateByUsername(username: Certificate['username']) {
return prisma.certificate.delete({ where: { username } });
}
43 changes: 43 additions & 0 deletions app/models/challenge.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { Challenge } from '@prisma/client';

import { prisma } from '~/db.server';

export type { Challenge } from '@prisma/client';

export async function getChallengeByCertificateId(certificateId: Challenge['certificateId']) {
return prisma.challenge.findMany({ where: { certificateId } });
}

export async function createChallenge(
domain: Challenge['domain'],
challengeKey: Challenge['challengeKey'],
certificateId: Challenge['certificateId']
) {
return prisma.challenge.create({
data: {
domain,
challengeKey,
certificateId,
},
});
}

export async function updateChallengeById(
id: Challenge['id'],
certificateId?: Challenge['certificateId'],
domain?: Challenge['domain'],
challengeKey?: Challenge['challengeKey']
) {
return prisma.challenge.update({
where: { id },
data: {
domain,
challengeKey,
certificateId,
},
});
}

export async function deleteChallengeById(id: Challenge['id']) {
return prisma.challenge.delete({ where: { id } });
}
70 changes: 70 additions & 0 deletions app/models/record.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import type { Record } from '@prisma/client';

import { prisma } from '~/db.server';

export type { Record } from '@prisma/client';

export async function getRecordsByUsername(username: Record['username']) {
return prisma.record.findMany({ where: { username } });
}

export async function createRecord(
username: Record['username'],
name: Record['name'],
type: Record['type'],
value: Record['value'],
status: Record['status'],
description?: Record['description'],
course?: Record['course'],
ports?: Record['ports']
) {
// Set expiration date 6 months from now
const expiresAt = new Date();
expiresAt.setMonth(expiresAt.getMonth() + 6);

return prisma.record.create({
data: {
username,
name,
type,
value,
description,
course,
ports,
expiresAt,
status,
},
});
}

export async function updateRecordById(
id: Record['id'],
username?: Record['username'],
name?: Record['name'],
type?: Record['type'],
value?: Record['value'],
description?: Record['description'],
course?: Record['course'],
ports?: Record['ports'],
expiresAt?: Record['expiresAt'],
status?: Record['status']
) {
return prisma.record.update({
where: { id },
data: {
username,
name,
type,
value,
description,
course,
ports,
expiresAt,
status,
},
});
}

export async function deleteRecordById(id: Record['id']) {
return prisma.record.delete({ where: { id } });
}
21 changes: 19 additions & 2 deletions app/models/user.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ export async function createUser(
});
}

export async function deleteUserByEmail(email: User['email']) {
return prisma.user.delete({ where: { email } });
export async function updateUserByUsername(
username: User['username'],
firstName?: User['firstName'],
lastName?: User['lastName'],
email?: User['email']
) {
return prisma.user.update({
where: { username },
data: {
username,
firstName,
lastName,
email,
},
});
}

export async function deleteUserByUsername(username: User['username']) {
return prisma.user.delete({ where: { username } });
}