79 lines
2.1 KiB
TypeScript
79 lines
2.1 KiB
TypeScript
|
import { dataSource } from "../data-source";
|
||
|
import { calendar } from "../entity/calendar";
|
||
|
import InternalException from "../exceptions/internalException";
|
||
|
|
||
|
export default abstract class CalendarService {
|
||
|
/**
|
||
|
* @description get all calendars
|
||
|
* @returns {Promise<Array<calendar>>}
|
||
|
*/
|
||
|
static async getAll(): Promise<Array<calendar>> {
|
||
|
return await dataSource
|
||
|
.getRepository(calendar)
|
||
|
.createQueryBuilder("calendar")
|
||
|
.getMany()
|
||
|
.then((res) => {
|
||
|
return res;
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException("calendars not found", err);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @description get calendar by id
|
||
|
* @returns {Promise<calendar>}
|
||
|
*/
|
||
|
static async getById(id: number): Promise<calendar> {
|
||
|
return await dataSource
|
||
|
.getRepository(calendar)
|
||
|
.createQueryBuilder("calendar")
|
||
|
.where("calendar.id = :id", { id: id })
|
||
|
.getOneOrFail()
|
||
|
.then((res) => {
|
||
|
return res;
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException("calendar not found by id", err);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @description get calendar by types
|
||
|
* @returns {Promise<Array<calendar>>}
|
||
|
*/
|
||
|
static async getByTypes(types: Array<number>): Promise<Array<calendar>> {
|
||
|
return await dataSource
|
||
|
.getRepository(calendar)
|
||
|
.createQueryBuilder("calendar")
|
||
|
.leftJoinAndSelect("calendar.type", "type")
|
||
|
.where("type.id IN (:...types)", { types: types })
|
||
|
.getMany()
|
||
|
.then((res) => {
|
||
|
return res;
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException("calendars not found by types", err);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @description get calendar by types nscdr
|
||
|
* @returns {Promise<Array<calendar>>}
|
||
|
*/
|
||
|
static async getByTypeNSCDR(): Promise<Array<calendar>> {
|
||
|
return await dataSource
|
||
|
.getRepository(calendar)
|
||
|
.createQueryBuilder("calendar")
|
||
|
.leftJoinAndSelect("calendar.type", "type")
|
||
|
.where("type.nscdr = :nscdr)", { nscdr: true })
|
||
|
.getMany()
|
||
|
.then((res) => {
|
||
|
return res;
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
throw new InternalException("calendars not found by type nscdr", err);
|
||
|
});
|
||
|
}
|
||
|
}
|