コード例 #1
0
        public void Add(VacationDto dto)
        {
            Vacation vacation = _mapper.Map <Vacation>(dto);

            SetExistingEmployeeByName(vacation, dto);
            _context.Vacations.Add(vacation);
        }
コード例 #2
0
        public void Remove(VacationDto dto)
        {
            if (DeleteEmployeeIfEmpty(dto))
            {
                return;
            }

            Vacation vacation = new Vacation {
                ID = dto.ID, RowVersion = dto.RowVersion
            };

            _context.Vacations.Attach(vacation);
            _context.Entry(vacation).State = EntityState.Deleted;
        }
コード例 #3
0
        private bool DeleteEmployeeIfEmpty(VacationDto dto)
        {
            if (_context.Vacations.Any(x => x.EmployeeID == dto.EmployeeID && x.ID != dto.ID))
            {
                return(false);
            }

            Employee employee = new Employee {
                ID = dto.EmployeeID, RowVersion = dto.EmployeeRowVersion
            };

            _context.Employees.Attach(employee);
            _context.Entry(employee).State = EntityState.Deleted;
            return(true);
        }
コード例 #4
0
        private bool SetExistingEmployeeByName(Vacation vacation, VacationDto dto)
        {
            Employee employee = _context.Employees.SingleOrDefault(x => x.ID != dto.EmployeeID && x.FirstName == dto.EmployeeFirstName && x.LastName == dto.EmployeeLastName);

            if (employee == null)
            {
                return(false);
            }

            _context.Entry(vacation.Employee).State = EntityState.Detached;
            vacation.EmployeeID = employee.ID;
            vacation.Employee   = employee;
            dto.EmployeeOwnerID = employee.OwnerID;
            return(true);
        }
コード例 #5
0
        public void Edit(VacationDto dto)
        {
            Vacation vacation = _mapper.Map <Vacation>(dto);

            _context.Vacations.Attach(vacation);
            _context.Entry(vacation).State = EntityState.Modified;

            if (_context.Employees.Any(x => x.ID == dto.EmployeeID && (x.FirstName != dto.EmployeeFirstName || x.LastName != dto.EmployeeLastName)))
            {
                if (SetExistingEmployeeByName(vacation, dto))
                {
                    DeleteEmployeeIfEmpty(dto);
                }
                else
                {
                    _context.Entry(vacation.Employee).State = EntityState.Modified;
                }
            }
        }