public ActionResult UpdateMainCategory(int maincategoryId, [FromBody] MainCategoryDtos mainCategoryDtos) { if (mainCategoryDtos == null || maincategoryId != mainCategoryDtos.id) { return(BadRequest(ModelState)); } // var mainCategoryObj = _mapper.Map<MainCategory>(mainCategoryDtos); var mainCategoryObj = _unitofWork.mainCategory.Get(maincategoryId); mainCategoryObj.name = mainCategoryDtos.name; if (mainCategoryDtos.img == null || mainCategoryDtos.img.Trim() == "") { // mainCategoryObj.img = ""; } else { // var obj = _unitofWork.mainCategory.Get(mainCategoryDtos.id); // var path = _hostingEnvironment.ContentRootPath; var rootFolder = Directory.GetCurrentDirectory(); var path = rootFolder.ToString().Replace(".API", ""); var folderPath1 = path + @"\wwwroot"; if (mainCategoryObj.img != null) { var imagePath = folderPath1; var test = folderPath1.Replace("\\", "/"); var test2 = test + mainCategoryObj.img; var imgdelete = test2.Replace("/", "\\"); //webRootPath + obj.img.ToString().Replace("/", "\\"); if (System.IO.File.Exists(imgdelete)) { System.IO.File.Delete(imgdelete); } } string fileName = Guid.NewGuid().ToString(); fileName = DateTime.UtcNow.ToString("yymmssfff") + fileName + ".jpg"; var folderPath = path + @"\wwwroot\uploads\MainCategory"; string s = Path.Combine(folderPath, fileName); if (!System.IO.Directory.Exists(folderPath)) { System.IO.Directory.CreateDirectory(folderPath); } System.IO.File.WriteAllBytes(Path.Combine(folderPath, fileName), Convert.FromBase64String(mainCategoryDtos.img)); mainCategoryObj.img = "/uploads/MainCategory/" + fileName; } _unitofWork.mainCategory.Update(mainCategoryObj); //_unitofWork.Save(); if (!_unitofWork.Save()) { ModelState.AddModelError("", $"Something went wrong saving record{mainCategoryDtos.name}"); return(StatusCode(500, ModelState)); } return(NoContent()); }
public async Task <IActionResult> Create(MainCategoryCreateViewModel model) { if (ModelState.IsValid) { MainCategoryDtos obj = new MainCategoryDtos { name = model.name }; if (model.img != null && model.img.Length > 0) { using (var ms = new MemoryStream()) { model.img.CopyTo(ms); var fileBytes = ms.ToArray(); string s = Convert.ToBase64String(fileBytes); obj.img = s; ms.Close(); // act on the Base64 data } } string path = SD.APIBaseUrl + "Maincategory/CreateMainCategory"; bool res = await _mainCategoryRepository.CreateAsync(path, obj); //_unitofWork.mainCategory.Add(obj); //bool res = _unitofWork.Save(); TempData["success"] = "Record Save successfully"; return(RedirectToAction(nameof(Index))); } else { return(View()); } }
public ActionResult GetMainCategory(int maincategoryId) { var obj = _unitofWork.mainCategory.Get(maincategoryId); if (obj == null) { return(NotFound()); } var Dtosobj = new MainCategoryDtos(); Dtosobj = _mapper.Map <MainCategoryDtos>(obj); return(Ok(Dtosobj)); }
public async Task <IActionResult> Edit(MainCategoryCreateViewModel model) { if (ModelState.IsValid) { string path = SD.APIBaseUrl + "Maincategory/GetMainCategorybyid?maincategoryId="; MainCategoryDtos obj = await _mainCategoryRepository.GetAsync(path, model.id); if (obj == null) { TempData["error"] = "Record Not Found"; return(NotFound()); } obj.id = model.id; obj.name = model.name; if (model.img != null && model.img.Length > 0) { using (var ms = new MemoryStream()) { model.img.CopyTo(ms); var fileBytes = ms.ToArray(); string s = Convert.ToBase64String(fileBytes); obj.img = s; ms.Close(); // act on the Base64 data } } else { obj.img = ""; } string path1 = SD.APIBaseUrl + "Maincategory/UpdateMainCategory"; bool res = await _mainCategoryRepository.UpdateAsync(path1, obj); if (res) { TempData["success"] = "Record Update successfully"; } else { TempData["error"] = "Record Not Update"; } return(RedirectToAction(nameof(Index))); } else { return(View()); } }
public ActionResult GetMainCategorybyid(int maincategoryId) { var obj = _unitofWork.mainCategory.GetAll().Where(x => x.id == maincategoryId).FirstOrDefault(); if (obj == null) { return(NotFound()); } var Dtosobj = new MainCategoryDtos(); Dtosobj = _mapper.Map <MainCategoryDtos>(obj); return(Ok(Dtosobj)); }
public IActionResult CreateMainCategory([FromBody] MainCategoryDtos mainCategoryInsertDtos) { if (mainCategoryInsertDtos == null) { return(BadRequest(ModelState)); } if (_unitofWork.mainCategory.MainCategoryExists(mainCategoryInsertDtos.name)) { ModelState.AddModelError("", "Main Category Exists!"); return(StatusCode(404, ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var mainCategoryObj = _mapper.Map <MainCategory>(mainCategoryInsertDtos); if (mainCategoryInsertDtos.img == null || mainCategoryInsertDtos.img.Trim() == "") { mainCategoryObj.img = ""; } else { SaveImageinFolder objsaveImageinFolder = new SaveImageinFolder(); string iamgename = objsaveImageinFolder.uploadImage("", "\\uploads\\MainCategory", mainCategoryInsertDtos.img); mainCategoryObj.img = iamgename; } mainCategoryObj.isdeleted = false; mainCategoryObj.isactive = false; _unitofWork.mainCategory.Add(mainCategoryObj); // bool res = _unitofWork.Save(); // return Ok(mainCategoryObj); if (!_unitofWork.Save()) { ModelState.AddModelError("", $"Something went wrong saving record"); return(StatusCode(500, ModelState)); } var obj = _mapper.Map <MainCategoryDtos>(mainCategoryObj); return(Ok(obj)); // return CreatedAtRoute("GetMainCategory", new { maincategoryId = mainCategoryObj.id }, mainCategoryObj); // return CreatedAtRoute("GetMainCategory", new { Version = HttpContext.GetRequestedApiVersion().ToString(), maincategoryId = mainCategoryObj.id }, mainCategoryObj); }
//[HttpPost] //[ValidateAntiForgeryToken] //public async Task<IActionResult> Create(MainCategoryCreateViewModel model) //{ // if (ModelState.IsValid) // { // var obj = new MainCategory // { // id = model.id // , // name = model.name // , // isdeleted = false // , // isactive = false // }; // if (model.img != null && model.img.Length > 0) // { // var uploadDir = @"uploads/MainCategory"; // var fileName = Path.GetFileNameWithoutExtension(model.img.FileName); // var extesion = Path.GetExtension(model.img.FileName); // var webRootPath = _hostingEnvironment.WebRootPath; // fileName = DateTime.UtcNow.ToString("yymmssfff") + fileName + extesion; // var path = Path.Combine(webRootPath, uploadDir, fileName); // FileStream fs = new FileStream(path, FileMode.Create); // await model.img.CopyToAsync(fs); // fs.Close(); // obj.img = '/' + uploadDir + '/' + fileName; // } // _unitofWork.mainCategory.Add(obj); // bool res = _unitofWork.Save(); // TempData["success"] = "Record Save successfully"; // return RedirectToAction(nameof(Index)); // } // else // { // return View(); // } //} public async Task <IActionResult> Edit(int id) { string path = SD.APIBaseUrl + "Maincategory/GetMainCategorybyid?maincategoryId="; MainCategoryDtos objcategory = await _mainCategoryRepository.GetAsync(path, id); if (objcategory == null) { return(NotFound()); } var model = new MainCategoryCreateViewModel() { id = objcategory.id, name = objcategory.name, imgName = objcategory.img }; return(View(model)); }
public ActionResult UpdateMainCategory(int maincategoryId, [FromBody] MainCategoryDtos mainCategoryDtos) { if (mainCategoryDtos == null || maincategoryId != mainCategoryDtos.id) { return(BadRequest(ModelState)); } var mainCategoryObj = _mapper.Map <MainCategory>(mainCategoryDtos); _unitofWork.mainCategory.Update(mainCategoryObj); //_unitofWork.Save(); if (!_unitofWork.Save()) { ModelState.AddModelError("", $"Something went wrong saving record{mainCategoryDtos.name}"); return(StatusCode(500, ModelState)); } return(NoContent()); }
public IActionResult CreateMainCategory([FromBody] MainCategoryDtos mainCategoryInsertDtos) { if (mainCategoryInsertDtos == null) { return(BadRequest(ModelState)); } if (_unitofWork.mainCategory.MainCategoryExists(mainCategoryInsertDtos.name)) { ModelState.AddModelError("", "Main Category Exists!"); return(StatusCode(404, ModelState)); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var mainCategoryObj = _mapper.Map <MainCategory>(mainCategoryInsertDtos); if (mainCategoryInsertDtos.img == null || mainCategoryInsertDtos.img.Trim() == "") { mainCategoryObj.img = ""; } else { // var filePath = HttpContext.Current.Server.MapPath("~/Userimage/" + postedFile.FileName + extension); string fileName = Guid.NewGuid().ToString() + ".jpg"; var folderPath = SD.ImageFolderPath.Replace(@"/", @"\") + @"\wwwroot\uploads\MainCategory"; System.IO.File.WriteAllBytes(Path.Combine(folderPath, fileName), Convert.FromBase64String(mainCategoryInsertDtos.img)); mainCategoryObj.img = "/uploads/MainCategory/" + fileName; // var rootFolder = _hostingEnvironment.ContentRootPath; //var path = rootFolder.ToString().Replace(".API", ""); // string fileName = Guid.NewGuid().ToString(); // fileName = DateTime.UtcNow.ToString("yymmssfff") + fileName + ".jpg"; // // var folderPath = path + @"\wwwroot\uploads\MainCategory"; // var folderPath = SD.ImageFolderPath.Replace(@"/",@"\") + @"\wwwroot\uploads\MainCategory"; // string s = Path.Combine(folderPath, fileName); // //if (!System.IO.Directory.Exists(folderPath)) // //{ // // System.IO.Directory.CreateDirectory(folderPath); // //} // System.IO.File.WriteAllBytes(Path.Combine(folderPath, fileName), Convert.FromBase64String(mainCategoryInsertDtos.img)); // mainCategoryObj.img = "/uploads/MainCategory/" + fileName; } mainCategoryObj.isdeleted = false; mainCategoryObj.isactive = false; _unitofWork.mainCategory.Add(mainCategoryObj); // bool res = _unitofWork.Save(); // return Ok(mainCategoryObj); if (!_unitofWork.Save()) { ModelState.AddModelError("", $"Something went wrong saving record"); return(StatusCode(500, ModelState)); } return(CreatedAtRoute("GetMainCategory", new { maincategoryId = mainCategoryObj.id }, mainCategoryObj)); // return CreatedAtRoute("GetMainCategory", new { Version = HttpContext.GetRequestedApiVersion().ToString(), maincategoryId = mainCategoryObj.id }, mainCategoryObj); }