public async Task EditAsync(FoodCategoryModel model) { if (await _context.FoodCategories.AnyAsync(x => x.Name.Trim().ToLower() == model.Name.Trim().ToLower() && x.Id != model.Id)) { throw new DuplicatedExcpection("Categoria de comida já cadastrada"); } var category = await _context.FoodCategories.FirstOrDefaultAsync(x => x.Id == model.Id); if (category == null) { throw new NotFoundException("Categoria não encontrada"); } category.Name = model.Name.Trim(); try { _context.FoodCategories.Update(category); await _context.SaveChangesAsync(); } catch { throw new Exception("Não foi possível cadastrar essa categoria"); } }
public async Task <IActionResult> Edit(FoodCategoryModel model) { try { await _appFoodCategory.EditAsync(model); ViewBag.Success = true; ViewBag.Message = "Sessão editada com sucesso"; return(View(model)); } catch (Exception e) { ViewBag.Success = false; ViewBag.Message = e.Message; return(View(model)); } }
public async Task <IActionResult> Delete(FoodCategoryModel model) { try { await _appFoodCategory.DeleteAsync(model); TempData["Success"] = true; TempData["Message"] = "Sessão deletada com sucesso"; return(RedirectToAction("Index")); } catch (Exception e) { ViewBag.Success = false; ViewBag.Message = e.Message; return(View(model)); } }
public async Task DeleteAsync(FoodCategoryModel model) { var category = await _context.FoodCategories.FirstOrDefaultAsync(x => x.Id == model.Id); if (category == null) { throw new NotFoundException("Categoria não encontrada"); } try { _context.FoodCategories.Remove(category); await _context.SaveChangesAsync(); } catch { throw new Exception("Não foi possível deletar essa categoria"); } }
public async Task RegisterAsync(FoodCategoryModel model) { if (_context.FoodCategories.Any(x => x.Name.Trim().ToLower() == model.Name.Trim().ToLower())) { throw new DuplicatedExcpection("Categoria de bomboniere já cadastrada"); } var foodcategory = new FoodCategory(model.Name.Trim()); try { await _context.FoodCategories.AddAsync(foodcategory); await _context.SaveChangesAsync(); } catch { throw new Exception("Não foi possível cadastrar essa categoria"); } }
public async Task <IActionResult> Register(FoodCategoryModel model) { if (!ModelState.IsValid) { return(View(model)); } try { await _appFoodCategory.RegisterAsync(model); TempData["Success"] = true; TempData["Message"] = "Sessão Cadastrada com sucesso"; return(RedirectToAction("Register")); } catch (Exception e) { ViewBag.Success = false; ViewBag.Message = e.Message; return(View(model)); } }