public async Task <IActionResult> AssignCarToUserAsync([FromBody] InUpdateToolAssignmentDTO toolAssignmentDTO) { var car = await _repositoryManager.CarRepository .GetAsync(toolAssignmentDTO.CarId, true, false); var toolExist = await _repositoryManager.ToolRepository .ExistAsync(toolAssignmentDTO.ToolId); if (car == null || !toolExist) { return(NotFound()); } if (!car.Tools.Any(ct => ct.CarId == toolAssignmentDTO.CarId && ct.ToolId == toolAssignmentDTO.ToolId)) { car.Tools.Add(toolAssignmentDTO); } else { car.Tools.First(ct => ct.CarId == toolAssignmentDTO.CarId && ct.ToolId == toolAssignmentDTO.ToolId).Activate(toolAssignmentDTO); } _repositoryManager.CarRepository.Update(car); var assignments = await _repositoryManager.CarRepository .GetCarToolsAsync(toolAssignmentDTO.ToolId, true, false); assignments.ToList().ForEach(a => a.Unassign()); await _repositoryManager.SaveAsync(); return(Ok()); }
public static void Activate(this CarTool tool, InUpdateToolAssignmentDTO toolAssignmentDTO) { tool.StartOfAssignment = DateTimeOffset .FromUnixTimeSeconds(toolAssignmentDTO.DateOfAssignment).UtcDateTime; tool.Active = true; tool.Deleted = false; tool.EndOfAssignment = null; }
public static void Add(this ICollection <CarTool> tools, InUpdateToolAssignmentDTO toolAssignmentDTO) { tools.Add(new CarTool { ToolId = toolAssignmentDTO.ToolId, StartOfAssignment = DateTimeOffset .FromUnixTimeSeconds(toolAssignmentDTO.DateOfAssignment).UtcDateTime, Active = true }); }