public IActionResult AddEdit(string id = null) { var model = new ObjView.Category(); try { int categoryId = Convert.ToInt32(id); if (categoryId > 0) { // Edit a category (retrieve the object from the db) var categoryDB = _categoryService.GetCategoryById(categoryId); if (categoryDB != null) { model.CategoryId = categoryDB.CategoryId; model.Name = categoryDB.Name; model.Description = categoryDB.Description; } } return(View(model)); } catch (Exception) { model.IsError = true; model.Message = $"Unexpected error ocurred while Retrieving Category for user: '******'. Try again later!"; return(View(model)); } }
public IActionResult AddEdit(ObjView.Category model) { try { if (ModelState.IsValid) { var catDb = ObjectTransformations.TransformViewToDbObj(model); catDb.UserEmail = GetUserEmail(); // Azure user information // Add/Update category in the db _categoryService.AddEditCategory(catDb); ViewBag.IsError = "false"; ViewBag.Message = "Category added/updated sucessfully!"; return(RedirectToAction("Index", "Picture")); } else { return(View()); } } catch (Exception) { ViewBag.IsError = true; ViewBag.Message = $"Unexpected error ocurred while Saving the Category for user: '******'. Try again later!"; return(RedirectToAction("Index", "Picture")); } }
public IActionResult Delete(string id) { try { int categoryId = Convert.ToInt32(id); var model = new ObjView.Category(); if (categoryId > 0) { _categoryService.DeleteCategory(categoryId); ViewBag.IsError = false; ViewBag.Message = "Category deleted sucessfully!"; return(View()); } else { return(RedirectToAction(nameof(Index), "Picture")); } } catch (Exception) { ViewBag.IsError = true; ViewBag.Message = $"Unexpected error ocurred while Deleting the Category for user: '******'. Try again later!"; return(RedirectToAction(nameof(Index), "Picture")); } }
/// <summary> /// Transform a category database object to a Category view Obj /// </summary> /// <param name="category"></param> /// <returns></returns> public static ViewObj.Category TransformDbtoViewObj(DbObj.Category category) { ViewObj.Category newCategory = null; if (category != null) { newCategory = new ViewObj.Category { CategoryId = category.CategoryId, Name = category.Name, Description = category.Description }; } return(newCategory); }
/// <summary> /// Transform a category view object to a Category EF Obj /// </summary> /// <param name="category"></param> /// <returns></returns> public static DbObj.Category TransformViewToDbObj(ViewObj.Category category) { DbObj.Category newCategory = null; if (category != null) { newCategory = new DbObj.Category { CategoryId = category.CategoryId, Name = category.Name, Description = category.Description, UserEmail = string.Empty // Field that needs to be setup from outside the transformation }; } return(newCategory); }