[2aea0fd] | 1 | using Microsoft.AspNetCore.Identity;
|
---|
| 2 | using Microsoft.AspNetCore.Mvc;
|
---|
| 3 | using Microsoft.EntityFrameworkCore;
|
---|
| 4 | using PostgreSqlDotnetCore.Models;
|
---|
[d6040ef] | 5 | using Microsoft.AspNetCore.Mvc.Rendering;
|
---|
[57fc402] | 6 | using System.Threading.Tasks;
|
---|
[e9bb9d1] | 7 | using Npgsql;
|
---|
| 8 | using PostgreSqlDotnetCore.Data;
|
---|
[2aea0fd] | 9 |
|
---|
| 10 | namespace PostgreSqlDotnetCore.Controllers
|
---|
| 11 | {
|
---|
| 12 | public class VetCenterController : BaseController
|
---|
| 13 | {
|
---|
[e9bb9d1] | 14 | /*
|
---|
| 15 | public VetCenterController(UserManager<IdentityUser> userManager) : base(userManager)
|
---|
| 16 | {
|
---|
| 17 | }
|
---|
| 18 | */
|
---|
| 19 | private readonly ApplicationDbContext db;
|
---|
| 20 |
|
---|
| 21 | public VetCenterController(UserManager<IdentityUser> userManager, ApplicationDbContext context) : base(userManager)
|
---|
[2aea0fd] | 22 | {
|
---|
[e9bb9d1] | 23 | db = context ?? throw new ArgumentNullException(nameof(context));
|
---|
[2aea0fd] | 24 | }
|
---|
| 25 |
|
---|
[57fc402] | 26 | public async Task<ActionResult> Create()
|
---|
| 27 | {
|
---|
| 28 | // Set if user is authenticated
|
---|
| 29 | UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
| 30 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
| 31 | if (customerClass == null)
|
---|
| 32 | {
|
---|
| 33 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 34 | }
|
---|
[118e414] | 35 | // no access for standard user
|
---|
| 36 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
[d6040ef] | 37 |
|
---|
[57fc402] | 38 | // Fetch cities for dropdown
|
---|
| 39 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
| 40 | ViewBag.Citiess = new SelectList(citiess, "id", "name");
|
---|
[d6040ef] | 41 |
|
---|
[57fc402] | 42 | return View();
|
---|
| 43 | }
|
---|
[d6040ef] | 44 |
|
---|
[72b1da2] | 45 | /* public async Task<ActionResult> Index()
|
---|
| 46 | {
|
---|
| 47 |
|
---|
| 48 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
| 49 | ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 50 |
|
---|
| 51 | // Check if the user is an admin
|
---|
| 52 | UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
| 53 | ViewBag.hasAccess = customerClass != null;
|
---|
| 54 |
|
---|
| 55 | return View(vetCenters);
|
---|
| 56 | }*/
|
---|
[e9bb9d1] | 57 | /* public async Task<ActionResult> Index()
|
---|
| 58 | {
|
---|
| 59 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
| 60 | ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 61 | // no access for standard user
|
---|
| 62 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
| 63 |
|
---|
| 64 | // Проверете дали корисникот е администратор или менаџер
|
---|
| 65 | UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
| 66 | // ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
| 67 |
|
---|
| 68 | ViewBag.hasAccess = customerClass != null;
|
---|
| 69 |
|
---|
| 70 | return View(vetCenters);
|
---|
| 71 | }
|
---|
| 72 | */
|
---|
[57fc402] | 73 | public async Task<ActionResult> Index()
|
---|
[2aea0fd] | 74 | {
|
---|
[99d0ecc] | 75 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
| 76 | ViewBag.hasAccess = await checkAuthorizationAsync();
|
---|
[e9bb9d1] | 77 | var vetCenters = await db.VetCentersWithCity.ToListAsync();
|
---|
[99d0ecc] | 78 | // ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
|
---|
[118e414] | 79 | // no access for standard user
|
---|
| 80 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
[d6040ef] | 81 |
|
---|
[e9bb9d1] | 82 |
|
---|
[57fc402] | 83 | UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
[72b1da2] | 84 |
|
---|
[57fc402] | 85 | ViewBag.hasAccess = customerClass != null;
|
---|
| 86 |
|
---|
[d6040ef] | 87 | return View(vetCenters);
|
---|
[2aea0fd] | 88 | }
|
---|
| 89 |
|
---|
[72b1da2] | 90 |
|
---|
[e9bb9d1] | 91 |
|
---|
| 92 | /*public async Task<ActionResult> Details(int? id)
|
---|
[2aea0fd] | 93 | {
|
---|
| 94 | if (id == null)
|
---|
| 95 | {
|
---|
| 96 | return RedirectToAction("NotExist", "Error");
|
---|
| 97 | }
|
---|
[57fc402] | 98 |
|
---|
| 99 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
[118e414] | 100 | UsersClass customerClass = await getCrrentUser();
|
---|
| 101 | ViewBag.isAuthenticated = customerClass;
|
---|
[2aea0fd] | 102 | if (vetClass == null)
|
---|
| 103 | {
|
---|
| 104 | return RedirectToAction("NotExist", "Error");
|
---|
| 105 | }
|
---|
[118e414] | 106 | // no access for standard user
|
---|
| 107 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
[57fc402] | 108 |
|
---|
[2aea0fd] | 109 | return View(vetClass);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[e9bb9d1] | 112 |
|
---|
| 113 | */
|
---|
| 114 | public async Task<IActionResult> Details(int? id)
|
---|
| 115 | {
|
---|
| 116 | if (id == null)
|
---|
| 117 | {
|
---|
| 118 | return RedirectToAction("NotExist", "Error");
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | // Логирајте го ID-то за дебугирање
|
---|
| 122 | Console.WriteLine($"ID: {id}");
|
---|
| 123 |
|
---|
| 124 | // Обидете се да најдете запис во view
|
---|
| 125 | VetCenterWithCity vetClass = await db.VetCentersWithCity
|
---|
| 126 | .Where(v => v.id == id)
|
---|
| 127 | .FirstOrDefaultAsync();
|
---|
| 128 | if (vetClass == null)
|
---|
| 129 | {
|
---|
| 130 | return RedirectToAction("NotExist", "Error");
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 |
|
---|
| 134 | UsersClass customerClass = await getCrrentUser();
|
---|
| 135 | ViewBag.isAuthenticated = customerClass;
|
---|
| 136 |
|
---|
| 137 |
|
---|
| 138 | return View(vetClass);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 |
|
---|
| 142 |
|
---|
| 143 |
|
---|
| 144 |
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 |
|
---|
[2aea0fd] | 152 | [HttpPost]
|
---|
| 153 | [ValidateAntiForgeryToken]
|
---|
[57fc402] | 154 | public async Task<ActionResult> Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
|
---|
[2aea0fd] | 155 | {
|
---|
| 156 | if (ModelState.IsValid)
|
---|
| 157 | {
|
---|
| 158 | db.VetCentersObj.Add(vetClass);
|
---|
[57fc402] | 159 | await db.SaveChangesAsync();
|
---|
[2aea0fd] | 160 | return RedirectToAction("Index");
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[57fc402] | 163 | // If model is invalid, repopulate the cities for dropdown
|
---|
| 164 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
| 165 | ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
|
---|
| 166 |
|
---|
[2aea0fd] | 167 | return View(vetClass);
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[e9bb9d1] | 170 |
|
---|
| 171 |
|
---|
| 172 | /*public async Task<ActionResult> Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
|
---|
| 173 | {
|
---|
| 174 | // Логирање на параметрите
|
---|
| 175 | Console.WriteLine($"Parameters: {vetClass.name}, {vetClass.adress}, {vetClass.description}, {vetClass.workinghours}, {vetClass.phonenumber}, {vetClass.latitude}, {vetClass.longitude}, {vetClass.citiesid}");
|
---|
| 176 |
|
---|
| 177 | if (ModelState.IsValid)
|
---|
| 178 | {
|
---|
| 179 | // Повик на складираната процедура
|
---|
| 180 | var parameters = new[]
|
---|
| 181 | {
|
---|
| 182 | new NpgsqlParameter("@name", vetClass.name),
|
---|
| 183 | new NpgsqlParameter("@adress", vetClass.adress),
|
---|
| 184 | new NpgsqlParameter("@description", vetClass.description),
|
---|
| 185 | new NpgsqlParameter("@workinghours", vetClass.workinghours),
|
---|
| 186 | new NpgsqlParameter("@phonenumber", vetClass.phonenumber),
|
---|
| 187 | new NpgsqlParameter("@latitude", (decimal)vetClass.latitude),
|
---|
| 188 | new NpgsqlParameter("@longitude", (decimal)vetClass.longitude),
|
---|
| 189 | new NpgsqlParameter("@citiesid", vetClass.citiesid)
|
---|
| 190 | };
|
---|
| 191 |
|
---|
| 192 | await db.Database.ExecuteSqlRawAsync("CALL project.AddVetCenter(@name, @adress, @description, @workinghours, @phonenumber, @latitude, @longitude, @citiesid)", parameters);
|
---|
| 193 |
|
---|
| 194 | return RedirectToAction("Index");
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | // Ако моделот не е валиден, повторно пополнете ги градовите за паѓачкиот мени
|
---|
| 198 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
| 199 | ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
|
---|
| 200 |
|
---|
| 201 | return View(vetClass);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 |
|
---|
| 205 |
|
---|
| 206 |
|
---|
| 207 | */
|
---|
| 208 |
|
---|
| 209 |
|
---|
| 210 |
|
---|
| 211 |
|
---|
[57fc402] | 212 | public async Task<ActionResult> Edit(int? id)
|
---|
[2aea0fd] | 213 | {
|
---|
| 214 | if (id == null)
|
---|
| 215 | {
|
---|
| 216 | return RedirectToAction("NotExist", "Error");
|
---|
| 217 | }
|
---|
[57fc402] | 218 |
|
---|
| 219 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
[2aea0fd] | 220 | if (vetClass == null)
|
---|
| 221 | {
|
---|
| 222 | return RedirectToAction("NotExist", "Error");
|
---|
| 223 | }
|
---|
[57fc402] | 224 |
|
---|
| 225 | // Check for permission
|
---|
[2aea0fd] | 226 | UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
[57fc402] | 227 | //UsersClass customerClass = await checkAuthorizationAsync();
|
---|
| 228 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
[2aea0fd] | 229 | if (customerClass == null)
|
---|
| 230 | {
|
---|
| 231 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 232 | }
|
---|
[118e414] | 233 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
| 234 |
|
---|
[57fc402] | 235 |
|
---|
| 236 | // Fetch cities for dropdown
|
---|
[d6040ef] | 237 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
| 238 | ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
|
---|
[57fc402] | 239 |
|
---|
[2aea0fd] | 240 | return View(vetClass);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | [HttpPost]
|
---|
| 244 | [ValidateAntiForgeryToken]
|
---|
[d6040ef] | 245 | public async Task<ActionResult> Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
|
---|
[2aea0fd] | 246 | {
|
---|
| 247 | if (ModelState.IsValid)
|
---|
| 248 | {
|
---|
| 249 | db.Entry(vetClass).State = EntityState.Modified;
|
---|
[d6040ef] | 250 | await db.SaveChangesAsync();
|
---|
[2aea0fd] | 251 | return RedirectToAction("Index");
|
---|
| 252 | }
|
---|
[d6040ef] | 253 |
|
---|
[57fc402] | 254 | // If model is invalid, repopulate the cities for dropdown
|
---|
[d6040ef] | 255 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
| 256 | ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
|
---|
| 257 |
|
---|
[2aea0fd] | 258 | return View(vetClass);
|
---|
| 259 | }
|
---|
| 260 |
|
---|
[e9bb9d1] | 261 | public async Task<ActionResult> Delete(int? id)
|
---|
| 262 | {
|
---|
| 263 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
| 264 |
|
---|
| 265 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
| 266 |
|
---|
[2aea0fd] | 267 | if (id == null)
|
---|
| 268 | {
|
---|
| 269 | return RedirectToAction("NotExist", "Error");
|
---|
| 270 | }
|
---|
[57fc402] | 271 |
|
---|
| 272 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
[2aea0fd] | 273 | if (vetClass == null)
|
---|
| 274 | {
|
---|
| 275 | return RedirectToAction("NotExist", "Error");
|
---|
| 276 | }
|
---|
[118e414] | 277 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
| 278 |
|
---|
[57fc402] | 279 |
|
---|
[2aea0fd] | 280 | return View(vetClass);
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | [HttpPost, ActionName("Delete")]
|
---|
| 284 | [ValidateAntiForgeryToken]
|
---|
[57fc402] | 285 | public async Task<ActionResult> DeleteConfirmed(int id)
|
---|
[2aea0fd] | 286 | {
|
---|
[57fc402] | 287 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
[2aea0fd] | 288 | db.VetCentersObj.Remove(vetClass);
|
---|
[57fc402] | 289 | await db.SaveChangesAsync();
|
---|
[2aea0fd] | 290 | return RedirectToAction("Index");
|
---|
| 291 | }
|
---|
| 292 |
|
---|
| 293 | protected override void Dispose(bool disposing)
|
---|
| 294 | {
|
---|
| 295 | if (disposing)
|
---|
| 296 | {
|
---|
| 297 | db.Dispose();
|
---|
| 298 | }
|
---|
| 299 | base.Dispose(disposing);
|
---|
| 300 | }
|
---|
| 301 |
|
---|
[57fc402] | 302 | public async Task<ActionResult> IndexWithSearch(string searchTerm)
|
---|
[2aea0fd] | 303 | {
|
---|
| 304 | if (string.IsNullOrEmpty(searchTerm))
|
---|
| 305 | {
|
---|
[57fc402] | 306 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
[2aea0fd] | 307 | return View(vetCenters);
|
---|
| 308 | }
|
---|
| 309 | else
|
---|
| 310 | {
|
---|
[57fc402] | 311 | var searchResults = await db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToListAsync();
|
---|
[2aea0fd] | 312 | return View(searchResults);
|
---|
| 313 | }
|
---|
| 314 | }
|
---|
| 315 | }
|
---|
[d6040ef] | 316 | }
|
---|