Esempio n. 1
0
        public async Task <AdvertResponse> Create(CreateAdvertModel model)
        {
            var advertApiModel = mapper.Map <AdvertModel>(model); //Automapper
            var jsonModel      = JsonConvert.SerializeObject(advertApiModel);
            var response       = await client.PostAsync(new Uri($"{client.BaseAddress}/create"),
                                                        new StringContent(jsonModel, Encoding.UTF8, "application/json")).ConfigureAwait(false);

            var responseJson = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            var createAdvertResponse = JsonConvert.DeserializeObject <CreateAdvertResponse>(responseJson);

            return(mapper.Map <AdvertResponse>(createAdvertResponse));
        }
Esempio n. 2
0
        public async Task <AdvertResponse> CreateAsync(CreateAdvertModel model)
        {
            var advertApiModel = _mapper.Map <AdvertModel>(model);

            var jsonModel = JsonConvert.SerializeObject(advertApiModel);
            var response  = await _client.PostAsync(new Uri($"{_baseAddress}/create"),
                                                    new StringContent(jsonModel, Encoding.UTF8, "application/json")).ConfigureAwait(false);

            var createAdvertResponse = await response.Content.ReadAsAsync <AdvertResponse>().ConfigureAwait(false);

            var advertResponse = _mapper.Map <AdvertResponse>(createAdvertResponse);

            return(advertResponse);
        }
Esempio n. 3
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));
        }
Esempio n. 4
0
        public IActionResult Create()
        {
            var model = new CreateAdvertModel();

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

                AdvertResponse apiCallResponse = await _advertApiClient.Create(createAdvertModel);

                string id = apiCallResponse.Id;

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

                    try
                    {
                        using (Stream readStream = imageFile.OpenReadStream())
                        {
                            bool 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.");
                            }
                        }

                        ConfirmAdvertRequest confirmModel = new ConfirmAdvertRequest()
                        {
                            Id       = id,
                            FilePath = filePath,
                            Status   = AdvertStatus.Active
                        };

                        var canConfirm = await _advertApiClient.Confirm(confirmModel);

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


                        return(RedirectToAction("Index", controllerName: "Home"));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        ConfirmAdvertRequest confirmModel = new ConfirmAdvertRequest()
                        {
                            Id       = id,
                            FilePath = filePath,
                            Status   = AdvertStatus.Pending
                        };

                        await _advertApiClient.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);

                var createAdvertModel = new CreateAdvertModel
                {
                    Title       = model.Title,
                    Description = model.Description,
                    Price       = model.Price,
                    UserName    = User.Identity.Name
                };

                createAdvertModel.UserName = User.Identity.Name;

                var apiCallResponse = await _advertApiClient.CreateAsync(createAdvertModel, User.Identity.Name).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));
        }