1 | using Microsoft.AspNetCore.Identity;
|
---|
2 | using Microsoft.AspNetCore.Mvc;
|
---|
3 | using Microsoft.EntityFrameworkCore;
|
---|
4 | using Microsoft.AspNetCore.Mvc.Rendering;
|
---|
5 | using PostgreSqlDotnetCore.Models;
|
---|
6 | using System;
|
---|
7 | using System.Net;
|
---|
8 |
|
---|
9 | namespace PostgreSqlDotnetCore.Controllers
|
---|
10 | {
|
---|
11 | public class PetCaresController : BaseController
|
---|
12 | {
|
---|
13 | public PetCaresController(UserManager<IdentityUser> userManager) : base(userManager)
|
---|
14 | {
|
---|
15 | }
|
---|
16 |
|
---|
17 |
|
---|
18 | [HttpGet]
|
---|
19 | /* public async Task<ActionResult> Create()
|
---|
20 | {
|
---|
21 |
|
---|
22 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
23 |
|
---|
24 | ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
|
---|
25 |
|
---|
26 | return View();
|
---|
27 | }*/
|
---|
28 | public async Task<ActionResult> Create()
|
---|
29 | {
|
---|
30 |
|
---|
31 |
|
---|
32 | UsersClass customerClass = await getCrrentUser();
|
---|
33 |
|
---|
34 | ViewBag.isAuthenticated = customerClass;
|
---|
35 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
36 | ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
|
---|
37 |
|
---|
38 |
|
---|
39 | // check if the user is authenticated so we can take only his pets
|
---|
40 | if (customerClass != null)
|
---|
41 | {
|
---|
42 | var queryPetsByUser = from st in db.PetsObj
|
---|
43 | where st.usersid == customerClass.id
|
---|
44 | select st;
|
---|
45 | var userPets = await queryPetsByUser.ToListAsync<PetsClass>();
|
---|
46 | ViewBag.Pets = new SelectList(userPets, "id", "name");
|
---|
47 |
|
---|
48 | }
|
---|
49 | return View();
|
---|
50 | }
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 |
|
---|
55 | // GET: Customer
|
---|
56 | public async Task<ActionResult> IndexAsync()
|
---|
57 | {
|
---|
58 | // check for permission
|
---|
59 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
60 | UsersClass customerClass = await getCrrentUser();
|
---|
61 | // set if is authenticated
|
---|
62 | ViewBag.isAuthenticated = customerClass;
|
---|
63 | if (customerClass == null)
|
---|
64 | {
|
---|
65 | return RedirectToAction("AccessDenied", "Error");
|
---|
66 | }
|
---|
67 | // no access for standard user
|
---|
68 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
69 |
|
---|
70 | if (customerClass.role_id == RoleConstants.Standard)
|
---|
71 | {
|
---|
72 | // query
|
---|
73 | var query = from st in db.PetCaresObj
|
---|
74 | where st.usersid == customerClass.id
|
---|
75 | select st;
|
---|
76 |
|
---|
77 | var userPetCares =
|
---|
78 | await query.Include(n => n.PetsClass).ToListAsync<Pet_CaresClass>();
|
---|
79 |
|
---|
80 | return View(userPetCares);
|
---|
81 | }
|
---|
82 | else
|
---|
83 | {
|
---|
84 | return View(db.PetCaresObj.Include(n => n.PetsClass).ToList());
|
---|
85 | }
|
---|
86 |
|
---|
87 | }
|
---|
88 |
|
---|
89 | // GET: Customer/Details/5
|
---|
90 | public async Task<ActionResult> Details(int? id)
|
---|
91 | {
|
---|
92 | if (id == null)
|
---|
93 | {
|
---|
94 | return RedirectToAction("NotExist", "Error");
|
---|
95 | }
|
---|
96 | UsersClass customerClass = await getCrrentUser();
|
---|
97 | ViewBag.isAuthenticated = customerClass;
|
---|
98 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
99 | if (peClass == null)
|
---|
100 | {
|
---|
101 | return RedirectToAction("NotExist", "Error");
|
---|
102 | }
|
---|
103 | // no access for standard user
|
---|
104 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
105 |
|
---|
106 | return View(peClass);
|
---|
107 | }
|
---|
108 |
|
---|
109 | // GET: Customer/Create
|
---|
110 | //public ActionResult Create()
|
---|
111 | //{
|
---|
112 | // return View();
|
---|
113 | //}
|
---|
114 |
|
---|
115 | /*public ActionResult Create()
|
---|
116 | {
|
---|
117 |
|
---|
118 | return View();
|
---|
119 | }*/
|
---|
120 |
|
---|
121 | // POST: Customer/Create
|
---|
122 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
123 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
124 | [HttpPost]
|
---|
125 | [ValidateAntiForgeryToken]
|
---|
126 | public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid, pet_id")] Pet_CaresClass peClass)
|
---|
127 | {
|
---|
128 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
129 | if (!isAuthenticated)
|
---|
130 | {
|
---|
131 | return RedirectToAction("AccessDenied", "Error");
|
---|
132 | }
|
---|
133 | ViewBag.isAuthenticated = new UsersClass();
|
---|
134 | // no access for standard user
|
---|
135 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
136 | UsersClass customerClass = null;
|
---|
137 | if (ModelState.IsValid)
|
---|
138 | {
|
---|
139 | ViewBag.isAuthenticated = new UsersClass();
|
---|
140 | peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
|
---|
141 | peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
|
---|
142 | var user = await _userManager.GetUserAsync(User);
|
---|
143 | customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
144 | peClass.usersid = customerClass.id;
|
---|
145 | db.PetCaresObj.Add(peClass);
|
---|
146 | db.SaveChanges();
|
---|
147 | return RedirectToAction("Index");
|
---|
148 | }
|
---|
149 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
150 | ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
|
---|
151 |
|
---|
152 |
|
---|
153 | var queryPetsByUser = from st in db.PetsObj
|
---|
154 | where st.usersid == customerClass.id
|
---|
155 | select st;
|
---|
156 | var userPets =await queryPetsByUser.ToListAsync<PetsClass>();
|
---|
157 | ViewBag.Pets= new SelectList(userPets, "id", "name");
|
---|
158 |
|
---|
159 |
|
---|
160 | return View(peClass);
|
---|
161 | }
|
---|
162 |
|
---|
163 |
|
---|
164 |
|
---|
165 | // GET: Customer/Edit/5
|
---|
166 | /* public ActionResult Edit(int? id)
|
---|
167 | {
|
---|
168 | if (id == null)
|
---|
169 | {
|
---|
170 | return RedirectToAction("NotExist", "Error");
|
---|
171 | }
|
---|
172 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
173 | if (peClass == null)
|
---|
174 | {
|
---|
175 | return RedirectToAction("NotExist", "Error");
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | return View(peClass);
|
---|
180 | }*/
|
---|
181 | // GET: Customer/Edit/5
|
---|
182 | /* public ActionResult Edit(int? id)
|
---|
183 | {
|
---|
184 | if (id == null)
|
---|
185 | {
|
---|
186 | return RedirectToAction("NotExist", "Error");
|
---|
187 | }
|
---|
188 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
189 | if (peClass == null)
|
---|
190 | {
|
---|
191 | return RedirectToAction("NotExist", "Error");
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | return View(peClass);
|
---|
196 | }*/
|
---|
197 | public async Task<ActionResult> Edit(int? id)
|
---|
198 | {
|
---|
199 | if (id == null)
|
---|
200 | {
|
---|
201 | return RedirectToAction("NotExist", "Error");
|
---|
202 | }
|
---|
203 |
|
---|
204 | Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
|
---|
205 | if (peClass == null)
|
---|
206 | {
|
---|
207 | return RedirectToAction("NotExist", "Error");
|
---|
208 | }
|
---|
209 |
|
---|
210 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
211 | ViewBag.VetCenters = new SelectList(vetCenters, "id", "name", peClass.vetcentersid);
|
---|
212 | // dodadeno na 22.08
|
---|
213 | UsersClass customerClass = await getCrrentUser();
|
---|
214 | // check if the user is authenticated so we can take only his pets
|
---|
215 | if (customerClass != null)
|
---|
216 | {
|
---|
217 | var queryPetsByUser = from st in db.PetsObj
|
---|
218 | where st.usersid == customerClass.id
|
---|
219 | select st;
|
---|
220 | var userPets = await queryPetsByUser.ToListAsync<PetsClass>();
|
---|
221 | ViewBag.Pets = new SelectList(userPets, "id", "name");
|
---|
222 |
|
---|
223 | }
|
---|
224 | ViewBag.isAuthenticated = customerClass;
|
---|
225 | return View(peClass);
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 |
|
---|
230 | // POST: Customer/Edit/5
|
---|
231 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
232 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
233 | [HttpPost]
|
---|
234 | [ValidateAntiForgeryToken]
|
---|
235 | public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, vetcentersid, pet_id")] Pet_CaresClass peClass)
|
---|
236 | {
|
---|
237 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
238 | if (!isAuthenticated)
|
---|
239 | {
|
---|
240 | return RedirectToAction("AccessDenied", "Error");
|
---|
241 | }
|
---|
242 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
243 | // no access for standard user
|
---|
244 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
245 |
|
---|
246 | if (ModelState.IsValid)
|
---|
247 | {
|
---|
248 | peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
|
---|
249 | var user = await _userManager.GetUserAsync(User);
|
---|
250 | var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
251 | peClass.usersid = customerClass.id;
|
---|
252 | db.Entry(peClass).State = EntityState.Modified;
|
---|
253 | db.SaveChanges();
|
---|
254 | return RedirectToAction("Index");
|
---|
255 | }
|
---|
256 | return View(peClass);
|
---|
257 | }
|
---|
258 |
|
---|
259 |
|
---|
260 |
|
---|
261 |
|
---|
262 | // GET: Customer/Delete/5
|
---|
263 | public async Task<ActionResult> Delete(int? id)
|
---|
264 | {
|
---|
265 | if (id == null)
|
---|
266 | {
|
---|
267 | return RedirectToAction("NotExist", "Error");
|
---|
268 | }
|
---|
269 | UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
|
---|
270 | ViewBag.isAuthenticated = customerClass;
|
---|
271 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
272 | if (peClass == null)
|
---|
273 | {
|
---|
274 | return RedirectToAction("NotExist", "Error");
|
---|
275 | }
|
---|
276 | // no access for standard user
|
---|
277 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
278 |
|
---|
279 | return View(peClass);
|
---|
280 | }
|
---|
281 |
|
---|
282 | // POST: Customer/Delete/5
|
---|
283 | /* [HttpPost, ActionName("Delete")]
|
---|
284 | [ValidateAntiForgeryToken]
|
---|
285 | public ActionResult DeleteConfirmed(int id)
|
---|
286 | {
|
---|
287 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
288 | db.PetCaresObj.Remove(peClass);
|
---|
289 | db.SaveChanges();
|
---|
290 | return RedirectToAction("Index");
|
---|
291 | }
|
---|
292 | */
|
---|
293 |
|
---|
294 | [HttpPost, ActionName("Delete")]
|
---|
295 | [ValidateAntiForgeryToken]
|
---|
296 | public async Task<ActionResult> DeleteConfirmed(int id)
|
---|
297 | {
|
---|
298 | Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
|
---|
299 | if (peClass == null)
|
---|
300 | {
|
---|
301 | return RedirectToAction("NotExist", "Error");
|
---|
302 | }
|
---|
303 | db.PetCaresObj.Remove(peClass);
|
---|
304 | await db.SaveChangesAsync();
|
---|
305 | return RedirectToAction("Index");
|
---|
306 | }
|
---|
307 |
|
---|
308 | protected override void Dispose(bool disposing)
|
---|
309 | {
|
---|
310 | if (disposing)
|
---|
311 | {
|
---|
312 | db.Dispose();
|
---|
313 | }
|
---|
314 | base.Dispose(disposing);
|
---|
315 | }
|
---|
316 | }
|
---|
317 | }
|
---|