/// <summary> /// Проверяет существует ли properties, в случае если в базе нет данного объекта, создаст новый /// и установит необходимые связи /// </summary> private async Task MapPropertyRequest(HardwareItemEntity entity, IEnumerable <CompatibilityPropertyRequest> properties) { var propertysItems = new List <CompatibilityPropertyHardwareItem>(); foreach (var item in properties) { var propertyEntity = await container.CompatibilityPropertyRepository.Get(item.PropertyType, item.PropertyName); if (propertyEntity == null) { propertyEntity = await container.CompatibilityPropertyRepository.AddAsync(new CompatibilityPropertyEntity { PropertyName = item.PropertyName, PropertyType = item.PropertyType }); } propertysItems.Add(new CompatibilityPropertyHardwareItem { Item = entity, Property = propertyEntity }); } entity.PropertysItems = propertysItems; }
public IActionResult AddHwItem(string name, double cost, string description, string manufacturerName, string hardwareType, string propertyName, string propertyType) { HardwareItemEntity hardwareItem = new HardwareItemEntity(name, cost, description, new ManufacturerEntity(manufacturerName), new HardwareTypeEntity(hardwareType)); CompatibilityPropertyEntity compatibilityProperty = new CompatibilityPropertyEntity(propertyName, propertyType); hardwareItem.PropertyList.Add(compatibilityProperty); _repository.AddAsync(hardwareItem); return(Ok());//как сделать Created и нужно ли? }
/// <summary> /// Добавляет новую деталь в базу, в случае если изготовитель или тип детали отсутствует в базе, /// он будет добавлен автоматически /// </summary> /// <param name="request"></param> /// <returns></returns> public async Task <ResultObject <HardwareItemResponse> > AddHardwareItem(HardwareItemRequest request) { var result = ResultObject <HardwareItemResponse> .Create(); var mapEntity = mapper.Map <HardwareItemEntity>(request); HardwareItemEntity entity = null; try { await MapPropertyRequest(mapEntity, request.PropertysItems); var manufacturerEntity = await GetOrCreateEntity(container.ManufacturerRepository, request.Manufacturer); mapEntity.Manufacturer = manufacturerEntity; var hardwareTypeEntity = await GetOrCreateEntity(container.HardwareTypeRepository, request.HardwareType); mapEntity.HardwareType = hardwareTypeEntity; entity = await container.HardwareItemRepository.AddAsync(mapEntity); await container.SaveAsync(); } catch (Exception ex) { if (entity != null) { await container.HardwareItemRepository.RemoveAsync(entity); } return(result.AddError(ex)); } var response = mapper.Map <HardwareItemResponse>(entity); return(result.SetValue(response)); }