Пример #1
0
        public IActionResult Delete(EditDeleteBookModel model)
        {
            if (!this.bookService.DeleteBookIsSuccess(model.Id))
            {
                this.TempData[GlobalConstants.TempDateErrorKey] = InvalidAttemptDelete;

                return(RedirectToAction(nameof(Delete)));
            }

            return(Redirect(GlobalConstants.IndexPath));
        }
Пример #2
0
        public IActionResult Edit(EditDeleteBookModel model)
        {
            string imgUrl            = null;
            string path              = null;
            var    ifNeedToCreateImg = false;

            if (!ModelState.IsValid)
            {
                this.TempData[GlobalConstants.TempDateErrorKey] = InvalidAttemptEdit;

                return(RedirectToAction(nameof(Edit)));
            }

            if (!this.bookService.IfCurrentBookHaveTheSameIsbn(model.Id, model.Isbn))
            {
                if (!this.bookService.IfIsbnExists(model.Isbn))
                {
                    this.TempData[GlobalConstants.TempDateErrorKey] = IsbnErrorMessage;

                    return(RedirectToAction(nameof(Edit)));
                }
            }

            var date = this.bookService.ConvertFromStringToDateTimeIfPossible(model.ReleaseDate);

            if (date == null)
            {
                this.TempData[GlobalConstants.TempDateErrorKey] = ErrorMessageDate;

                return(RedirectToAction(nameof(Edit)));
            }

            if (model.Image != null)
            {
                if (!this.imgService.CheckImgExtention(model.Image))
                {
                    this.TempData[GlobalConstants.TempDateErrorKey] = GlobalConstants.ErrorMessageExtention;

                    return(RedirectToAction(nameof(Edit)));
                }

                if (!this.imgService.MingLengthOnImg(model.Image))
                {
                    this.TempData[GlobalConstants.TempDateErrorKey] = GlobalConstants.ErrorMessageMinSizeImg;

                    return(RedirectToAction(nameof(Edit)));
                }

                var fileName = model.Image.FileName;

                var extention = this.imgService.ReturnExtension(fileName);

                var currentNumberOnFolder = this.bookService.CountOfAllBooks() % GlobalConstants.NumberOnFolder;

                var currentFoledName = GlobalConstants.FolderNameImg + currentNumberOnFolder;

                path = this.imgService.ReturnPath(environment, currentFoledName);

                imgUrl = this.imgService.ReturnUrl(environment, currentFoledName, extention);

                ifNeedToCreateImg = true;
            }

            var isSuccess = this.bookService.Edit(
                model.Id,
                model.Title,
                model.Price,
                model.Isbn,
                imgUrl,
                model.Description,
                date.Value.ToUniversalTime(),
                model.Authors,
                model.Categories);

            if (!isSuccess)
            {
                this.TempData[GlobalConstants.TempDateErrorKey] = ErrorMessageNoCategoryOrAuthorh;

                return(RedirectToAction(nameof(Edit)));
            }

            if (ifNeedToCreateImg)
            {
                this.imgService.CreateImg(model.Image, path, imgUrl);
            }

            return(Redirect(GlobalConstants.IndexPath));
        }