Beispiel #1
0
 /// <summary>
 ///     Adding or Updating Employee in a database
 /// </summary>
 /// <param name="employee">Business Object Employee</param>
 /// <returns>True if succeeded and false otherwise</returns>
 public async Task <bool> SaveEmployee(DtoEmployee employee)
 {
     try
     {
         using (var data = Context)
         {
             var e =
                 await
                     (from item in data.Employee where employee.Id == item.id select item).FirstOrDefaultAsync();
             // Updating Employee
             if (e != null)
             {
                 e.name                   = employee.Name;
                 e.surname                = employee.Surname;
                 e.street                 = employee.Street;
                 e.postalCode             = employee.PostalCode;
                 e.phoneNumber            = employee.PhoneNumber;
                 e.personalIdentityNumber = employee.PersonalIdentityNumber;
                 e.identityCardNumber     = employee.IdentityCardNumber;
                 e.icePhoneNumber         = employee.IcePhoneNumber;
                 e.houseNumber            = employee.HouseNumber;
                 e.email                  = employee.Email;
                 e.city                   = employee.City;
                 e.apartmentNumber        = employee.ApartmentNumber;
                 e.description            = employee.Description;
                 e.positionID             = employee.Position.Id;
                 e.supervisorID           = employee.SupervisorId;
                 e.password               = AuthenticationLogic.HashPassword(employee.Password, employee);
                 e.SportType.Clear();
                 foreach (var item in employee.SportTypes)
                 {
                     var st = data.SportType.First(s => s.id == item.Id);
                     e.SportType.Add(st);
                 }
             }
             // Adding new Employee
             else
             {
                 if (await(from item in data.Employee
                           where
                           employee.PersonalIdentityNumber == item.personalIdentityNumber ||
                           employee.IdentityCardNumber == item.identityCardNumber
                           select item).AnyAsync())
                 {
                     return(false);
                 }
                 var emp = EmployeeConverter.DtoToDataAccess(employee);
                 foreach (var item in employee.SportTypes)
                 {
                     var st = data.SportType.First(s => s.id == item.Id);
                     emp.SportType.Add(st);
                 }
                 data.Employee.Add(emp);
             }
             await data.SaveChangesAsync();
         }
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }