Skip to content

Commit 79e4b98

Browse files
committed
fix bags with DBContext
1 parent b1ee5fe commit 79e4b98

File tree

10 files changed

+36
-62
lines changed

10 files changed

+36
-62
lines changed

BookStore.DAL.EntityFramework/EfAuthorRepository.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public override Author GetById(int id)
4949
{
5050
using (EfDbContext context = new EfDbContext())
5151
{
52-
return context.Authors.Include(x => x.Books).FirstOrDefault(x => x.Author_ID == id);
52+
return context.Authors.Include(x => x.Books.Select(c=>c.BookAuthors)).Include(x=>x.FavoriteUsers).FirstOrDefault(q => q.Author_ID == id);
5353
}
5454
}
5555

BookStore.DAL.EntityFramework/EfBookRepository.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public Rate GetRate(int bookId, int userId)
227227
{
228228
using (EfDbContext context = new EfDbContext())
229229
{
230-
return context.Rates.FirstOrDefault(x => x.User_ID == userId && x.Book.Book_ID == bookId);
230+
return context.Rates.Include(x=>x.Book).FirstOrDefault(x => x.User_ID == userId && x.Book.Book_ID == bookId);
231231
}
232232
}
233233
}

BookStore.DAL.EntityFramework/EfUserRepository.cs

+10-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ namespace BookStore.DAL.EntityFramework
1212
{
1313
public class EfUserRepository : EfStoreRepository<User>, IUserRepository
1414
{
15+
public override User GetById(int id)
16+
{
17+
using (EfDbContext context = new EfDbContext())
18+
{
19+
return context.Users.Include(x=>x.Comments).FirstOrDefault(x=>x.User_ID==id);
20+
}
21+
}
22+
1523
public IList<Book> GetReccomendedBooks(int userId)
1624
{
1725
throw new NotImplementedException();
@@ -54,7 +62,8 @@ public User GetUserByEmail(string email)
5462
using (EfDbContext context = new EfDbContext())
5563
{
5664
return context.Users.Include(e => e.Roles)
57-
.Include(x => x.RatedBooks)
65+
.Include(x=>x.WishedBooks)
66+
.Include(x => x.RatedBooks.Select(b=>b.Book.BookAuthors))
5867
.Include(x => x.FavoriteAuthors)
5968
.FirstOrDefault(e => e.Email == email);
6069
}

BookStore.Tests/UnitTest1.cs

-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Web.Mvc;
6-
using BookStore.DLL.Interface.RepositoryService;
76
using Microsoft.VisualStudio.TestTools.UnitTesting;
87
using BookStore.Controllers;
9-
using BookStore.DAL.Interface.Abstract;
10-
using BookStore.DAL.Interface.EntityFramework;
118
using BookStore.DLL.Interface.Abstract;
129
using BookStore.DO.Entities;
1310
using BookStore.Models;

BookStore/BookStore.WebUI.csproj

+2-1
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
<Compile Include="Models\BookListViewModel.cs" />
225225
<Compile Include="Models\BookViewModel.cs" />
226226
<Compile Include="Models\CommentModel.cs" />
227+
<Compile Include="Models\Fb2Parser.cs" />
227228
<Compile Include="Models\RegisterViewModel.cs" />
228229
<Compile Include="Models\TextViewModel.cs" />
229230
<Compile Include="Models\LoginViewModel.cs" />
@@ -285,7 +286,7 @@
285286
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
286287
</Content>
287288
<Content Include="Scripts\addCommet.js" />
288-
<Content Include="Scripts\AddField.js" />
289+
<Content Include="Scripts\addField.js" />
289290
<Content Include="Scripts\bootstrap3-typeahead.js" />
290291
<Content Include="Scripts\bootstrap.js" />
291292
<Content Include="Scripts\bootstrap.min.js.map">

BookStore/Content/Admin.css.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BookStore/Content/bootstrap.min.css

+6-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BookStore/Controllers/BookController.cs

+5-39
Original file line numberDiff line numberDiff line change
@@ -175,49 +175,15 @@ public PartialViewResult BookRating(int bookId)
175175
return p;
176176
}
177177

178-
public ViewResult Fb2Text(string path, int section = 0, int page = 1)//TODO: Separate and move to DLL
178+
public ViewResult Fb2Text(string path, int section = 0, int page = 1)
179179
{
180-
XDocument doc = XDocument.Load(Server.MapPath(path));
181-
StringBuilder text = new StringBuilder();
182-
int pageCharacters = 15000;
183-
List<string> chapters = new List<string>();
184-
if (doc.Root != null)
185-
{
186-
var body = doc.Root.Elements().FirstOrDefault(x => x.Name.LocalName == "body");
187-
if (body != null)
188-
{
189-
var sections = body.Elements().Where(x => x.Name.LocalName == "section");
190-
var xElements = sections as IList<XElement> ?? sections.ToList();
191-
foreach (var chapter in xElements)
192-
{
193-
var name = chapter.Elements().FirstOrDefault(x => x.Name.LocalName == "title");
194-
if (name != null)
195-
{
196-
chapters.Add(name.Element(body.GetDefaultNamespace() + "p").Value);
197-
}
198-
}
199-
var main = xElements.Count() > 2 ? xElements.ElementAt(section) : body;
200-
using (var xReader = main.CreateReader())
201-
{
202-
xReader.MoveToContent();
203-
text.Append(xReader.ReadInnerXml());
204-
}
205-
}
206-
}
207-
if (text.Length >= pageCharacters)
208-
{
209-
text.Append(' ', pageCharacters - text.Length%pageCharacters);
210-
}
211-
else
212-
{
213-
pageCharacters = text.Length;
214-
}
180+
Fb2Parser fb2 = new Fb2Parser(Server.MapPath(path),section);
215181
TextViewModel model = new TextViewModel()
216182
{
217-
Text = text.ToString((page - 1) * pageCharacters, pageCharacters),
218-
Chapters = chapters,
183+
Text = fb2.Text.ToString((page - 1) * fb2.PageCharacters, fb2.PageCharacters)+" - ",
184+
Chapters = fb2.Chapters,
219185
CurrentChapter = section,
220-
PagingInfo = new PagingInfo(page, pageCharacters, text.Length),
186+
PagingInfo = new PagingInfo(page, fb2.PageCharacters, fb2.Text.Length),
221187
CurrentPath = path
222188
};
223189
return View(model);

BookStore/Scripts/AddField.js

+7-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
OwnershipsCount = $("#Tags>input").length;
33
AuthorFieldsCount = $("#Authors>input").length;
44
GenresFieldsCount = $("#Genres>input").length;
5-
$(".remove-field").click(RemoveField);
5+
$(".remove-field").click(removeField);
66

77
$(".bookCover").click(function () {
88
var link = $(this).child().attr(src);
@@ -13,13 +13,13 @@ var OwnershipsCount = 0;
1313
var AuthorFieldsCount = 0;
1414
var GenresFieldsCount = 0;
1515

16-
function AddField(type) {
16+
function addField(type) {
1717
//добавляем поля
1818
if (type == "Tag") {
1919
var OwnershipContainer = $("<div/>").attr("class", "ownership-container").attr("id", "OwnershipContainer" + OwnershipsCount).appendTo($("#Tags"));
2020
$("<input/>").attr("class", "text-box single-line").attr("type", "text").attr("id", "Ownerships[" + OwnershipsCount + "]_Name").attr("name", "Tages[" + OwnershipsCount + "].Tag_Name").attr("value", "").appendTo(OwnershipContainer);
2121
var RemoveButton = $("<a/>").attr("class", "remove-field glyphicon glyphicon-remove").attr("item", OwnershipsCount).attr("href", "#").appendTo(OwnershipContainer);
22-
RemoveButton.click(function () { RemoveField(type, RemoveButton); });
22+
RemoveButton.click(function () { removeField(type, RemoveButton); });
2323
OwnershipsCount++;
2424
}
2525
else if (type == "Author") {
@@ -28,21 +28,20 @@ function AddField(type) {
2828
$("<input/>").attr("class", "text-box single-line").attr("type", "text").attr("id", "Author" + AuthorFieldsCount + "Middle").attr("name", "BookAuthors[" + AuthorFieldsCount + "].Middle_Name").attr("value", "").appendTo(OwnershipContainer);
2929
$("<input/>").attr("class", "text-box single-line").attr("type", "text").attr("id", "Author" + AuthorFieldsCount + "Last").attr("name", "BookAuthors[" + AuthorFieldsCount + "].Last_Name").attr("value", "").appendTo(OwnershipContainer);
3030
var RemoveButton = $("<a/>").attr("class", "remove-field glyphicon glyphicon-remove").attr("item", AuthorFieldsCount).attr("href", "#").appendTo(OwnershipContainer);
31-
RemoveButton.click(function () { RemoveField(type, RemoveButton); });
31+
RemoveButton.click(function () { removeField(type, RemoveButton); });
3232
AuthorFieldsCount = AuthorFieldsCount + 1;
3333
}
3434
else if (type == "Genre") {
3535

3636
var OwnershipContainer = $("<div/>").attr("class", "ownership-container").attr("id", "OwnershipContainer" + GenresFieldsCount).appendTo($("#Genres"));
3737
$("<input/>").attr("class", "text-box single-line").attr("type", "text").attr("id", "Genre" + GenresFieldsCount).attr("name", "Genres[" + GenresFieldsCount + "].Genre_Name").attr("value", "").appendTo(OwnershipContainer);
3838
var RemoveButton = $("<a/>").attr("class", "remove-field glyphicon glyphicon-remove").attr("item", GenresFieldsCount).attr("href", "#").appendTo(OwnershipContainer);
39-
RemoveButton.click(function () { RemoveField(type, RemoveButton); });
39+
RemoveButton.click(function () { removeField(type, RemoveButton); });
4040
GenresFieldsCount++;
4141
}
42-
return false
4342
}
4443

45-
function RemoveField(type, RemoveButton) {
44+
function removeField(type, RemoveButton) {
4645
debugger;
4746
var RecalculateStartNum = parseInt(RemoveButton.attr("item"));
4847
var count = 0;
@@ -65,10 +64,9 @@ function RemoveField(type, RemoveButton) {
6564
RecalculateNamesAndIds(i, type);
6665
}
6766
// OwnershipsCount--;
68-
return false;
6967
}
7068

71-
function RecalculateNamesAndIds(number, type) {
69+
function recalculateNamesAndIds(number, type) {
7270
var prevNumber = number - 1;
7371
//скобки "[" и "]" которые присутствуют в id DOM-объекта в jquery селекторе необходим экранировать двойным обратным слэшем \\
7472
if (type == "Tag") {
@@ -82,5 +80,4 @@ function RecalculateNamesAndIds(number, type) {
8280
$("#Author" + number + "Middle").attr("name", "BookAuthors[" + prevNumber + "].Middle_Name");
8381
$("#Author" + number + "Last").attr("name", "BookAuthors[" + prevNumber + "].Last_Name");
8482
}
85-
return false;
8683
}

BookStore/Views/Admin/Edit.cshtml

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
<div class="editor-label">@Html.LabelFor(b => b.Title, new { @class = "single-line" })</div>
1515
<div class="editor-field">@Html.EditorFor(b => b.Title, new { @class = "single-line" })</div>
1616
<div id="Authors">
17-
<a href="#" class="glyphicon glyphicon-plus" aria-hidden="true" onclick="return AddField('Author')"></a>
17+
<a href="#" class="glyphicon glyphicon-plus" aria-hidden="true" onclick="return addField('Author')"></a>
1818
@Html.EditorFor(b => b.BookAuthors, new { @class = "single-line" })
1919
</div>
2020
<div class="editor-label">
2121
@Html.LabelFor(b => b.Genres, new { @class = "single-line" })
22-
<a href="#" class="glyphicon glyphicon-plus" aria-hidden="true" onclick="return AddField('Genre')"></a>
22+
<a href="#" class="glyphicon glyphicon-plus" aria-hidden="true" onclick="return addField('Genre')"></a>
2323
</div>
2424
<div id="Genres">
2525
@Html.EditorFor(b => b.Genres, new { @class = "single-line" })
@@ -40,7 +40,7 @@
4040
<div class="editor-field">@Html.EditorFor(b => b.Price, new { @class = "single-line", type = "number", step = "any" })</div>
4141
<div class="editor-label">
4242
@Html.LabelFor(b => b.Tages, new { @class = "single-line" })
43-
<a href="#" id="addField" class="glyphicon glyphicon-plus" aria-hidden="true" onclick=" return AddField('Tag') "></a>
43+
<a href="#" id="addField" class="glyphicon glyphicon-plus" aria-hidden="true" onclick=" return addField('Tag') "></a>
4444
</div>
4545
<div class="editor-field" id="Tags">
4646
@Html.EditorFor(t => t.Tages, new { @class = "single-line" })

0 commit comments

Comments
 (0)