-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProductController.cs
More file actions
84 lines (66 loc) · 2.53 KB
/
Copy pathProductController.cs
File metadata and controls
84 lines (66 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebStore.Models.Abstract;
using WebStore.Models.Entities;
using WebStore.Models.DAL;
using System.Data.Entity;
using WebStore.UI.Models;
namespace WebStore.UI.Controllers
{
public class ProductController : Controller
{
public int PageSize = 5;
private int OtherProduct = 10;
private IUnitOfWork repository;
public ProductController(IUnitOfWork repositories)
{
repository = repositories;
}
public ActionResult List(int? category, int page = 1)
{
var products = repository.Products.GetAll();
if (category != null)
{
products = products.Where(a => a.Categories
.Any(c => c.CategoryID == category))
.OrderByDescending(a => a.ProductID);
}
ProductsListViewModel viewModel = new ProductsListViewModel
{
Products = products.OrderByDescending(p => p.ProductID)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PaginInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = category == null ? repository.Products.GetAll().Count() : products.Count()
},
CurrentCategory = category
};
return View(viewModel);
}
public ViewResult GetOtherProduct(int productID)
{
var categories = repository.Products.GetAll(includeProperties: "Categories", filter: p => p.ProductID == productID).SingleOrDefault();
var Cat = new List<Int32>();//Список ID до яких належить продукт
foreach (var c in categories.Categories)
{
Cat.Add(c.CategoryID);
}
//Інші продукти
var product = repository.Products.GetAll(includeProperties: "Categories", filter: p => (p.ProductID != productID && p.Categories.Any(c => Cat.Contains(c.CategoryID)))).Take(OtherProduct);
return View(product);
}
public ActionResult View(int id)
{
var data = repository.Products.FindById(id);
if (data == null)
throw new HttpException(404, "Not found");
return View(data);
}
}
}