1 | using Microsoft.AspNetCore.Identity;
|
---|
2 | using Microsoft.AspNetCore.Mvc;
|
---|
3 | using Microsoft.EntityFrameworkCore;
|
---|
4 | using Npgsql;
|
---|
5 | using System.Linq;
|
---|
6 | using PostgreSqlDotnetCore.Models;
|
---|
7 | //using System.Net;
|
---|
8 | using System.Threading.Tasks;
|
---|
9 | using PostgreSqlDotnetCore.Data;
|
---|
10 |
|
---|
11 | namespace PostgreSqlDotnetCore.Controllers
|
---|
12 | {
|
---|
13 | public class ProductsController : BaseController
|
---|
14 | {
|
---|
15 |
|
---|
16 | public ProductsController(UserManager<IdentityUser> userManager) : base(userManager)
|
---|
17 | {
|
---|
18 | }
|
---|
19 |
|
---|
20 |
|
---|
21 | //// GET: Customer
|
---|
22 | //public ActionResult Index()
|
---|
23 | //{
|
---|
24 | // //return View(Enumerable.Empty<UsersClass>());
|
---|
25 | // return View(db.ProductObj.ToList());
|
---|
26 | //}
|
---|
27 | // GET: Customer
|
---|
28 | public async Task<ActionResult> IndexAsync(string? searchString)
|
---|
29 | {
|
---|
30 | // set if is authenticated
|
---|
31 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
32 | ViewBag.hasAccess = await checkAuthorizationAsync();
|
---|
33 | // no access for standard user
|
---|
34 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
35 |
|
---|
36 | if (!String.IsNullOrEmpty(searchString))
|
---|
37 | {
|
---|
38 | var products = from s in db.ProductObj
|
---|
39 |
|
---|
40 | where s.name.ToLower().Contains(searchString.ToLower())
|
---|
41 | select s;
|
---|
42 |
|
---|
43 | //products = products.Where(s => s.name.Contains(searchString));
|
---|
44 | return View(products.ToList());
|
---|
45 |
|
---|
46 | }
|
---|
47 | return View(db.ProductObj.ToList());
|
---|
48 | }
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 | // GET: Customer/Details/5
|
---|
53 | public async Task<ActionResult> Details(int? id)
|
---|
54 | {
|
---|
55 | if (id == null)
|
---|
56 | {
|
---|
57 | return RedirectToAction("NotExist", "Error");
|
---|
58 | }
|
---|
59 | // додано на 28.08
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | UsersClass customerClass = await getCrrentUser();
|
---|
65 | ViewBag.isAuthenticated = customerClass;
|
---|
66 | ProductsClass prodClass = db.ProductObj.Find(id);
|
---|
67 | if (prodClass == null)
|
---|
68 | {
|
---|
69 | return RedirectToAction("NotExist", "Error");
|
---|
70 | }
|
---|
71 | // no access for standard user
|
---|
72 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
73 |
|
---|
74 | return View(prodClass);
|
---|
75 | }
|
---|
76 |
|
---|
77 | // GET: Customer/Create
|
---|
78 | //public ActionResult Create()
|
---|
79 | //{
|
---|
80 | // return View();
|
---|
81 | //}
|
---|
82 |
|
---|
83 | public async Task<ActionResult> CreateAsync()
|
---|
84 | {
|
---|
85 | // check for permission
|
---|
86 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
87 | // set if is authenticated
|
---|
88 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
89 |
|
---|
90 | if (customerClass == null)
|
---|
91 | {
|
---|
92 | return RedirectToAction("AccessDenied", "Error");
|
---|
93 | }
|
---|
94 | // no access for standard user
|
---|
95 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
96 |
|
---|
97 | var model = new ProductsClass
|
---|
98 | {
|
---|
99 | // dateadded = DateTime.Now // Поставете го датумот на моменталниот датум
|
---|
100 | };
|
---|
101 | return View(model);
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 |
|
---|
106 |
|
---|
107 | // POST: Customer/Create
|
---|
108 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
109 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
110 | [HttpPost]
|
---|
111 | [ValidateAntiForgeryToken]
|
---|
112 | public ActionResult Create([Bind(include: "id,name,description,price, is_active, dateadded, category, int available_quantity")] ProductsClass prodClass)
|
---|
113 | {
|
---|
114 | if (ModelState.IsValid)
|
---|
115 | {
|
---|
116 | // prodClass.dateadded = new DateTime();
|
---|
117 | prodClass.isactive = true;
|
---|
118 | db.ProductObj.Add(prodClass);
|
---|
119 | db.SaveChanges();
|
---|
120 | return RedirectToAction("Index");
|
---|
121 | }
|
---|
122 |
|
---|
123 | return View(prodClass);
|
---|
124 | }
|
---|
125 |
|
---|
126 | // GET: Customer/Edit/5
|
---|
127 | public async Task<ActionResult> EditAsync(int? id)
|
---|
128 | {
|
---|
129 | if (id == null)
|
---|
130 | {
|
---|
131 | return RedirectToAction("NotExist", "Error");
|
---|
132 | }
|
---|
133 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
134 | // set if is authenticated
|
---|
135 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
136 |
|
---|
137 | if (customerClass == null)
|
---|
138 | {
|
---|
139 | return RedirectToAction("AccessDenied", "Error");
|
---|
140 | }
|
---|
141 | ProductsClass prodClass = db.ProductObj.Find(id);
|
---|
142 | if (prodClass == null)
|
---|
143 | {
|
---|
144 | return RedirectToAction("NotExist", "Error");
|
---|
145 | }
|
---|
146 | return View(prodClass);
|
---|
147 | }
|
---|
148 |
|
---|
149 | // POST: Customer/Edit/5
|
---|
150 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
151 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
152 | [HttpPost]
|
---|
153 | [ValidateAntiForgeryToken]
|
---|
154 | public ActionResult Edit([Bind(include: "id,name,description,price, is_active, dateadded, category, int available_quantity")] ProductsClass prodClass)
|
---|
155 | {
|
---|
156 | if (ModelState.IsValid)
|
---|
157 | {
|
---|
158 | db.Entry(prodClass).State = EntityState.Modified;
|
---|
159 | db.SaveChanges();
|
---|
160 | return RedirectToAction("Index");
|
---|
161 | }
|
---|
162 | return View(prodClass);
|
---|
163 | }
|
---|
164 |
|
---|
165 | // GET: Customer/Delete/5
|
---|
166 | public async Task<ActionResult> DeleteAsync(int? id)
|
---|
167 | {
|
---|
168 | if (id == null)
|
---|
169 | {
|
---|
170 | return RedirectToAction("NotExist", "Error");
|
---|
171 | }
|
---|
172 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
173 | // set if is authenticated
|
---|
174 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
175 |
|
---|
176 | if (customerClass == null)
|
---|
177 | {
|
---|
178 | return RedirectToAction("AccessDenied", "Error");
|
---|
179 | }
|
---|
180 | ProductsClass prodClass = db.ProductObj.Find(id);
|
---|
181 | if (prodClass == null)
|
---|
182 | {
|
---|
183 | return RedirectToAction("NotExist", "Error");
|
---|
184 | }
|
---|
185 | return View(prodClass);
|
---|
186 | }
|
---|
187 |
|
---|
188 | // POST: Customer/Delete/5
|
---|
189 | [HttpPost, ActionName("Delete")]
|
---|
190 | [ValidateAntiForgeryToken]
|
---|
191 | public ActionResult DeleteConfirmed(int id)
|
---|
192 | {
|
---|
193 | ProductsClass prodClass = db.ProductObj.Find(id);
|
---|
194 | db.ProductObj.Remove(prodClass);
|
---|
195 | db.SaveChanges();
|
---|
196 | return RedirectToAction("Index");
|
---|
197 | }
|
---|
198 |
|
---|
199 | protected override void Dispose(bool disposing)
|
---|
200 | {
|
---|
201 | if (disposing)
|
---|
202 | {
|
---|
203 | db.Dispose();
|
---|
204 | }
|
---|
205 | base.Dispose(disposing);
|
---|
206 | }
|
---|
207 | }
|
---|
208 | }
|
---|