import { HasManyInterface, BaseRequestModelInterface } from '../interfaces';
import { Id, Methods, Shape, Count } from '../../types';
import { combineUrl } from '../../helpers';
type BelongsToOptions
= {
parent: BaseRequestModelInterface
;
targetPath: string;
};
export class HasManyModel implements HasManyInterface {
parent: BaseRequestModelInterface;
targetPath: string;
constructor(options: BelongsToOptions
) {
this.parent = options.parent;
this.targetPath = options.targetPath;
this.getAll = this.getAll.bind(this);
this.post = this.post.bind(this);
this.patchAll = this.patchAll.bind(this);
this.delAll = this.delAll.bind(this);
}
async getAll(parentId: Id, filter?: Shape): Promise> {
return this.parent.request>({
url: combineUrl(this.parent.basePath, `${parentId}`, this.targetPath),
method: Methods.GET,
params: filter ? { filter } : undefined,
});
}
async post(parentId: Id, item: Omit): Promise {
return this.parent.request({
url: combineUrl(this.parent.basePath, `${parentId}`, this.targetPath),
method: Methods.POST,
data: item,
});
}
patchAll(parentId: Id, item: Partial, where?: Shape): Promise {
return this.parent.request({
url: combineUrl(this.parent.basePath, `${parentId}`, this.targetPath),
method: Methods.PATCH,
data: item,
params: where ? { where } : undefined,
});
}
delAll(parentId: Id, where?: Shape): Promise {
return this.parent.request({
url: combineUrl(this.parent.basePath, `${parentId}`, this.targetPath),
method: Methods.DELETE,
params: where ? { where } : undefined,
});
}
}