55 lines
1.4 KiB
TypeScript
55 lines
1.4 KiB
TypeScript
|
import { dataSource } from "../../data-source";
|
||
|
import { mission } from "../../entity/operation/mission";
|
||
|
import DatabaseActionException from "../../exceptions/databaseActionException";
|
||
|
|
||
|
export default abstract class MissionService {
|
||
|
/**
|
||
|
* @description get all missions
|
||
|
* @returns {Promise<[Array<mission>, number]>}
|
||
|
*/
|
||
|
static async getAll({
|
||
|
offset = 0,
|
||
|
count = 25,
|
||
|
noLimit = false,
|
||
|
}: {
|
||
|
offset?: number;
|
||
|
count?: number;
|
||
|
noLimit?: boolean;
|
||
|
}): Promise<[Array<mission>, number]> {
|
||
|
let query = dataSource.getRepository(mission).createQueryBuilder("mission");
|
||
|
|
||
|
if (!noLimit) {
|
||
|
query = query.offset(offset).limit(count);
|
||
|
}
|
||
|
|
||
|
return await query
|
||
|
.orderBy("mission.createdAt")
|
||
|
.getManyAndCount()
|
||
|
.then((res) => {
|
||
|
return res;
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
throw new DatabaseActionException("SELECT", "mission", err);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @description get mission by id
|
||
|
* @param {string} id
|
||
|
* @returns {Promise<mission>}
|
||
|
*/
|
||
|
static async getById(id: string): Promise<mission> {
|
||
|
return dataSource
|
||
|
.getRepository(mission)
|
||
|
.createQueryBuilder("mission")
|
||
|
.where("mission.id = :id", { id: id })
|
||
|
.getOneOrFail()
|
||
|
.then((res) => {
|
||
|
return res;
|
||
|
})
|
||
|
.catch((err) => {
|
||
|
throw new DatabaseActionException("SELECT", "mission", err);
|
||
|
});
|
||
|
}
|
||
|
}
|