[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 |
|
---|
[57fc402] | 33 | public async Task<ActionResult> Index()
|
---|
[2aea0fd] | 34 | {
|
---|
[57fc402] | 35 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
[d6040ef] | 36 | ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 37 |
|
---|
[57fc402] | 38 | // Check if the user is an admin
|
---|
| 39 | UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
| 40 | ViewBag.hasAccess = customerClass != null;
|
---|
| 41 |
|
---|
[d6040ef] | 42 | return View(vetCenters);
|
---|
[2aea0fd] | 43 | }
|
---|
| 44 |
|
---|
[57fc402] | 45 | public async Task<ActionResult> Details(int? id)
|
---|
[2aea0fd] | 46 | {
|
---|
| 47 | if (id == null)
|
---|
| 48 | {
|
---|
| 49 | return RedirectToAction("NotExist", "Error");
|
---|
| 50 | }
|
---|
[57fc402] | 51 |
|
---|
| 52 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
[2aea0fd] | 53 | if (vetClass == null)
|
---|
| 54 | {
|
---|
| 55 | return RedirectToAction("NotExist", "Error");
|
---|
| 56 | }
|
---|
[57fc402] | 57 |
|
---|
[2aea0fd] | 58 | return View(vetClass);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | [HttpPost]
|
---|
| 62 | [ValidateAntiForgeryToken]
|
---|
[57fc402] | 63 | public async Task<ActionResult> Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
|
---|
[2aea0fd] | 64 | {
|
---|
| 65 | if (ModelState.IsValid)
|
---|
| 66 | {
|
---|
| 67 | db.VetCentersObj.Add(vetClass);
|
---|
[57fc402] | 68 | await db.SaveChangesAsync();
|
---|
[2aea0fd] | 69 | return RedirectToAction("Index");
|
---|
| 70 | }
|
---|
| 71 |
|
---|
[57fc402] | 72 | // If model is invalid, repopulate the cities for dropdown
|
---|
| 73 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
| 74 | ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
|
---|
| 75 |
|
---|
[2aea0fd] | 76 | return View(vetClass);
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[57fc402] | 79 | public async Task<ActionResult> Edit(int? id)
|
---|
[2aea0fd] | 80 | {
|
---|
| 81 | if (id == null)
|
---|
| 82 | {
|
---|
| 83 | return RedirectToAction("NotExist", "Error");
|
---|
| 84 | }
|
---|
[57fc402] | 85 |
|
---|
| 86 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
[2aea0fd] | 87 | if (vetClass == null)
|
---|
| 88 | {
|
---|
| 89 | return RedirectToAction("NotExist", "Error");
|
---|
| 90 | }
|
---|
[57fc402] | 91 |
|
---|
| 92 | // Check for permission
|
---|
[2aea0fd] | 93 | UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
[57fc402] | 94 | //UsersClass customerClass = await checkAuthorizationAsync();
|
---|
| 95 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
[2aea0fd] | 96 | if (customerClass == null)
|
---|
| 97 | {
|
---|
| 98 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 99 | }
|
---|
[57fc402] | 100 |
|
---|
| 101 | // Fetch cities for dropdown
|
---|
[d6040ef] | 102 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
| 103 | ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
|
---|
[57fc402] | 104 |
|
---|
[2aea0fd] | 105 | return View(vetClass);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | [HttpPost]
|
---|
| 109 | [ValidateAntiForgeryToken]
|
---|
[d6040ef] | 110 | public async Task<ActionResult> Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
|
---|
[2aea0fd] | 111 | {
|
---|
| 112 | if (ModelState.IsValid)
|
---|
| 113 | {
|
---|
| 114 | db.Entry(vetClass).State = EntityState.Modified;
|
---|
[d6040ef] | 115 | await db.SaveChangesAsync();
|
---|
[2aea0fd] | 116 | return RedirectToAction("Index");
|
---|
| 117 | }
|
---|
[d6040ef] | 118 |
|
---|
[57fc402] | 119 | // If model is invalid, repopulate the cities for dropdown
|
---|
[d6040ef] | 120 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
| 121 | ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
|
---|
| 122 |
|
---|
[2aea0fd] | 123 | return View(vetClass);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[57fc402] | 126 | public async Task<ActionResult> Delete(int? id)
|
---|
[2aea0fd] | 127 | {
|
---|
| 128 | if (id == null)
|
---|
| 129 | {
|
---|
| 130 | return RedirectToAction("NotExist", "Error");
|
---|
| 131 | }
|
---|
[57fc402] | 132 |
|
---|
| 133 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
[2aea0fd] | 134 | if (vetClass == null)
|
---|
| 135 | {
|
---|
| 136 | return RedirectToAction("NotExist", "Error");
|
---|
| 137 | }
|
---|
[57fc402] | 138 |
|
---|
[2aea0fd] | 139 | return View(vetClass);
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | [HttpPost, ActionName("Delete")]
|
---|
| 143 | [ValidateAntiForgeryToken]
|
---|
[57fc402] | 144 | public async Task<ActionResult> DeleteConfirmed(int id)
|
---|
[2aea0fd] | 145 | {
|
---|
[57fc402] | 146 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
[2aea0fd] | 147 | db.VetCentersObj.Remove(vetClass);
|
---|
[57fc402] | 148 | await db.SaveChangesAsync();
|
---|
[2aea0fd] | 149 | return RedirectToAction("Index");
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | protected override void Dispose(bool disposing)
|
---|
| 153 | {
|
---|
| 154 | if (disposing)
|
---|
| 155 | {
|
---|
| 156 | db.Dispose();
|
---|
| 157 | }
|
---|
| 158 | base.Dispose(disposing);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[57fc402] | 161 | public async Task<ActionResult> IndexWithSearch(string searchTerm)
|
---|
[2aea0fd] | 162 | {
|
---|
| 163 | if (string.IsNullOrEmpty(searchTerm))
|
---|
| 164 | {
|
---|
[57fc402] | 165 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
[2aea0fd] | 166 | return View(vetCenters);
|
---|
| 167 | }
|
---|
| 168 | else
|
---|
| 169 | {
|
---|
[57fc402] | 170 | var searchResults = await db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToListAsync();
|
---|
[2aea0fd] | 171 | return View(searchResults);
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | }
|
---|
[d6040ef] | 175 | }
|
---|