public IActionResult Edit(AuthorEditViewModelcs model)
        {
            if (ModelState.IsValid)
            {
                if (model.Author.Id == 0)
                {
                    if (model.Photo != null)
                    {
                        if (model.Author.AuthorPhoto != null)
                        {
                            string filePath = Path.Combine(webHostEnvironment.WebRootPath, "images/authorImages/", model.Author.AuthorPhoto);
                            System.IO.File.Delete(filePath);
                        }
                        model.Author.AuthorPhoto = UploadAuthorPhoto(model);
                    }
                    model.Author = authorService.CreateAuthor(model.Author);

                    TempData["Message"] = "New Author Created!";
                }
                else
                {
                    if (model.Photo != null)
                    {
                        if (model.Author.AuthorPhoto != null)
                        {
                            string filePath = Path.Combine(webHostEnvironment.WebRootPath, "images/authorImages/", model.Author.AuthorPhoto);
                            System.IO.File.Delete(filePath);
                        }
                        model.Author.AuthorPhoto = UploadAuthorPhoto(model);
                    }

                    model.Author = authorService.UpdateAuthor(model.Author);

                    TempData["Message"] = "Author updated!";
                }

                authorService.Commit();

                return(RedirectToAction("Details", new { id = model.Author.Id }));
            }

            return(View(model));
        }
Esempio n. 2
0
        public IActionResult OnPost()
        {
            var temp = authorService.DeleteAuthor(Author.Id);

            if (temp == null)
            {
                return(RedirectToPage("NotFound"));
            }

            authorService.Commit();
            TempData["Message"] = "Author Deleted!";

            return(RedirectToPage("AuthorList"));
        }
        public IActionResult OnPost()
        {
            if (ModelState.IsValid)
            {
                if (Author.Id == 0)
                {
                    if (Photo != null)
                    {
                        if (Author.AuthorPhoto != null)
                        {
                            string filePath = Path.Combine(webHostEnvironment.WebRootPath, "images/authorImages/", Author.AuthorPhoto);
                            System.IO.File.Delete(filePath);
                        }
                        Author.AuthorPhoto = UploadAuthorPhoto();
                    }
                    Author = authorService.CreateAuthor(Author);
                    TempData["Message"] = "Author Created!";
                }
                else
                {
                    if (Photo != null)
                    {
                        if (Author.AuthorPhoto != null)
                        {
                            string filePath = Path.Combine(webHostEnvironment.WebRootPath, "images/authorImages/", Author.AuthorPhoto);
                            System.IO.File.Delete(filePath);
                        }
                        Author.AuthorPhoto = UploadAuthorPhoto();
                    }
                    Author = authorService.UpdateAuthor(Author);
                    TempData["Message"] = "Author Updated!";
                }

                authorService.Commit();

                return(RedirectToPage("AuthorDetails", new { id = Author.Id }));
            }

            return(Page());
        }