using System.ComponentModel.DataAnnotations; using Journal.Core.Dtos; using Journal.Core.Models; using Journal.Core.Repositories; namespace Journal.Core.Services.Lists; public class ListService(IListRepository repo) : IListService { private readonly IListRepository _repo = repo ?? throw new ArgumentNullException(nameof(repo)); private static ListDocumentDto Map(ListDocument d) => new( d.Id, d.Label, d.Content, d.CreatedAt, d.UpdatedAt ); public List GetAll() { var items = _repo.GetAll(); return [.. items.Select(Map)]; } public ListDocumentDto? GetById(Guid id) { var d = _repo.GetById(id); return d is null ? null : Map(d); } public ListDocumentDto Create(CreateListDto dto) { ArgumentNullException.ThrowIfNull(dto); var ctx = new ValidationContext(dto); Validator.ValidateObject(dto, ctx, validateAllProperties: true); var doc = new ListDocument(dto.Label, dto.Content ?? ""); _repo.Add(doc); return Map(doc); } public bool Update(Guid id, UpdateListDto dto) { ArgumentNullException.ThrowIfNull(dto); if (dto.Label is not null && string.IsNullOrWhiteSpace(dto.Label)) throw new ValidationException("Label cannot be empty"); return _repo.Update(id, dto.Label?.Trim(), dto.Content); } public bool Remove(Guid id) => _repo.Remove(id); }