1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import http from '@/lib/http'
- export interface ModelBase {
- id: number
- created_at: string
- updated_at: string
- }
- export interface Pagination {
- total: number
- per_page: number
- current_page: number
- total_pages: number
- }
- export interface IGetListResponse<T> {
- data: T[]
- pagination: Pagination
- }
- class Curd<T> {
- protected readonly baseUrl: string
- protected readonly plural: string
- get_list = this._get_list.bind(this)
- get = this._get.bind(this)
- save = this._save.bind(this)
- destroy = this._destroy.bind(this)
- recover = this._recover.bind(this)
- update_order = this._update_order.bind(this)
- constructor(baseUrl: string, plural: string | null = null) {
- this.baseUrl = baseUrl
- this.plural = plural ?? `${this.baseUrl}s`
- }
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- _get_list(params: any = null): Promise<IGetListResponse<T>> {
- return http.get(this.plural, { params })
- }
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- _get(id: any = null, params: any = {}): Promise<T> {
- return http.get(this.baseUrl + (id ? `/${id}` : ''), { params })
- }
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- _save(id: any = null, data: any, config: any = undefined): Promise<T> {
- return http.post(this.baseUrl + (id ? `/${id}` : ''), data, config)
- }
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- _destroy(id: any = null) {
- return http.delete(`${this.baseUrl}/${id}`)
- }
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
- _recover(id: any = null) {
- return http.patch(`${this.baseUrl}/${id}`)
- }
- _update_order(data: {
- target_id: number
- direction: number
- affected_ids: number[]
- }) {
- return http.post(`${this.plural}/order`, data)
- }
- }
- export default Curd
|