private async Task ValidateIfExist(RemmittanceModel remmittanceModel) { var existingEntity = await _remmittanceRepository.GetByIdAsync(remmittanceModel.Id); if (existingEntity != null) { throw new ApplicationException("Remmittance with this id already exists."); } }
public async Task <ActionResult <RemmittanceModel> > PostRemmittance(RemmittanceModel remmittanceModel) { if (remmittanceModel == null) { return(BadRequest()); } var created = await _remmittanceService.Create(remmittanceModel); return(CreatedAtAction("GetRemmittance", new { id = created.Id }, created)); }
public async Task <RemmittanceModel> Create(RemmittanceModel remmittanceModel) { await ValidateIfExist(remmittanceModel); try { var mappedEntity = ObjectMapper.Mapper.Map <Remittance>(remmittanceModel); if (mappedEntity == null) { throw new ApplicationException($"Entity could not be mapped."); } var newEntity = await _remmittanceRepository.Create(mappedEntity); _logger.LogInformation($"Entity successfully added."); var newMappedEntity = ObjectMapper.Mapper.Map <RemmittanceModel>(newEntity); return(newMappedEntity); } catch (Exception) { return(null); } }