Exemple #1
0
        public async Task <IActionResult> Index(IFormFile imageFile)
        {
            if (imageFile != null && IsImage(imageFile.FileName))
            {
                var id       = DateTimeOffset.Now.ToString("yyyyMMddTHHmmss");
                var bucket   = _configuration.GetValue <string>("RekognitionBucket");
                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, bucket)
                                     .ConfigureAwait(false);

                        if (!result)
                        {
                            throw new Exception(
                                      "Could not upload the image to file repository. Please see the logs for details.");
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    _logger.LogError($"{nameof(SnsController)} : {e.Message}");
                }
            }

            return(View());
        }
        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));
        }
Exemple #3
0
        public async Task <IActionResult> FileUploadAsync(FileViewModel fileViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(View("Index", fileViewModel));
            }

            try
            {
                await using var memoryStream = new MemoryStream();
                await fileViewModel.File.CopyToAsync(memoryStream);

                var file = new FileDto
                {
                    FileName  = Path.GetFileName(fileViewModel.File.FileName),
                    Extension = FileExtensionHelper.GetFileExtension(fileViewModel.File.FileName),
                };

                await _fileUploader.UploadFileAsync(file, memoryStream);

                TempData["IsSuccess"] = "File has been uploaded successfully.";
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                fileViewModel.Exception = e;
                ModelState.AddModelError("Exception", $"Internal server error. {e.Message}");
            }

            return(View("Index", fileViewModel));
        }
        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());
        }
Exemple #5
0
        public async Task <IActionResult> Create(CreateAdvertisementViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                var createAdvertModel = _mapper.Map <CreateAdvertisement>(model);
                createAdvertModel.UserName = User.Identity.Name;

                var apiCallResponse = await _advertApiClient.CreateAsync(createAdvertModel);

                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);

                        if (!result)
                        {
                            throw new Exception(
                                      "Could not upload the image to file repository. Please see the logs for details.");
                        }
                    }
                    catch (Exception)
                    {
                        isOkToConfirmAd = false;
                        var confirmModel = new ConfirmAdvertRequest()
                        {
                            Id       = id,
                            FilePath = filePath,
                            Status   = AdvertisementStatus.Pending
                        };
                        await _advertApiClient.ConfirmAsync(confirmModel);
                    }
                }

                if (isOkToConfirmAd)
                {
                    var confirmModel = new ConfirmAdvertRequest()
                    {
                        Id       = id,
                        FilePath = filePath,
                        Status   = AdvertisementStatus.Active
                    };
                    await _advertApiClient.ConfirmAsync(confirmModel);
                }

                return(RedirectToAction("Index", "Home"));
            }

            return(View(model));
        }
        public async Task <IActionResult> Create(CreateAdvertViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                CreateAdvertModel createAdvertModel = _mapper.Map <CreateAdvertModel>(model);
                AdvertResponse    apiCallResponse   = await _advertApiClient.CreateAsync(createAdvertModel);

                string id = apiCallResponse.Id;
                if (imageFile != null)
                {
                    string 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 image to the file repository. Please see logs for more detail");
                        }

                        ConfirmAdvertRequest confirmModel = new ConfirmAdvertRequest
                        {
                            Id       = id,
                            FilePath = filePath,
                            Status   = AdvertStatus.Active
                        };
                        bool canConfirm = await _advertApiClient.ConfirmAsync(confirmModel);

                        if (!canConfirm)
                        {
                            throw new Exception($"Cannot confirm upload for advert {id}");
                        }
                        return(RedirectToAction("Index", "Home"));
                    }
                    catch (Exception ex)
                    {
                        ConfirmAdvertRequest confirmModel = new ConfirmAdvertRequest
                        {
                            Id       = id,
                            FilePath = filePath,
                            Status   = AdvertStatus.Pending
                        };
                        await _advertApiClient.ConfirmAsync(confirmModel);

                        Console.WriteLine(ex.Message);
                    }
                }
            }

            return(View(model));
        }
Exemple #7
0
        private async Task UploadImage(IFormFile imageFile, string filePath)
        {
            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 see the logs for more details.");
                }
            }
        }
        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));
        }
        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));
        }
        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));
        }
        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));
        }
Exemple #12
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));
        }
Exemple #13
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));
        }
        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));
        }
        public async Task <ActionResult> News(CreateNewsViewModel model)
        {
            if (string.IsNullOrEmpty(model.Heading))
            {
                RedirectToAction("News");
            }

            var newsId     = model.Id;
            var folderPath = CreateFolder(newsId);
            var newsmodel  = new NewsModel
            {
                Id          = newsId,
                CreatedBy   = HttpContext.Session.GetString("Name"),
                CreatedDate = DateTime.Today,
                Heading     = model.Heading,
                NewsBody    = model.NewsBody,
                YouTubLink  = model.YouTubLink ?? string.Empty,
                CategoryId  = int.Parse(model.CategoryId)
            };
            var stringImages = new List <string>();

            if (model.MainImage != null)
            {
                stringImages.Add(model.MainImage.FileName);
                using (var readStream = model.MainImage.OpenReadStream())
                {
                    var result = await _fileUploader.UploadFileAsync($@"{folderPath}/{model.MainImage.FileName}", readStream)
                                 .ConfigureAwait(false);

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

            if (model.SubImage1 != null)
            {
                stringImages.Add(model.SubImage1.FileName);
                using (var readStream = model.SubImage1.OpenReadStream())
                {
                    var result = await _fileUploader.UploadFileAsync($@"{folderPath}/{model.SubImage1.FileName}", readStream)
                                 .ConfigureAwait(false);

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

            if (model.SubImage2 != null)
            {
                stringImages.Add(model.SubImage2.FileName);

                using (var readStream = model.SubImage2.OpenReadStream())
                {
                    var result = await _fileUploader.UploadFileAsync($@"{folderPath}/{model.SubImage2.FileName}", readStream)
                                 .ConfigureAwait(false);

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

            if (model.SubImage3 != null)
            {
                stringImages.Add(model.SubImage3.FileName);

                using (var readStream = model.SubImage3.OpenReadStream())
                {
                    var result = await _fileUploader.UploadFileAsync($@"{folderPath}/{model.SubImage3.FileName}", readStream)
                                 .ConfigureAwait(false);

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

            var jsonString = JsonSerializer.Serialize(newsmodel);

            var isSaved = await _fileUploader.SaveFileAsync($@"{newsItems}/{newsmodel.Id + ".json"}", jsonString)
                          .ConfigureAwait(false);

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

            // _cacheManager.RemoveCache("NewsItems");
            _cacheManager.SetNewsItem(newsmodel);

            return(RedirectToAction("index", "Home"));
        }
        public async Task <IActionResult> Create(CreateAdViewModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                var ad = _mapper.ToAd(model);
                ad.Username = GetUsername();
                var createResponse = await _client.CreateAsync(ad);

                var id       = createResponse.Id;
                var filePath = string.Empty;

                if (imageFile != null)
                {
                    var bucket   = _configuration.GetValue <string>("ImageBucket");
                    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, bucket)
                                         .ConfigureAwait(false);

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

                        var isConfirmed = await _client.ConfirmAsync(new ConfirmRequest {
                            Id       = id,
                            FilePath = filePath,
                            Status   = AdApi.Shared.Models.AdStatus.Active
                        });

                        if (!isConfirmed)
                        {
                            throw new Exception($"Cannot confirm ad id = {id}");
                        }

                        return(RedirectToAction("Index", "Home"));
                    }
                    catch (Exception e)
                    {
                        await _client.ConfirmAsync(new ConfirmRequest {
                            Id       = id,
                            FilePath = filePath,
                            Status   = AdApi.Shared.Models.AdStatus.Pending
                        });

                        Console.WriteLine(e.Message);
                        _logger.LogError($"{nameof(AdManagementController)} : {e.Message}");
                    }
                }

                return(RedirectToAction("Index", "Home"));
            }

            return(View(model));
        }
Exemple #17
0
        public async Task <IActionResult> Create(CreateAdvertModel model, IFormFile imageFile)
        {
            if (ModelState.IsValid)
            {
                var advertModel = _mapper.Map <AdvertModel>(model);

                var response = await _advertApiClient.CreateAsync(advertModel);

                // TODO: check for successful response
                var id = response.Id;

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

                    try
                    {
                        using (var readStrem = imageFile.OpenReadStream())
                        {
                            var result = await _fileUploader.UploadFileAsync(filePath, readStrem);

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

                        var confirmed = await _advertApiClient.ConfirmAsync(new ConfirmAdvertModel
                        {
                            Id       = id,
                            FilePath = filePath,
                            Status   = AdvertStatus.Active
                        });

                        if (!confirmed)
                        {
                            throw new Exception($"Could not confirm advert of id = {id}");
                        }

                        return(RedirectToAction("Index", "Home")); // TODO: redirect to list
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                        await _advertApiClient.ConfirmAsync(new ConfirmAdvertModel
                        {
                            Id     = id,
                            Status = AdvertStatus.Pending // TODO: use deleted status
                        });

                        // TODO: redirect to error page
                        throw ex;
                    }
                }
            }

            return(View(model));
        }
Exemple #18
0
 public async Task <ActionResult <string> > Post(IFormFile file) => Ok(await uploader.UploadFileAsync(file.OpenReadStream(), System.IO.Path.GetExtension(file.FileName)));
            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);*/
            }