public async Task <AircraftDto> Handle(Command request, CancellationToken cancellationToken) { if (await _context.Aircraft.Where(x => x.AircraftName == request.AircraftName).AnyAsync()) { throw new RestException(HttpStatusCode.BadRequest, new { AircraftName = "Aircraft name already exist" }); } string uploads = Path.Combine(_hostEnvironment.WebRootPath, "api", "uploads"); string filePath = Path.Combine(uploads, request.File.FileName); using (Stream fileStream = new FileStream(filePath, FileMode.Create)) { await request.File.CopyToAsync(fileStream); } var aircraftId = _context.Aircraft.Max(el => el.Id) + 1; var categoryId = _context.Category.Max(el => el.Id) + 1; var typeId = _context.Type.Max(el => el.Id) + 1; var categories = JsonSerializer.Deserialize <CategoryDto>(request.Categories, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); var types = JsonSerializer.Deserialize <TypeDto>(request.Types, new JsonSerializerOptions { PropertyNameCaseInsensitive = true }); foreach (var category in categories.Categories) { if (!_context.Category.Any(c => c.CategoryName == category)) { await _context.AircraftCategory.AddAsync(new AircraftCategory { AircraftId = aircraftId, CategoryId = categoryId }); await _context.Category.AddAsync(new Category { Id = categoryId, CategoryName = category, }); categoryId++; } else { var existingCategory = await _context.Category.Where(c => c.CategoryName == category).FirstOrDefaultAsync(); var existingCategoryId = existingCategory.Id; await _context.AircraftCategory.AddAsync(new AircraftCategory { AircraftId = aircraftId, CategoryId = existingCategoryId }); } } foreach (var type in types.Types) { if (!_context.Type.Any(c => c.TypeName == type)) { await _context.AircraftType.AddAsync(new AircraftType { AircraftId = aircraftId, TypeId = typeId }); await _context.Type.AddAsync(new Domain.Type { Id = typeId, TypeName = type, }); typeId++; } else { var existingType = await _context.Type.Where(t => t.TypeName == type).FirstOrDefaultAsync(); var existingTypeId = existingType.Id; await _context.AircraftType.AddAsync(new AircraftType { AircraftId = aircraftId, TypeId = existingTypeId }); } } var image = new Image { AircraftId = aircraftId, ImageUrl = "uploads/" + request.File.FileName }; await _context.Images.AddAsync(image); var aircraft = new Aircraft { Id = aircraftId, AircraftName = request.AircraftName, Description = request.Description, YearInService = request.YearInService, Country = request.Country, }; await _context.Aircraft.AddAsync(aircraft); await CreateNotification(_userAccessor.GetCurrentUsername(), aircraft.AircraftName); var success = await _context.SaveChangesAsync() > 0; if (success) { var retAircraft = await _context.Aircraft.FindAsync(new object[] { aircraftId }, cancellationToken); var aircraftDto = _mapper.Map <Aircraft, AircraftDto>(retAircraft); var addDocInIndexRes = await _elasticClient.IndexDocumentAsync(aircraftDto); if (addDocInIndexRes.OriginalException != null) { throw addDocInIndexRes.OriginalException; } return(aircraftDto); } throw new Exception("Problem saving changes"); }