public void Add(VacationDto dto) { Vacation vacation = _mapper.Map <Vacation>(dto); SetExistingEmployeeByName(vacation, dto); _context.Vacations.Add(vacation); }
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; }
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); }
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); }
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; } } }