Files
helpdesk/backend/HelpDesk.Data/Repositories/KnowledgeBaseRepository.cs
T

121 lines
4.1 KiB
C#

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<KnowledgeBaseArticle?> GetArticleByIdAsync(Guid id)
=> await _context.KnowledgeBaseArticles
.Include(a => a.Category)
.Include(a => a.Author)
.FirstOrDefaultAsync(a => a.Id == id);
public async Task<IEnumerable<KnowledgeBaseArticle>> GetAllArticlesAsync()
=> await _context.KnowledgeBaseArticles
.Include(a => a.Category)
.Include(a => a.Author)
.OrderByDescending(a => a.CreatedAt)
.ToListAsync();
public async Task<IEnumerable<KnowledgeBaseArticle>> GetPublishedArticlesAsync()
=> await _context.KnowledgeBaseArticles
.Include(a => a.Category)
.Include(a => a.Author)
.Where(a => a.IsPublished)
.OrderByDescending(a => a.CreatedAt)
.ToListAsync();
public async Task<IEnumerable<KnowledgeBaseArticle>> 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<KnowledgeBaseArticle> CreateArticleAsync(KnowledgeBaseArticle article)
{
_context.KnowledgeBaseArticles.Add(article);
await _context.SaveChangesAsync();
return article;
}
public async Task<KnowledgeBaseArticle?> 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<bool> 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<KnowledgeBaseCategory?> GetCategoryByIdAsync(Guid id)
=> await _context.KnowledgeBaseCategories
.Include(c => c.Articles)
.FirstOrDefaultAsync(c => c.Id == id);
public async Task<IEnumerable<KnowledgeBaseCategory>> GetAllCategoriesAsync()
=> await _context.KnowledgeBaseCategories
.Include(c => c.Articles)
.OrderBy(c => c.SortOrder)
.ThenBy(c => c.Name)
.ToListAsync();
public async Task<KnowledgeBaseCategory> CreateCategoryAsync(KnowledgeBaseCategory category)
{
_context.KnowledgeBaseCategories.Add(category);
await _context.SaveChangesAsync();
return category;
}
public async Task<KnowledgeBaseCategory?> 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<bool> DeleteCategoryAsync(Guid id)
{
var category = await _context.KnowledgeBaseCategories.FindAsync(id);
if (category == null) return false;
_context.KnowledgeBaseCategories.Remove(category);
await _context.SaveChangesAsync();
return true;
}
}