Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.77 KB | None | 0 0
  1. I'm working on a project where I need to get all the vacant classrooms for the day and basically filter out the ones being in use.
  2.  
  3. I get all the data from the school's own API with JSON response body that looks like this:
  4.  
  5. {
  6. "status": "success",
  7. "reservations": [
  8. {
  9. "id": "18935",
  10. "subject": "subjectName",
  11. "modifiedDate": "2017-04-27T06:04:51",
  12. "startDate": "2017-04-27T11:00:00",
  13. "endDate": "2017-04-27T13:00:00",
  14. "resources": [
  15. {
  16. "id": "50",
  17. "type": "room",
  18. "code": "A440.5",
  19. "parent": {
  20. "id": "2",
  21. "type": "building",
  22. "code": "A",
  23. "name": "buildingA"
  24. },
  25. "name": "A440.5"
  26. },
  27. {
  28. "id": "2995",
  29. "type": "realization",
  30. "code": "",
  31. "name": ""
  32. },
  33. {
  34. "id": "2267",
  35. "type": "student_group",
  36. "code": "",
  37. "name": ""
  38. }
  39. ],
  40. "description": ""
  41. },
  42. {
  43. "id": "20362",
  44. "subject": "subjectName",
  45. "modifiedDate": "2017-04-27T06:05:05",
  46. "startDate": "2017-04-27T11:00:00",
  47. "endDate": "2017-04-27T14:00:00",
  48. "resources": [
  49. {
  50. "id": "51",
  51. "type": "room",
  52. "code": "A450.1",
  53. "parent": {
  54. "id": "2",
  55. "type": "building",
  56. "code": "A",
  57. "name": "buildingA"
  58. },
  59. "name": "A450.1"
  60. },
  61. {
  62. "id": "2402",
  63. "type": "student_group",
  64. "code": "",
  65. "name": ""
  66. },
  67. {
  68. "id": "3064",
  69. "type": "realization",
  70. "code": "",
  71. "name": ""
  72. }
  73. ],
  74. "description": ""
  75. },
  76. {
  77. "id": "20237",
  78. "subject": "subjectName",
  79. "modifiedDate": "2017-04-27T06:05:05",
  80. "startDate": "2017-04-27T11:15:00",
  81. "endDate": "2017-04-27T13:00:00",
  82. "resources": [
  83. {
  84. "id": "45",
  85. "type": "room",
  86. "code": "A420.4",
  87. "parent": {
  88. "id": "2",
  89. "type": "building",
  90. "code": "A",
  91. "name": "buildingA"
  92. },
  93. "name": "A420.4"
  94. },
  95. {
  96. "id": "2433",
  97. "type": "student_group",
  98. "code": "",
  99. "name": ""
  100. },
  101. {
  102. "id": "3058",
  103. "type": "realization",
  104. "code": "",
  105. "name": ""
  106. }
  107. ],
  108. "description": ""
  109. },
  110. {
  111. "id": "20888",
  112. "subject": "subjectName",
  113. "modifiedDate": "2017-04-27T06:04:57",
  114. "startDate": "2017-04-27T13:15:00",
  115. "endDate": "2017-04-27T16:00:00",
  116. "resources": [
  117. {
  118. "id": "62",
  119. "type": "room",
  120. "code": "A520.5",
  121. "parent": {
  122. "id": "2",
  123. "type": "building",
  124. "code": "A",
  125. "name": "buildingA"
  126. },
  127. "name": "A520.5"
  128. },
  129. {
  130. "id": "3092",
  131. "type": "realization",
  132. "code": "",
  133. "name": ""
  134. },
  135. {
  136. "id": "2444",
  137. "type": "student_group",
  138. "code": "",
  139. "name": ""
  140. }
  141. ],
  142. "description": ""
  143. },
  144. {
  145. "id": "22586",
  146. "subject": "subjectName",
  147. "modifiedDate": "2017-04-27T06:04:48",
  148. "startDate": "2017-04-27T13:15:00",
  149. "endDate": "2017-04-27T17:00:00",
  150. "resources": [
  151. {
  152. "id": "52",
  153. "type": "room",
  154. "code": "A450.3",
  155. "parent": {
  156. "id": "2",
  157. "type": "building",
  158. "code": "A",
  159. "name": "buildingA"
  160. },
  161. "name": "A450.3"
  162. },
  163. {
  164. "id": "3004",
  165. "type": "realization",
  166. "code": "",
  167. "name": ""
  168. },
  169. {
  170. "id": "2294",
  171. "type": "student_group",
  172. "code": "",
  173. "name": ""
  174. },
  175. {
  176. "id": "525",
  177. "type": "student_group",
  178. "code": "",
  179. "name": ""
  180. }
  181. ],
  182. "description": ""
  183. },
  184. {
  185. "id": "18816",
  186. "subject": "subjectName",
  187. "modifiedDate": "2017-04-27T06:04:58",
  188. "startDate": "2017-04-27T13:15:00",
  189. "endDate": "2017-04-27T16:00:00",
  190. "resources": [
  191. {
  192. "id": "41",
  193. "type": "room",
  194. "code": "A340.1",
  195. "parent": {
  196. "id": "2",
  197. "type": "building",
  198. "code": "A",
  199. "name": "buildingA"
  200. },
  201. "name": "A340.1"
  202. },
  203. {
  204. "id": "2989",
  205. "type": "realization",
  206. "code": "",
  207. "name": ""
  208. },
  209. {
  210. "id": "795",
  211. "type": "student_group",
  212. "code": "",
  213. "name": ""
  214. },
  215. {
  216. "id": "599",
  217. "type": "student_group",
  218. "code": "",
  219. "name": ""
  220. }
  221. ],
  222. "description": ""
  223. },
  224. {
  225. "id": "20431",
  226. "subject": "subjectName",
  227. "modifiedDate": "2017-04-27T06:04:56",
  228. "startDate": "2017-04-27T13:15:00",
  229. "endDate": "2017-04-27T16:00:00",
  230. "resources": [
  231. {
  232. "id": "40",
  233. "type": "room",
  234. "code": "A320.7",
  235. "parent": {
  236. "id": "2",
  237. "type": "building",
  238. "code": "A",
  239. "name": "buildingA"
  240. },
  241. "name": "A320.7/8"
  242. },
  243. {
  244. "id": "2416",
  245. "type": "realization",
  246. "code": "",
  247. "name": ""
  248. },
  249. {
  250. "id": "2386",
  251. "type": "student_group",
  252. "code": "",
  253. "name": ""
  254. }
  255. ],
  256. "description": ""
  257. },
  258. {
  259. "id": "18588",
  260. "subject": "subjectName",
  261. "modifiedDate": "2017-04-27T06:04:49",
  262. "startDate": "2017-04-27T13:15:00",
  263. "endDate": "2017-04-27T16:00:00",
  264. "resources": [
  265. {
  266. "id": "25",
  267. "type": "room",
  268. "code": "A130.1",
  269. "parent": {
  270. "id": "2",
  271. "type": "building",
  272. "code": "A",
  273. "name": "buildingA"
  274. },
  275. "name": "A130.1"
  276. },
  277. {
  278. "id": "26",
  279. "type": "room",
  280. "code": "A130.3",
  281. "parent": {
  282. "id": "2",
  283. "type": "building",
  284. "code": "A",
  285. "name": "buildingA"
  286. },
  287. "name": "A130.3"
  288. },
  289. {
  290. "id": "2979",
  291. "type": "realization",
  292. "code": "",
  293. "name": ""
  294. },
  295. {
  296. "id": "582",
  297. "type": "student_group",
  298. "code": "",
  299. "name": ""
  300. }
  301. ],
  302. "description": ""
  303. },
  304. {
  305. "id": "18940",
  306. "subject": "subjectName",
  307. "modifiedDate": "2017-04-27T06:04:53",
  308. "startDate": "2017-04-27T13:15:00",
  309. "endDate": "2017-04-27T16:00:00",
  310. "resources": [
  311. {
  312. "id": "2996",
  313. "type": "realization",
  314. "code": "",
  315. "name": ""
  316. },
  317. {
  318. "id": "2267",
  319. "type": "student_group",
  320. "code": "",
  321. "name": ""
  322. },
  323. {
  324. "id": "2268",
  325. "type": "student_group",
  326. "code": "",
  327. "name": ""
  328. },
  329. {
  330. "id": "31",
  331. "type": "room",
  332. "code": "A210.2",
  333. "parent": {
  334. "id": "2",
  335. "type": "building",
  336. "code": "A",
  337. "name": "buildingA"
  338. },
  339. "name": "A210.2"
  340. }
  341. ],
  342. "description": ""
  343. },
  344. {
  345. "id": "12041",
  346. "subject": "subjectName",
  347. "modifiedDate": "2017-04-27T06:04:53",
  348. "startDate": "2017-04-27T14:15:00",
  349. "endDate": "2017-04-27T17:00:00",
  350. "resources": [
  351. {
  352. "id": "2510",
  353. "type": "realization",
  354. "code": "",
  355. "name": ""
  356. },
  357. {
  358. "id": "775",
  359. "type": "student_group",
  360. "code": "",
  361. "name": ""
  362. },
  363. {
  364. "id": "23",
  365. "type": "room",
  366. "code": "A520.7",
  367. "parent": {
  368. "id": "2",
  369. "type": "building",
  370. "code": "A",
  371. "name": "buildingA"
  372. },
  373. "name": "A520.7"
  374. }
  375. ],
  376. "description": ""
  377. },
  378. {
  379. "id": "24630",
  380. "subject": "subjectName",
  381. "modifiedDate": "2017-04-27T06:05:05",
  382. "startDate": "2017-04-27T14:15:00",
  383. "endDate": "2017-04-27T17:00:00",
  384. "resources": [
  385. {
  386. "id": "3277",
  387. "type": "realization",
  388. "code": "",
  389. "name": ""
  390. },
  391. {
  392. "id": "42",
  393. "type": "room",
  394. "code": "A340.2",
  395. "parent": {
  396. "id": "2",
  397. "type": "building",
  398. "code": "A",
  399. "name": "buildingA"
  400. },
  401. "name": "A340.2"
  402. }
  403. ],
  404. "description": ""
  405. },
  406. {
  407. "id": "27205",
  408. "subject": "subjectName",
  409. "modifiedDate": "2017-04-27T06:05:07",
  410. "startDate": "2017-04-27T14:15:00",
  411. "endDate": "2017-04-27T17:00:00",
  412. "resources": [
  413. {
  414. "id": "35",
  415. "type": "room",
  416. "code": "A240.2",
  417. "parent": {
  418. "id": "2",
  419. "type": "building",
  420. "code": "A",
  421. "name": "buildingA"
  422. },
  423. "name": "A240.2"
  424. },
  425. {
  426. "id": "775",
  427. "type": "student_group",
  428. "code": "",
  429. "name": ""
  430. },
  431. {
  432. "id": "3384",
  433. "type": "realization",
  434. "code": "",
  435. "name": ""
  436. }
  437. ],
  438. "description": ""
  439. },
  440. {
  441. "id": "25917",
  442. "subject": "subjectName",
  443. "modifiedDate": "2017-04-27T06:05:00",
  444. "startDate": "2017-04-27T15:15:00",
  445. "endDate": "2017-04-27T16:00:00",
  446. "resources": [
  447. {
  448. "id": "36",
  449. "type": "room",
  450. "code": "A240.4",
  451. "parent": {
  452. "id": "2",
  453. "type": "building",
  454. "code": "A",
  455. "name": "buildingA"
  456. },
  457. "name": "A240.4"
  458. },
  459. {
  460. "id": "593",
  461. "type": "realization",
  462. "code": "",
  463. "name": ""
  464. },
  465. {
  466. "id": "595",
  467. "type": "student_group",
  468. "code": "",
  469. "name": ""
  470. },
  471. {
  472. "id": "596",
  473. "type": "student_group",
  474. "code": "",
  475. "name": ""
  476. },
  477. {
  478. "id": "313",
  479. "type": "student_group",
  480. "code": "",
  481. "name": ""
  482. }
  483. ],
  484. "description": ""
  485. },
  486. {
  487. "id": "21932",
  488. "subject": "subjectName",
  489. "modifiedDate": "2017-04-27T06:05:06",
  490. "startDate": "2017-04-27T16:00:00",
  491. "endDate": "2017-04-27T18:00:00",
  492. "resources": [
  493. {
  494. "id": "43",
  495. "type": "room",
  496. "code": "A350.1",
  497. "parent": {
  498. "id": "2",
  499. "type": "building",
  500. "code": "A",
  501. "name": "buildingA"
  502. },
  503. "name": "A350.1"
  504. },
  505. {
  506. "id": "2464",
  507. "type": "student_group",
  508. "code": "",
  509. "name": ""
  510. },
  511. {
  512. "id": "2747",
  513. "type": "student_group",
  514. "code": "",
  515. "name": ""
  516. },
  517. {
  518. "id": "199",
  519. "type": "student_group",
  520. "code": "",
  521. "name": ""
  522. },
  523. {
  524. "id": "2470",
  525. "type": "student_group",
  526. "code": "",
  527. "name": ""
  528. },
  529. {
  530. "id": "2471",
  531. "type": "student_group",
  532. "code": "",
  533. "name": ""
  534. },
  535. {
  536. "id": "2444",
  537. "type": "student_group",
  538. "code": "",
  539. "name": ""
  540. },
  541. {
  542. "id": "2498",
  543. "type": "student_group",
  544. "code": "",
  545. "name": ""
  546. },
  547. {
  548. "id": "2481",
  549. "type": "student_group",
  550. "code": "",
  551. "name": ""
  552. },
  553. {
  554. "id": "2482",
  555. "type": "student_group",
  556. "code": "",
  557. "name": ""
  558. },
  559. {
  560. "id": "2748",
  561. "type": "student_group",
  562. "code": "",
  563. "name": ""
  564. },
  565. {
  566. "id": "2577",
  567. "type": "student_group",
  568. "code": "",
  569. "name": ""
  570. },
  571. {
  572. "id": "2555",
  573. "type": "student_group",
  574. "code": "",
  575. "name": ""
  576. },
  577. {
  578. "id": "246",
  579. "type": "student_group",
  580. "code": "",
  581. "name": ""
  582. },
  583. {
  584. "id": "2558",
  585. "type": "student_group",
  586. "code": "",
  587. "name": ""
  588. },
  589. {
  590. "id": "2559",
  591. "type": "student_group",
  592. "code": "",
  593. "name": ""
  594. },
  595. {
  596. "id": "305",
  597. "type": "student_group",
  598. "code": "",
  599. "name": ""
  600. }
  601. ],
  602. "description": ""
  603. }
  604. ]
  605. }
  606.  
  607. In order to get the JSON response I need to put `startDate` (the moment user searches the vacant rooms, in this situation it is: `2017-04-27T10:55`) and `endDate` (set to end of the day, `2017-04-27T22:00`). The result for this query is the JSON response above.
  608.  
  609. The problem is that the API I'm using doesn't contain any data for the vacant rooms but only for the ones booked for certain times so I made a list of all the rooms in the building and compared it to the booked ones to filter them out:
  610.  
  611. var rooms = ['A120.3', 'A130.1', 'A130.3', 'A140.1', 'A140.2', 'A140.4', 'A250.1', 'A240.4', 'A240.2', 'A220.5', 'A220.3',
  612. 'A220.1', 'A210.2', 'A320.2', 'A320.6', 'A320.7', 'A320.8', 'A340.1', 'A340.2', 'A350.1', 'A350.3', 'A440.5', 'A450.3','A450.1',
  613. 'A440.4', 'A440.2', 'A420.6', 'A420.5', 'A420.4', 'A420.2', 'A510.2', 'A520.5', 'A510.4', 'A520.6', 'A520.7','A540.1', 'A540.2'];
  614.  
  615.  
  616. var data = JSON.parse(responseText);
  617. var booking = Object.create(null);
  618. var free;
  619.  
  620. data.reservations.forEach(function (reservation) {
  621. reservation.resources.some(function (resource) {
  622. if (resource.type === 'room') {
  623. booking[resource.code] = booking[resource.code] || [];
  624. booking[resource.code].push({ startDate: reservation.startDate, endDate: reservation.endDate });
  625. return true;
  626. }
  627. });
  628. });
  629.  
  630. free = rooms.filter(function (a) {
  631. return !booking[a];
  632. });
  633.  
  634. console.log(free);
  635.  
  636. Results:
  637.  
  638. A210.3
  639. A220.5
  640. A320.2
  641. A520.6
  642. A510.4
  643.  
  644. This only returns the ones that are not being used at all during the day but I need to get the ones that are vacant for hour or two.
  645.  
  646. For this booked class for example:
  647.  
  648. "startDate": "2017-04-27T13:15:00",
  649. "endDate": "2017-04-27T16:00:00",
  650.  
  651. "type": "room",
  652. "code": "A520.5"
  653.  
  654. for this I would need to print out:
  655.  
  656. A520.5 - 2 hours 20 minutes
  657.  
  658.  
  659. So I need to get the room / time from the `startDate` of the search (**2017-04-27T10:55**) and compare it to the `startDate` of the booked room (**2017-04-27T13:15:00**) to get the time remaining for that room to be vacant.
  660.  
  661. Thanks in advance.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement