[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]
|
---|
| 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 |
|
---|
[2aea0fd] | 29 | // GET: Customer
|
---|
| 30 | public async Task<ActionResult> IndexAsync()
|
---|
| 31 | {
|
---|
| 32 | // check for permission
|
---|
| 33 | UsersClass customerClass = await getCrrentUser();
|
---|
[6782104] | 34 | // set if is authenticated
|
---|
| 35 | ViewBag.isAuthenticated = customerClass;
|
---|
[2aea0fd] | 36 | if (customerClass == null)
|
---|
| 37 | {
|
---|
| 38 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 39 | }
|
---|
| 40 | if (customerClass.role_id == RoleConstants.Standard)
|
---|
| 41 | {
|
---|
| 42 | // query
|
---|
| 43 | var query = from st in db.PetCaresObj
|
---|
| 44 | where st.usersid == customerClass.id
|
---|
| 45 | select st;
|
---|
| 46 |
|
---|
| 47 | var userPets =
|
---|
| 48 | //db.PetCaresObj.FromSql($"SELECT * FROM pets where usersid={customerClass.id}").ToListAsync();
|
---|
| 49 | await query.ToListAsync<Pet_CaresClass>();
|
---|
| 50 |
|
---|
| 51 | return View(userPets);
|
---|
| 52 |
|
---|
| 53 | PetCareAllData petCareAllData = new PetCareAllData();
|
---|
| 54 | petCareAllData.PetCares = userPets;
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | // query
|
---|
| 58 | var queryVetCenters = from kk in db.VetCentersObj
|
---|
| 59 | select kk;
|
---|
| 60 |
|
---|
| 61 | // query
|
---|
| 62 | var queryUsers = from st in db.CustomerObj
|
---|
| 63 | select st;
|
---|
| 64 |
|
---|
| 65 | var users = await queryUsers.ToListAsync<UsersClass>();
|
---|
| 66 | petCareAllData.Users = users;
|
---|
| 67 |
|
---|
| 68 | //var vetCenters = await queryVetCenters.ToListAsync<VetCenter>();
|
---|
| 69 | //petCareAllData.VetCenters = vetCenters;
|
---|
| 70 |
|
---|
| 71 | return View(petCareAllData);
|
---|
| 72 | } else
|
---|
| 73 | {
|
---|
| 74 | return View(db.PetCaresObj.ToList());
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | // GET: Customer/Details/5
|
---|
[57fc402] | 80 | public async Task<ActionResult> Details(int? id)
|
---|
[2aea0fd] | 81 | {
|
---|
| 82 | if (id == null)
|
---|
| 83 | {
|
---|
| 84 | return RedirectToAction("NotExist", "Error");
|
---|
| 85 | }
|
---|
[57fc402] | 86 | UsersClass customerClass = await getCrrentUser();
|
---|
| 87 | ViewBag.isAuthenticated = customerClass;
|
---|
[2aea0fd] | 88 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
| 89 | if (peClass == null)
|
---|
| 90 | {
|
---|
| 91 | return RedirectToAction("NotExist", "Error");
|
---|
| 92 | }
|
---|
| 93 | return View(peClass);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | // GET: Customer/Create
|
---|
| 97 | //public ActionResult Create()
|
---|
| 98 | //{
|
---|
| 99 | // return View();
|
---|
| 100 | //}
|
---|
| 101 |
|
---|
[ae6c071] | 102 | /*public ActionResult Create()
|
---|
[2aea0fd] | 103 | {
|
---|
| 104 |
|
---|
| 105 | return View();
|
---|
[ae6c071] | 106 | }*/
|
---|
[2aea0fd] | 107 |
|
---|
| 108 | // POST: Customer/Create
|
---|
| 109 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
| 110 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
| 111 | [HttpPost]
|
---|
| 112 | [ValidateAntiForgeryToken]
|
---|
[ae6c071] | 113 | public async Task<ActionResult> CreateAsync([Bind(include: "id,title,description,dateending, start_date, usersid, vetcentersid")] Pet_CaresClass peClass)
|
---|
[2aea0fd] | 114 | {
|
---|
| 115 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 116 | if (!isAuthenticated)
|
---|
| 117 | {
|
---|
| 118 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 119 | }
|
---|
| 120 | if (ModelState.IsValid)
|
---|
| 121 | {
|
---|
| 122 | peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
|
---|
[ae6c071] | 123 | peClass.start_date = DateTime.SpecifyKind(peClass.start_date, DateTimeKind.Utc);
|
---|
[2aea0fd] | 124 | var user = await _userManager.GetUserAsync(User);
|
---|
| 125 | var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
| 126 | peClass.usersid = customerClass.id;
|
---|
| 127 | db.PetCaresObj.Add(peClass);
|
---|
| 128 | db.SaveChanges();
|
---|
| 129 | return RedirectToAction("Index");
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | return View(peClass);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | // GET: Customer/Edit/5
|
---|
[8f8226c] | 136 | /* public ActionResult Edit(int? id)
|
---|
| 137 | {
|
---|
| 138 | if (id == null)
|
---|
| 139 | {
|
---|
| 140 | return RedirectToAction("NotExist", "Error");
|
---|
| 141 | }
|
---|
| 142 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
| 143 | if (peClass == null)
|
---|
| 144 | {
|
---|
| 145 | return RedirectToAction("NotExist", "Error");
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 | return View(peClass);
|
---|
| 150 | }*/
|
---|
| 151 | // GET: Customer/Edit/5
|
---|
| 152 | /* public ActionResult Edit(int? id)
|
---|
| 153 | {
|
---|
| 154 | if (id == null)
|
---|
| 155 | {
|
---|
| 156 | return RedirectToAction("NotExist", "Error");
|
---|
| 157 | }
|
---|
| 158 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
| 159 | if (peClass == null)
|
---|
| 160 | {
|
---|
| 161 | return RedirectToAction("NotExist", "Error");
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | return View(peClass);
|
---|
| 166 | }*/
|
---|
| 167 | public async Task<ActionResult> Edit(int? id)
|
---|
[2aea0fd] | 168 | {
|
---|
| 169 | if (id == null)
|
---|
| 170 | {
|
---|
| 171 | return RedirectToAction("NotExist", "Error");
|
---|
| 172 | }
|
---|
[8f8226c] | 173 |
|
---|
| 174 | Pet_CaresClass peClass = await db.PetCaresObj.FindAsync(id);
|
---|
[2aea0fd] | 175 | if (peClass == null)
|
---|
| 176 | {
|
---|
| 177 | return RedirectToAction("NotExist", "Error");
|
---|
| 178 | }
|
---|
[8f8226c] | 179 |
|
---|
| 180 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
| 181 | ViewBag.VetCenters = new SelectList(vetCenters, "id", "name", peClass.vetcentersid);
|
---|
[57fc402] | 182 | // dodadeno na 22.08
|
---|
| 183 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
[2aea0fd] | 184 | return View(peClass);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
[8f8226c] | 187 |
|
---|
| 188 |
|
---|
[2aea0fd] | 189 | // POST: Customer/Edit/5
|
---|
| 190 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
| 191 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
| 192 | [HttpPost]
|
---|
| 193 | [ValidateAntiForgeryToken]
|
---|
[8f8226c] | 194 | public async Task<ActionResult> EditAsync([Bind(include: "id,title,description,dateending, vetcentersid")] Pet_CaresClass peClass)
|
---|
[2aea0fd] | 195 | {
|
---|
| 196 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 197 | if (!isAuthenticated)
|
---|
| 198 | {
|
---|
| 199 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 200 | }
|
---|
[57fc402] | 201 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
[8f8226c] | 202 |
|
---|
[2aea0fd] | 203 | if (ModelState.IsValid)
|
---|
| 204 | {
|
---|
| 205 | peClass.dateending = DateTime.SpecifyKind(peClass.dateending, DateTimeKind.Utc);
|
---|
| 206 | var user = await _userManager.GetUserAsync(User);
|
---|
| 207 | var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
| 208 | peClass.usersid = customerClass.id;
|
---|
| 209 | db.Entry(peClass).State = EntityState.Modified;
|
---|
| 210 | db.SaveChanges();
|
---|
| 211 | return RedirectToAction("Index");
|
---|
| 212 | }
|
---|
| 213 | return View(peClass);
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[8f8226c] | 216 |
|
---|
| 217 |
|
---|
| 218 |
|
---|
[2aea0fd] | 219 | // GET: Customer/Delete/5
|
---|
[57fc402] | 220 | public async Task<ActionResult> Delete(int? id)
|
---|
[2aea0fd] | 221 | {
|
---|
| 222 | if (id == null)
|
---|
| 223 | {
|
---|
| 224 | return RedirectToAction("NotExist", "Error");
|
---|
| 225 | }
|
---|
[57fc402] | 226 | UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
|
---|
| 227 | ViewBag.isAuthenticated = customerClass;
|
---|
[2aea0fd] | 228 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
| 229 | if (peClass == null)
|
---|
| 230 | {
|
---|
| 231 | return RedirectToAction("NotExist", "Error");
|
---|
| 232 | }
|
---|
| 233 | return View(peClass);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | // POST: Customer/Delete/5
|
---|
| 237 | [HttpPost, ActionName("Delete")]
|
---|
| 238 | [ValidateAntiForgeryToken]
|
---|
| 239 | public ActionResult DeleteConfirmed(int id)
|
---|
| 240 | {
|
---|
| 241 | Pet_CaresClass peClass = db.PetCaresObj.Find(id);
|
---|
| 242 | db.PetCaresObj.Remove(peClass);
|
---|
| 243 | db.SaveChanges();
|
---|
| 244 | return RedirectToAction("Index");
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | protected override void Dispose(bool disposing)
|
---|
| 248 | {
|
---|
| 249 | if (disposing)
|
---|
| 250 | {
|
---|
| 251 | db.Dispose();
|
---|
| 252 | }
|
---|
| 253 | base.Dispose(disposing);
|
---|
| 254 | }
|
---|
| 255 | }
|
---|
| 256 | }
|
---|