def add_employee(body): # noqa: E501 """Add a new employee to the system. Role write:employees must be granted # noqa: E501 :param body: Employee object that needs to be added to the system :type body: dict | bytes :rtype: Employee """ if connexion.request.is_json: body = Employee.from_dict(connexion.request.get_json()) # noqa: E501 # check email already exists found = dao.find_by_email(body.email) if found is not None: return ErrorApiResponse.EmployeeEmailExistError(body.email), 409 # check for team number team = team_dao.get(body.team_id) if team is None: return ErrorApiResponse.TeamNotFoundError(id=body.team_id), 404 # persist new employee orm = Employee_orm( full_name=body.full_name, position=body.position, specialization=body.specialization if body.specialization else '', team_id=body.team_id, expert=body.expert, email=body.email) try: dao.persist(orm) return find_employee_by_email(body.email) except Exception as ex: return ErrorApiResponse.InternalServerError(ex, type='employee'), 500
def update_employee_by_id(employeeId, body): # noqa: E501 """Updates an employee in the system with form data. Role write:employees must be granted # noqa: E501 :param employeeId: ID of empoyee to return :type employeeId: int :param body: Employee object that needs to be added to the system :type body: dict | bytes :rtype: Employee """ found = dao.get(employeeId) if found is None: return ErrorApiResponse.EmployeeNotFoundError(id=employeeId), 404 if connexion.request.is_json: body = Employee.from_dict(connexion.request.get_json()) # noqa: E501 # check email already exists duplicate = dao.find_by_email(body.email) if duplicate is not None and duplicate.employee_id != body._employee_id: return ErrorApiResponse.EmployeeEmailExistError(body.email), 409 # check for team number team = team_dao.get(body.team_id) if team is None: return ErrorApiResponse.TeamNotFoundError(id=body.team_id), 404 found.full_name = body.full_name found.position = body.position found.specialization = body.specialization if body.specialization else '' found.team_id = body.team_id found.expert = body.expert found.email = body.email try: dao.persist(found) return get_employee_by_id(employeeId) except Exception as ex: return ErrorApiResponse.InternalServerError(ex, type='employee'), 500