Пример #1
0
    public ErrorAppRemax Add(Employee employee)
    {
        ErrorAppRemax err = new ErrorAppRemax();

        if (_list.ContainsKey(employee.EmployeeId))
        {
            err.ErrorCode    = -1;
            err.ErrorMessage = "There is another Employee with this same Employee Id.";
            return(err);
        }

        // Checking whether there is another Employee with the same Username.
        if (FindByUsername(employee.Username) != null)
        {
            err.ErrorCode    = -1;
            err.ErrorMessage = "There is another Employee with this same Username.";
            return(err);
        }

        // result is the number of affected rows.
        // The initial password is equal to the Username.
        int result = DataSource.AddEmployee(employee.FirstName, employee.LastName, employee.BirthDate, employee.Email, employee.Gender.GenderId,
                                            employee.Phone, employee.City.CityId, (int)employee.Position, employee.Salary, employee.Username, employee.Username);

        // Any error occurred?
        if (DataSource.ErrorCode() != 0)
        {
            err.ErrorCode    = DataSource.ErrorCode();
            err.ErrorMessage = DataSource.ErrorMessage();
            return(err);
        }

        if (result == 1)
        {
            // If the adding works fine only one row should be affected.

            // Reloading _list.
            LoadEmployees();

            return(err);
        }
        else
        {
            err.ErrorCode    = -1;
            err.ErrorMessage = "The add operation was not successful (" + result.ToString() + " rows affected). Please check the database.";
            return(err);
        }
    }