Request Model (#79)
* feat(api): request model Also adds request binding to search/discover results * fix(api): rename Request to MediaRequest and update nextjs tsconfig * refactor(api): move related request fetching code into MediaRequest entitypull/71/head
parent
342d1a3c75
commit
1910876706
@ -0,0 +1,71 @@
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
ManyToOne,
|
||||
Column,
|
||||
CreateDateColumn,
|
||||
UpdateDateColumn,
|
||||
getRepository,
|
||||
In,
|
||||
} from 'typeorm';
|
||||
import { User } from './User';
|
||||
|
||||
export enum Status {
|
||||
PENDING,
|
||||
APPROVED,
|
||||
DECLINED,
|
||||
AVAILABLE,
|
||||
}
|
||||
|
||||
@Entity()
|
||||
export class MediaRequest {
|
||||
public static async getRelatedRequests(
|
||||
mediaIds: number | number[]
|
||||
): Promise<MediaRequest[]> {
|
||||
const requestRepository = getRepository(MediaRequest);
|
||||
|
||||
try {
|
||||
let finalIds: number[];
|
||||
if (!Array.isArray(mediaIds)) {
|
||||
finalIds = [mediaIds];
|
||||
} else {
|
||||
finalIds = mediaIds;
|
||||
}
|
||||
|
||||
const requests = await requestRepository.find({ mediaId: In(finalIds) });
|
||||
|
||||
return requests;
|
||||
} catch (e) {
|
||||
console.error(e.messaage);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
@PrimaryGeneratedColumn()
|
||||
public id: number;
|
||||
|
||||
@Column()
|
||||
public mediaId: number;
|
||||
|
||||
@Column()
|
||||
public mediaType: 'movie' | 'tv';
|
||||
|
||||
@Column({ type: 'integer' })
|
||||
public status: Status;
|
||||
|
||||
@ManyToOne(() => User, (user) => user.requests, { eager: true })
|
||||
public requestedBy: User;
|
||||
|
||||
@ManyToOne(() => User, { nullable: true })
|
||||
public modifiedBy?: User;
|
||||
|
||||
@CreateDateColumn()
|
||||
public createdAt: Date;
|
||||
|
||||
@UpdateDateColumn()
|
||||
public updatedAt: Date;
|
||||
|
||||
constructor(init?: Partial<User>) {
|
||||
Object.assign(this, init);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue