public async Task AssignRepairTypeToRepairAsync(AssignRepairTypeToRepairRequest request, CancellationToken cancellationToken) { var validator = new IdValidator(); var listValidator = new ListIdsValidator(); await validator.ValidateAndThrowAsync(request.RepairId, null, cancellationToken); await listValidator.ValidateAndThrowAsync(request.RepairTypeIds, null, cancellationToken); var repairResult = await _repairRepository.GetByIdAsync(request.RepairId, cancellationToken); if (repairResult == null) { throw new ServiceException(ErrorCodes.RepairWithGivenIdNotFound, $"Repair with provided id {request.RepairId} doesn't exist"); } foreach (var repairTypeId in request.RepairTypeIds) { var repairTypeResult = await _repairTypeRepository.GetByIdAsync(repairTypeId, cancellationToken); if (repairTypeResult == null) { throw new ServiceException(ErrorCodes.RepairTypeWithGivenIdNotFound, $"Repair type with provided id {repairTypeId} doesn't exist"); } if (await _requiredRepairTypeRepository.AnyAsync(x => x.RepairId == request.RepairId && x.RepairTypeId == repairTypeId, cancellationToken)) { throw new ServiceException(ErrorCodes.RepairTypeAlreadyAssignToRepair, $"Repair type with provided id {repairTypeId} already assign to given repair with id {request.RepairId}"); } } List <RequiredRepairType> repairTypesToAssign = new List <RequiredRepairType>(); foreach (var repairTypeId in request.RepairTypeIds) { RequiredRepairType repairType = new RequiredRepairType() { RepairId = request.RepairId, RepairTypeId = repairTypeId }; repairTypesToAssign.Add(repairType); } await _requiredRepairTypeRepository.AddRangeAsync(repairTypesToAssign, cancellationToken); }
public async Task <IActionResult> AssignRepairTypeToRepair([FromBody] AssignRepairTypeToRepairRequest request, CancellationToken cancellationToken) { await _requiredRepairTypeService.AssignRepairTypeToRepairAsync(request, cancellationToken); return(Ok()); }