[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;
|
---|
[2aea0fd] | 7 |
|
---|
| 8 | namespace PostgreSqlDotnetCore.Controllers
|
---|
| 9 | {
|
---|
| 10 | public class VetCenterController : BaseController
|
---|
| 11 | {
|
---|
| 12 | public VetCenterController(UserManager<IdentityUser> userManager) : base(userManager)
|
---|
| 13 | {
|
---|
| 14 | }
|
---|
| 15 |
|
---|
[57fc402] | 16 | public async Task<ActionResult> Create()
|
---|
| 17 | {
|
---|
| 18 | // Set if user is authenticated
|
---|
| 19 | UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
| 20 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
| 21 | if (customerClass == null)
|
---|
| 22 | {
|
---|
| 23 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 24 | }
|
---|
[d6040ef] | 25 |
|
---|
[57fc402] | 26 | // Fetch cities for dropdown
|
---|
| 27 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
| 28 | ViewBag.Citiess = new SelectList(citiess, "id", "name");
|
---|
[d6040ef] | 29 |
|
---|
[57fc402] | 30 | return View();
|
---|
| 31 | }
|
---|
[d6040ef] | 32 |
|
---|
[72b1da2] | 33 | /* public async Task<ActionResult> Index()
|
---|
| 34 | {
|
---|
| 35 |
|
---|
| 36 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
| 37 | ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 38 |
|
---|
| 39 | // Check if the user is an admin
|
---|
| 40 | UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
| 41 | ViewBag.hasAccess = customerClass != null;
|
---|
| 42 |
|
---|
| 43 | return View(vetCenters);
|
---|
| 44 | }*/
|
---|
[57fc402] | 45 | public async Task<ActionResult> Index()
|
---|
[2aea0fd] | 46 | {
|
---|
[57fc402] | 47 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
[d6040ef] | 48 | ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 49 |
|
---|
[72b1da2] | 50 | // Проверете дали корисникот е администратор или менаџер
|
---|
[57fc402] | 51 | UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
[72b1da2] | 52 | // ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
| 53 |
|
---|
[57fc402] | 54 | ViewBag.hasAccess = customerClass != null;
|
---|
| 55 |
|
---|
[d6040ef] | 56 | return View(vetCenters);
|
---|
[2aea0fd] | 57 | }
|
---|
| 58 |
|
---|
[72b1da2] | 59 |
|
---|
[57fc402] | 60 | public async Task<ActionResult> Details(int? id)
|
---|
[2aea0fd] | 61 | {
|
---|
| 62 | if (id == null)
|
---|
| 63 | {
|
---|
| 64 | return RedirectToAction("NotExist", "Error");
|
---|
| 65 | }
|
---|
[57fc402] | 66 |
|
---|
| 67 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
[2aea0fd] | 68 | if (vetClass == null)
|
---|
| 69 | {
|
---|
| 70 | return RedirectToAction("NotExist", "Error");
|
---|
| 71 | }
|
---|
[57fc402] | 72 |
|
---|
[2aea0fd] | 73 | return View(vetClass);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | [HttpPost]
|
---|
| 77 | [ValidateAntiForgeryToken]
|
---|
[57fc402] | 78 | public async Task<ActionResult> Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
|
---|
[2aea0fd] | 79 | {
|
---|
| 80 | if (ModelState.IsValid)
|
---|
| 81 | {
|
---|
| 82 | db.VetCentersObj.Add(vetClass);
|
---|
[57fc402] | 83 | await db.SaveChangesAsync();
|
---|
[2aea0fd] | 84 | return RedirectToAction("Index");
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[57fc402] | 87 | // If model is invalid, repopulate the cities for dropdown
|
---|
| 88 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
| 89 | ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
|
---|
| 90 |
|
---|
[2aea0fd] | 91 | return View(vetClass);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[57fc402] | 94 | public async Task<ActionResult> Edit(int? id)
|
---|
[2aea0fd] | 95 | {
|
---|
| 96 | if (id == null)
|
---|
| 97 | {
|
---|
| 98 | return RedirectToAction("NotExist", "Error");
|
---|
| 99 | }
|
---|
[57fc402] | 100 |
|
---|
| 101 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
[2aea0fd] | 102 | if (vetClass == null)
|
---|
| 103 | {
|
---|
| 104 | return RedirectToAction("NotExist", "Error");
|
---|
| 105 | }
|
---|
[57fc402] | 106 |
|
---|
| 107 | // Check for permission
|
---|
[2aea0fd] | 108 | UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
[57fc402] | 109 | //UsersClass customerClass = await checkAuthorizationAsync();
|
---|
| 110 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
[2aea0fd] | 111 | if (customerClass == null)
|
---|
| 112 | {
|
---|
| 113 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 114 | }
|
---|
[57fc402] | 115 |
|
---|
| 116 | // Fetch cities for dropdown
|
---|
[d6040ef] | 117 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
| 118 | ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
|
---|
[57fc402] | 119 |
|
---|
[2aea0fd] | 120 | return View(vetClass);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | [HttpPost]
|
---|
| 124 | [ValidateAntiForgeryToken]
|
---|
[d6040ef] | 125 | public async Task<ActionResult> Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
|
---|
[2aea0fd] | 126 | {
|
---|
| 127 | if (ModelState.IsValid)
|
---|
| 128 | {
|
---|
| 129 | db.Entry(vetClass).State = EntityState.Modified;
|
---|
[d6040ef] | 130 | await db.SaveChangesAsync();
|
---|
[2aea0fd] | 131 | return RedirectToAction("Index");
|
---|
| 132 | }
|
---|
[d6040ef] | 133 |
|
---|
[57fc402] | 134 | // If model is invalid, repopulate the cities for dropdown
|
---|
[d6040ef] | 135 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
| 136 | ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
|
---|
| 137 |
|
---|
[2aea0fd] | 138 | return View(vetClass);
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[72b1da2] | 141 | public async Task<ActionResult> Delete(int? id) {
|
---|
| 142 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
| 143 |
|
---|
| 144 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
| 145 |
|
---|
[2aea0fd] | 146 | if (id == null)
|
---|
| 147 | {
|
---|
| 148 | return RedirectToAction("NotExist", "Error");
|
---|
| 149 | }
|
---|
[57fc402] | 150 |
|
---|
| 151 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
[2aea0fd] | 152 | if (vetClass == null)
|
---|
| 153 | {
|
---|
| 154 | return RedirectToAction("NotExist", "Error");
|
---|
| 155 | }
|
---|
[57fc402] | 156 |
|
---|
[2aea0fd] | 157 | return View(vetClass);
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | [HttpPost, ActionName("Delete")]
|
---|
| 161 | [ValidateAntiForgeryToken]
|
---|
[57fc402] | 162 | public async Task<ActionResult> DeleteConfirmed(int id)
|
---|
[2aea0fd] | 163 | {
|
---|
[57fc402] | 164 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
[2aea0fd] | 165 | db.VetCentersObj.Remove(vetClass);
|
---|
[57fc402] | 166 | await db.SaveChangesAsync();
|
---|
[2aea0fd] | 167 | return RedirectToAction("Index");
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | protected override void Dispose(bool disposing)
|
---|
| 171 | {
|
---|
| 172 | if (disposing)
|
---|
| 173 | {
|
---|
| 174 | db.Dispose();
|
---|
| 175 | }
|
---|
| 176 | base.Dispose(disposing);
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[57fc402] | 179 | public async Task<ActionResult> IndexWithSearch(string searchTerm)
|
---|
[2aea0fd] | 180 | {
|
---|
| 181 | if (string.IsNullOrEmpty(searchTerm))
|
---|
| 182 | {
|
---|
[57fc402] | 183 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
[2aea0fd] | 184 | return View(vetCenters);
|
---|
| 185 | }
|
---|
| 186 | else
|
---|
| 187 | {
|
---|
[57fc402] | 188 | var searchResults = await db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToListAsync();
|
---|
[2aea0fd] | 189 | return View(searchResults);
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
[d6040ef] | 193 | }
|
---|