Created
September 1, 2017 22:17
-
-
Save asvae/89f906cdee5f48ebf3a370906d33433f to your computer and use it in GitHub Desktop.
FieldRepository.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Field from '../Domain/Entity/Field.js' | |
import FieldMapper from '../Domain/Mapper/FieldMapper.js' | |
import repoBus from '../../oop-store/repoBus' | |
import axios from '../../plugins/internal/axios-instance.js' | |
import MappingHelpers from '../Utility/Static/MappingHelpers.js' | |
export const FIELD_UPDATED = 'Field updated' | |
export const FIELD_CREATED = 'Field created' | |
export const FIELD_REMOVED = 'Field removed' | |
export default class FieldRepository { | |
/** | |
* Get all fields with subfields. | |
*/ | |
static async getAllFields () { | |
const fieldsData = (await axios.get('fields?with_values=1')).data | |
return fieldsData.map(FieldMapper.map) | |
} | |
static async getOne (field: Field): void { | |
const fieldData = (await axios.get('fields/' + field.id)).data | |
MappingHelpers.fixIds(fieldData) | |
return FieldMapper.map(fieldData) | |
} | |
static async create (field: Field): Field { | |
const payload = FieldMapper.transform(field) | |
MappingHelpers.unfixIds(payload) | |
const createdField = new Field(field) | |
createdField.id = (await axios.post('fields', payload)).data | |
repoBus.emit(FIELD_CREATED, createdField) | |
return createdField | |
} | |
static async update (field: Field): Field { | |
const payload = FieldMapper.transform(field) | |
MappingHelpers.unfixIds(payload) | |
const updatedField = new Field(field) | |
await axios.put(`fields/${field.id}`, payload) | |
repoBus.emit(FIELD_UPDATED, updatedField) | |
return updatedField | |
} | |
static async getManyWithQuery (params: Object): Field[] { | |
const data = (await axios.get(`fields`, { params })).data | |
return data.map(FieldMapper.map) | |
} | |
static async remove (field: Field): void { | |
await axios.delete(`fields/${field.id}`) | |
repoBus.emit(FIELD_REMOVED, field) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment