source: PostgreSqlDotnetCore/Controllers/PetCaresController.cs@ e9bb9d1

main
Last change on this file since e9bb9d1 was e9bb9d1, checked in by ElenaMoskova <elena.moskova99@…>, 3 months ago

Use of views

  1. Use of views in VetCenters.
  2. Ability to provide a response to a given response.
  • Property mode set to 100644
File size: 10.6 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 UsersClass customerClass = await getCrrentUser();
60 // set if is authenticated
61 ViewBag.isAuthenticated = customerClass;
62 if (customerClass == null)
63 {
64 return RedirectToAction("AccessDenied", "Error");
65 }
66 // no access for standard user
67 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
68
69 if (customerClass.role_id == RoleConstants.Standard)
70 {
71 // query
72 var query = from st in db.PetCaresObj
73 where st.usersid == customerClass.id
74 select st;
75
76 var userPetCares =
77 await query.ToListAsync<Pet_CaresClass>();
78
79 return View(userPetCares);
80 }
81 else
82 {
83 return View(db.PetCaresObj.ToList());
84 }
85
86 }
87
88 // GET: Customer/Details/5
89 public async Task<ActionResult> Details(int? id)
90 {
91 if (id == null)
92 {
93 return RedirectToAction("NotExist", "Error");
94 }
95 UsersClass customerClass = await getCrrentUser();
96 ViewBag.isAuthenticated = customerClass;
97 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
98 if (peClass == null)
99 {
100 return RedirectToAction("NotExist", "Error");
101 }
102 // no access for standard user
103 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
104
105 return View(peClass);
106 }
107
108 // GET: Customer/Create
109 //public ActionResult Create()
110 //{
111 // return View();
112 //}
113
114 /*public ActionResult Create()
115 {
116
117 return View();
118 }*/
119
120 // POST: Customer/Create
121 // To protect from overposting attacks, enable the specific properties you want to bind to, for
122 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
123 [HttpPost]
124 [ValidateAntiForgeryToken]
125 public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid, pet_id")] Pet_CaresClass peClass)
126 {
127 bool isAuthenticated = User.Identity.IsAuthenticated;
128 if (!isAuthenticated)
129 {
130 return RedirectToAction("AccessDenied", "Error");
131 }
132 ViewBag.isAuthenticated = new UsersClass();
133 // no access for standard user
134 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
135 UsersClass customerClass = null;
136 if (ModelState.IsValid)
137 {
138 ViewBag.isAuthenticated = new UsersClass();
139 peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
140 peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
141 var user = await _userManager.GetUserAsync(User);
142 customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
143 peClass.usersid = customerClass.id;
144 db.PetCaresObj.Add(peClass);
145 db.SaveChanges();
146 return RedirectToAction("Index");
147 }
148 var vetCenters = await db.VetCentersObj.ToListAsync();
149 ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
150
151
152 var queryPetsByUser = from st in db.PetsObj
153 where st.usersid == customerClass.id
154 select st;
155 var userPets =await queryPetsByUser.ToListAsync<PetsClass>();
156 ViewBag.Pets= new SelectList(userPets, "id", "name");
157
158
159 return View(peClass);
160 }
161
162
163
164 // GET: Customer/Edit/5
165 /* public ActionResult Edit(int? id)
166 {
167 if (id == null)
168 {
169 return RedirectToAction("NotExist", "Error");
170 }
171 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
172 if (peClass == null)
173 {
174 return RedirectToAction("NotExist", "Error");
175 }
176
177
178 return View(peClass);
179 }*/
180 // GET: Customer/Edit/5
181 /* public ActionResult Edit(int? id)
182 {
183 if (id == null)
184 {
185 return RedirectToAction("NotExist", "Error");
186 }
187 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
188 if (peClass == null)
189 {
190 return RedirectToAction("NotExist", "Error");
191 }
192
193
194 return View(peClass);
195 }*/
196 public async Task<ActionResult> Edit(int? id)
197 {
198 if (id == null)
199 {
200 return RedirectToAction("NotExist", "Error");
201 }
202
203 Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
204 if (peClass == null)
205 {
206 return RedirectToAction("NotExist", "Error");
207 }
208
209 var vetCenters = await db.VetCentersObj.ToListAsync();
210 ViewBag.VetCenters = new SelectList(vetCenters, "id", "name", peClass.vetcentersid);
211 // dodadeno na 22.08
212 UsersClass customerClass = await getCrrentUser();
213 // check if the user is authenticated so we can take only his pets
214 if (customerClass != null)
215 {
216 var queryPetsByUser = from st in db.PetsObj
217 where st.usersid == customerClass.id
218 select st;
219 var userPets = await queryPetsByUser.ToListAsync<PetsClass>();
220 ViewBag.Pets = new SelectList(userPets, "id", "name");
221
222 }
223 ViewBag.isAuthenticated = customerClass;
224 return View(peClass);
225 }
226
227
228
229 // POST: Customer/Edit/5
230 // To protect from overposting attacks, enable the specific properties you want to bind to, for
231 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
232 [HttpPost]
233 [ValidateAntiForgeryToken]
234 public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, vetcentersid, pet_id")] Pet_CaresClass peClass)
235 {
236 bool isAuthenticated = User.Identity.IsAuthenticated;
237 if (!isAuthenticated)
238 {
239 return RedirectToAction("AccessDenied", "Error");
240 }
241 ViewBag.isAuthenticated = await getCrrentUser();
242 // no access for standard user
243 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
244
245 if (ModelState.IsValid)
246 {
247 peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
248 var user = await _userManager.GetUserAsync(User);
249 var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
250 peClass.usersid = customerClass.id;
251 db.Entry(peClass).State = EntityState.Modified;
252 db.SaveChanges();
253 return RedirectToAction("Index");
254 }
255 return View(peClass);
256 }
257
258
259
260
261 // GET: Customer/Delete/5
262 public async Task<ActionResult> Delete(int? id)
263 {
264 if (id == null)
265 {
266 return RedirectToAction("NotExist", "Error");
267 }
268 UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
269 ViewBag.isAuthenticated = customerClass;
270 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
271 if (peClass == null)
272 {
273 return RedirectToAction("NotExist", "Error");
274 }
275 // no access for standard user
276 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
277
278 return View(peClass);
279 }
280
281 // POST: Customer/Delete/5
282 [HttpPost, ActionName("Delete")]
283 [ValidateAntiForgeryToken]
284 public ActionResult DeleteConfirmed(int id)
285 {
286 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
287 db.PetCaresObj.Remove(peClass);
288 db.SaveChanges();
289 return RedirectToAction("Index");
290 }
291
292 protected override void Dispose(bool disposing)
293 {
294 if (disposing)
295 {
296 db.Dispose();
297 }
298 base.Dispose(disposing);
299 }
300 }
301}
Note: See TracBrowser for help on using the repository browser.