GeneIT/geneit_app/src/api/CoupleApi.ts

219 lines
4.9 KiB
TypeScript
Raw Normal View History

2023-08-16 10:17:04 +00:00
import { APIClient } from "./ApiClient";
2023-08-16 10:29:27 +00:00
import { DateValue } from "./MemberApi";
2023-08-16 10:17:04 +00:00
interface CoupleApiInterface {
id: number;
family_id: number;
wife?: number;
husband?: number;
state?: string;
photo_id?: string;
signed_photo_id?: string;
time_create?: number;
time_update?: number;
wedding_year?: number;
wedding_month?: number;
wedding_day?: number;
divorce_year?: number;
divorce_month?: number;
divorce_day?: number;
}
export class Couple implements CoupleApiInterface {
id: number;
family_id: number;
wife?: number;
husband?: number;
state?: string;
photo_id?: string;
signed_photo_id?: string;
time_create?: number;
time_update?: number;
wedding_year?: number;
wedding_month?: number;
wedding_day?: number;
divorce_year?: number;
divorce_month?: number;
divorce_day?: number;
constructor(int: CoupleApiInterface) {
this.id = int.id;
this.family_id = int.family_id;
this.wife = int.wife;
this.husband = int.husband;
this.state = int.state;
this.photo_id = int.photo_id;
this.signed_photo_id = int.signed_photo_id;
this.time_create = int.time_create;
this.time_update = int.time_update;
this.wedding_year = int.wedding_year;
this.wedding_month = int.wedding_month;
this.wedding_day = int.wedding_day;
this.divorce_year = int.divorce_year;
this.divorce_month = int.divorce_month;
this.divorce_day = int.divorce_day;
}
/**
* Create an empty couple object
*/
static New(family_id: number): Couple {
return new Couple({
id: 0,
family_id: family_id,
});
}
get hasPhoto(): boolean {
return this.photo_id !== null;
}
get photoURL(): string | null {
if (!this.signed_photo_id) return null;
return `${APIClient.backendURL()}/photo/${this.signed_photo_id}`;
}
get thumbnailURL(): string | null {
if (!this.signed_photo_id) return null;
return `${APIClient.backendURL()}/photo/${this.signed_photo_id}/thumbnail`;
}
2023-08-16 10:29:27 +00:00
get dateOfWedding(): DateValue | undefined {
if (!this.wedding_day && !this.wedding_month && !this.wedding_year)
return undefined;
return {
year: this.wedding_year,
month: this.wedding_month,
day: this.wedding_day,
};
}
get dateOfDivorce(): DateValue | undefined {
if (!this.divorce_day && !this.divorce_month && !this.divorce_year)
return undefined;
return {
year: this.divorce_year,
month: this.divorce_month,
day: this.divorce_day,
};
}
2023-08-16 10:17:04 +00:00
}
export class CouplesList {
private list: Couple[];
private map: Map<number, Couple>;
constructor(list: Couple[]) {
this.list = list;
this.map = new Map();
for (const m of list) {
this.map.set(m.id, m);
}
}
public get isEmpty(): boolean {
return this.list.length === 0;
}
public get fullList(): Couple[] {
return this.list;
}
filter(predicate: (m: Couple) => boolean): Couple[] {
return this.list.filter(predicate);
}
get(id: number): Couple | undefined {
return this.map.get(id);
}
}
export class CoupleApi {
/**
* Create a new couple
*/
static async Create(m: Couple): Promise<Couple> {
const res = await APIClient.exec({
uri: `/family/${m.family_id}/couple/create`,
method: "POST",
jsonData: m,
});
return new Couple(res.data);
}
/**
* Get the information about a single couple
*/
static async GetSingle(
family_id: number,
couple_id: number
): Promise<Couple> {
const res = await APIClient.exec({
uri: `/family/${family_id}/couple/${couple_id}`,
method: "GET",
});
return new Couple(res.data);
}
/**
* Get the entire list of couples of a family
*/
static async GetEntireList(family_id: number): Promise<CouplesList> {
const res = await APIClient.exec({
uri: `/family/${family_id}/couples`,
method: "GET",
});
return new CouplesList(res.data.map((d: any) => new Couple(d)));
}
/**
* Update a couple information
*/
static async Update(m: Couple): Promise<void> {
await APIClient.exec({
uri: `/family/${m.family_id}/couple/${m.id}`,
method: "PUT",
jsonData: m,
});
}
/**
* Set a new photo for a couple
*/
static async SetCouplePhoto(m: Couple, b: Blob): Promise<void> {
const fd = new FormData();
fd.append("photo", b);
await APIClient.exec({
uri: `/family/${m.family_id}/couple/${m.id}/photo`,
method: "PUT",
formData: fd,
});
}
/**
* Remove the photo of a couple
*/
static async RemoveCouplePhoto(m: Couple): Promise<void> {
await APIClient.exec({
uri: `/family/${m.family_id}/couple/${m.id}/photo`,
method: "DELETE",
});
}
/**
* Delete a family couple
*/
static async Delete(m: Couple): Promise<void> {
await APIClient.exec({
uri: `/family/${m.family_id}/couple/${m.id}`,
method: "DELETE",
});
}
}