Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Course and Course Item API Rewrite #19

Open
wants to merge 3 commits into
base: DatabaseAPIRewrite
Choose a base branch
from
Open
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
60 changes: 60 additions & 0 deletions src/operations/course.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { get } from 'http';
import getDb from '../initDB';
import { Course, CourseCreate, CourseUpdate,} from '../models';

const db = getDb();

export const createCourse = async (course: CourseCreate): Promise<Course> => {
return db.course.create({ data: course });
};

export const getCourse = async (
id: number,
ownerId: string
): Promise<Course> => {
try {
const course = await db.course.findFirstOrThrow({ where: { id, ownerId } });
return course;
} catch (e) {
throw new Error('Course not found');
}
};

export const getCourses = async (
ownerId: string,
semesterId?: number
): Promise<Course[]> => {
try {
const courses = await db.course.findMany({
where: { ownerId, semesterId },
});
return courses;
} catch (e) {
throw new Error('Courses not found');
}
};

export const updateCourse = async (
id: number,
ownerId: string,
data: CourseUpdate
): Promise<Course> => {
try {
const course = await getCourse(id, ownerId);
return db.course.update({ where: { id: course.id }, data });
} catch (e) {
throw new Error('Course not found');
}
};

export const deleteCourse = async (
id: number,
ownerId: string
): Promise<Course> => {
try {
const course = await getCourse(id, ownerId);
return db.course.delete({ where: { id: course.id } });
} catch (e) {
throw new Error('Course not found');
}
};
64 changes: 64 additions & 0 deletions src/operations/courseItem.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { get } from 'http';
import getDb from '../initDB';
import { CourseItem, CourseItemCreate, CourseItemUpdate } from '../models';

const db = getDb();

export const createCourseItem = async (
courseItem: CourseItemCreate
): Promise<CourseItem> => {
return db.courseItem.create({ data: courseItem });
};

export const getCourseItem = async (
id: number,
ownerId: string
): Promise<CourseItem> => {
try {
const courseItem = await db.courseItem.findFirstOrThrow({
where: { id, ownerId },
});
return courseItem;
} catch (e) {
throw new Error('CourseItem not found');
}
};

export const getCourseItems = async (
ownerId: string,
courseId?: number
): Promise<CourseItem[]> => {
try {
const courseItems = await db.courseItem.findMany({
where: { ownerId, courseId },
});
return courseItems;
} catch (e) {
throw new Error('CourseItems not found');
}
};

export const updateCourseItem = async (
id: number,
ownerId: string,
data: CourseItemUpdate
): Promise<CourseItem> => {
try {
const courseItem = await getCourseItem(id, ownerId);
return db.courseItem.update({ where: { id: courseItem.id }, data });
} catch (e) {
throw new Error('CourseItem not found');
}
};

export const deleteCourseItem = async (
id: number,
ownerId: string
): Promise<CourseItem> => {
try {
const courseItem = await getCourseItem(id, ownerId);
return db.courseItem.delete({ where: { id: courseItem.id } });
} catch (e) {
throw new Error('CourseItem not found');
}
};