| | | 1 | | /* |
| | | 2 | | * eventListSave.cs |
| | | 3 | | * event-list |
| | | 4 | | * |
| | | 5 | | * Created by Tiago Amaral on 06/09/2025. |
| | | 6 | | * Copyright ©2024 Tiago Amaral. All rights reserved. |
| | | 7 | | */ |
| | | 8 | | |
| | | 9 | | using System.ComponentModel.DataAnnotations; |
| | | 10 | | using event_list.modules.eventlist.storage; |
| | | 11 | | |
| | | 12 | | namespace event_list.modules.eventlist.services; |
| | | 13 | | |
| | | 14 | | public interface IEventListSaveService |
| | | 15 | | { |
| | | 16 | | Task Add(EventFormDto dto); |
| | | 17 | | } |
| | | 18 | | public class EventListSaveService : IEventListSaveService |
| | | 19 | | { |
| | | 20 | | private readonly IEventListStorage _storage; |
| | | 21 | | |
| | 2 | 22 | | public EventListSaveService(IEventListStorage storage) |
| | 2 | 23 | | { |
| | 2 | 24 | | _storage = storage; |
| | 2 | 25 | | } |
| | | 26 | | |
| | 2 | 27 | | public async Task Add(EventFormDto dto) { |
| | | 28 | | |
| | 2 | 29 | | var results = new List<ValidationResult>(); |
| | 2 | 30 | | var context = new ValidationContext(dto); |
| | 2 | 31 | | if (!Validator.TryValidateObject(dto, context, results, true)) |
| | 1 | 32 | | { |
| | 1 | 33 | | throw new ValidationException(results.First().ErrorMessage); |
| | | 34 | | } |
| | 1 | 35 | | dto.Id = Guid.NewGuid(); |
| | 1 | 36 | | await _storage.CreateAsync(dto); |
| | 1 | 37 | | } |
| | | 38 | | } |