public async Task AddCategory(CategoryForInformation category)
 {
     try
     {
         _ctx.CategoriesForInformation.Add(category);
         await _ctx.SaveChangesAsync();
     }
     catch (Exception ex)
     {
         _logger.LogInformation($"Category for Information record not created: {ex.Message}");
     }
 }
Exemple #2
0
        public async Task <IActionResult> Create(CategoryForInformation category)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            try
            {
                await _cat.AddCategory(category);

                // TODO: Add insert logic here
                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(RedirectToAction("Error", "Home"));
            }
        }
 public void DeleteCategoryById(int id)
 {
     if (CategoryForInformationExists(id))
     {
         CategoryForInformation categoryToRemove = _ctx.CategoriesForInformation.Find(id);
         try
         {
             _ctx.CategoriesForInformation.Remove(categoryToRemove);
             _ctx.SaveChanges();
             return;
         }
         catch (Exception ex)
         {
             _logger.LogInformation($"Category record not deleted: {ex.Message}");
         }
     }
     throw new Exception();
 }
        public async Task UpdateCategoryById(CategoryForInformation category)
        {
            if (CategoryForInformationExists(category.CategoryId))
            {
                try
                {
                    _ctx.CategoriesForInformation.Update(category);
                    await _ctx.SaveChangesAsync();

                    return;
                }
                catch (Exception ex)
                {
                    _logger.LogInformation($"Category record not updated: {ex.Message}");
                }
            }
            throw new Exception();
        }
Exemple #5
0
        public async Task <IActionResult> Edit(int id, CategoryForInformation category)
        {
            if (id != category.CategoryId)
            {
                return(RedirectToAction("Error", "Home"));
            }
            if (!ModelState.IsValid)
            {
                return(View("Edit", new { id }));
            }
            try
            {
                await _cat.UpdateCategoryById(category);

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                return(RedirectToAction("Error", "Home"));
            }
        }