// TODO: move this method to service private async Task RaiseAdvertConfirmedMessageAsync(ConfirmAdvertDto confirmModel) { var topicArn = _configuration.GetValue <string>("TopicArn"); // To get the title we fetch the advert from db, // or in another way you can pass Title with ConfirmAdvertDto var advert = await _advertStorageService.GetByIdAsync(confirmModel.Id); if (advert == null) { throw new KeyNotFoundException($"Record with Id: {confirmModel.Id} was not found."); } using (var snsClient = new AmazonSimpleNotificationServiceClient()) { var message = new AdvertConfirmedMessage { Id = confirmModel.Id, Title = advert.Title }; var messageJson = JsonConvert.SerializeObject(message); await snsClient.PublishAsync(topicArn, messageJson); } }
public async Task ConfirmAsync(ConfirmAdvertDto model) { using (var client = new AmazonDynamoDBClient()) { using (var context = new DynamoDBContext(client)) { var advert = await context.LoadAsync <AdvertDbModel>(model.Id); if (advert == null) { throw new KeyNotFoundException($"Record with Id: {model.Id} was not found."); } if (model.Status == AdvertStatus.Active) { advert.Status = AdvertStatus.Active; advert.FilePath = model.FilePath; await context.SaveAsync(advert); } else { await context.DeleteAsync(advert); } } } }
public async Task <IActionResult> Confirm(ConfirmAdvertDto model) { // TODO: use messaging (lecture 15) try { await _advertStorageService.ConfirmAsync(model); await RaiseAdvertConfirmedMessageAsync(model); } catch (KeyNotFoundException ex) { return(NotFound(ex.Message)); } return(Ok()); }
public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile) { if (ModelState.IsValid) { var createAdvertDto = _mapper.Map <CreateAdvertDto>(model); var apiCallResponse = await _advertApiClient.Create(createAdvertDto).ConfigureAwait(false); var id = apiCallResponse.Id; // bool isOkToConfirmAd = true; string filePath = string.Empty; if (imageFile != null) { var fileName = !string.IsNullOrEmpty(imageFile.FileName) ? Path.GetFileName(imageFile.FileName) : id; filePath = $"{id}/{fileName}"; try { using (var readStream = imageFile.OpenReadStream()) { var result = await _fileUploader.UploadFileAsync(filePath, readStream) .ConfigureAwait(false); if (!result) { throw new Exception( "Could not upload the image to file repository. Please see the logs for details."); } } } catch (Exception e) { var confirmDto = new ConfirmAdvertDto() { Id = id, FilePath = filePath, Status = AdvertStatus.Pending }; await _advertApiClient.Confirm(confirmDto).ConfigureAwait(false); Console.WriteLine(e); } } var confirmModel = new ConfirmAdvertDto() { Id = id, FilePath = filePath, Status = AdvertStatus.Active }; await _advertApiClient.Confirm(confirmModel).ConfigureAwait(false); return(RedirectToAction("Index", "Home")); } return(View(model)); /*var apiCallResponse = await _advertApiClient.CreateAsync(createAdvertModel).ConfigureAwait(false); * var id = apiCallResponse.Id; * * bool isOkToConfirmAd = true; * string filePath = string.Empty; * if (imageFile != null) * { * var fileName = !string.IsNullOrEmpty(imageFile.FileName) ? Path.GetFileName(imageFile.FileName) : id; * filePath = $"{id}/{fileName}"; * * try * { * using (var readStream = imageFile.OpenReadStream()) * { * var result = await _fileUploader.UploadFileAsync(filePath, readStream) * .ConfigureAwait(false); * if (!result) * throw new Exception( * "Could not upload the image to file repository. Please see the logs for details."); * } * } * catch (Exception e) * { * isOkToConfirmAd = false; * var confirmModel = new ConfirmAdvertRequest() * { * Id = id, * FilePath = filePath, * Status = AdvertStatus.Pending * }; * await _advertApiClient.ConfirmAsync(confirmModel).ConfigureAwait(false); * Console.WriteLine(e); * } * * * } * * if (isOkToConfirmAd) * { * var confirmModel = new ConfirmAdvertRequest() * { * Id = id, * FilePath = filePath, * Status = AdvertStatus.Active * }; * await _advertApiClient.ConfirmAsync(confirmModel).ConfigureAwait(false); * } * * return RedirectToAction("Index", "Home"); * } * * return View(model);*/ }