public async Task <ActionResult> DeleteConfirmed(long id) { try { using (var dataApiClient = CompleteDataApi.NewDataApiClient()) { var ad = await dataApiClient.Ads.DeleteAdAsync(id); await DeleteAdBlobsAsync(ad); return(RedirectToAction("Index")); } } catch (OperationCanceledException ex) { return(LogAndShowError(ex)); } catch (AggregateException ex) { return(LogAndShowError(ex)); } catch (HttpOperationException ex) { return(LogAndShowError(ex)); } //catch (Exception ex) //{ // return LogAndShowError(ex); //} }
// GET: Ads/Create public async Task <ActionResult> Create() { try { using (var dataApiClient = CompleteDataApi.NewDataApiClient()) { await InitCategoriesAsync(dataApiClient); return(View()); } } catch (OperationCanceledException ex) { return(LogAndShowError(ex)); } catch (AggregateException ex) { return(LogAndShowError(ex)); } catch (HttpOperationException ex) { return(LogAndShowError(ex)); } //catch (Exception ex) //{ // return LogAndShowError(ex); //} }
public async Task <ActionResult> Edit( [Bind(Include = "Id,Title,Description,CategoryId,ImageUrl,ThumbnailUrl,Price,Phone,Posted")] Ad ad, //ImageUrl,ThumbnailUrl, HttpPostedFileBase imageFile) { if (ad == null) { return(View(ad)); } try { using (var dataApiClient = CompleteDataApi.NewDataApiClient()) { if (ModelState.IsValid && ad.Id != null) { CloudBlockBlob imageBlob = null; if (imageFile != null && imageFile.ContentLength != 0) { // User is changing the image -- delete the existing // image blobs and then upload and save a new one. await DeleteAdBlobsAsync(ad); imageBlob = await AzureConfig.UploadAndSaveBlobAsync(imageFile); ad.ImageUrl = imageBlob.Uri.ToString(); } await dataApiClient.Ads.PutAdAsync(ad.Id.Value, ad); await AzureConfig.AddAdBlobToQueueAsync(ad.Id, imageBlob); return(RedirectToAction("Index")); } await InitCategoriesAsync(dataApiClient, ad.CategoryId); return(View(ad)); } } catch (OperationCanceledException ex) { return(LogAndShowError(ex)); } catch (AggregateException ex) { return(LogAndShowError(ex)); } catch (HttpOperationException ex) { return(LogAndShowError(ex)); } //catch (Exception ex) //{ // return LogAndShowError(ex); //} }
public async Task <ActionResult> Create( [Bind(Include = "Id,Title,Description,CategoryId,Price,Phone,Posted")] Ad ad, HttpPostedFileBase imageFile) { try { using (var dataApiClient = CompleteDataApi.NewDataApiClient()) { if (ModelState.IsValid) { CloudBlockBlob imageBlob = null; if (imageFile != null && imageFile.ContentLength != 0) { imageBlob = await AzureConfig.UploadAndSaveBlobAsync(imageFile); ad.ImageUrl = imageBlob.Uri.ToString(); } ad.Posted = DateTime.Now; ad = await dataApiClient.Ads .PostAdAsync(ad) .ConfigureAwait(false); _logger.Log($"Created Ad (Id={ad.Id}) in database"); await AzureConfig.AddAdBlobToQueueAsync(ad.Id, imageBlob); return(RedirectToAction("Index")); } await InitCategoriesAsync(dataApiClient, ad.CategoryId); return(View(ad)); } } catch (OperationCanceledException ex) { return(LogAndShowError(ex)); } catch (AggregateException ex) { return(LogAndShowError(ex)); } catch (HttpOperationException ex) { return(LogAndShowError(ex)); } //catch (Exception ex) //{ // return LogAndShowError(ex); //} }
// GET: Ads public async Task <ActionResult> Index(Category category = null) { try { using (var dataApiClient = CompleteDataApi.NewDataApiClient()) { HttpOperationResponse <IList <Ad> > result; // call to data api if (category?.Id == null) { result = await dataApiClient.Ads.GetAdsWithHttpMessagesAsync(); } else { result = await dataApiClient.Ads.GetAdsByCategoryWithHttpMessagesAsync(category.Id.Value); } if (result.Response.StatusCode != HttpStatusCode.OK) { //ViewBag.errorMessage = // $"Action Index, Data Api returns the following code {result.Response.StatusCode}, reason \'{result.Response.ReasonPhrase}\'"; //return View("Error"); return(new HttpStatusCodeResult(result.Response.StatusCode, result.Response.ReasonPhrase)); } var adList = result.Body; return(View(adList)); } } catch (OperationCanceledException ex) { return(LogAndShowError(ex)); } catch (AggregateException ex) { return(LogAndShowError(ex)); } //catch (Exception ex) //{ // return LogAndShowError(ex); //} }
// GET: Ads/Details/5 public async Task <ActionResult> Details(long?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } try { using (var dataApiClient = CompleteDataApi.NewDataApiClient()) { // call to data api var ad = await dataApiClient.Ads.GetAdAsync(id.Value); if (ad == null) { return(HttpNotFound()); } return(View(ad)); } } catch (OperationCanceledException ex) { return(LogAndShowError(ex)); } catch (AggregateException ex) { return(LogAndShowError(ex)); } catch (HttpOperationException ex) { return(LogAndShowError(ex)); } //catch (Exception ex) //{ // return LogAndShowError(ex); //} }