EventsProvider.ts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import BaseProvider from '~/services/data/BaseProvider'
  2. import Address from "~/components/Ui/Search/Address.vue";
  3. import HydraParser from "~/services/data/HydraParser";
  4. const LOCATION_RADIUS = 20
  5. class PublicEventsProvider extends BaseProvider {
  6. protected normalize (e: any) : PublicEvent {
  7. e.address = {
  8. type: '',
  9. latitude: e.latitude,
  10. longitude: e.longitude,
  11. streetAddress: e.streetAddress,
  12. postalCode: e.postalCode,
  13. addressCity: e.city,
  14. country: ''
  15. } as Address
  16. delete e['@id']
  17. delete e['@type']
  18. delete e.latitude
  19. delete e.longitude
  20. delete e.streetAddress
  21. delete e.postalCode
  22. delete e.city
  23. return e
  24. }
  25. async getBy (
  26. name: string | null = null,
  27. organizationId: number | null = null,
  28. dateMin: string | null = null,
  29. dateMax: string | null = null,
  30. location: Coordinates | null = null,
  31. page: number = 1,
  32. itemsPerPage = 20
  33. ): Promise<HydraCollection<PublicEvent>> {
  34. const query = new URLSearchParams();
  35. if (name !== null) {
  36. query.append('name', name)
  37. }
  38. if (organizationId !== null) {
  39. query.append('organizationId', String(organizationId))
  40. }
  41. if (dateMin !== null && dateMin !== '') {
  42. query.append('datetimeEnd[after]', dateMin)
  43. }
  44. if (dateMax !== null && dateMax !== '') {
  45. query.append('datetimeEnd[before]', dateMax)
  46. }
  47. if (location !== null) {
  48. query.append('withinDistance', [location.latitude, location.longitude, LOCATION_RADIUS].join(','))
  49. }
  50. if (page !== null) {
  51. query.append('page', `${page}`)
  52. }
  53. if (itemsPerPage !== null) {
  54. query.append('itemsPerPage', `${itemsPerPage}`)
  55. }
  56. query.append('order[datetimeEnd]', 'asc')
  57. let url = `/api/public/events`
  58. if (query) {
  59. url += `?${query}`
  60. }
  61. return await this.get(url).then((res) => {
  62. return HydraParser.parseCollection(res, this.normalize)
  63. })
  64. }
  65. async getById (eventUuid: string): Promise<PublicEvent> {
  66. return await this.get(
  67. `/api/public/events/${eventUuid}`
  68. ).then((s) => {
  69. return this.normalize(s)
  70. })
  71. }
  72. }
  73. export default PublicEventsProvider