public async Task <Maybe <string> > Handle(CreateTechSpecificationByVehicleIdCommand request, CancellationToken cancellationToken) { var vehicleIdFilter = Builders <Vehicle> .Filter.Eq(v => v.Id, new ObjectId(request.VehicleId)); var techSpecificationFilter = Builders <Vehicle> .Filter.ElemMatch( v => v.VehicleTechSpecifications, request.VehicleTechSpecificationDTO.ToBsonDocument()); var filter = Builders <Vehicle> .Filter.And(vehicleIdFilter, techSpecificationFilter); var vehicles = await sparePartsDbContext.Vehicles.FindAsync(filter); var vehicle = await vehicles.FirstOrDefaultAsync(); if (vehicle == null) { return(Maybe <string> .None); } var vehicleTechSpecifictaion = MapDtoToVehicleTechSpecification(request.VehicleTechSpecificationDTO); var vehicleTechSpecificationUpdateDefinition = Builders <Vehicle> .Update.Push(v => v.VehicleTechSpecifications, vehicleTechSpecifictaion); var askForSparePartsEvent = new AskForSparePartsPricesIntegrationEvent( new VehicleDTO { Id = vehicle.Id.ToString(), ManufacturerName = vehicle.ManufacturerName, Model = vehicle.Model, Generation = vehicle.Generation, EndProductionYear = vehicle.EndProductionYear, StartProductionYear = vehicle.StartProductionYear, VehicleTechSpecification = request.VehicleTechSpecificationDTO }); clientSessionHandle.StartTransaction(); try { await sparePartsDbContext.Vehicles.UpdateOneAsync(filter, vehicleTechSpecificationUpdateDefinition); await sparePartsIntegrationEventService.PublishThroughEventBusAsync(askForSparePartsEvent); await clientSessionHandle.CommitTransactionAsync(); } catch (Exception ex) { logger.Information($"Can`t create vehicle tech specification : {vehicleTechSpecifictaion.ToJson()}. Exception: {ex.Message}"); clientSessionHandle.AbortTransaction(); return(Maybe <string> .None); } return(vehicleTechSpecifictaion.Id.ToString()); }
public async Task <Maybe <string> > Handle(CreateVehicleCommand request, CancellationToken cancellationToken) { //TODO think about IDs var vehicleDTO = request.vehicleDTO; vehicleDTO.GeneratePopertyIds(); var vehicle = vehicleMapper.MapToVehicle(vehicleDTO); var existingVehiclesCursor = await sparePartsDbContext.Vehicles.FindAsync(v => v.ManufacturerName == vehicle.ManufacturerName && v.Model == vehicle.Model && v.Generation == vehicle.Generation); var existingVehicle = await existingVehiclesCursor.ToListAsync(); if (existingVehicle.Count > 0) { return(existingVehicle.FirstOrDefault().Id.ToString()); } var askForSparePartsEvent = new AskForSparePartsPricesIntegrationEvent(vehicleDTO); clientSessionHandle.StartTransaction(); try { await sparePartsDbContext.Vehicles.InsertOneAsync(vehicle); await sparePartsIntegrationEventService.PublishThroughEventBusAsync(askForSparePartsEvent); await clientSessionHandle.CommitTransactionAsync(); } catch (Exception ex) { logger.Information($"Can`t write to db vehicle: {vehicle.ToJson()} Exception: {ex.Message}"); await clientSessionHandle.AbortTransactionAsync(); return(Maybe <string> .None); } return(vehicle.Id.ToString()); }