Exemplo n.º 1
0
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            if (imageFile == null)
            {
                return(View(model));
            }

            string id = await CreateAdvert(model);

            string filePath = GetFilePath(imageFile, id);

            try
            {
                await UploadImage(imageFile, filePath);
                await ConfirmAdvert(id);

                return(RedirectToAction("Index", "Home"));
            }
            catch (Exception e)
            {
                await CancelAdvertCreation(id);

                // Missing loggers for now
                Console.WriteLine(e);
            }
            return(View(model));
        }
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                var createAdvertModel = _mapper.Map <CreateAdvertModel>(model);
                var apiCallResponse   = await _advertApiClient.Create(createAdvertModel).ConfigureAwait(false);

                var id = apiCallResponse.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 the image to file repository. Please see the logs for details.");
                            }
                        }

                        var confirmModel = new ConfirmAdvertRequest()
                        {
                            Id       = id,
                            FilePath = filePath,
                            Status   = AdvertStatus.Active
                        };
                        var canConfirm = await _advertApiClient.Confirm(confirmModel).ConfigureAwait(false);

                        if (!canConfirm)
                        {
                            throw new Exception($"Cannot confirm advert of id ={id}");
                        }

                        return(RedirectToAction("Index", "Home"));
                    }
                    catch (Exception e)
                    {
                        var confirmModel = new ConfirmAdvertRequest()
                        {
                            Id       = id,
                            FilePath = filePath,
                            Status   = AdvertStatus.Pending
                        };
                        await _advertApiClient.Confirm(confirmModel).ConfigureAwait(false);

                        Console.WriteLine(e);
                    }
                }
            }

            return(View(model));
        }
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                var advert = await _advertApiClient.Create(_mapper.Map <ApiAdvert>(model));

                var fileName = "";
                if (imageFile != null)
                {
                    fileName = !string.IsNullOrEmpty(imageFile.FileName) ? Path.GetFileName(imageFile.FileName) : advert.Id;
                    var filePath = $"{advert.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");
                            }
                        }

                        var confirmModel = new ApiConfirmAdvert()
                        {
                            Id       = advert.Id,
                            FilePath = filePath,
                            Status   = AdvertApi.Models.AdvertStatus.Active
                        };
                        var canConfirm = await _advertApiClient.Confirm(confirmModel);

                        if (!canConfirm)
                        {
                            throw new Exception($"Cannot confirm advert of id = {advert.Id}");
                        }

                        return(RedirectToAction("Index", "Home"));
                    }
                    catch (Exception ex)
                    {
                        var confirmModel = new ApiConfirmAdvert()
                        {
                            Id       = advert.Id,
                            FilePath = filePath,
                            Status   = AdvertApi.Models.AdvertStatus.Pending
                        };
                        var canConfirm = await _advertApiClient.Confirm(confirmModel);

                        Console.WriteLine(ex);
                    }
                }
                else
                {
                    ModelState.AddModelError("NullImg", "Image cannot be null");
                }
            }
            return(View());
        }
Exemplo n.º 4
0
        private async Task <string> CreateAdvert(CreateAdvertViewModel model)
        {
            var createAdvertModel = _mapper.Map <CreateAdvertModel>(model);
            var response          = await _advertClientApi.Create(createAdvertModel);

            var id = response.Id;

            return(id);
        }
Exemplo n.º 5
0
        public IActionResult Create()
        {
            CreateAdvertViewModel viewModel = new CreateAdvertViewModel()
            {
                OwnedCharactersList = _charactersService.GetUserCharacters(_userManager.GetUserAsync(User).Result.Id)
            };

            return(View(viewModel));
        }
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile formFile)
        {
            if (ModelState.IsValid)
            {
                var createAdvertModel = _mapper.Map <CreateAdvertModel>(model);
                var advertResponse    = await _client.Create(createAdvertModel);

                var id       = advertResponse.Id;
                var fileName = "";

                if (formFile != null)
                {
                    fileName = !string.IsNullOrEmpty(formFile.FileName) ? formFile.FileName : id;
                    var filePath = $"{id}/{fileName}";

                    try
                    {
                        using (var fileStream = formFile.OpenReadStream())
                        {
                            var response = await _fileUploader.FileUploader(filePath, fileStream).ConfigureAwait(false);

                            if (!response)
                            {
                                throw new Exception("Something went wrong. Could not upload the image to file repository.");
                            }
                        }

                        var advertConfirmRequest = new CreateAdvertConfirmModel
                        {
                            Id     = id,
                            Status = AdvertStatus.Active
                        };
                        var confirmResponse = await _client.Confirm(advertConfirmRequest).ConfigureAwait(false);

                        if (!confirmResponse)
                        {
                            throw new Exception($"Cannot confirm advert of Id = {id}");
                        }
                        return(RedirectToAction("Index", "Home"));
                    }
                    catch (Exception ex)
                    {
                        var advertConfirmRequest = new CreateAdvertConfirmModel
                        {
                            Id     = id,
                            Status = AdvertStatus.Pending
                        };
                        await _client.Confirm(advertConfirmRequest).ConfigureAwait(false);

                        Console.WriteLine(ex);
                    }
                }
            }
            return(View(model));
        }
Exemplo n.º 7
0
        public ActionResult CreateAdvert(int mask)
        {
            var advertType = _getAvailableAdvertTypes.FindAdvertTypeByMask(mask);
            var properties = _getPropertiesByAdvertType.GetProperties(advertType);
            var model      = new CreateAdvertViewModel()
            {
                Properties = properties, AdvertType = advertType
            };

            return(View(model));
        }
Exemplo n.º 8
0
        public ActionResult EditAdvert(CreateAdvertViewModel model, List <PropertyViewModel> property, int id)
        {
            model.Properties = property;
            if (ModelState.IsValid)
            {
                var idd = _updateAdvertService.UpdateAdvert(model, id);
                return(RedirectToAction("Index"));
            }

            model.SavedPhotos = _photoService.GetPhotosByIdAndAdvertId(model.PhotosToSave, id);
            return(View(model));
        }
Exemplo n.º 9
0
        public ActionResult CreateAdvert(CreateAdvertViewModel model, List <PropertyViewModel> property)
        {
            model.Properties = property;
            if (ModelState.IsValid)
            {
                var id = _createAdvertService.CreateAdvert(model);
                return(RedirectToAction("Index"));
            }

            model.SavedPhotos = _photoService.GetPhotosById(model.PhotosToSave);
            return(View(model));
        }
        public int CreateAdvert(CreateAdvertViewModel createAdvert)
        {
            var advertToSave = Mapper.Map <Advert>(createAdvert);

            advertToSave.AdvertType = _advertTypeRepository.GetSet().Single(x => x.Mask == createAdvert.AdvertType.Mask);


            var advert = _genericRepository.Add(advertToSave);

            var savedPhotos = _findPhotosByIdService.Find(createAdvert.PhotosToSave);

            _photoService.AddAdvertToPhotos(advert.Id, savedPhotos);

            return(advert.Id);
        }
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                //string id = ""; // this id comes from advert api / after successfully saved.

                var createAdvertRequestModel = _mapper.Map <CreateAdvertRequestModel>(model);
                var createApiCallResponse    = await _advertApiClient.Create(createAdvertRequestModel).ConfigureAwait(false);

                var id = createApiCallResponse.Id; //happy path


                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(continueOnCapturedContext: false);

                            if (!result)
                            {
                                throw new Exception(message: "Could not upload the image to file repository.Please see the logs for details");
                            }
                        }

                        //call advert api and confirm the advertisement

                        await ConfirmAdvertAsync(id, filePath, AdvertStatus.Active);

                        return(RedirectToAction(actionName: "Index", controllerName: "Home"));
                    }
                    catch (Exception exception)
                    {
                        //call advert api and cancel the advertisement
                        await ConfirmAdvertAsync(id, filePath, AdvertStatus.Pending);


                        Console.WriteLine(exception); //logger will be implemented
                    }
                }
            }

            return(View(model));
        }
Exemplo n.º 12
0
        public async Task <IActionResult> Create(CreateAdvertViewModel model)
        {
            ApplicationUser user = _userManager.GetUserAsync(HttpContext.User).Result;

            if (_advertsService.CheckIfCharacterHasAdvert(model.SelectedCharacterId))
            {
                ModelState.AddModelError(string.Empty, "This character has advert already");
                model.OwnedCharactersList = _charactersService.GetUserCharacters(_userManager.GetUserAsync(User).Result.Id);
                return(View(model));
            }

            var result = await _advertsService.Create(model.Ad, user, model.SelectedCharacterId);

            if (result.Succeeded)
            {
                return(RedirectToAction("MyAdverts", new { userName = user.UserName }));
            }

            ModelState.AddModelError(string.Empty, "This character does not exist");
            return(View(model));
        }
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                var apiCallResponse = await _advertAPIClient.Create(_mapper.Map <CreateAdvertModel>(model));

                //var id = apiCallResponse.Id;
                //if (imageFile != null)
                //{
                //    var fileName = string.IsNullOrEmpty(imageFile.FileName)? Path.GetFileName(imageFile.FileName) : id;
                //    var filePath = $"{id}/{fileName}";
                //    try
                //    {
                //        using(var readerStream = imageFile.OpenReadStream())
                //        {
                //            var result = await _fileUploader.UploadFileAsync(filePath, readerStream);
                //            if (!result)
                //                throw new Exception();
                //        }
                //      var canConfirm =  await _advertAPIClient.Confirm(new ConfirmAdvertRequest() { Id = id, FilePath = filePath, Status = AdvertStatus.Active });
                //        if (!canConfirm)
                //            throw new Exception();
                //    }
                //    catch
                //    {
                //        await _advertAPIClient.Confirm(new ConfirmAdvertRequest() { Id = id, FilePath = filePath, Status = AdvertStatus.Pending });
                //    }
                //}
            }
            if (model != null)
            {
                var data = await _advertAPIClient.Get(_mapper.Map <string>(""));

                return(View("Dashboard", data));
            }
            else
            {
                return(View(model));
            }
        }
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                //ToDo: this id will be generated from API
                var id = "11111";

                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.");
                            }
                        }

                        return(RedirectToAction("Index", controllerName: "Home"));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                }
            }

            return(View(model));
        }
Exemplo n.º 15
0
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                // here we must call the AdvertApi, create the Advertisement in the database and return id
                var id       = "xxxxx";
                var filename = "";
                if (imageFile != null)
                {
                    filename = !string.IsNullOrEmpty(imageFile.FileName) ? Path.GetFileName(imageFile.FileName) : id;
                    var filePath = $"{id}/{filename}";

                    try
                    {
                        using (var readStream = imageFile.OpenReadStream())
                        {
                            var result = await this._s3FileLoader.UploadFileAsync(filePath, readStream)
                                         .ConfigureAwait(false);

                            if (!result)
                            {
                                throw new Exception("Could not upload the image to file repository. Please see the logs for details");
                            }

                            // now we call AdvertAPI and confirm the Advertisement

                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                    catch (Exception e)
                    {
                        // Call the AdvertApi and cancel the Advertisement
                        Console.WriteLine(string.Format("[AdvertManagementController] Create Action: Error - {0}", e.Message));
                    }
                }
            }
            return(View(model));
        }
Exemplo n.º 16
0
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                // here we must call the AdvertApi, create the Advertisement in the database and return id
                var createAdvertModel = this._mapper.Map <CreateAdvertModel>(model);
                var apiCallResponse   = await this._advertApiCleint.CreateAsync(createAdvertModel);

                var id = apiCallResponse.Id;

                var filename = "";
                if (imageFile != null)
                {
                    filename = !string.IsNullOrEmpty(imageFile.FileName) ? Path.GetFileName(imageFile.FileName) : id;
                    var filePath = $"{id}/{filename}";

                    try
                    {
                        using (var readStream = imageFile.OpenReadStream())
                        {
                            var result = await this._s3FileLoader.UploadFileAsync(filePath, readStream)
                                         .ConfigureAwait(false);

                            if (!result)
                            {
                                throw new Exception("Could not upload the image to file repository. Please see the logs for details");
                            }

                            // now we call AdvertAPI and confirm the Advertisement
                            var confirmModel = new ConfirmAdvertRequest()
                            {
                                Id       = id,
                                FilePath = filePath,
                                Status   = AdvertStatus.Active
                            };
                            var canConfirm = await this._advertApiCleint.ConfirmAsync(confirmModel).ConfigureAwait(false);

                            if (!canConfirm)
                            {
                                throw new Exception($"Cannot Confirm advert of Id = {id}");
                            }


                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                    catch (Exception e)
                    {
                        // Call the AdvertApi and cancel the Advertisement
                        var confirmModel = new ConfirmAdvertRequest()
                        {
                            Id       = id,
                            FilePath = filePath,
                            Status   = AdvertStatus.Pending
                        };
                        await this._advertApiCleint.ConfirmAsync(confirmModel).ConfigureAwait(false);

                        Console.WriteLine(string.Format("[AdvertManagementController] Create Action: Error - {0}", e.Message));
                    }
                }
            }
            return(View(model));
        }
            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);*/
            }
Exemplo n.º 18
0
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imagefile)
        {
            if (ModelState.IsValid)
            {
                //var id = "11111";
                //Call advert Api to store details in DB

                var createadvertmodel = _mapper.Map <CreateAdvertModel>(model);

                var apiCallresponse = await _adverapi.Create(createadvertmodel);

                createadvertmodel.UserName = User.Identity.Name;

                var id       = apiCallresponse.Id;
                var 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);

                            if (!result)
                            {
                                throw new Exception
                                      (
                                          "could not upload image to file directory. Please see the logs for details"
                                      );
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        //rollback
                        isOkToConfirmAd = false;
                        var confirmmodel = new ConfirmAdvertRequest
                        {
                            Id       = id,
                            FilePath = filepath,
                            Status   = AdvertStatus.pending
                        };

                        await _adverapi.Confirm(confirmmodel);


                        Console.WriteLine(e);
                    }
                }

                if (isOkToConfirmAd)
                {
                    var confirmModel = new ConfirmAdvertRequest()
                    {
                        Id       = id,
                        FilePath = filepath,
                        Status   = AdvertStatus.Active
                    };
                    await _adverapi.Confirm(confirmModel);
                }

                return(RedirectToAction("Index", "Home"));
            }
            return(View(model));
        }
Exemplo n.º 19
0
        public IActionResult Create()
        {
            var model = new CreateAdvertViewModel();

            return(View(model));
        }
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                //var createAdvertModel = _mapper.Map<CreateAdvertModel>(model);
                //createAdvertModel.UserName = User.Identity.Name;

                //var apiCallResponse = await _advertApiClient.CreateAsync(createAdvertModel).ConfigureAwait(false);

                // you must make a call to AdveraPI, create the advertisement in the datatbse and return ID
                var createAdvertModel = _mapper.Map <CreateAdvertModel>(model);
                var apiCallResponse   = await _advertApiClient.Create(createAdvertModel).ConfigureAwait(false);

                //var id = "11111"; // apiCallResponse.Id;
                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.");
                            }
                        }

                        //CALL ADVERT API
                        var confirmModel = new ConfirmAdvertRequest()
                        {
                            Id       = id,
                            FilePath = filePath,
                            Status   = AdvertStatus.Active
                        };
                        var canConfirm = await _advertApiClient.Confirm(confirmModel).ConfigureAwait(false);

                        if (!canConfirm)
                        {
                            throw new Exception($"Cannot confirm advert of id={id}");
                        }
                        return(RedirectToAction("Index", "Home"));
                    }
                    catch (Exception e)
                    {
                        // CALL TO ADVERT API COMMNETED OUT

                        //isOkToConfirmAd = false;

                        // 26
                        // if we cannot upload the file we need to delete everythig
                        var confirmModel = new ConfirmAdvertRequest()
                        {
                            Id       = id,
                            FilePath = filePath,
                            Status   = AdvertStatus.Pending                                 // now is pending because is not confirmed, something was wrong
                        };
                        await _advertApiClient.Confirm(confirmModel).ConfigureAwait(false); // 26 update to PENDING

                        Console.WriteLine(e);                                               // we need add loggin
                    }
                }

                //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));
        }
 public IActionResult Create(CreateAdvertViewModel model)
 {
     return(View(model));
 }
Exemplo n.º 22
0
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                //var id = "11111";
                //must make a call to Advert Api, create Advertisement in the database and return the Id.
                var createAdvertWebRequest = _mapper.Map <CreateAdvertWebRequest>(model);
                createAdvertWebRequest.UserName = User.Identity.Name;

                var apiCallResponse = await _advertApiClient.CreateAsync(createAdvertWebRequest);

                var Id = apiCallResponse.Id;

                var fileName = "";
                if (imageFile != null)
                {
                    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);

                            if (!result)
                            {
                                throw new Exception(
                                          "Could not upload the image to file repository. Please check the logs for details.");
                            }
                        }

                        //Call Advert Api and confirm the advertisement.
                        var confirmModel = new ConfirmAdvertWebRequest()
                        {
                            Id       = Id,
                            FilePath = filePath,
                            Status   = AdvertStatus.Active
                        };

                        var canConfirm = await _advertApiClient.ConfirmAsync(confirmModel);

                        if (!canConfirm)
                        {
                            throw new Exception($"Cannot confrim advert of id = {Id}");
                        }

                        return(RedirectToAction("index", "home"));
                    }
                    catch (Exception ex)
                    {
                        //Call Advert Api and cancel the advertisement.
                        var confirmModel = new ConfirmAdvertWebRequest()
                        {
                            Id       = Id,
                            FilePath = filePath,
                            Status   = AdvertStatus.Pending
                        };

                        await _advertApiClient.ConfirmAsync(confirmModel);

                        Console.WriteLine(ex);
                    }
                }
            }

            return(View(model));
        }
Exemplo n.º 23
0
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                var createAdvertModel = _mapper.Map <CreateAdvertModel>(model);
                //createAdvertModel.UserName = User.Identity.Name;

                var apiCallResponse = await _clientApi.Create(createAdvertModel).ConfigureAwait(false);

                var id = Guid.NewGuid().ToString();

                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(
                                          message: "Could not upload the image to file repository. Please see the logs for details.");
                            }
                        }
                        var confirmModel = new ConfirmAdvertRequest()
                        {
                            Id     = id,
                            Status = AdvertStatus.Active
                        };
                        var canConfirm = await _clientApi.Confirm(confirmModel).ConfigureAwait(false);

                        if (!canConfirm)
                        {
                            throw new Exception("Cannot Confirm");
                        }

                        return(RedirectToAction("index", "Home"));
                    }
                    catch (Exception e)
                    {
                        isOkToConfirmAd = false;
                        var confirmModel = new ConfirmAdvertRequest()
                        {
                            Id = id,

                            Status = AdvertStatus.Pending
                        };
                        await _clientApi.Confirm(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));
        }
Exemplo n.º 24
0
 public async Task <IActionResult> Create(CreateAdvertViewModel model)
 {
     return(View(model));
 }
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                //we must call to AdvertApi,create advertement in database and return id

                var createAdvertModel = mapper.Map <CreateAdvertModel>(model);

                createAdvertModel.FilePath = imageFile.FileName;

                var apiCallResponse = await advertApiClient.Create(createAdvertModel);

                var id = apiCallResponse.Id;

                var fileName = "";
                if (imageFile != null)
                {
                    fileName = !string.IsNullOrEmpty(imageFile.FileName) ? Path.GetFileName(imageFile.FileName) : imageFile.Name;
                    var filePath = $"{id}/{fileName}";

                    try
                    {
                        using (var readStream = imageFile.OpenReadStream())
                        {
                            var result = await fileUploader.UploadFileAsync(filePath, readStream).ConfigureAwait(false);

                            if (!result)
                            {
                                throw new System.Exception(message: "Could not load the image into the repository, Please see the logs");
                            }
                        }


                        var confirmModel = new ConfirmAdvertRequest
                        {
                            Id     = id,
                            Status = AdvertApi.models.AdvertStatus.Active
                        };

                        var canConfirm = await advertApiClient.Confirm(confirmModel);

                        if (!canConfirm)
                        {
                            throw new Exception(message: $"Cannot confirm advert of id = {id}");
                        }

                        return(RedirectToAction("Index", controllerName: "Home"));
                    }
                    catch (Exception ex)
                    {
                        var confirmModel = new ConfirmAdvertRequest
                        {
                            Id     = id,
                            Status = AdvertApi.models.AdvertStatus.Pending
                        };

                        await advertApiClient.Confirm(confirmModel);

                        Console.WriteLine(ex);
                    }
                }
            }

            return(View(model));
        }