public async Task <IActionResult> CreateSheetAsync([Required][FromBody] InspectionSheetDto dto) { try { _logger.LogInformation("try to create inspection sheet"); if (!_service.IsValidInspectionSheet(dto)) { return(BadRequest()); } var result = await _service.CreateInspectionSheetAsync(dto); return(CreatedAtAction( nameof(GetInspectionSheet), new { id = result.SheetId }, result )); } catch (Exception ex) { _logger.LogError(ex.Message); return(StatusCode( StatusCodes.Status500InternalServerError, "Internal Sever Error" )); } }
/// <inheritdoc/> public async Task <InspectionSheetDto> CreateInspectionSheetAsync(InspectionSheetDto dto) { var entity = _mapper.Map <InspectionSheet>(dto); await _context.InspectionSheets.AddAsync(entity); await _context.SaveChangesAsync(); var result = _mapper.Map <InspectionSheetDto>(entity); return(result); }
public void MapToInspectionSheetExportDtoCorrectly() { var expect = new InspectionSheetDto { SheetId = 11, SheetName = "sheet name", }; var mapper = CreateMapper(); var actual = mapper.Map <InspectionSheetExportDto>(expect); Assert.Equal(expect.SheetId.ToString(), actual.SheetId); Assert.Equal(expect.SheetName, actual.SheetName); }
private byte[] ConvertInspectionSheetDtoToJson(InspectionSheetDto sheet) { var options = new JsonSerializerOptions(); options.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); var dto = _mapper.Map <InspectionSheetExportDto>(sheet); dto.InspectionType = _repository.InspectionTypeName(sheet.InspectionTypeId); dto.InspectionGroup = _repository.InspectionGroupName(sheet.InspectionGroupId); for (var i = 0; i < dto.Equipments.Count; i++) { var isLastEquipment = (i == dto.Equipments.Count - 1); foreach (var(item, index) in dto.Equipments[i].InspectionItems.Select((x, j) => (x, j))) { var isLastInspectionItem = (index == dto.Equipments[i].InspectionItems.Count - 1); item.InspectionItemId = index; if (isLastInspectionItem) { if (!isLastEquipment) { item.Transitions.Add(new TransitionExportDto { SheetId = dto.SheetId, EquipmentId = dto.Equipments[i + 1].EquipmentId, InspectionItemId = 0, }); } } else { item.Transitions.Add(new TransitionExportDto { SheetId = dto.SheetId, EquipmentId = dto.Equipments[i].EquipmentId, InspectionItemId = item.InspectionItemId + 1, }); } } } var json = JsonSerializer.Serialize( new InspectionExportDto { Sheet = dto, }, options ); var data = System.Text.Encoding.UTF8.GetBytes(json); return(data); }
/// <inheritdoc/> public bool IsValidInspectionSheet(InspectionSheetDto dto) { if (!_context.InspectionGroups.Any(x => x.InspectionGroupId == dto.InspectionGroupId)) { return(false); } if (!_context.InspectionTypes.Any(x => x.InspectionTypeId == dto.InspectionTypeId)) { return(false); } return(true); }
/// <inheritdoc/> public async Task <InspectionSheetDto> UpdateInspectionSheetAsync(InspectionSheetDto dto) { var entity = await _context.InspectionSheets .Where(x => x.SheetId == dto.SheetId) .Include(x => x.Equipments) .ThenInclude(x => x.InspectionItems) .ThenInclude(x => x.Choices) .AsSplitQuery() .SingleAsync(); if (entity is not null) { _mapper.Map(dto, entity); _context.InspectionSheets.Update(entity); await _context.SaveChangesAsync(); } var result = _mapper.Map <InspectionSheetDto>(entity); return(result); }
public async Task <IActionResult> UpdateInspectionSheetAsync( [Required][FromRoute] int id, [Required][FromBody] InspectionSheetDto dto) { try { _logger.LogInformation($"try to update inspection sheet {id}"); if (id != dto.SheetId) { return(BadRequest("Invalid ID supplied")); } if (!_service.IsValidInspectionSheet(dto)) { return(BadRequest()); } if (!_service.InspectionSheetExists(id)) { return(NotFound($"Sheet with Id = {dto.SheetId} not found")); } var result = await _service.UpdateInspectionSheetAsync(dto); return(CreatedAtAction( nameof(GetInspectionSheet), new { sheetId = result.SheetId }, result )); } catch (Exception ex) { _logger.LogError(ex.Message); return(StatusCode( StatusCodes.Status500InternalServerError, "Internal Sever Error" )); } }
public void MapInspectionSheetDtoToEntityCorrectly() { var dto = new InspectionSheetDto { SheetId = 1, SheetName = "sheet name", InspectionTypeId = 10, InspectionGroupId = 20, Equipments = new List <EquipmentDto> { new EquipmentDto { EquipmentId = 2, EquipmentName = "equipment", InspectionItems = new List <InspectionItemDto> { new InspectionItemDto { InspectionItemId = 3, InspectionContent = "inspection", InputTypeId = 4, Choices = new List <ChoiceDto> { new ChoiceDto { ChoiceId = 5, Description = "choice" } } } } } } }; var mapper = CreateMapper(); var actual = mapper.Map <InspectionSheet>(dto); Assert.Equal(dto.SheetId, actual.SheetId); Assert.Equal(dto.SheetName, actual.SheetName); Assert.Equal(dto.InspectionTypeId, actual.InspectionTypeId); Assert.Equal(dto.InspectionGroupId, actual.InspectionGroupId); var expectEquipment = dto.Equipments.First(); var actualEquipment = actual.Equipments.First(); Assert.Equal(expectEquipment.EquipmentId, actualEquipment.EquipmentId); Assert.Equal(expectEquipment.EquipmentName, actualEquipment.EquipmentName); var expectInspectionItem = expectEquipment.InspectionItems.First(); var actualInspectionItem = actualEquipment.InspectionItems.First(); Assert.Equal(expectInspectionItem.InspectionItemId, actualInspectionItem.InspectionItemId); Assert.Equal(expectInspectionItem.InspectionContent, actualInspectionItem.InspectionContent); Assert.Equal(expectInspectionItem.InputTypeId, actualInspectionItem.InputTypeId); var expectChoice = expectInspectionItem.Choices.First(); var actualChoice = actualInspectionItem.Choices.First(); Assert.Equal(expectChoice.ChoiceId, actualChoice.ChoiceId); Assert.Equal(expectChoice.Description, actualChoice.Description); }
/// <inheritdoc/> public async Task <InspectionSheetDto> UpdateInspectionSheetAsync(InspectionSheetDto dto) => await _repository.UpdateInspectionSheetAsync(dto);
/// <inheritdoc/> public bool IsValidInspectionSheet(InspectionSheetDto dto) => _repository.IsValidInspectionSheet(dto);