public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile) { if (ModelState.IsValid) { var createAdvertModel = _mapper.Map <CreateAdvertModel>(model); var advertResponse = await _advertApiClient.CreateAsync(createAdvertModel); var id = advertResponse.Id; if (imageFile != null) { var fileName = !string.IsNullOrEmpty(imageFile.FileName) ? Path.GetFileName(imageFile.FileName) : id; var 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 file"); } } var confirmModel = new ConfirmAdvertModelRequest() { Id = id, FilePath = filePath, Status = AdvertStatus.Active }; var canConfirm = await _advertApiClient.ConfirmAsync(confirmModel); if (!canConfirm) { throw new Exception($"Cannot confirm advert of id = {id}"); } return(RedirectToAction("Index", "Home")); } catch (Exception e) { var confirmModel = new ConfirmAdvertModelRequest() { Id = id, FilePath = filePath, Status = AdvertStatus.Pending }; await _advertApiClient.ConfirmAsync(confirmModel); } } } return(View(model)); }
public async Task <bool> ConfirmAsync(ConfirmAdvertModelRequest model) { var confirmAdvertModel = _mapper.Map <ConfirmAdvertModel>(model); var jsonModel = JsonConvert.SerializeObject(confirmAdvertModel); var response = await _httpClient.PutAsync($"{_httpClient.BaseAddress}/confirm", new StringContent(jsonModel)); return(response.StatusCode == HttpStatusCode.OK); }