Skip to content

Commit 5293f9c

Browse files
committed
fix registration abd suggestion
1 parent 7d99e52 commit 5293f9c

File tree

13 files changed

+125
-112
lines changed

13 files changed

+125
-112
lines changed

BookStore.BLL.RepositoryService/UserService.cs

+6
Original file line numberDiff line numberDiff line change
@@ -75,5 +75,11 @@ public async Task RateBook(float rate, int userId, int bookId,bool isSuggestion)
7575
//Rate Rate=new Rate{RateValue = rate, User_ID = userId};
7676
await _repository.RateBook(rate, userId, bookId,isSuggestion);
7777
}
78+
79+
80+
public void Resuggest1()
81+
{
82+
_repository.Resuggest1();
83+
}
7884
}
7985
}

BookStore.DAL.EntityFramework/EfBookRepository.cs

-5
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public IList<Book> GetBooksByLetter(string letter)
1616
var num = Enumerable.Range(0, 10).Select(i => i.ToString());
1717
return context.Books
1818
.Include(b => b.BookAuthors)
19-
//.Include(b => b.Genres)
20-
//.Include(b => b.Tages)
2119
.Where(p =>letter == "All" || p.Title.StartsWith(letter) ||(num.Contains(p.Title.Substring(0, 1)) && letter == "0-9")).ToList();
2220
}
2321
}
@@ -76,8 +74,6 @@ public IList<Book> GetBooksByTag(int tagId)
7674
{
7775
return context.Books
7876
.Include(b => b.BookAuthors)
79-
//.Include(b => b.Genres)
80-
//.Include(b => b.Tages)
8177
.Where(b => b.BookDetail.Tages.Any(t => t.Tag_ID == tagId)).ToList();
8278
}
8379
}
@@ -103,7 +99,6 @@ public override Book GetById(int id)
10399
.Include(a => a.BookAuthors)
104100
.Include(a => a.BookDetail.Genres)
105101
.Include(a => a.BookDetail.Tages)
106-
//.Include(x => x.BookDetail.Comments.Select(c=>c.Book))
107102
.Include(x => x.BookDetail.Comments.Select(c => c.User))
108103
.Include(x => x.BookDetail.WishedUsers)
109104
.FirstOrDefault(b => b.Book_ID == id);

BookStore.DAL.EntityFramework/EfUserRepository.cs

+94-81
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using BookStore.DO.Entities;
55
using System.Data.Entity;
6+
using System.Data.SqlTypes;
67
using System.Diagnostics;
78
using System.Threading.Tasks;
89
using BookStore.DAL.Interface.Abstract;
@@ -86,7 +87,7 @@ public async Task RateBook(float rate, int userId, int bookId, bool isSuggestion
8687
context.SaveChanges();
8788
//sw.Stop();
8889
//sw.Elapsed.Duration().Seconds;
89-
//Resuggest();
90+
Resuggest1();
9091
}
9192
}
9293

@@ -147,12 +148,12 @@ public void Suggest(float rate, int userId, int bookId, bool isSuggestion)
147148
context.SaveChanges();
148149
}
149150
}
150-
public float PreditRating(float[] UserFeatures, float[] BookFeatures, int features)
151+
public float PreditRating(float[] userFeatures, float[] bookFeatures, int features)
151152
{
152153
float sum = 0;
153154
for (int i = 0; i < features; i++)
154155
{
155-
sum += UserFeatures[i] * BookFeatures[i];
156+
sum += userFeatures[i] * bookFeatures[i];
156157
}
157158
return sum;
158159
}
@@ -267,86 +268,98 @@ public void Resuggest()
267268
}
268269
}
269270
}
270-
int a = 0;
271-
//var users = GetAll().ToList();
272-
////int maxUserId =users.Select(x => x.User_ID).Max();
273-
//int maxBookId = Context.Books.Select(x => x.Book_ID).Max();
274-
//float[,] matrix = new float[users.Count, maxBookId];
275-
//float[,] result = new float[users.Count, maxBookId];
276-
277-
////int[,] korelation = new int[users.Count, users.Count];
278-
//float[,] corelation = new float[users.Count, users.Count];
279-
////List<double> corelation = new List<double>();
280-
//List<float> average = new List<float>();
281-
//List<float> diff = new List<float>();
282-
//foreach (var user in users.Select((x, i) => new { Value = x, Index = i }))
283-
//{
284-
// int userId = user.Value.User_ID;
285-
// float averageRate = 0;
286-
// int countNonZero = 0;
287-
// foreach (var rate in GetRatedBooks(userId))
288-
// {
289-
// int bookId = rate.Book.Book_ID;
290-
// matrix[user.Index, bookId] = rate.RateValue;
291-
// if (rate.RateValue > 0)
292-
// {
293-
// averageRate += rate.RateValue;
294-
// countNonZero++;
295-
// }
296-
// }
297-
// average.Add((float)averageRate / countNonZero);
271+
}
272+
}
298273

299-
// if (user.Index > 0)
300-
// {
301-
// for (int i = 1; i <= user.Index; i++) //ïî þçåðàì
302-
// {
303-
// double kor = 0;
304-
// float sum = 0, sumSquare1 = 0, sumSquare2 = 0;
305-
// for (int j = 0; j < maxBookId; j++) //ïî êíèæêàì
306-
// {
307-
// if (matrix[user.Index, j] > 0 && matrix[user.Index - i, j] > 0)
308-
// {
309-
// sum += (matrix[user.Index, j] - average.ElementAt(user.Index)) *
310-
// (matrix[user.Index - i, j] - average.ElementAt(user.Index - i));
311-
// sumSquare1 += (matrix[user.Index, j] - average.ElementAt(user.Index)) *
312-
// (matrix[user.Index, j] - average.ElementAt(user.Index));
313-
// sumSquare2 += (matrix[user.Index - i, j] - average.ElementAt(user.Index - i)) *
314-
// (matrix[user.Index - i, j] - average.ElementAt(user.Index - i));
315-
// }
316-
// }
317-
// diff.Add(sumSquare1);
318-
// kor = sum / (Math.Sqrt(sumSquare1) * Math.Sqrt(sumSquare2));
319-
// //corelation.Add(kor);
320-
// corelation[user.Index, user.Index - i] = corelation[user.Index - i, user.Index] = (float)kor;
321-
// }
322-
// }
323-
//}
324-
//float[,] SubMatrix = (float[,])matrix.Clone();
325-
//for (int i = 0; i < SubMatrix.GetLength(0); i++)
326-
//{
327-
// for (int j = 0; j < SubMatrix.GetLength(1); j++)
328-
// {
329-
// if (SubMatrix[i, j] == 0)
330-
// {
331-
// float sum = 0;
332-
// float sumCor = 0;
333-
// for (int k = 0; k < SubMatrix.GetLength(0); k++)
334-
// {
335-
// if (SubMatrix[k, j] != 0)
336-
// {
337-
// sum += (SubMatrix[k, j] - average[k]) * corelation[k, i];
338-
// sumCor += Math.Abs(corelation[k, i]);
339-
// }
340-
// }
341-
// if (sum != 0)
342-
// {
343-
// Suggest(average[i] + sum / sumCor, users[i].User_ID, j, true);
344-
// result[i, j] = average[i] + sum / sumCor;
345-
// }
346-
// }
347-
// }
348-
//}
274+
public void Resuggest1()
275+
{
276+
using (EfDbContext context = new EfDbContext())
277+
{
278+
var users = context.Users.Include(x=>x.Profile.RatedBooks).ToList();
279+
var books = context.Books.Include(x=>x.BookDetail.RatedUsers).ToList();
280+
int maxBookId =context.Books.Select(x => x.Book_ID).Max();
281+
float[,] matrix = new float[users.Count, books.Count];
282+
float[,] result = new float[users.Count, books.Count];
283+
Rate[,] matr = new Rate[users.Count, books.Count];
284+
int rateCounter = 0;
285+
//int[,] korelation = new int[users.Count, users.Count];
286+
float[,] corelation = new float[users.Count, users.Count];
287+
//List<double> corelation = new List<double>();
288+
List<float> average = new List<float>();
289+
List<float> diff = new List<float>();
290+
for (int i = 0; i < users.Count; i++)
291+
{
292+
var rates = users[i].Profile.RatedBooks.Where(x => x.IsSuggestion == false).ToList();
293+
float averageRate = 0;
294+
int countNonZero = 0;
295+
for (int j = 0; j < books.Count; j++)
296+
{
297+
var rate = rates.FirstOrDefault(x => x.Book.Book_ID == books[j].Book_ID);
298+
if (rate != null)
299+
{
300+
matr[i,j] = rate;
301+
rateCounter++;
302+
averageRate += rate.RateValue;
303+
countNonZero++;
304+
average.Add(averageRate / countNonZero);
305+
}
306+
else
307+
{
308+
matr[i,j] =
309+
(new Rate { User = users[i].Profile,IsSuggestion = true,Book = books[j].BookDetail });
310+
}
311+
}
312+
if (i> 0)
313+
{
314+
for (int k = 1; k<= i; k++) //ïî þçåðàì
315+
{
316+
double kor = 0;
317+
float sum = 0, sumSquare1 = 0, sumSquare2 = 0;
318+
for (int j = 0; j < books.Count; j++) //ïî êíèæêàì
319+
{
320+
if (!matr[i, j].IsSuggestion && !matr[i - k, j].IsSuggestion)
321+
{
322+
sum += (matr[i, j].RateValue - average.ElementAt(i)) *
323+
(matr[i - k, j].RateValue - average.ElementAt(i - k));
324+
sumSquare1 += (matr[i, j].RateValue - average.ElementAt(i)) *
325+
(matr[i, j].RateValue - average.ElementAt(i));
326+
sumSquare2 += (matr[i - k, j].RateValue - average.ElementAt(i - k)) *
327+
(matr[i - k, j].RateValue - average.ElementAt(i - k));
328+
}
329+
}
330+
diff.Add(sumSquare1);
331+
kor = sum / (Math.Sqrt(sumSquare1) * Math.Sqrt(sumSquare2));
332+
corelation[i, i - k] =corelation[i - k, i] = (float)kor;
333+
}
334+
}
335+
}
336+
Rate[,] SubMatrix = (Rate[,]) matr.Clone();
337+
for (int i = 0; i < SubMatrix.GetLength(0); i++)
338+
{
339+
for (int j = 0; j < SubMatrix.GetLength(1); j++)
340+
{
341+
if (SubMatrix[i, j].IsSuggestion)
342+
{
343+
float sum = 0;
344+
float sumCor = 0;
345+
for (int k = 0; k < SubMatrix.GetLength(0); k++)
346+
{
347+
if (!SubMatrix[k, j] .IsSuggestion&& !float.IsNaN(average[k]))
348+
{
349+
sum += (SubMatrix[k, j].RateValue - average[k])*corelation[k, i];
350+
sumCor += Math.Abs(corelation[k, i]);
351+
}
352+
}
353+
if (sum != 0 && !float.IsNaN(sum))
354+
{
355+
Suggest((average[i] + sum / sumCor) > 10 ? 10 : (average[i] + sum / sumCor), users[i].User_ID, books[j].Book_ID, true);
356+
result[i, j] = average[i] + sum/sumCor;
357+
}
358+
}
359+
}
360+
}
349361
}
350362
}
351363
}
364+
352365
}

BookStore.DAL/Abstract/IUserRepository.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ public interface IUserRepository : IStoreRepository<User>
2020
void WishBook(int bookId, int userId);
2121
void AddComment(Book book);
2222
void LikeAuthor(int authorId, int userId);
23-
// void Resuggest();
23+
void Resuggest1();
2424
}
2525
}

BookStore.DLL/Abstract/IUserService.cs

+1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@ public interface IUserService:IStoreService<User>
2020
void WishBook(int bookId,int userId);
2121
void AddComment(Book book);
2222
void LikeAuthor(int authorId, int userId);
23+
void Resuggest1();
2324
}
2425
}

BookStore/BookStore.WebUI.csproj

+1-5
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@
315315
<Content Include="fonts\glyphicons-halflings-regular.eot" />
316316
<Content Include="Content\bootstrap-theme.css.map" />
317317
<Content Include="Content\bootstrap.css.map" />
318+
<Content Include="packages.config" />
318319
<None Include="Scripts\jquery.validate-vsdoc.js" />
319320
<Content Include="Scripts\jquery-ui-1.8.24.min.js" />
320321
<Content Include="Scripts\jquery.unobtrusive-ajax.js" />
@@ -391,11 +392,6 @@
391392
<Folder Include="Content\Images\" />
392393
<Folder Include="Views\Suggest\" />
393394
</ItemGroup>
394-
<ItemGroup>
395-
<Content Include="packages.config">
396-
<SubType>Designer</SubType>
397-
</Content>
398-
</ItemGroup>
399395
<ItemGroup>
400396
<ProjectReference Include="..\BookStore.BLL.RepositoryService\BookStore.DLL.RepositoryService.csproj">
401397
<Project>{c5fe34fe-4a88-4a8a-8ab5-20640c99a2d3}</Project>

BookStore/Controllers/AccountController.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ public ActionResult Register()
5959
}
6060

6161
[HttpPost]
62-
public ActionResult Register(User model)
62+
public ActionResult Register(RegisterViewModel model)
6363
{
6464
if (ModelState.IsValid)
6565
{
66-
MembershipUser membershipUser = ((CustomMembershipProvider)Membership.Provider).CreateUser(model.Profile.First_Name, model.Profile.Last_Name, model.Password, model.Email, model.Profile.Avatar_Url, model.Profile.Birthday, model.Profile.Sex);
66+
MembershipUser membershipUser = ((CustomMembershipProvider)Membership.Provider).CreateUser(model.First_Name, model.Last_Name, model.Password, model.Email, model.Avatar_Url, model.Birthday, model.Sex);
6767

6868
if (membershipUser != null)
6969
{

BookStore/Controllers/UserController.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ public int FavoriteAuthor(int userId, int authorId)
7777
_userService.LikeAuthor(authorId, userId);
7878
return _authorService.GetById(authorId).AuthorDetail.FavoriteUsers.Count;
7979
}
80-
80+
81+
public void Suggest()
82+
{
83+
_userService.Resuggest1();
84+
}
8185
}
8286
}

BookStore/Infrastructure/CustomMembershipProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public MembershipUser CreateUser(string firstName, string lastName, string passw
7676
{
7777
try
7878
{
79-
var user = new User();
79+
var user = new User(){Profile = new UserProfile()};
8080
user.Email = email;
8181
user.Profile.First_Name = firstName;
8282
user.Profile.Last_Name = lastName;

BookStore/Scripts/save-image.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,12 @@ function uploadFile(title, author) {
5555

5656
function saveImage(obj, Id, types) {
5757
var link = obj.attr("src");
58-
var im = $("#AuthorDetail_Image_url");
5958
$.ajax({
6059
type: "POST",
6160
url: '/Admin/CopyImageToHost',
6261
data: { imageUrl: link, Id: Id, typesearch: types },
6362
success: function (res) {
64-
debugger;
65-
im.val(res);
63+
$("#image").val(res);
6664
$("#search").empty();
6765
}
6866
});

BookStore/Views/Admin/Edit.cshtml

+11-11
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@
1111
@using (Html.BeginForm())
1212
{
1313
@Html.HiddenFor(b => b.Book_ID)
14-
<div class="editor-label">@Html.LabelFor(b => b.Title, new { @class = "single-line" })</div>
15-
<div class="editor-field">@Html.EditorFor(b => b.Title, new { @class = "single-line" })</div>
14+
<div class="editor-label">@Html.LabelFor(b => b.Title, new {@class = "single-line"})</div>
15+
<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>
18-
@Html.EditorFor(b => b.BookAuthors, new { @class = "single-line" })
17+
<a href="#" class="glyphicon glyphicon-plus" aria-hidden="true" onclick=" return addField('Author') "></a>
18+
@Html.EditorFor(b => b.BookAuthors, new {@class = "single-line"})
1919
</div>
2020
<div class="editor-label">
21-
@Html.LabelFor(b => b.BookDetail.Genres, new { @class = "single-line" })
22-
<a href="#" class="glyphicon glyphicon-plus" aria-hidden="true" onclick="return addField('Genre')"></a>
21+
@Html.LabelFor(b => b.BookDetail.Genres, new {@class = "single-line"})
22+
<a href="#" class="glyphicon glyphicon-plus" aria-hidden="true" onclick=" return addField('Genre') "></a>
2323
</div>
2424
<div id="Genres">
25-
@Html.EditorFor(b => b.BookDetail.Genres, new { @class = "single-line" })
25+
@Html.EditorFor(b => b.BookDetail.Genres, new {@class = "single-line"})
2626
</div>
2727
<div class="editor-label">
2828
@Ajax.ActionLink("Search Annotation", "FindBookAnnotation", "Admin",
2929
new {title = @Model.Title, lastName = @Model.BookAuthors.First().Last_Name, firstName = @Model.BookAuthors.First().First_Name, bookId = @Model.Book_ID},
3030
new AjaxOptions {UpdateTargetId = "search", InsertionMode = InsertionMode.Replace, HttpMethod = "POST"})
3131
</div>
32-
<div class="editor-field">@Html.TextAreaFor(b => b.BookDetail.Annotation, new { @class = "text-box multi-line", id = "AreaText" })</div>
32+
<div class="editor-field">@Html.TextAreaFor(b => b.BookDetail.Annotation, new {@class = "text-box multi-line", id = "AreaText"})</div>
3333
<div class="editor-label">
3434
@Ajax.ActionLink("Search Image", "FindImage", "Admin",
35-
new { title = @Model.Title, lastName = @Model.BookAuthors.First().Last_Name, firstName = @Model.BookAuthors.First().First_Name, Id = @Model.Book_ID },
36-
new AjaxOptions { UpdateTargetId = "search", InsertionMode = InsertionMode.Replace, HttpMethod = "POST" })
35+
new {title = @Model.Title, lastName = @Model.BookAuthors.First().Last_Name, firstName = @Model.BookAuthors.First().First_Name, Id = @Model.Book_ID},
36+
new AjaxOptions {UpdateTargetId = "search", InsertionMode = InsertionMode.Replace, HttpMethod = "POST"})
3737
</div>
38-
<div class="editor-field">@Html.EditorFor(b => b.Image_url, new { @class = "single-line" })</div>
38+
<div class="editor-field">@Html.EditorFor(b => b.Image_url, new {htmlAttributes=new {@class = "single-line", id = "image"}})</div>
3939
<div class="editor-label">@Html.LabelFor(b => b.BookDetail.Price, new { @class = "single-line" })</div>
4040
<div class="editor-field">@Html.EditorFor(b => b.BookDetail.Price, new { @class = "single-line", type = "number", step = "any" })</div>
4141
<div class="editor-label">

BookStore/Views/Admin/EditAuthor.cshtml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
new { title = "", lastName = @Model.Last_Name, firstName = @Model.First_Name, Id = @Model.Author_ID },
3030
new AjaxOptions { UpdateTargetId = "search", InsertionMode = InsertionMode.Replace, HttpMethod = "POST" })
3131
</div>
32-
<div class="editor-field">@Html.EditorFor(b => b.AuthorDetail.Image_url, new {@class = "single-line"})</div>
32+
<div class="editor-field">@Html.EditorFor(b => b.AuthorDetail.Image_url,new {htmlAttributes= new {@class = "single-line",id="image"}})</div>
3333
<p>
3434
<input type="submit" class="btn-default btn" value="Save" />
3535
@Html.ActionLink("Delete", "DeleteAuthor", new { authId = Model.Author_ID }, new { @class = "btn-default btn" })

0 commit comments

Comments
 (0)