Skip to content

Commit f9dea4b

Browse files
add modal dialog, cache book's covers to host, edit save book
1 parent 55315f0 commit f9dea4b

20 files changed

+258
-49
lines changed

BookStore.BLL.RepositoryService/BookStore.BLL.RepositoryService.csproj

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<AssemblyName>BookStore.BLL.RepositoryService</AssemblyName>
1212
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
15+
<RestorePackages>true</RestorePackages>
1416
</PropertyGroup>
1517
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1618
<DebugSymbols>true</DebugSymbols>
@@ -68,6 +70,13 @@
6870
<None Include="packages.config" />
6971
</ItemGroup>
7072
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
73+
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
74+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
75+
<PropertyGroup>
76+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
77+
</PropertyGroup>
78+
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
79+
</Target>
7180
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
7281
Other similar extension points exist, see Microsoft.Common.targets.
7382
<Target Name="BeforeBuild">

BookStore.DAL.EntityFramework/EfBookRepository.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,17 @@ public override void Save(Book obj)
119119
public override void Create(Book obj)
120120
{
121121
ICollection<Author> authors = obj.Authors;
122-
obj.Authors = null;
122+
obj.Authors = new List<Author>();
123123
foreach (var author in authors)
124124
{
125-
Author authorForSave = Context.Authors.FirstOrDefault(a => a.Last_Name == author.Last_Name && author.First_Name == a.First_Name) ??
125+
Author authorForSave = Context
126+
.Authors
127+
.FirstOrDefault(a => a.Last_Name == author.Last_Name && author.First_Name == a.First_Name) ??
126128
new Author() { Last_Name = author.Last_Name, First_Name = author.First_Name, Middle_Name = author.Middle_Name };
127-
authorForSave.Books.Add(obj);
129+
130+
obj.Authors.Add(authorForSave);
128131
}
132+
Context.Books.Add(obj);
129133
Context.SaveChanges();
130134
}
131135
}

BookStore.Domain/Entities/Author.cs

+5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ namespace BookStore.DO.Entities
99
{
1010
public class Author
1111
{
12+
public Author()
13+
{
14+
Books = new List<Book>();
15+
}
16+
1217
[Key]
1318
[ScaffoldColumn(false)]
1419
public int Author_ID { get; set; }

BookStore/App_Start/BundleConfig.cs

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public static void RegisterBundles(BundleCollection bundles)
2626
bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
2727
"~/Scripts/bootstrap*"));
2828

29+
bundles.Add(new ScriptBundle("~/bundles/common").Include(
30+
"~/Scripts/common.js"));
31+
2932
bundles.Add(new StyleBundle("~/Content/css")
3033
.Include("~/Content/site1.css") /* не перепутайте порядок */
3134
.Include("~/Content/site.css")

BookStore/BookStore.csproj

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
<IISExpressAnonymousAuthentication />
2121
<IISExpressWindowsAuthentication />
2222
<IISExpressUseClassicPipelineMode />
23+
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
24+
<RestorePackages>true</RestorePackages>
2325
</PropertyGroup>
2426
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
2527
<DebugSymbols>true</DebugSymbols>
@@ -317,6 +319,7 @@
317319
<None Include="Scripts\jquery-1.9.1.intellisense.js" />
318320
<Content Include="Scripts\bootstrap.js" />
319321
<Content Include="Scripts\bootstrap.min.js" />
322+
<Content Include="Scripts\common.js" />
320323
<Content Include="Scripts\demo.js" />
321324
<Content Include="Scripts\jquery-1.9.1.js" />
322325
<Content Include="Scripts\jquery-1.9.1.min.js" />
@@ -381,6 +384,8 @@
381384
<Content Include="Views\Admin\EditorTemplates\Tag.cshtml" />
382385
<Content Include="Views\Admin\EditorTemplates\Author.cshtml" />
383386
<Content Include="Views\Admin\EditorTemplates\Genre.cshtml" />
387+
<Content Include="Views\Account\Ajax.cshtml" />
388+
<Content Include="Views\Account\_Ok.cshtml" />
384389
</ItemGroup>
385390
<ItemGroup>
386391
<Folder Include="App_Data\" />
@@ -446,6 +451,13 @@
446451
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
447452
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
448453
</Target>
454+
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
455+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
456+
<PropertyGroup>
457+
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
458+
</PropertyGroup>
459+
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
460+
</Target>
449461
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
450462
Other similar extension points exist, see Microsoft.Common.targets.
451463
<Target Name="BeforeBuild">
-26 KB
Binary file not shown.
-53.1 KB
Binary file not shown.
-29.3 KB
Binary file not shown.
-15.6 KB
Binary file not shown.

BookStore/Controllers/AccountController.cs

+25-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public AccountController(IUserService user_service, IRoleService role_service)
2222
_userService = user_service;
2323
_roleService = role_service;
2424
}
25-
25+
2626
public ViewResult Login()
2727
{
2828
return View();
@@ -40,7 +40,7 @@ public ActionResult Login(LoginViewModel model, string returnUrl)
4040
}
4141
ModelState.AddModelError("", "Incorrect username or password");
4242
}
43-
return View();//model
43+
return View();
4444
}
4545

4646
public ActionResult LogOff()
@@ -75,12 +75,34 @@ public ActionResult UserLogin()
7575
{
7676
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
7777
if (authCookie != null)
78-
{
78+
{
7979
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
8080
return PartialView(Membership.GetUser(ticket.Name, false));
8181
}
8282
return PartialView(Membership.GetUser());
8383
}
84+
85+
[HttpGet]
86+
public ActionResult Ajax()
87+
{
88+
return PartialView(new LoginViewModel());
89+
}
90+
91+
[HttpPost]
92+
public ActionResult Ajax(LoginViewModel model)
93+
{
94+
if (ModelState.IsValid)
95+
{
96+
if (Membership.ValidateUser(model.UserName, model.Password))
97+
{
98+
Roles.IsUserInRole(model.UserName, "admin");
99+
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
100+
return View("_Ok");
101+
}
102+
ModelState.AddModelError("", "Incorrect username or password");
103+
}
104+
return View(model);
105+
}
84106
}
85107

86108
}

BookStore/Controllers/AdminController.cs

+41-12
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,35 @@
66
using BookStore.DO.Entities;
77
using BookStore.Models;
88
using BookStore.DLL.Abstract;
9+
using System.Net;
10+
using System.IO;
11+
using System.Web.Hosting;
912

1013
namespace BookStore.Controllers
1114
{
1215

1316
[Authorize(Roles = "admin")]
1417
public class AdminController : Controller
1518
{
16-
//
17-
// GET: /Admin/
1819
public AdminController()
1920
{
2021

2122
}
23+
2224
private readonly IBookService _bookService;
2325
private readonly IGenreService _genreService;
2426
private IAuthorService _authorService;
27+
2528
public AdminController(IBookService repo, IGenreService genreService, IAuthorService authorService)
2629
{
2730
_bookService = repo;
2831
_genreService = genreService;
2932
_authorService = authorService;
3033
}
31-
public ViewResult Edit(int bookId, string imageUrl = null)
34+
35+
public ViewResult Edit(int bookId)
3236
{
3337
Book book = _bookService.GetById(bookId);
34-
if (imageUrl != null) book.Image_url = imageUrl;
3538
List<SelectListItem> genreList = new List<SelectListItem>();
3639
foreach (Genre genre in _genreService.Genres)
3740
{
@@ -44,29 +47,58 @@ public ViewResult Edit(int bookId, string imageUrl = null)
4447
ViewBag.Genres = genreList;
4548
return View(book);
4649
}
50+
4751
public ViewResult Index()
4852
{
4953
return View(_bookService.GetAll());
5054
}
55+
5156
public ViewResult Create()
5257
{
5358
return View(new Book { Authors = new List<Author> { new Author() }, Genres = new List<Genre> { new Genre()} });
5459
}
60+
5561
[HttpPost]
5662
public ActionResult FindBookImage(string title, string lastName, string firstName, int bookId)
5763
{
5864
IList<SearchResult> searchResults = SearchResult.getSearch(title + " " + lastName + " " + firstName, "&searchType=image");
5965
ViewData["BookID"] = bookId;
6066
return PartialView(searchResults.Select(x => x.link));
6167
}
62-
//[HttpPost]
68+
6369
public ActionResult SaveBookImage(string imageUrl, int bookId)
6470
{
71+
var dbUrl = copyImageToHost(imageUrl, bookId);
72+
6573
Book book = _bookService.Books.FirstOrDefault(c => c.Book_ID == bookId);
66-
book.Image_url = imageUrl;
74+
book.Image_url = dbUrl;
6775
_bookService.Save(book);
6876
return RedirectToAction("Edit", new { bookID = bookId });
6977
}
78+
79+
private string copyImageToHost(string imageUrl, int bookId)
80+
{
81+
var httpWebRequest = (HttpWebRequest)WebRequest.Create(imageUrl);
82+
httpWebRequest.AllowAutoRedirect = false;
83+
var ex = imageUrl.Split('.');
84+
var imgName = "imgBook" + bookId.ToString() + '.' + ex.Last();
85+
var dbUrl = "~/Content/Images/" + imgName;
86+
var svUrl = Server.MapPath("~/Content/Images/");
87+
var path = string.Format(svUrl + imgName);
88+
89+
using (var fileStream = System.IO.File.Create(path))
90+
{
91+
using (var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
92+
{
93+
using (var stream = httpWebResponse.GetResponseStream())
94+
{
95+
stream.CopyTo(fileStream);
96+
}
97+
}
98+
}
99+
return dbUrl;
100+
}
101+
70102
[HttpPost]
71103
public ActionResult SaveAnnotation(string annotation, int bookId)
72104
{
@@ -75,6 +107,7 @@ public ActionResult SaveAnnotation(string annotation, int bookId)
75107
_bookService.Save(book);
76108
return RedirectToAction("Edit", new { bookID = bookId });
77109
}
110+
78111
[HttpPost]
79112
public ActionResult FindBookAnnotation(string title, string lastName, string firstName, int bookId)
80113
{
@@ -83,32 +116,28 @@ public ActionResult FindBookAnnotation(string title, string lastName, string fir
83116
ViewData["BookID"] = bookId;
84117
return PartialView(SearchResult.GetInnerText(links));
85118
}
119+
86120
[HttpPost]
87121
public ActionResult Create(Book book)
88122
{
89123
if (ModelState.IsValid)
90124
{
91125
_bookService.Create(book);
92-
//book.Authors = authors;
93-
//.Save(book);
94126
TempData["message"] = string.Format("{0} has been saved", book.Title);
95127
return RedirectToAction("Edit", new { bookID = book.Book_ID });
96128
}
97-
// there is something wrong with the data values
98129
return View(book);
99130
}
131+
100132
[HttpPost]
101133
public ActionResult Edit(Book book)
102134
{
103135
if (ModelState.IsValid)
104136
{
105-
//Genre genre = genreService.Genres.FirstOrDefault(g => g.Genre_Name == book.Genre.Genre_Name);
106-
//book.Genre = genre;
107137
_bookService.Save(book);
108138
TempData["message"] = string.Format("{0} has been saved", book.Title);
109139
return RedirectToAction("Index");
110140
}
111-
// there is something wrong with the data values
112141
return View(book);
113142
}
114143

BookStore/Scripts/common.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
function Common() {
2+
_this = this;
3+
this.init = function () {
4+
$("#LoginPopup").click(function () {
5+
_this.showPopup("/Account/Ajax", initLoginPopup);
6+
});
7+
}
8+
9+
this.showPopup = function (url, callback) {
10+
$.ajax({
11+
type: "GET",
12+
url: url,
13+
success: function (data) {
14+
showModalData(data, callback);
15+
}
16+
});
17+
}
18+
19+
function initLoginPopup(modal) {
20+
$("#LoginButton").click(function () {
21+
$.ajax({
22+
type: "POST",
23+
url: "/Account/Ajax",
24+
data: $("#LoginForm").serialize(),
25+
success: function (data) {
26+
showModalData(data);
27+
initLoginPopup(modal);
28+
}
29+
});
30+
});
31+
$(".modal-dialog").keydown(function(event)
32+
{
33+
if (event.keyCode == 13) {
34+
$("#LoginButton").click();
35+
}
36+
});
37+
}
38+
39+
function showModalData(data, callback) {
40+
$(".modal-backdrop").remove();
41+
var popupWrapper = $("#PopupWrapper");
42+
popupWrapper.empty();
43+
popupWrapper.html(data);
44+
var popup = $(".modal", popupWrapper);
45+
$(".modal", popupWrapper).modal('show');
46+
if (callback != undefined) {
47+
callback(popup);
48+
}
49+
}
50+
}
51+
52+
var common = null;
53+
$().ready(function () {
54+
common = new Common();
55+
common.init();
56+
});

0 commit comments

Comments
 (0)