fix(refactor): add services/adapters/useCases
This commit is contained in:
95
users-manage/src/adapters/adapterOfServices/Services.js
Normal file
95
users-manage/src/adapters/adapterOfServices/Services.js
Normal file
@@ -0,0 +1,95 @@
|
||||
import {get, post, put, remove} from './apiHelpers.js'
|
||||
import {convertList} from '@helpers/adapter/adapter.js'
|
||||
|
||||
const config = {
|
||||
id: "id",
|
||||
created_at: "created_at",
|
||||
updated_at: "updated_at",
|
||||
deleted_at: "deleted_at",
|
||||
name: "name",
|
||||
port: "port",
|
||||
proxy_ip: "proxy_ip",
|
||||
internet_uri: "internet_uri",
|
||||
description: "description",
|
||||
is_online: "is_online",
|
||||
site_ip: "device_ip",
|
||||
}
|
||||
|
||||
class Services {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {String} apiAddr - path to service
|
||||
*/
|
||||
|
||||
constructor(apiAddr) {
|
||||
this.apiAddr = apiAddr
|
||||
this.config = config
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @returns {Array<Object>} res.data - all services
|
||||
*/
|
||||
|
||||
async getServices() {
|
||||
const res = await get(`${this.apiAddr}/servers`)
|
||||
return convertList(res.data, {config: this.config})
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Object} payload - add new service
|
||||
* @returns {Object} newService - added new service
|
||||
*/
|
||||
|
||||
async createService(payload) {
|
||||
let newService = []
|
||||
const updatedPort = parseFloat(payload.port)
|
||||
const updatedService = {...convertObject(payload, {config: servConfig}), port: updatedPort}
|
||||
await post(`${this.apiAddr}/servers`, updatedService).then(res => {
|
||||
newService = convertObject(res.value, {config: this.config})
|
||||
}).catch(err => {
|
||||
console.log('err', err)
|
||||
})
|
||||
return newService
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Object} payload - edit params in selected service
|
||||
* @returns {Object} resService - updated service with edited params
|
||||
*/
|
||||
|
||||
async updateService(payload) {
|
||||
let resService = []
|
||||
const updatedPort = parseFloat(payload.port)
|
||||
const updatedService = {...convertObject(payload, {config: servConfig}), port: updatedPort}
|
||||
if (payload.id) {
|
||||
await put(`${this.apiAddr}/servers`, updatedService, payload.id).then(res => {
|
||||
resService = convertObject(res.value, {config: this.config})
|
||||
}).catch(err => {
|
||||
console.log('err', err)
|
||||
})
|
||||
}
|
||||
return resService
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Number} id - id selected service for remove
|
||||
* @returns {Number} deletedServiceId - id removed service
|
||||
*/
|
||||
|
||||
async deleteService(id) {
|
||||
let deletedServiceId = null
|
||||
await remove(`${this.apiAddr}/servers`, id).then((res) => {
|
||||
deletedServiceId = res.id
|
||||
}).catch(err => {
|
||||
console.log('err', err)
|
||||
})
|
||||
return deletedServiceId
|
||||
}
|
||||
}
|
||||
|
||||
export default Services
|
||||
39
users-manage/src/adapters/adapterOfServices/apiHelpers.js
Normal file
39
users-manage/src/adapters/adapterOfServices/apiHelpers.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import axios from "axios";
|
||||
|
||||
const post = async (path, data, onError) => {
|
||||
return await axios.post(path, data)
|
||||
.then(r => r.data)
|
||||
.catch(error => {
|
||||
onError && onError(error)
|
||||
console.error('Error post request:', error)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
const get = async (path) => {
|
||||
return await axios.get(path)
|
||||
.catch(error => {
|
||||
console.error('Error get request:', error)
|
||||
})
|
||||
}
|
||||
|
||||
const put = async (path, data, id, onError) => {
|
||||
return await axios.put(`${path}/${id}`, data)
|
||||
.then(r => r.data)
|
||||
.catch(error => {
|
||||
onError && onError(error)
|
||||
console.error('Error put request:', error)
|
||||
})
|
||||
}
|
||||
|
||||
const remove = async (path, id, onError) => {
|
||||
return await axios.delete(`${path}/${id}`)
|
||||
.then(r => r.data)
|
||||
.catch(error => {
|
||||
onError && onError(error)
|
||||
console.error('Error delete request:', error)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
export {get, post, put, remove}
|
||||
28
users-manage/src/adapters/adapterOfUsers/StaticData.js
Normal file
28
users-manage/src/adapters/adapterOfUsers/StaticData.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const devUsersList = [
|
||||
{
|
||||
"id": 1,
|
||||
"first_name": "Leanne",
|
||||
"last_name": "Graham",
|
||||
"email": "test@mail.ru",
|
||||
"role": "admin",
|
||||
"is_active": true
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"first_name": "Leanne",
|
||||
"last_name": "Graham",
|
||||
"email": "test@mail.ru",
|
||||
"role": "admin",
|
||||
"is_active": true
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"first_name": "Leanne",
|
||||
"last_name": "Graham",
|
||||
"email": "test@mail.ru",
|
||||
"role": "admin",
|
||||
"is_active": true
|
||||
},
|
||||
]
|
||||
|
||||
export {devUsersList}
|
||||
206
users-manage/src/adapters/adapterOfUsers/Users.js
Normal file
206
users-manage/src/adapters/adapterOfUsers/Users.js
Normal file
@@ -0,0 +1,206 @@
|
||||
import {get, post, put, remove} from './apiHelpers.js'
|
||||
import {convertList} from '@helpers/adapter/adapter.js'
|
||||
import {devUsersList} from './StaticData.js'
|
||||
|
||||
/**
|
||||
* Интерфейс пользователя
|
||||
* @typedef {Object} User
|
||||
* @param {Number} id
|
||||
*/
|
||||
|
||||
class Users {
|
||||
/**
|
||||
* Класс управления роутерами
|
||||
* @param {String} apiAddr - path to service
|
||||
* @param {Object | undefined} adapter_config - oldKey: newKey
|
||||
* @param {Object | undefined} params - Конфиг настроек
|
||||
* @param {'prod' | 'test'} params.mode - Конфиг настроек
|
||||
*/
|
||||
constructor(apiAddr, adapter_config = {}, params = {mode: 'prod'}) {
|
||||
this.apiAddr = apiAddr
|
||||
this.config = adapter_config
|
||||
this.mode = params.mode
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Object} params - Конфиг настроек
|
||||
* @param {'prod' | 'dev'} params.mode - Конфиг настроек
|
||||
* @returns {Promise<*[]> | *[]}
|
||||
*/
|
||||
async getUsers(params) {
|
||||
if (params.mode === "dev") {
|
||||
return devUsersList
|
||||
}
|
||||
let res = await get(`${this.apiAddr}/users`)
|
||||
let updatedUsers = convertList(res.data, {config: this.config})
|
||||
return updatedUsers
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Number} id - Сервис id, если id не указан, отображается список всех роутеров
|
||||
* @param {'dev' | 'prod'} mode - Сервис id, если id не указан, отображается список всех роутеров
|
||||
* @returns {Promise<*[]>}
|
||||
*/
|
||||
async getUsersBySiteId(id, mode) {
|
||||
if (mode === "dev") {
|
||||
return devUsersList
|
||||
}
|
||||
let res = await get(`${this.apiAddr}/users/by_server/${id}`)
|
||||
let updatedUsers = convertList(res.data, {config: this.config})
|
||||
return updatedUsers
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {User} userData
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async createUser(userData) {
|
||||
const newUser = await post(`${this.apiAddr}/users`, userData)
|
||||
return newUser
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {User} userData
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
async updateUser(userData) {
|
||||
const updatedUserData = {...userData}
|
||||
delete updatedUserData.id
|
||||
const newUser = await put(`${this.apiAddr}/users/${userData.id}`, updatedUserData)
|
||||
return newUser
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Number} userId - Сервис id, если id не указан, отображается список всех роутеров
|
||||
* @returns {Promise<*[]>}
|
||||
*/
|
||||
async removeUser(userId) {
|
||||
const removedUser = await remove(`${this.apiAddr}/users/${userId}`)
|
||||
return removedUser
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Функция запускает список запросов к апишке и логает ответы
|
||||
* @returns {Promise<String>}
|
||||
*/
|
||||
async test() {
|
||||
console.log("_______START TEST_______")
|
||||
|
||||
const allUsers = await this.getUsers()
|
||||
console.log("allUsers", allUsers)
|
||||
const serverUser = await this.getUserById(1)
|
||||
console.log("getUserById 1", serverUser)
|
||||
const newUser = await this.createUser({
|
||||
"server_id": 1,
|
||||
"path": "/",
|
||||
"role": 1,
|
||||
"description": "tests swagger",
|
||||
"deepness": 6,
|
||||
"order": 0,
|
||||
"is_cb_on": true,
|
||||
"cb_request_limit": 100,
|
||||
"cb_min_requests": 100,
|
||||
"cb_error_threshold_percentage": 0.35,
|
||||
"cb_interval_duration": 2000000,
|
||||
"cb_open_state_timeout": 1000000
|
||||
})
|
||||
console.log("newUser", newUser)
|
||||
const updatedUser = await this.updateUser({
|
||||
"path": "/updated_path/",
|
||||
"description": "updated_description",
|
||||
"id": newUser.id
|
||||
})
|
||||
console.log("updatedUser", updatedUser)
|
||||
const removedUser = await this.removeUser(newUser.id)
|
||||
console.log("removedUser", removedUser)
|
||||
|
||||
const newUser1 = await this.createUser({
|
||||
"server_id": 1,
|
||||
"path": "/",
|
||||
"role": 1,
|
||||
"description": "tests swagger",
|
||||
"deepness": 6,
|
||||
"order": 0,
|
||||
"is_cb_on": true,
|
||||
"cb_request_limit": 100,
|
||||
"cb_min_requests": 100,
|
||||
"cb_error_threshold_percentage": 0.35,
|
||||
"cb_interval_duration": 2000000,
|
||||
"cb_open_state_timeout": 1000000
|
||||
})
|
||||
const newUser2 = await this.createUser({
|
||||
"server_id": 1,
|
||||
"path": "/",
|
||||
"role": 1,
|
||||
"description": "tests swagger",
|
||||
"deepness": 6,
|
||||
"order": 0,
|
||||
"is_cb_on": true,
|
||||
"cb_request_limit": 100,
|
||||
"cb_min_requests": 100,
|
||||
"cb_error_threshold_percentage": 0.35,
|
||||
"cb_interval_duration": 2000000,
|
||||
"cb_open_state_timeout": 1000000
|
||||
})
|
||||
const newUser3 = await this.createUser({
|
||||
"server_id": 1,
|
||||
"path": "/",
|
||||
"role": 1,
|
||||
"description": "tests swagger",
|
||||
"deepness": 6,
|
||||
"order": 0,
|
||||
"is_cb_on": true,
|
||||
"cb_request_limit": 100,
|
||||
"cb_min_requests": 100,
|
||||
"cb_error_threshold_percentage": 0.35,
|
||||
"cb_interval_duration": 2000000,
|
||||
"cb_open_state_timeout": 1000000
|
||||
})
|
||||
|
||||
const actions = [
|
||||
{
|
||||
...newUser1,
|
||||
action: "update",
|
||||
"path": "/updated_path2/",
|
||||
"description": "updated_description2",
|
||||
}, {
|
||||
...newUser2,
|
||||
action: "update",
|
||||
"path": "/updated_path3/",
|
||||
"description": "updated_description3",
|
||||
}, {
|
||||
...newUser3,
|
||||
action: "remove"
|
||||
},
|
||||
{
|
||||
action: "create",
|
||||
"server_id": 1,
|
||||
"path": "/lalalala/lalalal",
|
||||
"role": 1,
|
||||
"description": "new_route_created",
|
||||
"deepness": 6,
|
||||
"order": 0,
|
||||
"is_cb_on": true,
|
||||
"cb_request_limit": 100,
|
||||
"cb_min_requests": 100,
|
||||
"cb_error_threshold_percentage": 0.35,
|
||||
"cb_interval_duration": 2000000,
|
||||
"cb_open_state_timeout": 1000000
|
||||
}
|
||||
]
|
||||
|
||||
const mutationsList = await this.updateGroupUsers(actions)
|
||||
console.log("mutationsList", mutationsList)
|
||||
console.log("________END TEST________")
|
||||
|
||||
return "ok"
|
||||
}
|
||||
}
|
||||
|
||||
export default Users
|
||||
39
users-manage/src/adapters/adapterOfUsers/apiHelpers.js
Normal file
39
users-manage/src/adapters/adapterOfUsers/apiHelpers.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import axios from "axios";
|
||||
|
||||
const post = async (path, data, onError) => {
|
||||
return await axios.post(path, data)
|
||||
.then(r => r.data)
|
||||
.catch(error => {
|
||||
onError && onError(error)
|
||||
console.error('Error post request:', error)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
const get = async (path) => {
|
||||
return await axios.get(path)
|
||||
.catch(error => {
|
||||
console.error('Error get request:', error)
|
||||
})
|
||||
}
|
||||
|
||||
const put = async (path, data, id, onError) => {
|
||||
return await axios.put(`${path}/${id}`, data)
|
||||
.then(r => r.data)
|
||||
.catch(error => {
|
||||
onError && onError(error)
|
||||
console.error('Error put request:', error)
|
||||
})
|
||||
}
|
||||
|
||||
const remove = async (path, id, onError) => {
|
||||
return await axios.delete(`${path}/${id}`)
|
||||
.then(r => r.data)
|
||||
.catch(error => {
|
||||
onError && onError(error)
|
||||
console.error('Error delete request:', error)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
export {get, post, put, remove}
|
||||
Reference in New Issue
Block a user