public async Task <IActionResult> PutDevice(int id, [FromBody] DeviceForCreationDTO deviceForCreationDTO) { var device = _mapper.Map <Device>(deviceForCreationDTO); device.Id = id; // Get the updated device's EmployeeDevice List var updatedDeviceEmployeeDevices = _unitOfWork.Repository <EmployeeDevice>() .Find(new EmployeeDevicesOfADeviceSpec(id)) .ToList(); // Remove the list _unitOfWork.Repository <EmployeeDevice>() .RemoveRange(updatedDeviceEmployeeDevices); _unitOfWork.Repository <Device>().Update(device); try { await _unitOfWork.Complete(); } catch (DbUpdateConcurrencyException) { if (!await _unitOfWork.Repository <Device>().Contains(d => device.Id == id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <Device> > PostDevice([FromBody] DeviceForCreationDTO deviceForCreationDTO) { var device = _mapper.Map <Device>(deviceForCreationDTO); await _unitOfWork.Repository <Device>().Add(device); await _unitOfWork.Complete(); // fetch created entity with child values var deviceFromDb = _unitOfWork.Repository <Device>().Find( new DevicesWithCategoryAndMakerAndEmployeeDevices(device.Id) ).SingleOrDefault(); // mapp to a returnable obj var deviceToReturn = _mapper.Map <DeviceToReturnDto>(deviceFromDb); return(new CreatedAtRouteResult(nameof(GetDevice), new { deviceToReturn.Id }, deviceToReturn)); }
private List <EmployeeDevice> MapEmployeesDevices( DeviceForCreationDTO deviceForCreationDTO, Device device ) { var result = new List <EmployeeDevice>(); foreach (var id in deviceForCreationDTO.EmployeesIds) { result.Add( new EmployeeDevice() { DeviceId = device.Id, EmployeeId = id, CheckOutDate = DateTime.Now }); } return(result); }