public async Task <IActionResult> Edit(int id, SubCategoryOne subCategoryOne) { if (id != subCategoryOne.SubCategoryOneId) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(subCategoryOne); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException e) { if (!SubCategoryOneExists(subCategoryOne.SubCategoryOneId)) { return(NotFound()); } else { _logger.LogError(e.Message, new[] { "ImportSubCategories", "SubCategoryController" }); } } return(RedirectToAction(nameof(Index))); } return(View(subCategoryOne)); }
public async Task <IActionResult> Create(SubCategoryOneViewModel subCategoryVM, IFormFile file, IFormCollection fc) { if (ModelState.IsValid) { try { subCategoryVM.CategoryId = Convert.ToInt32(fc["CategoryId"]); SubCategoryOne subCategoryOne = PrepareSubCategoryOneFromViewModel(subCategoryVM); _context.Add(subCategoryOne); var result = await _context.SaveChangesAsync(); if (result > 0) { string webRootPath = _hostingEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; //var productImage = new ProductImage(); if (files[0] != null && files[0].Length > 0) { //when user uploads an image var uploads = Path.Combine(webRootPath, "Images"); string uploadedImageName = files[0].FileName.Substring(0, files[0].FileName.LastIndexOf(".")); var extension = files[0].FileName.Substring(files[0].FileName.LastIndexOf("."), files[0].FileName.Length - files[0].FileName.LastIndexOf(".")); using (var filestream = new FileStream(Path.Combine(uploads, uploadedImageName + subCategoryOne.SubCategoryOneId + extension), FileMode.Create)) { files[0].CopyTo(filestream); } subCategoryOne.SC_ImagePath = @"\Images\" + uploadedImageName + subCategoryOne.SubCategoryOneId + extension; subCategoryOne.SC_ImageName = uploadedImageName; } else { //when user does not upload image var uploads = Path.Combine(webRootPath, @"Images\" + SD.DefaultSubCategoryImage); System.IO.File.Copy(uploads, webRootPath + @"\Images\" + subCategoryOne.SubCategoryName + subCategoryOne.CategoryId + ".PNG"); subCategoryOne.SC_ImagePath = @"\Images\" + subCategoryOne.CategoryId + ".PNG"; subCategoryOne.SC_ImageName = subCategoryOne.SubCategoryName; } var finalResult = await _context.SaveChangesAsync(); _logger.LogInformation("SubCategory created successfully"); } return(RedirectToAction(nameof(Index))); } catch (Exception ex) { _logger.LogError(ex.Message, new[] { "Create", "SubCategoryController" }); } } return(View(subCategoryVM)); }
/// <summary> /// Inserts bulk Sub Categories /// </summary> /// <param name="subCategoryExcelList"></param> /// <param name="userId"></param> /// <returns></returns> public async Task <bool> InsertSubCategoriesInBulk(List <SubCategoryImportExcel> subCategoryExcelList, string userId) { if (subCategoryExcelList.Count > 0) { try { IList <SubCategoryOne> subCategoriesList = new List <SubCategoryOne>(); ApplicationUser applicationUser = _subCategoryRepository.GetApplicationUser(userId); foreach (var item in subCategoryExcelList) { SubCategoryOne subCategoryExcel = new SubCategoryOne(); subCategoryExcel.CategoryId = Convert.ToInt32(item.CategoryId); subCategoryExcel.SubCategoryName = item.SubCategoryName; subCategoryExcel.Description = item.Description; subCategoryExcel.DisplayOrder = item.DisplayOrder; subCategoryExcel.IsActive = item.IsActive; subCategoryExcel.ApplicationUser = applicationUser; //CreatedByUserId subCategoryExcel.ApplicationUser1 = applicationUser; //UpdatedByUserId subCategoryExcel.CreatedDate = DateTime.Now; subCategoryExcel.UpdatedDate = DateTime.Now; if (!string.IsNullOrEmpty(item.ImageName)) { subCategoryExcel.SC_ImageName = item.ImageName; } else { subCategoryExcel.SC_ImageName = "Default"; } if (!string.IsNullOrEmpty(item.ImagePath)) { subCategoryExcel.SC_ImagePath = item.ImagePath; } subCategoriesList.Add(subCategoryExcel); } return(await _subCategoryRepository.BulkCreate(subCategoriesList)); } catch (Exception ex) { throw ex; } } return(true); }
/// <summary> /// Prepare SubCategoryOne From SubCategoryViewModel /// </summary> /// <param name="subCategoryVM"></param> /// <returns></returns> private SubCategoryOne PrepareSubCategoryOneFromViewModel(SubCategoryOneViewModel subCategoryVM) { SubCategoryOne subCategoryOne = new SubCategoryOne(); if (subCategoryVM != null) { subCategoryOne.CategoryId = subCategoryVM.CategoryId; subCategoryOne.SubCategoryName = subCategoryVM.SubCategoryName; subCategoryOne.Description = subCategoryVM.Description; subCategoryOne.DisplayOrder = subCategoryVM.DisplayOrder; subCategoryOne.IsActive = subCategoryVM.IsActive; subCategoryOne.CreatedDate = subCategoryVM.CreatedDate; subCategoryOne.UpdatedDate = subCategoryVM.UpdatedDate; subCategoryOne.SC_ImagePath = subCategoryVM.ImagePath; //Temporarly storing b'coz this column is expecting not null.so later we will update exact name. subCategoryOne.SC_ImageName = "Default"; } return(subCategoryOne); }
private void SaveImages(SubCategoryOne subCategoryExcel) { }
public async Task <bool> DeleteSubCategory(SubCategoryOne subCategory) { return(await _subCategoryRepository.Delete(subCategory)); }
public async Task <bool> InsertSubCategory(SubCategoryOne subCategory) { return(await _subCategoryRepository.Insert(subCategory)); }