HM-23. Добавелены классы для работы с api servie-store (#2)

Co-authored-by: vigdorov <vigdorov@yandex-team.ru>
This commit is contained in:
Nikolay
2020-07-06 13:52:19 +03:00
committed by GitHub
parent 243f2ce614
commit 146b780812
5 changed files with 192 additions and 6 deletions

100
src/api/StorageListAPI.js Normal file
View File

@ -0,0 +1,100 @@
import StorageServiceApi from './StorageServiceAPI';
/**
* Класс создания api для списков данных
* @class
* @public
*/
class StorageListApi {
/**
* @param {string} key - уникальный ключ для api, который будет записан в сервисе
*/
constructor(key) {
this.key = key;
this.api = new StorageServiceApi();
}
/**
* @private
* @param {string} _id - _id искомого элемента
*
* @returns {Promise<Object, string>}
*/
async _findIndex(_id) {
const list = await this.request();
return list.findIndex(item => item._id === _id);
}
/**
* @public
* @returns {Promise} - возвращает все элементы списка
*/
async request() {
const data = await this.api.find(this.key);
return (data && data.value) || [];
}
/**
* @param {string} _id - _id искомого элемента списка
*
* @returns {Promise} - возвращает элемент списка или генерит ошибку
*/
async find(_id) {
const list = await this.request();
const findIndex = LocalStorageListApi.findIndex(list, _id);
if (findIndex === -1) {
throw new Error(`Not Found _id: ${_id}`);
}
return list[findIndex];
}
/**
* @param {Object} data - элемент списка
*
* @returns {Promise} - Возвращает вновь созданный элемент с уникальным полем _id
*/
async create(data) {
const list = await this.request();
const _id = uuidv4();
const newData = {
...data,
_id,
};
await this.api.createOrUpdate(this.key, list.concat(newData));
return newData;
}
/**
* @param {Object} data - элемент списка
* @param {string} data._id - наличие _id обязательно
*
* @returns {Promise} - Возвращает обновленный элемент списка
*/
async update(data) {
const list = await this.request();
const findIndex = LocalStorageListApi.findIndex(list, data._id);
if (findIndex === -1) {
throw new Error(`Not Found _id: ${data._id}`);
}
list.splice(findIndex, 1, data);
await this.api.createOrUpdate(this.key, list);
return data;
}
/**
* @param {string} _id - _id удаляемого элемента
*
* @returns {Promise} - Возвращает _id удаленного элемента или ошибку
*/
async remove(_id) {
const list = await this.request();
const findIndex = LocalStorageListApi.findIndex(list, _id);
if (findIndex === -1) {
throw new Error(`Not Found _id: ${_id}`);
}
list.splice(findIndex, 1);
await this.api.createOrUpdate(this.key, list);
return _id;
}
}

View File

@ -0,0 +1,28 @@
import {API_URL, ENDPOINT} from './consts';
class StorageServiceApi {
URL = `${API_URL}${ENDPOINT}`;
async request() {
const {data} = await axios.get(this.URL);
return data;
}
async find(key) {
const {data} = await axios.get(`${this.URL}/${key}`)
return data;
}
async createOrUpdate(key, value) {
const {data} = await axios.post(this.URL, {key, value})
return data;
}
async remove(key) {
const {data} = await axios.delete(`${this.URL}/${key}`);
return data;
}
}
export default StorageServiceApi;

5
src/api/consts.js Normal file
View File

@ -0,0 +1,5 @@
export const API_URL = 'http://vigdorov.ru:4001';
export const ENDPOINT = '/store';
export const API_KEYS = {
};