[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 | }
|
---|
[99d0ecc] | 133 | ModelState.Remove("PetsClass");
|
---|
[72b1da2] | 134 | ViewBag.isAuthenticated = new UsersClass();
|
---|
[118e414] | 135 | // no access for standard user
|
---|
| 136 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
[e9bb9d1] | 137 | UsersClass customerClass = null;
|
---|
[2aea0fd] | 138 | if (ModelState.IsValid)
|
---|
[118e414] | 139 | {
|
---|
[72b1da2] | 140 | ViewBag.isAuthenticated = new UsersClass();
|
---|
[2aea0fd] | 141 | peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
|
---|
[118e414] | 142 | peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
|
---|
| 143 | var user = await _userManager.GetUserAsync(User);
|
---|
[e9bb9d1] | 144 | customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
[118e414] | 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 |
|
---|
[99d0ecc] | 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");
|
---|
[72b1da2] | 160 |
|
---|
[99d0ecc] | 161 | }
|
---|
[72b1da2] | 162 |
|
---|
[e9bb9d1] | 163 | return View(peClass);
|
---|
| 164 | }
|
---|
[72b1da2] | 165 |
|
---|
[2aea0fd] | 166 |
|
---|
| 167 |
|
---|
| 168 | // GET: Customer/Edit/5
|
---|
[8f8226c] | 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)
|
---|
[2aea0fd] | 201 | {
|
---|
| 202 | if (id == null)
|
---|
| 203 | {
|
---|
| 204 | return RedirectToAction("NotExist", "Error");
|
---|
| 205 | }
|
---|
[8f8226c] | 206 |
|
---|
| 207 | Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
|
---|
[2aea0fd] | 208 | if (peClass == null)
|
---|
| 209 | {
|
---|
| 210 | return RedirectToAction("NotExist", "Error");
|
---|
| 211 | }
|
---|
[8f8226c] | 212 |
|
---|
| 213 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
| 214 | ViewBag.VetCenters = new SelectList(vetCenters, "id", "name", peClass.vetcentersid);
|
---|
[57fc402] | 215 | // dodadeno na 22.08
|
---|
[e9bb9d1] | 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;
|
---|
[2aea0fd] | 228 | return View(peClass);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[8f8226c] | 231 |
|
---|
| 232 |
|
---|
[2aea0fd] | 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]
|
---|
[e9bb9d1] | 238 | public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, vetcentersid, pet_id")] Pet_CaresClass peClass)
|
---|
[2aea0fd] | 239 | {
|
---|
| 240 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 241 | if (!isAuthenticated)
|
---|
| 242 | {
|
---|
| 243 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 244 | }
|
---|
[99d0ecc] | 245 |
|
---|
| 246 | ModelState.Remove("PetsClass");
|
---|
[57fc402] | 247 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
[118e414] | 248 | // no access for standard user
|
---|
| 249 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
[8f8226c] | 250 |
|
---|
[2aea0fd] | 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 |
|
---|
[8f8226c] | 264 |
|
---|
| 265 |
|
---|
| 266 |
|
---|
[2aea0fd] | 267 | // GET: Customer/Delete/5
|
---|
[57fc402] | 268 | public async Task<ActionResult> Delete(int? id)
|
---|
[2aea0fd] | 269 | {
|
---|
| 270 | if (id == null)
|
---|
| 271 | {
|
---|
| 272 | return RedirectToAction("NotExist", "Error");
|
---|
| 273 | }
|
---|
[57fc402] | 274 | UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
|
---|
| 275 | ViewBag.isAuthenticated = customerClass;
|
---|
[2aea0fd] | 276 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
| 277 | if (peClass == null)
|
---|
| 278 | {
|
---|
| 279 | return RedirectToAction("NotExist", "Error");
|
---|
| 280 | }
|
---|
[118e414] | 281 | // no access for standard user
|
---|
| 282 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
| 283 |
|
---|
[2aea0fd] | 284 | return View(peClass);
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | // POST: Customer/Delete/5
|
---|
[e90ba32] | 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 |
|
---|
[2aea0fd] | 299 | [HttpPost, ActionName("Delete")]
|
---|
| 300 | [ValidateAntiForgeryToken]
|
---|
[e90ba32] | 301 | public async Task<ActionResult> DeleteConfirmed(int id)
|
---|
[2aea0fd] | 302 | {
|
---|
[e90ba32] | 303 | Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
|
---|
| 304 | if (peClass == null)
|
---|
| 305 | {
|
---|
| 306 | return RedirectToAction("NotExist", "Error");
|
---|
| 307 | }
|
---|
[2aea0fd] | 308 | db.PetCaresObj.Remove(peClass);
|
---|
[e90ba32] | 309 | await db.SaveChangesAsync();
|
---|
[2aea0fd] | 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 | }
|
---|