Example #1
0
 def post(self, dep_id):
     """
     This method is called when POST request is sent to url
     "/api/v1/departments/<int:dep_id>/employees" with json data.
     Creates a new employee entry in database with department_id=dep_id.
     :return: if valid data provided returns the created entry serialized
     to json, status code 201, if invalid data - returns the error massage
     in json format, status code 400. If invalid department id specified in url -
     returns not-found error message, status code 404.
     """
     department = DepartmentServices.get_by_id(dep_id)
     if not department:
         return {"message": f"Department with id {dep_id} not found"}, 404
     json_data = request.get_json(force=True)
     try:
         data = self.employee_schema.load(json_data,
                                          partial=["department_id"])
     except ValidationError as e:
         return {"message": e.messages}, 400
     data["department_id"] = dep_id
     try:
         new_employee = EmployeeServices.create(data)
     except IntegrityError:
         return {"message": "Not valid department id"}, 400
     return self.employee_schema.dump(new_employee), 201
Example #2
0
 def post(self):
     """
     This method is called when POST request is sent to url "/api/v1/employees"
      with json data. Creates a new employee entry in database.
     :return: if valid data provided returns the created entry serialized
     to json, status code 201, if invalid data - returns the error massage
     in json format, status code 400.
     """
     json_data = request.get_json(force=True)
     try:
         data = self.employee_schema.load(json_data)
     except ValidationError as e:
         return e.messages, 400
     try:
         new_employee = EmployeeServices.create(data)
     except IntegrityError:
         return {"message": "Not valid department id"}, 400
     return self.employee_schema.dump(new_employee), 201