コード例 #1
0
    def put(employee_id):
        """Update employee"""
        data = parser.parse_args()
        first_name = data['first_name']
        second_name = data['second_name']
        username = data['username']

        if first_name is None and second_name is None and username is None:
            api.abort(400, 'You need to specify minimum one parameter')

        employee = EmployeeModel.find_by_id(employee_id)
        if employee is not None:
            if first_name is not None:
                employee.first_name = first_name
            if second_name is not None:
                employee.second_name = second_name
            if username is not None:
                if EmployeeModel.find_by_username(username) is not None:
                    api.abort(400, f'Employee {username} already exist')
                employee.username = username
            try:
                employee.save_employee()
            except:
                api.abort(500, 'Failed to update employee')
            return employee
        api.abort(404, f'Employee {employee_id} does not exist')
コード例 #2
0
def test_find_by_username(app):
    with app.app_context():
        employee = EmployeeModel(first_name='test',
                                 second_name='test2',
                                 username='******')
        db.session.add(employee)
        db.session.commit()

        employee_db = EmployeeModel.find_by_username(employee.username)
        assert employee_db.first_name == employee.first_name
        assert employee_db.second_name == employee.second_name
        assert employee_db.username == employee.username
        assert employee_db.id == employee.id
コード例 #3
0
    def post(self):
        """Add new employee"""
        data = parser_required.parse_args()
        username = data['username']

        if EmployeeModel.find_by_username(username) is not None:
            api.abort(400, f'Employee {username} already exist')

        employee = EmployeeModel(**data)
        try:
            employee.save_employee()
        except:
            api.abort(500, 'Failed to add new employee')

        return employee