public async Task <IActionResult> Add(int id)
        {
            var creatorUserName = await this.advertisement.CreatorUserName(id);

            var currentUserName = this.User.Identity.Name;

            if (creatorUserName != currentUserName)
            {
                TempData.AddErrorMessage(WebConstants.ErrorMessageNotAllowedToPage);
                return(RedirectToAction(nameof(AdvertisementsController.All), WebConstants.AdvertisementsControllerName));
            }

            var exists = await this.advertisement.Exists(id);

            if (!exists)
            {
                TempData.AddErrorMessage(WebConstants.ErrorMessageAdvertisementDontExist);
                return(RedirectToAction(nameof(AdvertisementsController.All), WebConstants.AdvertisementsControllerName));
            }

            var model = new PicturesFormModel
            {
                AdvertisementId = id
            };

            var isValid = await this.picture.Count(id) >= 3;

            if (isValid)
            {
                TempData.AddErrorMessage(WebConstants.ErrorMessageThreePictureAllowed);
                return(RedirectToAction(nameof(AdvertisementsController.Details), WebConstants.AdvertisementsControllerName, new { id = id }));
            }

            return(View(model));
        }
        public async Task <IActionResult> Add(int id, PicturesFormModel model)
        {
            var creatorUserName = await this.advertisement.CreatorUserName(id);

            var currentUserName = this.User.Identity.Name;

            if (creatorUserName != currentUserName)
            {
                TempData.AddErrorMessage(WebConstants.ErrorMessageNotAllowedToPage);
                return(RedirectToAction(nameof(AdvertisementsController.All), WebConstants.AdvertisementsControllerName));
            }

            if (!ModelState.IsValid)
            {
                model.AdvertisementId = id;
                return(View(model));
            }

            var firstUrlEnding  = model.UrlPathFirst.Trim().Substring(model.UrlPathFirst.Length - 4);
            var secondUrlEnding = model.UrlPathSecond.Trim().Substring(model.UrlPathSecond.Length - 4);
            var thirdUrlEnding  = model.UrlPathThird.Trim().Substring(model.UrlPathThird.Length - 4);

            var correctPicture = true;

            if (firstUrlEnding != WebConstants.EndsWithJpg &&
                firstUrlEnding != WebConstants.EndsWithPng)
            {
                correctPicture     = false;
                model.UrlPathFirst = DataConstants.ImgDefoutNotFound;
            }

            if (secondUrlEnding != WebConstants.EndsWithJpg &&
                secondUrlEnding != WebConstants.EndsWithPng)
            {
                correctPicture      = false;
                model.UrlPathSecond = DataConstants.ImgDefoutNotFound;
            }

            if (thirdUrlEnding != WebConstants.EndsWithJpg &&
                thirdUrlEnding != WebConstants.EndsWithPng)
            {
                correctPicture     = false;
                model.UrlPathThird = DataConstants.ImgDefoutNotFound;
            }

            await picture.Create(
                model.UrlPathFirst,
                true,
                model.UrlPathSecond,
                false,
                model.UrlPathThird,
                false,
                id);

            if (correctPicture)
            {
                TempData.AddSuccessMessage(WebConstants.SuccessMessagePictureAdd);
            }
            else
            {
                TempData.AddErrorMessage(WebConstants.ErrorMessagePicutureWithNoPngOrJpg);
            }
            return(RedirectToAction(nameof(AdvertisementsController.Details), WebConstants.AdvertisementsControllerName, new { id = id }));
        }