| | | 1 | | using Chronicis.Api.Data; |
| | | 2 | | using Chronicis.Shared.DTOs; |
| | | 3 | | using Microsoft.EntityFrameworkCore; |
| | | 4 | | |
| | | 5 | | namespace Chronicis.Api.Services; |
| | | 6 | | |
| | | 7 | | public class ArticleValidationService : IArticleValidationService |
| | | 8 | | { |
| | | 9 | | private readonly ChronicisDbContext _context; |
| | | 10 | | |
| | 16 | 11 | | public ArticleValidationService(ChronicisDbContext context) |
| | | 12 | | { |
| | 16 | 13 | | _context = context; |
| | 16 | 14 | | } |
| | | 15 | | |
| | | 16 | | public async Task<ValidationResult> ValidateCreateAsync(ArticleCreateDto dto) |
| | | 17 | | { |
| | 4 | 18 | | var result = new ValidationResult(); |
| | | 19 | | |
| | | 20 | | // Parent must exist if specified |
| | 4 | 21 | | if (dto.ParentId.HasValue) |
| | | 22 | | { |
| | 2 | 23 | | var parentExists = await _context.Articles |
| | 2 | 24 | | .AnyAsync(a => a.Id == dto.ParentId.Value); |
| | | 25 | | |
| | 2 | 26 | | if (!parentExists) |
| | | 27 | | { |
| | 1 | 28 | | result.AddError("ParentId", "Parent article does not exist"); |
| | | 29 | | } |
| | | 30 | | } |
| | | 31 | | |
| | 4 | 32 | | return result; |
| | 4 | 33 | | } |
| | | 34 | | |
| | | 35 | | public async Task<ValidationResult> ValidateUpdateAsync(Guid articleId, ArticleUpdateDto dto) |
| | | 36 | | { |
| | 5 | 37 | | var result = new ValidationResult(); |
| | | 38 | | |
| | | 39 | | // Article must exist |
| | 5 | 40 | | var articleExists = await _context.Articles |
| | 5 | 41 | | .AnyAsync(a => a.Id == articleId); |
| | | 42 | | |
| | 5 | 43 | | if (!articleExists) |
| | | 44 | | { |
| | 1 | 45 | | result.AddError("Id", "Article not found"); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | // Title is required |
| | 5 | 49 | | if (string.IsNullOrWhiteSpace(dto.Title)) |
| | | 50 | | { |
| | 3 | 51 | | result.AddError("Title", "Title is required"); |
| | | 52 | | } |
| | | 53 | | |
| | 5 | 54 | | return result; |
| | 5 | 55 | | } |
| | | 56 | | |
| | | 57 | | public async Task<ValidationResult> ValidateDeleteAsync(Guid articleId) |
| | | 58 | | { |
| | 3 | 59 | | var result = new ValidationResult(); |
| | | 60 | | |
| | | 61 | | // Article must exist |
| | 3 | 62 | | var article = await _context.Articles |
| | 3 | 63 | | .Include(a => a.Children) |
| | 3 | 64 | | .FirstOrDefaultAsync(a => a.Id == articleId); |
| | | 65 | | |
| | 3 | 66 | | if (article == null) |
| | | 67 | | { |
| | 1 | 68 | | result.AddError("Id", "Article not found"); |
| | 1 | 69 | | return result; |
| | | 70 | | } |
| | | 71 | | |
| | 2 | 72 | | return result; |
| | 3 | 73 | | } |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Represents the result of a validation operation. |
| | | 78 | | /// </summary> |
| | | 79 | | public class ValidationResult |
| | | 80 | | { |
| | | 81 | | private readonly Dictionary<string, List<string>> _errors = new(); |
| | | 82 | | |
| | | 83 | | public bool IsValid => _errors.Count == 0; |
| | | 84 | | |
| | | 85 | | public IReadOnlyDictionary<string, List<string>> Errors => _errors; |
| | | 86 | | |
| | | 87 | | public void AddError(string field, string message) |
| | | 88 | | { |
| | | 89 | | if (!_errors.ContainsKey(field)) |
| | | 90 | | { |
| | | 91 | | _errors[field] = new List<string>(); |
| | | 92 | | } |
| | | 93 | | _errors[field].Add(message); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | public string GetFirstError() |
| | | 97 | | { |
| | | 98 | | return _errors.Values.FirstOrDefault()?.FirstOrDefault() ?? "Validation failed"; |
| | | 99 | | } |
| | | 100 | | } |