[2aea0fd] | 1 | using Microsoft.AspNetCore.Identity;
|
---|
| 2 | using Microsoft.AspNetCore.Mvc;
|
---|
| 3 | using Microsoft.EntityFrameworkCore;
|
---|
[ae6c071] | 4 | using Microsoft.AspNetCore.Mvc.Rendering;
|
---|
[2aea0fd] | 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 |
|
---|
[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 | }*/
|
---|
[118e414] | 28 | public async Task<ActionResult> Create()
|
---|
| 29 | {
|
---|
| 30 |
|
---|
| 31 |
|
---|
[72b1da2] | 32 | UsersClass customerClass = await getCrrentUser();
|
---|
[118e414] | 33 |
|
---|
[72b1da2] | 34 | ViewBag.isAuthenticated = customerClass;
|
---|
[ae6c071] | 35 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
[118e414] | 36 | ViewBag.VetCenters = new SelectList(vetCenters, "id", "name");
|
---|
| 37 |
|
---|
[e9bb9d1] | 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 | }
|
---|
[118e414] | 49 | return View();
|
---|
| 50 | }
|
---|
[ae6c071] | 51 |
|
---|
[72b1da2] | 52 |
|
---|
[ae6c071] | 53 |
|
---|
| 54 |
|
---|
[2aea0fd] | 55 | // GET: Customer
|
---|
| 56 | public async Task<ActionResult> IndexAsync()
|
---|
| 57 | {
|
---|
| 58 | // check for permission
|
---|
[e90ba32] | 59 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
[2aea0fd] | 60 | UsersClass customerClass = await getCrrentUser();
|
---|
[6782104] | 61 | // set if is authenticated
|
---|
| 62 | ViewBag.isAuthenticated = customerClass;
|
---|
[2aea0fd] | 63 | if (customerClass == null)
|
---|
| 64 | {
|
---|
| 65 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 66 | }
|
---|
[118e414] | 67 | // no access for standard user
|
---|
| 68 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
| 69 |
|
---|
[2aea0fd] | 70 | if (customerClass.role_id == RoleConstants.Standard)
|
---|
| 71 | {
|
---|
| 72 | // query
|
---|
[118e414] | 73 | var query = from st in db.PetCaresObj
|
---|
[2aea0fd] | 74 | where st.usersid == customerClass.id
|
---|
| 75 | select st;
|
---|
| 76 |
|
---|
[e9bb9d1] | 77 | var userPetCares =
|
---|
[e90ba32] | 78 | await query.Include(n => n.PetsClass).ToListAsync<Pet_CaresClass>();
|
---|
[2aea0fd] | 79 |
|
---|
[e9bb9d1] | 80 | return View(userPetCares);
|
---|
[118e414] | 81 | }
|
---|
| 82 | else
|
---|
[2aea0fd] | 83 | {
|
---|
[e90ba32] | 84 | return View(db.PetCaresObj.Include(n => n.PetsClass).ToList());
|
---|
[2aea0fd] | 85 | }
|
---|
| 86 |
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | // GET: Customer/Details/5
|
---|
[57fc402] | 90 | public async Task<ActionResult> Details(int? id)
|
---|
[2aea0fd] | 91 | {
|
---|
| 92 | if (id == null)
|
---|
| 93 | {
|
---|
| 94 | return RedirectToAction("NotExist", "Error");
|
---|
| 95 | }
|
---|
[118e414] | 96 | UsersClass customerClass = await getCrrentUser();
|
---|
[57fc402] | 97 | ViewBag.isAuthenticated = customerClass;
|
---|
[2aea0fd] | 98 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
| 99 | if (peClass == null)
|
---|
| 100 | {
|
---|
| 101 | return RedirectToAction("NotExist", "Error");
|
---|
| 102 | }
|
---|
[118e414] | 103 | // no access for standard user
|
---|
| 104 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
| 105 |
|
---|
[2aea0fd] | 106 | return View(peClass);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | // GET: Customer/Create
|
---|
| 110 | //public ActionResult Create()
|
---|
| 111 | //{
|
---|
| 112 | // return View();
|
---|
| 113 | //}
|
---|
| 114 |
|
---|
[ae6c071] | 115 | /*public ActionResult Create()
|
---|
[2aea0fd] | 116 | {
|
---|
| 117 |
|
---|
| 118 | return View();
|
---|
[ae6c071] | 119 | }*/
|
---|
[2aea0fd] | 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]
|
---|
[e9bb9d1] | 126 | public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid, pet_id")] Pet_CaresClass peClass)
|
---|
[118e414] | 127 | {
|
---|
| 128 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 129 | if (!isAuthenticated)
|
---|
| 130 | {
|
---|
| 131 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 132 | }
|
---|
[72b1da2] | 133 | ViewBag.isAuthenticated = new UsersClass();
|
---|
[118e414] | 134 | // no access for standard user
|
---|
| 135 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
[e9bb9d1] | 136 | UsersClass customerClass = null;
|
---|
[2aea0fd] | 137 | if (ModelState.IsValid)
|
---|
[118e414] | 138 | {
|
---|
[72b1da2] | 139 | ViewBag.isAuthenticated = new UsersClass();
|
---|
[2aea0fd] | 140 | peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
|
---|
[118e414] | 141 | peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
|
---|
| 142 | var user = await _userManager.GetUserAsync(User);
|
---|
[e9bb9d1] | 143 | customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
[118e414] | 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 |
|
---|
[72b1da2] | 152 |
|
---|
[e9bb9d1] | 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");
|
---|
[72b1da2] | 158 |
|
---|
| 159 |
|
---|
[e9bb9d1] | 160 | return View(peClass);
|
---|
| 161 | }
|
---|
[72b1da2] | 162 |
|
---|
[2aea0fd] | 163 |
|
---|
| 164 |
|
---|
| 165 | // GET: Customer/Edit/5
|
---|
[8f8226c] | 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)
|
---|
[2aea0fd] | 198 | {
|
---|
| 199 | if (id == null)
|
---|
| 200 | {
|
---|
| 201 | return RedirectToAction("NotExist", "Error");
|
---|
| 202 | }
|
---|
[8f8226c] | 203 |
|
---|
| 204 | Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
|
---|
[2aea0fd] | 205 | if (peClass == null)
|
---|
| 206 | {
|
---|
| 207 | return RedirectToAction("NotExist", "Error");
|
---|
| 208 | }
|
---|
[8f8226c] | 209 |
|
---|
| 210 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
| 211 | ViewBag.VetCenters = new SelectList(vetCenters, "id", "name", peClass.vetcentersid);
|
---|
[57fc402] | 212 | // dodadeno na 22.08
|
---|
[e9bb9d1] | 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;
|
---|
[2aea0fd] | 225 | return View(peClass);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
[8f8226c] | 228 |
|
---|
| 229 |
|
---|
[2aea0fd] | 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]
|
---|
[e9bb9d1] | 235 | public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, vetcentersid, pet_id")] Pet_CaresClass peClass)
|
---|
[2aea0fd] | 236 | {
|
---|
| 237 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 238 | if (!isAuthenticated)
|
---|
| 239 | {
|
---|
| 240 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 241 | }
|
---|
[57fc402] | 242 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
[118e414] | 243 | // no access for standard user
|
---|
| 244 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
[8f8226c] | 245 |
|
---|
[2aea0fd] | 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 |
|
---|
[8f8226c] | 259 |
|
---|
| 260 |
|
---|
| 261 |
|
---|
[2aea0fd] | 262 | // GET: Customer/Delete/5
|
---|
[57fc402] | 263 | public async Task<ActionResult> Delete(int? id)
|
---|
[2aea0fd] | 264 | {
|
---|
| 265 | if (id == null)
|
---|
| 266 | {
|
---|
| 267 | return RedirectToAction("NotExist", "Error");
|
---|
| 268 | }
|
---|
[57fc402] | 269 | UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
|
---|
| 270 | ViewBag.isAuthenticated = customerClass;
|
---|
[2aea0fd] | 271 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
| 272 | if (peClass == null)
|
---|
| 273 | {
|
---|
| 274 | return RedirectToAction("NotExist", "Error");
|
---|
| 275 | }
|
---|
[118e414] | 276 | // no access for standard user
|
---|
| 277 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
| 278 |
|
---|
[2aea0fd] | 279 | return View(peClass);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | // POST: Customer/Delete/5
|
---|
[e90ba32] | 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 |
|
---|
[2aea0fd] | 294 | [HttpPost, ActionName("Delete")]
|
---|
| 295 | [ValidateAntiForgeryToken]
|
---|
[e90ba32] | 296 | public async Task<ActionResult> DeleteConfirmed(int id)
|
---|
[2aea0fd] | 297 | {
|
---|
[e90ba32] | 298 | Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
|
---|
| 299 | if (peClass == null)
|
---|
| 300 | {
|
---|
| 301 | return RedirectToAction("NotExist", "Error");
|
---|
| 302 | }
|
---|
[2aea0fd] | 303 | db.PetCaresObj.Remove(peClass);
|
---|
[e90ba32] | 304 | await db.SaveChangesAsync();
|
---|
[2aea0fd] | 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 | }
|
---|