208 lines
6.7 KiB
TypeScript
208 lines
6.7 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import { assign, cloneDeep } from 'lodash-es';
|
|
import { FuseMockApiService, FuseMockApiUtils } from '@fuse/lib/mock-api';
|
|
import { noticeOnelines as noticeOnelinesData } from './data';
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class BoardNoticeOnelineMockApi {
|
|
private _noticeOnelines: any[] = noticeOnelinesData;
|
|
|
|
/**
|
|
* Constructor
|
|
*/
|
|
constructor(private _fuseMockApiService: FuseMockApiService) {
|
|
// Register Mock API handlers
|
|
this.registerHandlers();
|
|
}
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ Public methods
|
|
// -----------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Register Mock API handlers
|
|
*/
|
|
registerHandlers(): void {
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ NoticeOnelines - GET
|
|
// -----------------------------------------------------------------------------------------------------
|
|
this._fuseMockApiService
|
|
.onGet('api/apps/board/notice-oneline/notice-onelines', 300)
|
|
.reply(({ request }) => {
|
|
// Get available queries
|
|
const search = request.params.get('search');
|
|
|
|
const page = parseInt(request.params.get('page') ?? '1', 10);
|
|
const size = parseInt(request.params.get('size') ?? '10', 10);
|
|
|
|
// Clone the noticeOnelines
|
|
let noticeOnelines: any[] | null = cloneDeep(this._noticeOnelines);
|
|
|
|
// If search exists...
|
|
if (search) {
|
|
// Filter the noticeOnelines
|
|
noticeOnelines = noticeOnelines.filter(
|
|
(contact: any) =>
|
|
contact.name &&
|
|
contact.name.toLowerCase().includes(search.toLowerCase())
|
|
);
|
|
}
|
|
|
|
// Paginate - Start
|
|
const noticeOnelinesLength = noticeOnelines.length;
|
|
|
|
// Calculate pagination details
|
|
const begin = page * size;
|
|
const end = Math.min(size * (page + 1), noticeOnelinesLength);
|
|
const lastPage = Math.max(Math.ceil(noticeOnelinesLength / size), 1);
|
|
|
|
// Prepare the pagination object
|
|
let pagination = {};
|
|
|
|
// If the requested page number is bigger than
|
|
// the last possible page number, return null for
|
|
// noticeOnelines but also send the last possible page so
|
|
// the app can navigate to there
|
|
if (page > lastPage) {
|
|
noticeOnelines = null;
|
|
pagination = {
|
|
lastPage,
|
|
};
|
|
} else {
|
|
// Paginate the results by size
|
|
noticeOnelines = noticeOnelines.slice(begin, end);
|
|
|
|
// Prepare the pagination mock-api
|
|
pagination = {
|
|
length: noticeOnelinesLength,
|
|
size: size,
|
|
page: page,
|
|
lastPage: lastPage,
|
|
startIndex: begin,
|
|
endIndex: end - 1,
|
|
};
|
|
}
|
|
|
|
// Return the response
|
|
return [
|
|
200,
|
|
{
|
|
noticeOnelines,
|
|
pagination,
|
|
},
|
|
];
|
|
});
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ NoticeOneline - GET
|
|
// -----------------------------------------------------------------------------------------------------
|
|
this._fuseMockApiService
|
|
.onGet('api/apps/board/notice-oneline/notice-oneline')
|
|
.reply(({ request }) => {
|
|
// Get the id from the params
|
|
const id = request.params.get('id');
|
|
|
|
// Clone the noticeOnelines
|
|
const noticeOnelines = cloneDeep(this._noticeOnelines);
|
|
|
|
// Find the noticeOneline
|
|
const noticeOneline = noticeOnelines.find(
|
|
(item: any) => item.id === id
|
|
);
|
|
|
|
// Return the response
|
|
return [200, noticeOneline];
|
|
});
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ NoticeOneline - POST
|
|
// -----------------------------------------------------------------------------------------------------
|
|
this._fuseMockApiService
|
|
.onPost('api/apps/board/notice-oneline/notice-oneline')
|
|
.reply(() => {
|
|
// Generate a new noticeOneline
|
|
const newNoticeOneline = {
|
|
id: FuseMockApiUtils.guid(),
|
|
category: '',
|
|
name: 'A New User',
|
|
description: '',
|
|
tags: [],
|
|
sku: '',
|
|
barcode: '',
|
|
brand: '',
|
|
vendor: '',
|
|
stock: '',
|
|
reserved: '',
|
|
cost: '',
|
|
basePrice: '',
|
|
taxPercent: '',
|
|
price: '',
|
|
weight: '',
|
|
thumbnail: '',
|
|
images: [],
|
|
active: false,
|
|
};
|
|
|
|
// Unshift the new noticeOneline
|
|
this._noticeOnelines.unshift(newNoticeOneline);
|
|
|
|
// Return the response
|
|
return [200, newNoticeOneline];
|
|
});
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ NoticeOneline - PATCH
|
|
// -----------------------------------------------------------------------------------------------------
|
|
this._fuseMockApiService
|
|
.onPatch('api/apps/board/notice-oneline/notice-oneline')
|
|
.reply(({ request }) => {
|
|
// Get the id and noticeOneline
|
|
const id = request.body.id;
|
|
const noticeOneline = cloneDeep(request.body.noticeOneline);
|
|
|
|
// Prepare the updated noticeOneline
|
|
let updatedNoticeOneline = null;
|
|
|
|
// Find the noticeOneline and update it
|
|
this._noticeOnelines.forEach((item, index, noticeOnelines) => {
|
|
if (item.id === id) {
|
|
// Update the noticeOneline
|
|
noticeOnelines[index] = assign(
|
|
{},
|
|
noticeOnelines[index],
|
|
noticeOneline
|
|
);
|
|
|
|
// Store the updated noticeOneline
|
|
updatedNoticeOneline = noticeOnelines[index];
|
|
}
|
|
});
|
|
|
|
// Return the response
|
|
return [200, updatedNoticeOneline];
|
|
});
|
|
|
|
// -----------------------------------------------------------------------------------------------------
|
|
// @ NoticeOneline - DELETE
|
|
// -----------------------------------------------------------------------------------------------------
|
|
this._fuseMockApiService
|
|
.onDelete('api/apps/board/notice-oneline/notice-oneline')
|
|
.reply(({ request }) => {
|
|
// Get the id
|
|
const id = request.params.get('id');
|
|
|
|
// Find the noticeOneline and delete it
|
|
this._noticeOnelines.forEach((item, index) => {
|
|
if (item.id === id) {
|
|
this._noticeOnelines.splice(index, 1);
|
|
}
|
|
});
|
|
|
|
// Return the response
|
|
return [200, true];
|
|
});
|
|
}
|
|
}
|