/
plug1at
/
Personal_Digital_Library
Обзор
Документация
Войти
/
plug1at
/
Personal_Digital_Library
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
DigitalLibrary.Data/Repositories/CategoryRepository.cs
131 строка
4 KB
plug1at
Create: Class1.cs, DigitalLibrary.Data.csproj, LibraryDbContext.cs, BookRepository.cs, CategoryRepository.cs, IBookRepository.cs, ICategoryRepository.cs
10 июн 2026, 11:29
Верифицирован
10 июн 2026, 11:29
8593c6e
Код
Авторство
О чём код?
using Microsoft.EntityFrameworkCore; using DigitalLibrary.Core.Models; using System.Collections.ObjectModel; namespace DigitalLibrary.Data.Repositories { public class CategoryRepository : ICategoryRepository { private readonly LibraryDbContext _context; public CategoryRepository(LibraryDbContext context) { _context = context; } public async Task<List<Category>> GetAllCategoriesAsync() { var allCategories = await _context.Categories .AsNoTracking() .ToListAsync(); // ��� ������ ��������� ������ ObservableCollection �� �������� foreach (var category in allCategories) { category.SubCategories = new ObservableCollection<Category>( allCategories.Where(c => c.ParentCategoryId == category.Id) ); } var rootCategories = allCategories .Where(c => c.ParentCategoryId == null) .ToList(); return rootCategories; } public async Task<Category?> GetCategoryByIdAsync(int id) { return await _context.Categories .FirstOrDefaultAsync(c => c.Id == id); } public async Task AddCategoryAsync(Category category) { if (category.ParentCategoryId == 0) category.ParentCategoryId = null; try { _context.Categories.Add(category); await _context.SaveChangesAsync(); } catch (Exception ex) { throw new InvalidOperationException($"������ ���������� ���������: {ex.Message}", ex); } } public async Task UpdateCategoryAsync(Category category) { try { var existingCategory = await _context.Categories.FindAsync(category.Id); if (existingCategory != null) { existingCategory.Name = category.Name; existingCategory.ParentCategoryId = category.ParentCategoryId; await _context.SaveChangesAsync(); } } catch (Exception ex) { throw new InvalidOperationException($"������ ���������� ���������: {ex.Message}", ex); } } public async Task DeleteCategoryAsync(int id) { var category = await _context.Categories.FindAsync(id); if (category != null) { // ���������� ������� ��� ������������ await DeleteSubCategoriesRecursive(id); // ����������� ����� �� ���� ��������� var books = await _context.Books .Where(b => b.CategoryId == id) .ToListAsync(); foreach (var book in books) { book.CategoryId = null; } _context.Categories.Remove(category); await _context.SaveChangesAsync(); } } // ��������������� ����� ��� ������������ �������� ������������ private async Task DeleteSubCategoriesRecursive(int parentCategoryId) { var subCategories = await _context.Categories .Where(c => c.ParentCategoryId == parentCategoryId) .ToListAsync(); foreach (var subCategory in subCategories) { // ���������� ������� ������������ �������� ������ await DeleteSubCategoriesRecursive(subCategory.Id); // ����������� ����� �� ������������ var books = await _context.Books .Where(b => b.CategoryId == subCategory.Id) .ToListAsync(); foreach (var book in books) { book.CategoryId = null; } // ������� ������������ _context.Categories.Remove(subCategory); } } } }