diff --git a/src/api/NestRepository.ts b/src/api/NestRepository.ts new file mode 100644 index 0000000..6dc56f5 --- /dev/null +++ b/src/api/NestRepository.ts @@ -0,0 +1,11 @@ +import EntityBaseRepository from '@/api/EntityBaseRepository'; + +/** + * Nest repository + */ +export default class NestRepository extends EntityBaseRepository { + + public async findAll(areaId: string): Promise { + return this.axiosClient.get(`nest?areaId=${areaId}`); + } +} diff --git a/src/api/RepositoryFactory.ts b/src/api/RepositoryFactory.ts index 25a3f4d..d77c073 100644 --- a/src/api/RepositoryFactory.ts +++ b/src/api/RepositoryFactory.ts @@ -1,9 +1,13 @@ import AuthRepository from '@/api/AuthRepository'; import AreaRepository from '@/api/AreaRepository'; +import StatusRepository from '@/api/StatusRepository'; +import NestRepository from '@/api/NestRepository'; const repositories: any = { auth: { Repository: AuthRepository, instance: null }, - area: { Repository: AreaRepository, instance: null } + area: { Repository: AreaRepository, instance: null }, + status: { Repository: StatusRepository, instance: null }, + nest: { Repository: NestRepository, instance: null } }; /** diff --git a/src/api/StatusRepository.ts b/src/api/StatusRepository.ts new file mode 100644 index 0000000..13f8e18 --- /dev/null +++ b/src/api/StatusRepository.ts @@ -0,0 +1,21 @@ +import EntityBaseRepository from '@/api/EntityBaseRepository'; + +/** + * Status repository + */ +export default class StatusRepository extends EntityBaseRepository { + + public async findAll(): Promise { + return this.axiosClient.get(`status`); + } + + public async getIcons(lastUpdate?: Date): Promise { + let query = ''; + + if (lastUpdate) { + query = `?lastUpdate=${lastUpdate.toISOString()}` + } + + return this.axiosClient.get(`status/getIcons${query}`); + } +} diff --git a/src/models/Area.ts b/src/models/Area.ts index 3f6b48e..a71034b 100644 --- a/src/models/Area.ts +++ b/src/models/Area.ts @@ -11,9 +11,7 @@ export class Area { Object.assign(this, data); if (data.users) { - data.users.map((userData: any) => { - this.users.push(new User(userData)); - }); + this.users = data.users.map((userData: any) => new User(userData)); } } } diff --git a/src/models/Nest.ts b/src/models/Nest.ts new file mode 100644 index 0000000..f0f1cff --- /dev/null +++ b/src/models/Nest.ts @@ -0,0 +1,16 @@ +import User from '@/models/User'; +import NestEntry from '@/models/NestEntry'; + +export class Nest { + + public id!: string; + public history!: NestEntry[]; + + constructor(data: any) { + Object.assign(this, data); + + if (data.history) { + this.history = data.history.map((historyData: any) => new NestEntry(historyData)); + } + } +} diff --git a/src/models/NestEntry.ts b/src/models/NestEntry.ts new file mode 100644 index 0000000..07e0f72 --- /dev/null +++ b/src/models/NestEntry.ts @@ -0,0 +1,37 @@ +import { Status } from '@/models/Status'; +import User from '@/models/User'; + +type Coordinates = { latitude: number, longitude: number }; + +export default class NestEntry { + + public id!: string; + public numberOfEggs?: number; + public reportDate!: Date; + public dateOfHatching?: Date; + public note?: string; + public location!: Coordinates; + + public status!: Status; + public user!: User; + public entity!: any; + + constructor(nestData: any) { + Object.assign(this, nestData); + + const parsedLocation = JSON.parse(nestData.location); + + this.location = { latitude: parsedLocation[0], + longitude: parsedLocation[1] }; + + if (nestData.status) { + this.status = new Status(nestData.status); + } + + if (nestData.user) { + this.user = new User(nestData.user); + } + + } + +} diff --git a/src/models/Status.ts b/src/models/Status.ts new file mode 100644 index 0000000..0865523 --- /dev/null +++ b/src/models/Status.ts @@ -0,0 +1,12 @@ + +export class Status { + + public id?: string; + public name?: string; + public active: boolean = false; + public iconName?: string + + constructor(data: any) { + Object.assign(this, data); + } +} diff --git a/src/store/nest.store.ts b/src/store/nest.store.ts new file mode 100644 index 0000000..be73790 --- /dev/null +++ b/src/store/nest.store.ts @@ -0,0 +1,40 @@ +import { ActionTree, Module } from 'vuex'; +import { RepositoryFactory } from '@/api/RepositoryFactory'; +import NestRepository from '@/api/NestRepository'; +import { Nest } from '@/models/Nest'; + +const nestRepository: NestRepository = RepositoryFactory.get('nest'); + +const store = { + nests: undefined, +}; + +const actions: ActionTree = { + findAll: async ({ commit }, payload: { areaId: string }): Promise => { + const response = await nestRepository.findAll(payload.areaId); + const nests: Nest[] = response.map((nestData: any) => + new Nest(nestData)); + + commit('saveNests', nests); + return nests; + }, +}; + +const mutations = { + saveNests: (state: any, nests: any) => { + state.nests = nests; + }, +}; + +const getters = { + nests: (state: any) => state.nests, +}; + +const nestStore: Module = { + state: store, + actions, + mutations, + getters +}; + +export default nestStore; diff --git a/src/store/status.store.ts b/src/store/status.store.ts new file mode 100644 index 0000000..7be70c8 --- /dev/null +++ b/src/store/status.store.ts @@ -0,0 +1,58 @@ +import { ActionTree, Module } from 'vuex'; +import AuthRepository from '@/api/AuthRepository'; +import { RepositoryFactory } from '@/api/RepositoryFactory'; +import AreaRepository from '@/api/AreaRepository'; +import { Area } from '@/models/Area'; +import StatusRepository from '@/api/StatusRepository'; +import { Status } from '@/models/Status'; + +const statusRepository: StatusRepository = RepositoryFactory.get('status'); + +const store = { + status: undefined, + icons: undefined +}; + +const actions: ActionTree = { + findAll: async ({ commit }): Promise => { + const response = await statusRepository.findAll(); + const status: Status[] = response.map((statusData: any) => + new Status(statusData)); + + commit('saveStatus', status); + return status; + }, + getIcons: async ({ commit }, lastUpdate?: Date): + Promise => { + const response = await statusRepository.getIcons(lastUpdate); + + console.log(response); + + commit('saveIcons', response); + + return response; + }, +}; + +const mutations = { + saveStatus: (state: any, status: any) => { + state.status = status; + }, + saveIcons: (state: any, iconData: any) => { + state.icons = iconData; + } +}; + +const getters = { + status: (state: any) => state.status, + icons: (state: any) => state.icons, +}; + +const statusStore: Module = { + state: store, + actions, + mutations, + getters +}; + +export default statusStore; diff --git a/src/store/store.ts b/src/store/store.ts index 9fe6bf3..72ef833 100644 --- a/src/store/store.ts +++ b/src/store/store.ts @@ -4,6 +4,8 @@ import VuexPersistence from 'vuex-persist'; import authStore from '@/store/auth.store'; import globalStore from '@/store/global.store'; import areaStore from '@/store/area.store'; +import statusStore from '@/store/status.store'; +import nestStore from '@/store/nest.store'; Vue.use(Vuex); // set up persistence storage plugin @@ -29,6 +31,14 @@ export default new Vuex.Store({ area: { namespaced: true, ...areaStore + }, + status: { + namespaced: true, + ...statusStore + }, + nest: { + namespaced: true, + ...nestStore } }, plugins: [vuexPersistence.plugin] diff --git a/src/views/MapOverview.view.vue b/src/views/MapOverview.view.vue index c639109..052e9d9 100644 --- a/src/views/MapOverview.view.vue +++ b/src/views/MapOverview.view.vue @@ -19,12 +19,16 @@