| | | 1 | | /* |
| | | 2 | | * EventListStorageTests.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 | | namespace event_list.modules.eventlist.storage; |
| | | 10 | | public class EventListStorage : IEventListStorage |
| | | 11 | | { |
| | | 12 | | private readonly EventListDbContext _context; |
| | | 13 | | |
| | 0 | 14 | | public EventListStorage(EventListDbContext context) |
| | 0 | 15 | | { |
| | 0 | 16 | | _context = context; |
| | 0 | 17 | | } |
| | | 18 | | public async Task CreateAsync(EventFormDto entity) |
| | 0 | 19 | | { |
| | 0 | 20 | | await _context.Events.AddAsync(entity); |
| | 0 | 21 | | await _context.SaveChangesAsync(); |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task DeleteAsync(Guid id) |
| | 0 | 25 | | { |
| | | 26 | | |
| | 0 | 27 | | var eventItem = await _context.Events.FindAsync(id); |
| | 0 | 28 | | if (eventItem != null) { |
| | 0 | 29 | | _context.Events.Remove(eventItem); |
| | 0 | 30 | | await _context.SaveChangesAsync(); |
| | 0 | 31 | | } |
| | 0 | 32 | | } |
| | | 33 | | |
| | | 34 | | // public IEnumerable<EventFormDto> GetAllAsync() => _context.Events.ToList(); |
| | | 35 | | |
| | | 36 | | public IEnumerable<EventListDto> GetAllAsync() |
| | 0 | 37 | | { |
| | | 38 | | |
| | 0 | 39 | | return _context.Events.Select(e => new EventListDto |
| | 0 | 40 | | { |
| | 0 | 41 | | Id = e.Id, |
| | 0 | 42 | | Title = e.Title, |
| | 0 | 43 | | Date = e.Date |
| | 0 | 44 | | }) |
| | 0 | 45 | | .ToList(); |
| | 0 | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | public async Task<EventFormDto?> GetByIdAsync(Guid id) => await _context.Events.FindAsync(id); |
| | | 49 | | } |