diff --git a/ITPlanning/api/appointement.py b/ITPlanning/api/appointement.py index 3748738..b3f41be 100644 --- a/ITPlanning/api/appointement.py +++ b/ITPlanning/api/appointement.py @@ -1,7 +1,7 @@ from flask import request, jsonify from pony.orm import ObjectNotFound -from datetime import datetime -from ITPlanning.db import Appointment +from datetime import datetime, timedelta +from ITPlanning.db import Appointment, Planning, Service, Ticket, Customer from ITPlanning.api.exception import NotFound, GenericError from . import api @@ -17,77 +17,47 @@ def datefiltering(query): return query +def search_appointment(by=None, id=None): + rv = [] + if by == None and id is not None: + rv.append(Appointment[id].to_dict()) + else: + query = Appointment.select() + if by is not None: + query.filter(lambda a: getattr(a, by).id == id) + query = datefiltering(query) + for f in query: + rv.append(f.to_dict()) + if len(rv) == 0: + raise NotFound("Appointment") + return jsonify(rv) + + @api.route("/appointment") @api.route("/appointment/") @api.route("/appointment/") def get_appointment(id=None): - rv = None - if id: - rv = Appointment[id] - else: - query = Appointment.select() - query = datefiltering(query) - for f in query: - rv += query - if rv is None: - raise NotFound("Appointment") - return jsonify(rv) + return search_appointment(id=id) @api.route("/appointment/planning/") -def get_appointment_by_planning(id): - rv = None - if id is None: - raise GenericError("enter an ID") - query = Appointment.select(lambda a: a.planning.id == id) - query = datefiltering(query) - for f in query: - rv += f - if rv is None: - raise NotFound("Appointment") - return jsonify(rv) +def get_appointment_by_planning(id=None): + return search_appointment("planning", id) @api.route("/appointment/service/") def get_appointment_by_service(id): - rv = None - if id is None: - raise GenericError("enter an ID") - query = Appointment.select(lambda a: a.service.id == id) - query = datefiltering(query) - for f in query: - rv += f - if rv is None: - raise NotFound("Appointment") - return jsonify(rv) + return search_appointment("service", id) @api.route("/appointment/customer/") def get_appointment_by_customer(id): - rv = None - if id is None: - raise GenericError("enter an ID") - query = Appointment.select(lambda a: a.customer.id == id) - query = datefiltering(query) - for f in query: - rv += f - if rv is None: - raise NotFound("Appointment") - return jsonify(rv) + return search_appointment("customer", id) @api.route("/appointment/ticket/") def get_appointment_by_ticket(id): - rv = None - if id is None: - raise GenericError("enter an ID") - query = Appointment.select(lambda a: a.ticket.id == id) - query = datefiltering(query) - for f in query: - rv += f - if rv is None: - raise NotFound("Appointment") - return jsonify(rv) + return search_appointment("ticket", id) @api.route("/appointment", methods=["POST"]) diff --git a/tests/api/test_appointment.py b/tests/api/test_appointment.py index 57961c4..2d7d218 100644 --- a/tests/api/test_appointment.py +++ b/tests/api/test_appointment.py @@ -8,3 +8,58 @@ class AppointmentTestCase(APITestBase): def test_get_appointement(self): self.make_request("appointment/", 404) + self.make_request("appointment/1", 404) + current_date = datetime.now() + with db_session: + service = filldatabase.create_service() + customer = filldatabase.create_Customer() + planning = filldatabase.create_planning() + planning.services.add(service) + + end_datetime = current_date + timedelta( + minutes=service.service_category.duration + ) + + appointment = Appointment( + book_datetime=current_date, + start_datetime=current_date, + end_datetime=end_datetime, + is_unavaillable=False, + service=service, + planning=planning, + customer=customer, + ) + start_datetime2 = current_date + timedelta(days=2) + end_datetime2 = start_datetime2 + timedelta( + minutes=service.service_category.duration + ) + appointment2 = Appointment( + book_datetime=current_date, + start_datetime=start_datetime2, + end_datetime=end_datetime2, + is_unavaillable=False, + service=service, + planning=planning, + customer=customer, + ) + + req, data = self.make_request("appointment/", 200) + self.assertEqual(len(data), 2) + + req, data = self.make_request("appointment/1", 200) + self.assertEqual(len(data), 1) + + filteringdate = (current_date + timedelta(days=1)).strftime("%Y-%m-%dT%H%M") + + req, data = self.make_request("appointment/", 200, {"after": filteringdate}) + self.assertEqual(len(data), 1) + + req, data = self.make_request("appointment/", 200, {"before": filteringdate}) + self.assertEqual(len(data), 1) + + req, data = self.make_request("appointment/planning/", 404) + req, data = self.make_request("appointment/planning/1", 200) + req, data = self.make_request("appointment/service/1", 200) + req, data = self.make_request("appointment/customer/1", 200) + req, data = self.make_request("appointment/ticket/1", 200) +