[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
|
---|
| 59 | UsersClass customerClass = await getCrrentUser();
|
---|
[6782104] | 60 | // set if is authenticated
|
---|
| 61 | ViewBag.isAuthenticated = customerClass;
|
---|
[2aea0fd] | 62 | if (customerClass == null)
|
---|
| 63 | {
|
---|
| 64 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 65 | }
|
---|
[118e414] | 66 | // no access for standard user
|
---|
| 67 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
| 68 |
|
---|
[2aea0fd] | 69 | if (customerClass.role_id == RoleConstants.Standard)
|
---|
| 70 | {
|
---|
| 71 | // query
|
---|
[118e414] | 72 | var query = from st in db.PetCaresObj
|
---|
[2aea0fd] | 73 | where st.usersid == customerClass.id
|
---|
| 74 | select st;
|
---|
| 75 |
|
---|
[e9bb9d1] | 76 | var userPetCares =
|
---|
[2aea0fd] | 77 | await query.ToListAsync<Pet_CaresClass>();
|
---|
| 78 |
|
---|
[e9bb9d1] | 79 | return View(userPetCares);
|
---|
[118e414] | 80 | }
|
---|
| 81 | else
|
---|
[2aea0fd] | 82 | {
|
---|
| 83 | return View(db.PetCaresObj.ToList());
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | // GET: Customer/Details/5
|
---|
[57fc402] | 89 | public async Task<ActionResult> Details(int? id)
|
---|
[2aea0fd] | 90 | {
|
---|
| 91 | if (id == null)
|
---|
| 92 | {
|
---|
| 93 | return RedirectToAction("NotExist", "Error");
|
---|
| 94 | }
|
---|
[118e414] | 95 | UsersClass customerClass = await getCrrentUser();
|
---|
[57fc402] | 96 | ViewBag.isAuthenticated = customerClass;
|
---|
[2aea0fd] | 97 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
| 98 | if (peClass == null)
|
---|
| 99 | {
|
---|
| 100 | return RedirectToAction("NotExist", "Error");
|
---|
| 101 | }
|
---|
[118e414] | 102 | // no access for standard user
|
---|
| 103 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
| 104 |
|
---|
[2aea0fd] | 105 | return View(peClass);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | // GET: Customer/Create
|
---|
| 109 | //public ActionResult Create()
|
---|
| 110 | //{
|
---|
| 111 | // return View();
|
---|
| 112 | //}
|
---|
| 113 |
|
---|
[ae6c071] | 114 | /*public ActionResult Create()
|
---|
[2aea0fd] | 115 | {
|
---|
| 116 |
|
---|
| 117 | return View();
|
---|
[ae6c071] | 118 | }*/
|
---|
[2aea0fd] | 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]
|
---|
[e9bb9d1] | 125 | public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid, pet_id")] Pet_CaresClass peClass)
|
---|
[118e414] | 126 | {
|
---|
| 127 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 128 | if (!isAuthenticated)
|
---|
| 129 | {
|
---|
| 130 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 131 | }
|
---|
[72b1da2] | 132 | ViewBag.isAuthenticated = new UsersClass();
|
---|
[118e414] | 133 | // no access for standard user
|
---|
| 134 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
[e9bb9d1] | 135 | UsersClass customerClass = null;
|
---|
[2aea0fd] | 136 | if (ModelState.IsValid)
|
---|
[118e414] | 137 | {
|
---|
[72b1da2] | 138 | ViewBag.isAuthenticated = new UsersClass();
|
---|
[2aea0fd] | 139 | peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
|
---|
[118e414] | 140 | peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
|
---|
| 141 | var user = await _userManager.GetUserAsync(User);
|
---|
[e9bb9d1] | 142 | customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
[118e414] | 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 |
|
---|
[72b1da2] | 151 |
|
---|
[e9bb9d1] | 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");
|
---|
[72b1da2] | 157 |
|
---|
| 158 |
|
---|
[e9bb9d1] | 159 | return View(peClass);
|
---|
| 160 | }
|
---|
[72b1da2] | 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
|
---|
[e9bb9d1] | 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;
|
---|
[2aea0fd] | 224 | return View(peClass);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[8f8226c] | 227 |
|
---|
| 228 |
|
---|
[2aea0fd] | 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]
|
---|
[e9bb9d1] | 234 | public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, vetcentersid, pet_id")] Pet_CaresClass peClass)
|
---|
[2aea0fd] | 235 | {
|
---|
| 236 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 237 | if (!isAuthenticated)
|
---|
| 238 | {
|
---|
| 239 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 240 | }
|
---|
[57fc402] | 241 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
[118e414] | 242 | // no access for standard user
|
---|
| 243 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
[8f8226c] | 244 |
|
---|
[2aea0fd] | 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 |
|
---|
[8f8226c] | 258 |
|
---|
| 259 |
|
---|
| 260 |
|
---|
[2aea0fd] | 261 | // GET: Customer/Delete/5
|
---|
[57fc402] | 262 | public async Task<ActionResult> Delete(int? id)
|
---|
[2aea0fd] | 263 | {
|
---|
| 264 | if (id == null)
|
---|
| 265 | {
|
---|
| 266 | return RedirectToAction("NotExist", "Error");
|
---|
| 267 | }
|
---|
[57fc402] | 268 | UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
|
---|
| 269 | ViewBag.isAuthenticated = customerClass;
|
---|
[2aea0fd] | 270 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
| 271 | if (peClass == null)
|
---|
| 272 | {
|
---|
| 273 | return RedirectToAction("NotExist", "Error");
|
---|
| 274 | }
|
---|
[118e414] | 275 | // no access for standard user
|
---|
| 276 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
| 277 |
|
---|
[2aea0fd] | 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 | }
|
---|