public void UpdateHolidays(Employee employee)
 {
     employeeRepository.UpdateHolidays(employee);
 }
 public void Create(Employee employee)
 {
     employeeRepository.Create(employee);
 }
 public void UpdateEmployeeAndHoliday(Employee employeeAndHoliday)
 {
     employeeDataService.UpdateHolidays(employeeAndHoliday);
 }
예제 #4
0
        public async Task<IHttpActionResult> RegisterUserAndEmployee(
            RegisterUserAndEmployeeViewModel userAndEmployeeViewModel)
        {
            userAndEmployeeViewModel.StaffId = Guid.NewGuid();
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var user = new IdentityEmployee
            {
                StaffId = userAndEmployeeViewModel.StaffId,
                UserName = userAndEmployeeViewModel.EmailAddress
            };

            var result = await userManager.CreateAsync(user, "123123");
            var errorResult = GetErrorResult(result);

            if (errorResult != null)
            {
                return errorResult;
            }
            var userSetRoleViewModel = new UserSetRoleViewModel();
            userSetRoleViewModel.IdentityId = user.Id;
            userSetRoleViewModel.RoleName = "user";
            await UserSetRole(userSetRoleViewModel);

            var employee = new Employee
            {
                FirstName = userAndEmployeeViewModel.FirstName,
                LastName = userAndEmployeeViewModel.LastName,
                HolidayAllowance = 25,
                RemainingAllowance = 25,
                HolidayBookings = new List<EmployeeHolidayBooking>(),
                StaffId = userAndEmployeeViewModel.StaffId,
                TeamId = userAndEmployeeViewModel.TeamId
            };
            employeeDataService.Create(employee);

            return Ok();
        }
        public void UpdateHolidays(Employee employee)
        {
            var employeeInDb = context.Employees.Find(employee.StaffId);

            context.Entry(employeeInDb).CurrentValues.SetValues(employee);

            foreach (var holidayBooking in employeeInDb.HolidayBookings.ToList())
            {
                if (!employee.HolidayBookings.Any(h => h.HolidayId == holidayBooking.HolidayId))
                {
                    context.Entry(
                        employeeInDb.HolidayBookings.SingleOrDefault(h => h.HolidayId == holidayBooking.HolidayId))
                        .State = EntityState.Deleted;
                    employeeInDb.HolidayBookings.Remove(holidayBooking);
                }
            }

            foreach (var holidayBooking in employee.HolidayBookings)
            {
                var holidayBookingInDb =
                    employeeInDb.HolidayBookings.SingleOrDefault(h => h.HolidayId == holidayBooking.HolidayId);
                if (holidayBookingInDb != null)
                {
                    context.Entry(holidayBookingInDb).CurrentValues.SetValues(holidayBooking);
                }
                else
                {
                    context.HolidayBookings.Attach(holidayBooking);
                    employeeInDb.HolidayBookings.Add(holidayBooking);
                    context.Entry(
                        employeeInDb.HolidayBookings.SingleOrDefault(h => h.HolidayId == holidayBooking.HolidayId))
                        .State = EntityState.Added;
                    context.SaveChanges();
                }
            }
            context.SaveChanges();
        }
 public void Create(Employee employee)
 {
     context.Employees.Add(employee);
     context.SaveChanges();
 }