public async Task <ActionResult> UpdateProduct([FromForm] ProductToUpdateDto productUpdateData) { var product = await unitOfWork.Repository <Product>().GetByIdAsync(productUpdateData.Id); if (product == null) { return(Ok()); } product.Name = productUpdateData.Name ?? product.Name; product.Description = productUpdateData.Description ?? product.Description; product.Price = productUpdateData.Price ?? product.Price; product.ProductGenderBase = await productGenderRepo.GetEntityWithSpec(new GetGenderByName(productUpdateData.ProductGenderBase)); ProductType type = await productTypesRepo.GetEntityWithSpec(new ProductTypeByProductTypeName(productUpdateData.ProductType)); if (type == null) { } else { product.ProductTypeId = type.Id; } string path = Path.Combine(env.WebRootPath, product.PictureUrl); if (productUpdateData.ProductImage != null) { using (var stream = new FileStream(path, FileMode.Create)) { await productUpdateData.ProductImage.CopyToAsync(stream); } } var user = await userManager.FindByEmailFromClaimsPrinciple(HttpContext.User); AdminActionHistory reg = new AdminActionHistory(); reg.AdminEmail = user.Email; reg.Date = DateTime.Now; reg.Operation = AdminActionOperations.Update; reg.AdminAction = "Updated product: " + product.Name; reg.UserId = user.Id; reg.Product = product; adminActionHistoryRepo.Add(reg); unitOfWork.Repository <Product>().Update(product); await unitOfWork.Complete(); return(Ok()); }
public async Task <ActionResult> DeleteProduct(int id) { var product = await unitOfWork.Repository <Product>().GetEntityWithSpec(new GetAllAdminActionForProduct(id)); unitOfWork.Repository <Product>().Delete(product); var user = await userManager.FindByEmailFromClaimsPrinciple(HttpContext.User); AdminActionHistory reg = new AdminActionHistory(); reg.AdminEmail = user.Email; reg.Date = DateTime.Now; reg.Operation = AdminActionOperations.Delete; reg.AdminAction = "Deleted product: " + product.Name; reg.UserId = user.Id; adminActionHistoryRepo.Add(reg); await unitOfWork.Complete(); return(Ok()); }
public async Task <ActionResult> AddNewProduct([FromForm] AddNewProductDto product) { string fName = product.productImage.FileName; //TODO if type sent by admin is not in table , create new Type of Cloth var type = await productTypesRepo.GetEntityWithSpec(new ProductTypeByProductTypeName(product.ProductType)); var gender = await productGenderRepo.GetEntityWithSpec(new GetGenderByName(product.ProductGenderBase)); string rootPath = "images/ProductImages/"; switch (type.Name) { case "T-shirts": rootPath += "T-shirts/"; break; case "Jeans": rootPath += "Jeans/"; break; case "Shoes": rootPath += "Shoes/"; break; case "Hats": rootPath += "Hats/"; break; default: rootPath += "Default/"; break; } string path = Path.Combine(env.WebRootPath, rootPath + fName); string ProductImageRootPath = rootPath + fName; using (var stream = new FileStream(path, FileMode.Create)) { await product.productImage.CopyToAsync(stream); } Product productToBeAdded = new Product() { Name = product.Name, Description = product.Description, Price = product.Price, ProductGenderBase = gender, //context.ProductGenderBase.Find(product.ProductGenderBaseId), ProductType = type, PictureUrl = ProductImageRootPath, }; unitOfWork.Repository <Product>().Add(productToBeAdded); var user = await userManager.FindByEmailFromClaimsPrinciple(HttpContext.User); AdminActionHistory reg = new AdminActionHistory(); reg.AdminEmail = user.Email; reg.Date = DateTime.Now; reg.Operation = AdminActionOperations.Create; reg.AdminAction = "Added product: " + product.Name; reg.UserId = user.Id; adminActionHistoryRepo.Add(reg); await unitOfWork.Complete(); return(Ok()); }