using HelpDesk.Models.Entities; using Microsoft.EntityFrameworkCore; namespace HelpDesk.Data.Repositories; public class KnowledgeBaseRepository : IKnowledgeBaseRepository { private readonly HelpDeskDbContext _context; public KnowledgeBaseRepository(HelpDeskDbContext context) { _context = context; } // Articles public async Task GetArticleByIdAsync(Guid id) => await _context.KnowledgeBaseArticles .Include(a => a.Category) .Include(a => a.Author) .FirstOrDefaultAsync(a => a.Id == id); public async Task> GetAllArticlesAsync() => await _context.KnowledgeBaseArticles .Include(a => a.Category) .Include(a => a.Author) .OrderByDescending(a => a.CreatedAt) .ToListAsync(); public async Task> GetPublishedArticlesAsync() => await _context.KnowledgeBaseArticles .Include(a => a.Category) .Include(a => a.Author) .Where(a => a.IsPublished) .OrderByDescending(a => a.CreatedAt) .ToListAsync(); public async Task> SearchArticlesAsync(string query) { var lowerQuery = query.ToLower(); return await _context.KnowledgeBaseArticles .Include(a => a.Category) .Include(a => a.Author) .Where(a => a.IsPublished && (a.Title.ToLower().Contains(lowerQuery) || a.Content.ToLower().Contains(lowerQuery))) .OrderByDescending(a => a.ViewCount) .ToListAsync(); } public async Task CreateArticleAsync(KnowledgeBaseArticle article) { _context.KnowledgeBaseArticles.Add(article); await _context.SaveChangesAsync(); return article; } public async Task UpdateArticleAsync(KnowledgeBaseArticle article) { var existing = await _context.KnowledgeBaseArticles.FindAsync(article.Id); if (existing == null) return null; existing.Title = article.Title; existing.Content = article.Content; existing.CategoryId = article.CategoryId; existing.IsPublished = article.IsPublished; existing.UpdatedAt = DateTime.UtcNow; await _context.SaveChangesAsync(); return existing; } public async Task DeleteArticleAsync(Guid id) { var article = await _context.KnowledgeBaseArticles.FindAsync(id); if (article == null) return false; _context.KnowledgeBaseArticles.Remove(article); await _context.SaveChangesAsync(); return true; } // Categories public async Task GetCategoryByIdAsync(Guid id) => await _context.KnowledgeBaseCategories .Include(c => c.Articles) .FirstOrDefaultAsync(c => c.Id == id); public async Task> GetAllCategoriesAsync() => await _context.KnowledgeBaseCategories .Include(c => c.Articles) .OrderBy(c => c.SortOrder) .ThenBy(c => c.Name) .ToListAsync(); public async Task CreateCategoryAsync(KnowledgeBaseCategory category) { _context.KnowledgeBaseCategories.Add(category); await _context.SaveChangesAsync(); return category; } public async Task UpdateCategoryAsync(KnowledgeBaseCategory category) { var existing = await _context.KnowledgeBaseCategories.FindAsync(category.Id); if (existing == null) return null; existing.Name = category.Name; existing.Description = category.Description; existing.SortOrder = category.SortOrder; await _context.SaveChangesAsync(); return existing; } public async Task DeleteCategoryAsync(Guid id) { var category = await _context.KnowledgeBaseCategories.FindAsync(id); if (category == null) return false; _context.KnowledgeBaseCategories.Remove(category); await _context.SaveChangesAsync(); return true; } }