def delete(emp_id): """ This method is called when DELETE request is sent to url "/api/v1/employees/id" deletes the employee entry with specified id from database. :return: returns an empty response body and status code 204 if valid id specified. If invalid id specified - returns error message and status code 404. """ employee = EmployeeServices.get_by_id(emp_id) if not employee: return {"message": f"Employee with id {emp_id} not found"}, 404 EmployeeServices.delete(employee) return "", 204
def get(self, emp_id=None): """ This method is called when GET request is sent to "/api/v1/employees/[<int:id>]" url. :return: if "id" not specified the list of all employees in json format, status code 200. If id specified - the employee with the specified id serialized to json. If invalid id - error message and status code 404. """ if not emp_id: employees = EmployeeServices.get_all() return self.employee_schema.dump(employees, many=True), 200 employee = EmployeeServices.get_by_id(emp_id) if not employee: return {"message": f"Employee with id {emp_id} not found"}, 404 return self.employee_schema.dump(employee), 200
def put(self, emp_id): """ This method is called when PUT request is sent to url "/api/v1/employees/id" with json data, updates the employee entry with specified id in database. :return: if valid data provided returns the updated entry serialized to json, status code 200, if invalid data - returns the error massage in json format, status code 400. If invalid id specified - returns error message and status code 404. """ employee = EmployeeServices.get_by_id(emp_id) if not employee: return {"message": f"Employee with id {emp_id} not found"}, 404 json_data = request.get_json(force=True) try: data = self.employee_schema.load(json_data) except ValidationError as e: return e.messages, 400 try: employee = EmployeeServices.update(employee, data) except IntegrityError: return {"message": "Not valid department id"}, 400 return self.employee_schema.dump(employee), 200