source: PostgreSqlDotnetCore/Controllers/PetCaresController.cs

main
Last change on this file was 99d0ecc, checked in by ElenaMoskova <elena.moskova99@…>, 4 weeks ago

fix update/create petcares

add new field modify functions

  • Property mode set to 100644
File size: 11.4 KB
Line 
1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
4using Microsoft.AspNetCore.Mvc.Rendering;
5using PostgreSqlDotnetCore.Models;
6using System;
7using System.Net;
8
9namespace 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 ModelState.Remove("PetsClass");
134 ViewBag.isAuthenticated = new UsersClass();
135 // no access for standard user
136 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
137 UsersClass customerClass = null;
138 if (ModelState.IsValid)
139 {
140 ViewBag.isAuthenticated = new UsersClass();
141 peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
142 peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
143 var user = await _userManager.GetUserAsync(User);
144 customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
145 peClass.usersid = customerClass.id;
146 db.PetCaresObj.Add(peClass);
147 db.SaveChanges();
148 return RedirectToAction("Index");
149 }
150 var vetCenters = await db.VetCentersObj.ToListAsync();
151 ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
152
153 if (customerClass != null)
154 {
155 var queryPetsByUser = from st in db.PetsObj
156 where st.usersid == customerClass.id
157 select st;
158 var userPets = await queryPetsByUser.ToListAsync<PetsClass>();
159 ViewBag.Pets = new SelectList(userPets, "id", "name");
160
161 }
162
163 return View(peClass);
164 }
165
166
167
168 // GET: Customer/Edit/5
169 /* public ActionResult Edit(int? id)
170 {
171 if (id == null)
172 {
173 return RedirectToAction("NotExist", "Error");
174 }
175 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
176 if (peClass == null)
177 {
178 return RedirectToAction("NotExist", "Error");
179 }
180
181
182 return View(peClass);
183 }*/
184 // GET: Customer/Edit/5
185 /* public ActionResult Edit(int? id)
186 {
187 if (id == null)
188 {
189 return RedirectToAction("NotExist", "Error");
190 }
191 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
192 if (peClass == null)
193 {
194 return RedirectToAction("NotExist", "Error");
195 }
196
197
198 return View(peClass);
199 }*/
200 public async Task<ActionResult> Edit(int? id)
201 {
202 if (id == null)
203 {
204 return RedirectToAction("NotExist", "Error");
205 }
206
207 Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
208 if (peClass == null)
209 {
210 return RedirectToAction("NotExist", "Error");
211 }
212
213 var vetCenters = await db.VetCentersObj.ToListAsync();
214 ViewBag.VetCenters = new SelectList(vetCenters, "id", "name", peClass.vetcentersid);
215 // dodadeno na 22.08
216 UsersClass customerClass = await getCrrentUser();
217 // check if the user is authenticated so we can take only his pets
218 if (customerClass != null)
219 {
220 var queryPetsByUser = from st in db.PetsObj
221 where st.usersid == customerClass.id
222 select st;
223 var userPets = await queryPetsByUser.ToListAsync<PetsClass>();
224 ViewBag.Pets = new SelectList(userPets, "id", "name");
225
226 }
227 ViewBag.isAuthenticated = customerClass;
228 return View(peClass);
229 }
230
231
232
233 // POST: Customer/Edit/5
234 // To protect from overposting attacks, enable the specific properties you want to bind to, for
235 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
236 [HttpPost]
237 [ValidateAntiForgeryToken]
238 public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, vetcentersid, pet_id")] Pet_CaresClass peClass)
239 {
240 bool isAuthenticated = User.Identity.IsAuthenticated;
241 if (!isAuthenticated)
242 {
243 return RedirectToAction("AccessDenied", "Error");
244 }
245
246 ModelState.Remove("PetsClass");
247 ViewBag.isAuthenticated = await getCrrentUser();
248 // no access for standard user
249 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
250
251 if (ModelState.IsValid)
252 {
253 peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
254 var user = await _userManager.GetUserAsync(User);
255 var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
256 peClass.usersid = customerClass.id;
257 db.Entry(peClass).State = EntityState.Modified;
258 db.SaveChanges();
259 return RedirectToAction("Index");
260 }
261 return View(peClass);
262 }
263
264
265
266
267 // GET: Customer/Delete/5
268 public async Task<ActionResult> Delete(int? id)
269 {
270 if (id == null)
271 {
272 return RedirectToAction("NotExist", "Error");
273 }
274 UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
275 ViewBag.isAuthenticated = customerClass;
276 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
277 if (peClass == null)
278 {
279 return RedirectToAction("NotExist", "Error");
280 }
281 // no access for standard user
282 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
283
284 return View(peClass);
285 }
286
287 // POST: Customer/Delete/5
288 /* [HttpPost, ActionName("Delete")]
289 [ValidateAntiForgeryToken]
290 public ActionResult DeleteConfirmed(int id)
291 {
292 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
293 db.PetCaresObj.Remove(peClass);
294 db.SaveChanges();
295 return RedirectToAction("Index");
296 }
297 */
298
299 [HttpPost, ActionName("Delete")]
300 [ValidateAntiForgeryToken]
301 public async Task<ActionResult> DeleteConfirmed(int id)
302 {
303 Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
304 if (peClass == null)
305 {
306 return RedirectToAction("NotExist", "Error");
307 }
308 db.PetCaresObj.Remove(peClass);
309 await db.SaveChangesAsync();
310 return RedirectToAction("Index");
311 }
312
313 protected override void Dispose(bool disposing)
314 {
315 if (disposing)
316 {
317 db.Dispose();
318 }
319 base.Dispose(disposing);
320 }
321 }
322}
Note: See TracBrowser for help on using the repository browser.