コード例 #1
0
 //редактировать категорию
 public async Task<CategoryForEdit> EditCategoryAsync(CategoryForEdit cat)
 {
     var catEdit = await Database.Categories.GetAsync(cat.Id);
     if (catEdit != null)
     {
         catEdit.Title = cat.Title;
         catEdit.CategoryRoles.Clear();
         if (cat.Roles != null)
         {
             foreach (var role in cat.Roles)
             {
                 catEdit.CategoryRoles.Add(new CategoryRole
                 {
                     CatalogRoleId = role.Id,
                     DownloadFiles = role.DownloadFiles,
                 });
             }
             //админ имеет доступ по умолчанию
             var adminRole = await Database.Roles.FindAsync(r => r.Name == "admin");
             catEdit.CategoryRoles.Add(new CategoryRole 
             { 
                 CatalogRoleId = adminRole.FirstOrDefault().Id,
                 DownloadFiles = true,
             });
         }                
         Database.Categories.Update(catEdit);
         int result = await Database.SaveAsync();
         if (result >= 0)
         {
             return cat;
         }
     }
     return null;
 }
コード例 #2
0
 //получить категорию по id
 public async Task<CategoryForEdit> GetCategoryAsync(int catId)
 {
     var cat = await Database.Categories.GetAsync(catId);
     if (cat != null)
     {
         CategoryForEdit catModel = new CategoryForEdit
         {
             Id = cat.Id,
             Title = cat.Title,
             Roles = cat.CategoryRoles.Select(r => new RoleForCategory
             {
                 Id = r.CatalogRoleId,
                 DownloadFiles = r.DownloadFiles,
             }).ToList(),
         };
         return catModel;
     }
     return null;
 }
コード例 #3
0
ファイル: AdminController.cs プロジェクト: IvanNkl/Catalog
 public async Task<ActionResult> AddCategory(CategoryForEdit catAdd)
 {
     if (Request.IsAjaxRequest())
     {
         var cat = await CategoryService.AddCategoryAsync(catAdd);
         if (cat != null)
         {
             ViewBag.Status = "Категория " + cat.Title + " успешно добавлена.";
             return PartialView("Success");
         }
     }
     return HttpNotFound();
 }
コード例 #4
0
 //добавить категорию
 public async Task<CategoryForEdit> AddCategoryAsync(CategoryForEdit cat)
 {
     if (cat != null)
     {
         Category catAdd = new Category
         {
             Title = cat.Title,
         };
         foreach (var role in cat.Roles)
         {
             catAdd.CategoryRoles.Add(new CategoryRole
             {
                 CatalogRoleId = role.Id,
                 DownloadFiles = role.DownloadFiles,
             });
             //админ имеет доступ по умолчанию
             var adminRole = await Database.Roles.FindAsync(r => r.Name == "admin");
             catAdd.CategoryRoles.Add(new CategoryRole
             {
                 CatalogRoleId = adminRole.FirstOrDefault().Id,
                 DownloadFiles = true,
             });
         }
         Database.Categories.Create(catAdd);
         int result = await Database.SaveAsync();
         if (result > 0)
         {
             return cat;
         }
     }
     return null;
 }