|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Web; |
| 5 | +using System.Web.Mvc; |
| 6 | +using BookStore.Domain.Abstract; |
| 7 | +using BookStore.Domain.Entities; |
| 8 | +using BookStore.Models; |
| 9 | +using System.Data.Entity; |
| 10 | +using System.Net; |
| 11 | + |
| 12 | +namespace BookStore.Controllers |
| 13 | +{ |
| 14 | +<<<<<<< HEAD |
| 15 | + [Authorize] |
| 16 | +======= |
| 17 | + |
| 18 | + [Authorize(Roles = "admin")] |
| 19 | +>>>>>>> origin/Build |
| 20 | + public class AdminController : Controller |
| 21 | + { |
| 22 | + // |
| 23 | + // GET: /Admin/ |
| 24 | + public AdminController() |
| 25 | + { |
| 26 | + |
| 27 | + } |
| 28 | + private IBookRepository repository; |
| 29 | + public AdminController(IBookRepository repo) |
| 30 | + { |
| 31 | + repository = repo; |
| 32 | + } |
| 33 | + public ViewResult Edit(int bookID, string image_url = null) |
| 34 | + { |
| 35 | + Book book = repository.Books.Include(x => x.Author).Include(x => x.Tages) |
| 36 | + .FirstOrDefault(p => p.Book_ID == bookID); |
| 37 | + if (image_url != null) book.Image_url = image_url; |
| 38 | + return View(book); |
| 39 | + } |
| 40 | + |
| 41 | + public ViewResult AuthorsView(int authorID) |
| 42 | + { |
| 43 | + return View(repository.Authors.FirstOrDefault(x => x.Author_ID == authorID)); |
| 44 | + } |
| 45 | + public ViewResult Index() |
| 46 | + { |
| 47 | + return View(repository.Books.Include(x => x.Author).Include(x => x.Tages)); |
| 48 | + // return View(repository.Books); |
| 49 | + } |
| 50 | + public ViewResult Create() |
| 51 | + { |
| 52 | + return View(new Book()); |
| 53 | + } |
| 54 | + [HttpPost] |
| 55 | + public ActionResult FindBookImage(string title, string last_name, string first_name, int book_ID) |
| 56 | + { |
| 57 | + IList<SearchResult> searchResults = SearchResult.getSearch(title + " " + last_name + " " + first_name, "&searchType=image"); |
| 58 | + ViewData["BookID"] = book_ID; |
| 59 | + return PartialView(searchResults.Select(x => x.link)); |
| 60 | + } |
| 61 | + //[HttpPost] |
| 62 | + public ActionResult SaveBookImage(string image_url, int bookID) |
| 63 | + { |
| 64 | + Book book = repository.Books.FirstOrDefault(c => c.Book_ID == bookID); |
| 65 | + book.Image_url = image_url; |
| 66 | + repository.SaveBook(book); |
| 67 | + return RedirectToAction("Edit", new { bookID }); |
| 68 | + } |
| 69 | + public ActionResult FindBookAnnotation(string title, string last_name, string first_name, int bookID) |
| 70 | + { |
| 71 | + string query = title + " " + last_name + " " + first_name+" "+"litres"; |
| 72 | + var list = SearchResult.getSearch(query).Select(x => x); |
| 73 | + string liveUrl = SearchResult.getSearch(query).FirstOrDefault(x => x.link.Contains("livelib")).link; |
| 74 | + //string liveUrl1 = SearchResult.getSearch(query).FirstOrDefault(x => x.link.Contains("mybook")).link; |
| 75 | + //var liveUrl2 = SearchResult.getSearch(query).FirstOrDefault(x => x.link.Contains("litres")); |
| 76 | + //string liveUrl3 = SearchResult.getSearch(query).FirstOrDefault(x => x.link.Contains("aldebaran")).link; |
| 77 | + List<string> links=new List<string>(); |
| 78 | + List<string> libraries=new List<string>(){"livelib","loveread","litres", "mybook","aldebaran","wikipedia","knizhnik","imhonet"}; |
| 79 | + |
| 80 | + foreach (string result in SearchResult.getSearch(query).Select(l=>l.link)) |
| 81 | + { |
| 82 | + if(libraries.Any(result.Contains)){ |
| 83 | + links.Add(result); |
| 84 | + } |
| 85 | + } |
| 86 | + Book book = repository.Books.FirstOrDefault(x => x.Book_ID == bookID); |
| 87 | + book.Annotation = HttpUtility.HtmlDecode(SearchResult.GetInnerText(liveUrl)); |
| 88 | + repository.SaveBook(book); |
| 89 | + return RedirectToAction("Edit", new { bookID }); |
| 90 | + } |
| 91 | + |
| 92 | + [HttpPost] |
| 93 | + public ActionResult Create(Book book) |
| 94 | + { |
| 95 | + if (ModelState.IsValid) |
| 96 | + { |
| 97 | + Author author = repository.Authors.FirstOrDefault(x => x.Last_Name == book.Author.Last_Name && x.First_Name == book.Author.First_Name); |
| 98 | + if (author == null) |
| 99 | + { |
| 100 | + author = new Author() { Last_Name = book.Author.Last_Name, First_Name = book.Author.First_Name, Middle_Name = book.Author.Middle_Name }; |
| 101 | + } |
| 102 | + |
| 103 | + book.Author = author; |
| 104 | + ICollection<Tag> list = repository.GetTags(book); |
| 105 | + book.Tages = list; |
| 106 | + |
| 107 | + repository.SaveBook(book); |
| 108 | + TempData["message"] = string.Format("{0} has been saved", book.Title); |
| 109 | + return RedirectToAction("Edit", new { bookID = book.Book_ID }); |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + // there is something wrong with the data values |
| 114 | + return View(book); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + [HttpPost] |
| 119 | + public ActionResult Edit(Book book) |
| 120 | + { |
| 121 | + if (ModelState.IsValid) |
| 122 | + { |
| 123 | + repository.SaveBook(book); |
| 124 | + TempData["message"] = string.Format("{0} has been saved", book.Title); |
| 125 | + return RedirectToAction("Index"); |
| 126 | + } |
| 127 | + else |
| 128 | + { |
| 129 | + // there is something wrong with the data values |
| 130 | + return View(book); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + [HttpPost] |
| 135 | + public ActionResult Delete(int bookId) |
| 136 | + { |
| 137 | + Book deletedBook = repository.DeleteBook(bookId); |
| 138 | + if (deletedBook != null) |
| 139 | + { |
| 140 | + TempData["message"] = string.Format("{0} was deleted", deletedBook.Title); |
| 141 | + } |
| 142 | + return RedirectToAction("Index"); |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + } |
| 147 | +} |
0 commit comments