Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PatternService.cs 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using Diligent.WebAPI.Contracts.DTOs.Pattern;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace Diligent.WebAPI.Business.Services
  8. {
  9. public class PatternService : IPatternService
  10. {
  11. private readonly DatabaseContext _context;
  12. private readonly IMapper _mapper;
  13. private readonly ISelectionLevelService _selectionLevelService;
  14. public PatternService(DatabaseContext context, IMapper mapper, ISelectionLevelService selectionLevelService)
  15. {
  16. _context = context;
  17. _mapper = mapper;
  18. _selectionLevelService = selectionLevelService;
  19. }
  20. public async Task<List<PatternResponseDto>> GetAllAsync()
  21. {
  22. return _mapper.Map<List<PatternResponseDto>>(await _context.Patterns.Include(x => x.SelectionLevel).ToListAsync());
  23. }
  24. public async Task<PatternResponseDto> GetByIdAsync(int id)
  25. {
  26. var pattern = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Id == id).FirstOrDefaultAsync();
  27. if (pattern is null)
  28. throw new EntityNotFoundException("Pattern not found");
  29. return _mapper.Map<PatternResponseDto>(pattern);
  30. }
  31. public async Task CreateAsync(PatternCreateDto patternCreateDto)
  32. {
  33. var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternCreateDto.Title && x.SelectionLevelId == patternCreateDto.SelectionLevelId).FirstOrDefaultAsync();
  34. if (patternExists is not null)
  35. throw new EntityNotFoundException("Pattern already exists in database");
  36. var pattern = _mapper.Map<Pattern>(patternCreateDto);
  37. var selectionLevel = await _selectionLevelService.GetByIdEntity(patternCreateDto.SelectionLevelId);
  38. if(selectionLevel == null)
  39. throw new EntityNotFoundException("Selection level not found");
  40. pattern.SelectionLevel = selectionLevel;
  41. await _context.AddAsync(pattern);
  42. await _context.SaveChangesAsync();
  43. }
  44. public async Task UpdateAsync(PatternUpdateDto patternUpdateDto, int id)
  45. {
  46. var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternUpdateDto.Title && x.SelectionLevelId == patternUpdateDto.SelectionLevelId).FirstOrDefaultAsync();
  47. if (patternExists is not null)
  48. throw new EntityNotFoundException("Pattern already exists in database");
  49. var pattern = await _context.Patterns.Where(x => x.Id == id).FirstOrDefaultAsync();
  50. if (pattern is null)
  51. throw new EntityNotFoundException("Pattern not found");
  52. _mapper.Map(patternUpdateDto, pattern);
  53. var selectionLevel = await _selectionLevelService.GetByIdEntity(patternUpdateDto.SelectionLevelId);
  54. pattern.SelectionLevel = selectionLevel;
  55. _context.Entry(pattern).State = EntityState.Modified;
  56. await _context.SaveChangesAsync();
  57. }
  58. public async Task DeleteAsync(int id)
  59. {
  60. var pattern = await _context.Patterns.FindAsync(id);
  61. if (pattern is null)
  62. throw new EntityNotFoundException("Pattern not found");
  63. _context.Patterns.Remove(pattern);
  64. await _context.SaveChangesAsync();
  65. }
  66. }
  67. }