Просмотр исходного кода

Added logging to PatternService methods and SelectionLevelService GetByIdEntity method

pull/79/head
Ermin Bronja 3 лет назад
Родитель
Сommit
d0f9138d10

+ 68
- 6
Diligent.WebAPI.Business/Services/PatternService.cs Просмотреть файл

@@ -12,81 +12,143 @@ namespace Diligent.WebAPI.Business.Services
private readonly DatabaseContext _context;
private readonly IMapper _mapper;
private readonly ISelectionLevelService _selectionLevelService;
private readonly ILogger<PatternService> _logger;

public PatternService(DatabaseContext context, IMapper mapper, ISelectionLevelService selectionLevelService)
public PatternService(DatabaseContext context, IMapper mapper, ISelectionLevelService selectionLevelService, ILogger<PatternService> logger)
{
_context = context;
_mapper = mapper;
_selectionLevelService = selectionLevelService;
_logger = logger;
}

public async Task<List<PatternResponseDto>> GetAllAsync()
{
return _mapper.Map<List<PatternResponseDto>>(await _context.Patterns.Include(x => x.SelectionLevel).ToListAsync());
_logger.LogInformation("Start getting all Patterns");
_logger.LogInformation("Getting data from DB");
var fromDb = await _context.Patterns.Include(x => x.SelectionLevel).ToListAsync();
_logger.LogInformation($"Received {fromDb.Count} patterns from db.");
_logger.LogInformation($"Mapping received patterns to PatternResponseDto");
var result = _mapper.Map<List<PatternResponseDto>>(fromDb);
_logger.LogInformation($"Patterns has been mapped and received to client: {result.Count} mapped patterns");
return result;
}

public async Task<PatternResponseDto> GetByIdAsync(int id)
{
_logger.LogInformation($"Start searching Pattern with id = {id}");
var pattern = await _context.Patterns.Include(x => x.SelectionLevel).Where(x => x.Id == id).FirstOrDefaultAsync();

if (pattern is null)
{
_logger.LogError($"Pattern with id = {id} not found");
throw new EntityNotFoundException("Pattern not found");
}

return _mapper.Map<PatternResponseDto>(pattern);
_logger.LogInformation($"Mapping Pattern with id = {id}");
PatternResponseDto result = _mapper.Map<PatternResponseDto>(pattern);
_logger.LogInformation($"Pattern with id = {id} mapped successfully");
return result;
}

public async Task CreateAsync(PatternCreateDto patternCreateDto)
{
_logger.LogInformation($"Start creating Pattern");
_logger.LogInformation($"Check is Pattern in database");
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)
{
_logger.LogError($"Pattern already exists in database");
throw new EntityNotFoundException("Pattern already exists in database");
}

_logger.LogError($"Pattern are not in database");
_logger.LogInformation($"Mapping PatternCreateDto to model");
var pattern = _mapper.Map<Pattern>(patternCreateDto);
_logger.LogInformation($"Pattern mapped successfully");


_logger.LogInformation($"Start searching SelectionLevel with id = {patternCreateDto.SelectionLevelId}");
var selectionLevel = await _selectionLevelService.GetByIdEntity(patternCreateDto.SelectionLevelId);

if(selectionLevel == null)
{
_logger.LogError($"SelectionLevel with id = {patternCreateDto.SelectionLevelId} not found");
throw new EntityNotFoundException("Selection level not found");
}

_logger.LogInformation($"Add founded SelectionLevel to pattern");
pattern.SelectionLevel = selectionLevel;

await _context.AddAsync(pattern);
_logger.LogInformation($"Saving Ad to db...");
var result = _context.SaveChangesAsync();
_logger.LogInformation($"Saved Ad to db...");

await _context.SaveChangesAsync();
await result;
}

public async Task UpdateAsync(PatternUpdateDto patternUpdateDto, int id)
{
_logger.LogInformation($"Start updating Pattern");
_logger.LogInformation($"Check is Pattern in database");
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)
{
_logger.LogError($"Pattern already exists in database");
throw new EntityNotFoundException("Pattern already exists in database");
}

_logger.LogInformation($"Start searching Pattern with id = {id}");
var pattern = await _context.Patterns.Where(x => x.Id == id).FirstOrDefaultAsync();

if (pattern is null)
{
_logger.LogError($"Pattern with id = {id} not found");
throw new EntityNotFoundException("Pattern not found");
}

_logger.LogInformation($"Mapping Pattern with id = {id}");
_mapper.Map(patternUpdateDto, pattern);
_logger.LogInformation($"Pattern with id = {id} mapped successfully");


_logger.LogInformation($"Start searching SelectionLevel with id = {patternUpdateDto.SelectionLevelId}");
var selectionLevel = await _selectionLevelService.GetByIdEntity(patternUpdateDto.SelectionLevelId);

if (selectionLevel == null)
{
_logger.LogError($"SelectionLevel with id = {patternUpdateDto.SelectionLevelId} not found");
throw new EntityNotFoundException("Selection level not found");
}

_logger.LogInformation($"Add founded SelectionLevel to pattern");
pattern.SelectionLevel = selectionLevel;

_context.Entry(pattern).State = EntityState.Modified;
await _context.SaveChangesAsync();
var result = _context.SaveChangesAsync();
_logger.LogInformation($"Pattern saved to DB");
await result;
}

public async Task DeleteAsync(int id)
{
_logger.LogInformation($"Start searching Pattern with id = {id}");
var pattern = await _context.Patterns.FindAsync(id);

if (pattern is null)
{
_logger.LogError($"Pattern with id = {id} not found");
throw new EntityNotFoundException("Pattern not found");
}

_context.Patterns.Remove(pattern);
await _context.SaveChangesAsync();
var result = _context.SaveChangesAsync();
_logger.LogInformation($"Ad saved to DB");
await result;
}
}
}

+ 5
- 0
Diligent.WebAPI.Business/Services/SelectionLevelService.cs Просмотреть файл

@@ -49,11 +49,16 @@

public async Task<SelectionLevel> GetByIdEntity(int id)
{
_logger.LogInformation($"Start searching Level with id = {id}");
var sl = await _context.SelectionLevels.FindAsync(id);

if (sl is null)
{
_logger.LogError($"Level with id = {id} not found");
throw new EntityNotFoundException("Selection level not found");
}

_logger.LogError($"Level with id = {id} found and returned to client");
return sl;
}


Загрузка…
Отмена
Сохранить