source: PostgreSqlDotnetCore/Controllers/PetCaresController.cs@ 72b1da2

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

Providing access

Providing access to various functionalities

  • Property mode set to 100644
File size: 9.1 KB
RevLine 
[2aea0fd]1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
[ae6c071]4using Microsoft.AspNetCore.Mvc.Rendering;
[2aea0fd]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
[ae6c071]17
18 [HttpGet]
[72b1da2]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();
[ae6c071]33
[72b1da2]34 ViewBag.isAuthenticated = customerClass;
[ae6c071]35 var vetCenters = await db.VetCentersObj.ToListAsync();
[72b1da2]36 ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
[ae6c071]37
[72b1da2]38 return View();
39 }
40
41
[ae6c071]42
43
[2aea0fd]44 // GET: Customer
45 public async Task<ActionResult> IndexAsync()
46 {
47 // check for permission
48 UsersClass customerClass = await getCrrentUser();
[6782104]49 // set if is authenticated
50 ViewBag.isAuthenticated = customerClass;
[2aea0fd]51 if (customerClass == null)
52 {
53 return RedirectToAction("AccessDenied", "Error");
54 }
55 if (customerClass.role_id == RoleConstants.Standard)
56 {
57 // query
58 var query = from st in db.PetCaresObj
59 where st.usersid == customerClass.id
60 select st;
61
62 var userPets =
63 //db.PetCaresObj.FromSql($"SELECT * FROM pets where usersid={customerClass.id}").ToListAsync();
64 await query.ToListAsync<Pet_CaresClass>();
65
66 return View(userPets);
67
68 PetCareAllData petCareAllData = new PetCareAllData();
69 petCareAllData.PetCares = userPets;
70
71
72 // query
73 var queryVetCenters = from kk in db.VetCentersObj
74 select kk;
75
76 // query
77 var queryUsers = from st in db.CustomerObj
78 select st;
79
80 var users = await queryUsers.ToListAsync<UsersClass>();
81 petCareAllData.Users = users;
82
83 //var vetCenters = await queryVetCenters.ToListAsync<VetCenter>();
84 //petCareAllData.VetCenters = vetCenters;
85
86 return View(petCareAllData);
87 } else
88 {
89 return View(db.PetCaresObj.ToList());
90 }
91
92 }
93
94 // GET: Customer/Details/5
[57fc402]95 public async Task<ActionResult> Details(int? id)
[2aea0fd]96 {
97 if (id == null)
98 {
99 return RedirectToAction("NotExist", "Error");
100 }
[57fc402]101 UsersClass customerClass = await getCrrentUser();
102 ViewBag.isAuthenticated = customerClass;
[2aea0fd]103 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
104 if (peClass == null)
105 {
106 return RedirectToAction("NotExist", "Error");
107 }
108 return View(peClass);
109 }
110
111 // GET: Customer/Create
112 //public ActionResult Create()
113 //{
114 // return View();
115 //}
116
[ae6c071]117 /*public ActionResult Create()
[2aea0fd]118 {
119
120 return View();
[ae6c071]121 }*/
[2aea0fd]122
123 // POST: Customer/Create
124 // To protect from overposting attacks, enable the specific properties you want to bind to, for
125 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
126 [HttpPost]
127 [ValidateAntiForgeryToken]
[72b1da2]128 public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid")] Pet_CaresClass peClass)
129 {
130 bool isAuthenticated = User.Identity.IsAuthenticated;
131 if (!isAuthenticated)
132 {
133 return RedirectToAction("AccessDenied", "Error");
134 }
135 ViewBag.isAuthenticated = new UsersClass();
136
[2aea0fd]137 if (ModelState.IsValid)
[72b1da2]138 {
139 ViewBag.isAuthenticated = new UsersClass();
[2aea0fd]140 peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
[72b1da2]141 peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
142 var user = await _userManager.GetUserAsync(User);
143 var 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 return View(peClass);
153 }
154
155
156
157
158
159
160
161
[2aea0fd]162
163
164 // GET: Customer/Edit/5
[8f8226c]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)
[2aea0fd]197 {
198 if (id == null)
199 {
200 return RedirectToAction("NotExist", "Error");
201 }
[8f8226c]202
203 Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
[2aea0fd]204 if (peClass == null)
205 {
206 return RedirectToAction("NotExist", "Error");
207 }
[8f8226c]208
209 var vetCenters = await db.VetCentersObj.ToListAsync();
210 ViewBag.VetCenters = new SelectList(vetCenters, "id", "name", peClass.vetcentersid);
[57fc402]211 // dodadeno na 22.08
212 ViewBag.isAuthenticated = await getCrrentUser();
[2aea0fd]213 return View(peClass);
214 }
215
[8f8226c]216
217
[2aea0fd]218 // POST: Customer/Edit/5
219 // To protect from overposting attacks, enable the specific properties you want to bind to, for
220 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
221 [HttpPost]
222 [ValidateAntiForgeryToken]
[8f8226c]223 public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, vetcentersid")] Pet_CaresClass peClass)
[2aea0fd]224 {
225 bool isAuthenticated = User.Identity.IsAuthenticated;
226 if (!isAuthenticated)
227 {
228 return RedirectToAction("AccessDenied", "Error");
229 }
[57fc402]230 ViewBag.isAuthenticated = await getCrrentUser();
[8f8226c]231
[2aea0fd]232 if (ModelState.IsValid)
233 {
234 peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
235 var user = await _userManager.GetUserAsync(User);
236 var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
237 peClass.usersid = customerClass.id;
238 db.Entry(peClass).State = EntityState.Modified;
239 db.SaveChanges();
240 return RedirectToAction("Index");
241 }
242 return View(peClass);
243 }
244
[8f8226c]245
246
247
[2aea0fd]248 // GET: Customer/Delete/5
[57fc402]249 public async Task<ActionResult> Delete(int? id)
[2aea0fd]250 {
251 if (id == null)
252 {
253 return RedirectToAction("NotExist", "Error");
254 }
[57fc402]255 UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
256 ViewBag.isAuthenticated = customerClass;
[2aea0fd]257 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
258 if (peClass == null)
259 {
260 return RedirectToAction("NotExist", "Error");
261 }
262 return View(peClass);
263 }
264
265 // POST: Customer/Delete/5
266 [HttpPost, ActionName("Delete")]
267 [ValidateAntiForgeryToken]
268 public ActionResult DeleteConfirmed(int id)
269 {
270 Pet_CaresClass peClass = db.PetCaresObj.Find(id);
271 db.PetCaresObj.Remove(peClass);
272 db.SaveChanges();
273 return RedirectToAction("Index");
274 }
275
276 protected override void Dispose(bool disposing)
277 {
278 if (disposing)
279 {
280 db.Dispose();
281 }
282 base.Dispose(disposing);
283 }
284 }
285}
Note: See TracBrowser for help on using the repository browser.