| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using Diligent.WebAPI.Contracts.DTOs.Pattern;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Diligent.WebAPI.Business.Services
- {
- public class PatternService : IPatternService
- {
- private readonly DatabaseContext _context;
- private readonly IMapper _mapper;
- private readonly ISelectionLevelService _selectionLevelService;
-
- public PatternService(DatabaseContext context, IMapper mapper, ISelectionLevelService selectionLevelService)
- {
- _context = context;
- _mapper = mapper;
- _selectionLevelService = selectionLevelService;
- }
-
- public async Task<List<PatternResponseDto>> GetAllAsync()
- {
- return _mapper.Map<List<PatternResponseDto>>(await _context.Patterns.Include(x => x.SelectionLevel).ToListAsync());
- }
-
- public async Task<PatternResponseDto> GetByIdAsync(int id)
- {
- var pattern = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Id == id).FirstOrDefaultAsync();
-
- if (pattern is null)
- throw new EntityNotFoundException("Pattern not found");
-
- return _mapper.Map<PatternResponseDto>(pattern);
- }
-
- public async Task CreateAsync(PatternCreateDto patternCreateDto)
- {
- var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternCreateDto.Title && x.SelectionLevelId == patternCreateDto.SelectionLevelId).FirstOrDefaultAsync();
-
- if (patternExists is not null)
- throw new EntityNotFoundException("Pattern already exists in database");
-
- var pattern = _mapper.Map<Pattern>(patternCreateDto);
-
- var selectionLevel = await _selectionLevelService.GetByIdEntity(patternCreateDto.SelectionLevelId);
-
- if(selectionLevel == null)
- throw new EntityNotFoundException("Selection level not found");
-
- pattern.SelectionLevel = selectionLevel;
-
- await _context.AddAsync(pattern);
-
- await _context.SaveChangesAsync();
- }
-
- public async Task UpdateAsync(PatternUpdateDto patternUpdateDto, int id)
- {
- var patternExists = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Title == patternUpdateDto.Title && x.SelectionLevelId == patternUpdateDto.SelectionLevelId).FirstOrDefaultAsync();
-
- if (patternExists is not null)
- throw new EntityNotFoundException("Pattern already exists in database");
-
- var pattern = await _context.Patterns.Where(x => x.Id == id).FirstOrDefaultAsync();
-
- if (pattern is null)
- throw new EntityNotFoundException("Pattern not found");
-
- _mapper.Map(patternUpdateDto, pattern);
-
- var selectionLevel = await _selectionLevelService.GetByIdEntity(patternUpdateDto.SelectionLevelId);
-
- pattern.SelectionLevel = selectionLevel;
-
- _context.Entry(pattern).State = EntityState.Modified;
- await _context.SaveChangesAsync();
- }
-
- public async Task DeleteAsync(int id)
- {
- var pattern = await _context.Patterns.FindAsync(id);
-
- if (pattern is null)
- throw new EntityNotFoundException("Pattern not found");
-
- _context.Patterns.Remove(pattern);
- await _context.SaveChangesAsync();
- }
- }
- }
|