public ProductAttributeValues SaveProductAttributeValueMap(ProductAttributeValues model, int userId, int tenantId, int productId) { if (model.AttributeValueId == 0) { var valuetoAdd = SaveProductAttributeValue(model.AttributeId, model.Value, userId); var valueMap = new ProductAttributeValuesMap { AttributeValueId = valuetoAdd.AttributeValueId, CreatedBy = userId, TenantId = tenantId, DateCreated = DateTime.UtcNow, ProductId = productId, }; _currentDbContext.ProductAttributeValuesMap.Add(valueMap); } else { model.UpdateUpdatedInfo(userId); _currentDbContext.ProductAttributeValues.Attach(model); var entry = _currentDbContext.Entry(model); entry.Property(e => e.Value).IsModified = true; entry.Property(e => e.AttributeId).IsModified = true; } _currentDbContext.SaveChanges(); return(model); }
public async Task <IResult> UpdateProductAttributeValue(ProductAttributeValueViewModel productAttributeValue) { var result = new Result { Operation = Operation.Update, Status = Status.Success }; try { var pdtAttrValueModel = new ProductAttributeValues(); pdtAttrValueModel.MapFromViewModel(productAttributeValue); var attributeValueCheck = _productRepository.CheckAttributeValue(pdtAttrValueModel); if (!attributeValueCheck) { result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; result.Message = "Product value exists already."; return(result); } var updateAttrValue = await _productRepository.UpdateProductAttributeValue(pdtAttrValueModel); return(updateAttrValue); } catch (Exception e) { result.Status = Status.Error; result.Message = e.Message; result.StatusCode = HttpStatusCode.InternalServerError; return(result); } }
public ProductAttributeValues SaveProductAttributeValue(int attributeId, string attributeValue, int userId = 0) { var value = _currentDbContext.ProductAttributeValues.FirstOrDefault(m => m.AttributeId == attributeId && m.Value == attributeValue); if (value == null && userId > 0) { value = new ProductAttributeValues() { AttributeId = attributeId, Value = attributeValue }; value.UpdateCreatedInfo(userId); _currentDbContext.Entry(value).State = EntityState.Added; _currentDbContext.SaveChanges(); return(value); } else if (userId == 0) { throw new Exception("Unable to create attribute : User information not available"); } return(value); }
public async Task <IResult> UpdateProductAttributeValue(ProductAttributeValues productAttribute) { var result = new Result() { Operation = Operation.Update, Status = Status.Success }; try { var attrValue = _context.ProductAttributeValues.Where(p => p.Id == productAttribute.Id).FirstOrDefault(); attrValue.MapFromModel(productAttribute); var product = _context.Products.Where(p => p.ProductId == productAttribute.ProductId).FirstOrDefault(); product.ModifiedBy = _specificClaim.GetSpecificClaim("Id"); product.ModifiedDate = DateTime.Now; await _context.SaveChangesAsync(); result.StatusCode = HttpStatusCode.OK; return(result); } catch (Exception e) { result.Status = Status.Error; result.StatusCode = HttpStatusCode.InternalServerError; result.Body = e; return(result); } }
public async Task <IResult> InsertProductAttributeValue(ProductAttributeValueViewModel productAttributeValue) { var result = new Result { Operation = Operation.Create, Status = Status.Success }; try { ProductAttributeValues attributeValue = new ProductAttributeValues(); attributeValue.MapFromViewModel(productAttributeValue); var attributeValueCheck = _productRepository.CheckAttributeValue(attributeValue); if (!attributeValueCheck) { result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; result.Message = "isValue"; return(result); } var addAttrValue = await _productRepository.AddProductAttributeValue(attributeValue); return(addAttrValue); } catch (Exception e) { result.Status = Status.Error; result.Message = e.Message; result.StatusCode = HttpStatusCode.InternalServerError; return(result); } }
public async Task <ActionResult <IResult> > UpdateProductAttributeValue([FromBody] ProductAttributeValues attributeValue) { var result = new Result { Operation = Operation.Update, Status = Status.Success }; try { if (!ModelState.IsValid) { result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; return(StatusCode((int)result.StatusCode, result)); } var attribute = await context.ProductAttributeValues.Where(x => x.ID == attributeValue.ID).FirstOrDefaultAsync(); var attributeValueCheck = await context.ProductAttributeValues.Where(x => x.Value == attributeValue.Value).ToListAsync(); if (attributeValueCheck.Count() != 0) { result.Status = Status.Fail; result.StatusCode = HttpStatusCode.BadRequest; result.Message = "Product value exists already."; return(StatusCode((int)result.StatusCode, result)); } attribute.ID = attributeValue.ID; attribute.AttributeID = attributeValue.AttributeID; attribute.ProductID = attributeValue.ProductID; attribute.Value = attributeValue.Value; await context.SaveChangesAsync(); var product = await context.Products.Where(x => x.ProductID == attributeValue.ProductID).FirstOrDefaultAsync(); product.ModifiedBy = helper.GetSpecificClaim("ID"); product.ModifiedDate = DateTime.Now; ProductViewModel viewModel = new ProductViewModel() { ModifiedUser = helper.GetSpecificClaim("Name") }; await context.SaveChangesAsync(); result.StatusCode = HttpStatusCode.OK; result.Status = Status.Success; return(StatusCode((int)result.StatusCode, result)); } catch (Exception e) { result.Status = Status.Error; result.Message = e.Message; result.StatusCode = HttpStatusCode.InternalServerError; return(StatusCode((int)result.StatusCode, result)); } }
public bool CheckAttributeValue(ProductAttributeValues attributeValue) { var attributeValueCheck = _context.ProductAttributeValues.Where(x => x.Value == attributeValue.Value && x.ProductId == attributeValue.ProductId).FirstOrDefault(); if (attributeValueCheck != null) { return(false); } return(true); }
public async Task <string> createProductAttributesValuesAsync(int product_id, int attribute_id, int value_id) { ProductAttributeValues _productAttributeValues = new ProductAttributeValues(); _productAttributeValues.product_id = product_id; _productAttributeValues.attribute_id = attribute_id; _productAttributeValues.value_id = value_id; String returnID = await _BadgerApiHelper.GenericPostAsyncString <String>(JsonConvert.SerializeObject(_productAttributeValues), "/product/createAttributesValues"); return(returnID); }
public async Task <string> PostAsyncAttributesValues([FromBody] string value) { string NewInsertionID = "0"; try { ProductAttributeValues newProductAttributeValues = JsonConvert.DeserializeObject <ProductAttributeValues>(value); NewInsertionID = await _ProductRepo.CreateAttributeValues(newProductAttributeValues); } catch (Exception ex) { var logger = _loggerFactory.CreateLogger("internal_error_log"); logger.LogInformation("Problem happened in making new product with message" + ex.Message); } return(NewInsertionID); }
/*Developer: ubaid * Date:5-7-19 * Action:get AttributeValues Model from controller and insert the AttributeValues * Input: AttributeValues Model * output: New AttributeValues id */ public async Task <string> CreateAttributeValues(ProductAttributeValues NewProductAttributeValues) { string productAttirubteExistsQuery = "SELECT * FROM product_attribute_values WHERE product_id='" + NewProductAttributeValues.product_id + "' and attribute_id='" + NewProductAttributeValues.attribute_id + "';"; using (IDbConnection conn = Connection) { var productAttributeExists = await conn.QueryAsync <ProductAttributeValues>(productAttirubteExistsQuery); if (productAttributeExists == null || productAttributeExists.Count() == 0) { var result = await conn.InsertAsync <ProductAttributeValues>(NewProductAttributeValues); return(result.ToString()); } else { return(productAttributeExists.First().product_attribute_value_id.ToString()); } } }
public async Task SaveToDbContextAsync(ApplicationDbContext context) { await semaphoreSlim.WaitAsync(); try { ProductsInfo currentDbState = new ProductsInfo(); await currentDbState.LoadFromDbContextAsync(context); WrappedProductsInfo wrappedProductsInfo = new WrappedProductsInfo(this); //LEFT TO ADD: Product, ProductDiscount, ProductAd, ProductImage, ProductAttributeValue, AttributeValue, Attribute, ProductProperty, ProductCategory, Category, CategoryCategory //ADDING IN THIS STEP: Product, Attribute, Categories //Add missing products foreach (var importProduct in Products) { Product dbProduct = currentDbState.Products.FirstOrDefault((p) => { return(p.Equals(importProduct)); }); if (dbProduct == null) { importProduct.Id = 0; context.Product.Add(importProduct); } } //Add missing attributes foreach (var importAttribute in Attributes) { Models.Attribute dbAttribute = currentDbState.Attributes.FirstOrDefault((a) => { return(a.Equals(importAttribute)); }); if (dbAttribute == null) { importAttribute.Id = 0; context.Attribute.Add(importAttribute); } } //Add missing categories foreach (var importCategory in Categories) { Category dbCategory = currentDbState.Categories.FirstOrDefault((c) => { return(c.Equals(importCategory)); }); if (dbCategory == null) { importCategory.Id = 0; context.Category.Add(importCategory); } } await context.SaveChangesAsync(); //Update every entity's ProductId value foreach (var updatedProduct in wrappedProductsInfo.Products) { ProductDiscounts.Where((x) => { return(x.ProductId == updatedProduct.Item1); }).All((x) => { x.ProductId = updatedProduct.Item2.Id; return(true); }); ProductAds.Where((x) => { return(x.ProductId == updatedProduct.Item1); }).All((x) => { x.ProductId = updatedProduct.Item2.Id; return(true); }); ProductImages.Where((x) => { return(x.ProductId == updatedProduct.Item1); }).All((x) => { x.ProductId = updatedProduct.Item2.Id; return(true); }); ProductAttributeValues.Where((x) => { return(x.ProductId == updatedProduct.Item1); }).All((x) => { x.ProductId = updatedProduct.Item2.Id; return(true); }); ProductProperties.Where((x) => { return(x.ProductId == updatedProduct.Item1); }).All((x) => { x.ProductId = updatedProduct.Item2.Id; return(true); }); ProductCategories.Where((x) => { return(x.ProductId == updatedProduct.Item1); }).All((x) => { x.ProductId = updatedProduct.Item2.Id; return(true); }); } //Update every entity's AttributeId values foreach (var updatedAttribute in wrappedProductsInfo.Attributes) { AttributeValues.Where((x) => { return(x.AttributeId == updatedAttribute.Item1); }).All((x) => { x.AttributeId = updatedAttribute.Item2.Id; return(true); }); } //Update every entity's CategoryId and ParentCategoryId values foreach (var updatedCategory in wrappedProductsInfo.Categories) { ProductCategories.Where((x) => { return(x.CategoryId == updatedCategory.Item1); }).All((x) => { x.CategoryId = updatedCategory.Item2.Id; return(true); }); CategoryCategories.Where((x) => { return(x.CategoryId == updatedCategory.Item1); }).All((x) => { x.CategoryId = updatedCategory.Item2.Id; return(true); }); CategoryCategories.Where((x) => { return(x.ParentCategoryId == updatedCategory.Item1); }).All((x) => { x.ParentCategoryId = updatedCategory.Item2.Id; return(true); }); } //LEFT TO ADD: ProductDiscount, ProductAd, ProductImage, ProductAttributeValue, AttributeValue, ProductProperty, ProductCategory, CategoryCategory //ADDING IN THIS STEP: ProductDiscount, ProductAd, ProductImage, AttributeValue, ProductProperty, CategoryCategory, ProductCategory //Add missing product discounts foreach (var importProductDiscount in ProductDiscounts) { ProductDiscount dbProductDiscount = currentDbState.ProductDiscounts.FirstOrDefault((pd) => { return(pd.Equals(importProductDiscount)); }); if (dbProductDiscount == null) { importProductDiscount.Id = 0; context.ProductDiscount.Add(importProductDiscount); } } //Add missing product ads foreach (var importProductAd in ProductAds) { ProductAd dbProductAd = currentDbState.ProductAds.FirstOrDefault((pa) => { return(pa.Equals(importProductAd)); }); if (dbProductAd == null) { importProductAd.Id = 0; context.ProductAd.Add(importProductAd); } } //Add missing product images foreach (var importProductImage in ProductImages) { ProductImage dbProductImage = currentDbState.ProductImages.FirstOrDefault((pi) => { return(pi.Equals(importProductImage)); }); if (dbProductImage == null) { importProductImage.Id = 0; context.ProductImage.Add(importProductImage); } } //Add missing product properties foreach (var importProductProperty in ProductProperties) { ProductProperty dbProductProperty = currentDbState.ProductProperties.FirstOrDefault((pp) => { return(pp.Equals(importProductProperty)); }); if (dbProductProperty == null) { importProductProperty.Id = 0; context.ProductProperty.Add(importProductProperty); } } //Add missing attribute values foreach (var importAttributeValue in AttributeValues) { AttributeValue dbAttributeValue = currentDbState.AttributeValues.FirstOrDefault((av) => { return(av.Equals(importAttributeValue)); }); if (dbAttributeValue == null) { importAttributeValue.Id = 0; context.AttributeValue.Add(importAttributeValue); } } //Add missing category categories foreach (var importCategoryCategory in CategoryCategories) { CategoryCategory dbCategoryCategory = currentDbState.CategoryCategories.FirstOrDefault((cc) => { return(cc.Equals(importCategoryCategory)); }); if (dbCategoryCategory == null) { importCategoryCategory.Id = 0; context.CategoryCategory.Add(importCategoryCategory); } } //Add missing product categories foreach (var importProductCategory in ProductCategories) { ProductCategory dbProductCategory = currentDbState.ProductCategories.FirstOrDefault((pc) => { return(pc.Equals(importProductCategory)); }); if (dbProductCategory == null) { importProductCategory.Id = 0; context.ProductCategory.Add(importProductCategory); } } await context.SaveChangesAsync(); //Update every entity's AttributeValueId foreach (var updatedAttributeValue in wrappedProductsInfo.AttributeValues) { ProductAttributeValues.Where((x) => { return(x.AttributeValueId == updatedAttributeValue.Item1); }).All((x) => { x.AttributeValueId = updatedAttributeValue.Item2.Id; return(true); }); } //LEFT TO ADD: ProductAttributeValue //ADDING IN THIS STEP: ProductAttributeValue //Add missing product attribute values foreach (var importProductAttributeValue in ProductAttributeValues) { ProductAttributeValue dbProductAttributeValue = currentDbState.ProductAttributeValues.FirstOrDefault((pav) => { return(pav.Equals(importProductAttributeValue)); }); if (dbProductAttributeValue == null) { importProductAttributeValue.Id = 0; context.ProductAttributeValue.Add(importProductAttributeValue); } } await context.SaveChangesAsync(); } finally { semaphoreSlim.Release(); } }