- Timestamp:
- 08/22/24 01:46:17 (3 months ago)
- Branches:
- main
- Children:
- 72b1da2
- Parents:
- d6040ef
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
PostgreSqlDotnetCore/Controllers/VetCenterController.cs
rd6040ef r57fc402 4 4 using PostgreSqlDotnetCore.Models; 5 5 using Microsoft.AspNetCore.Mvc.Rendering; 6 using System.Data; 7 using System.Net; 6 using System.Threading.Tasks; 8 7 9 8 namespace PostgreSqlDotnetCore.Controllers … … 15 14 } 16 15 17 public async Task<ActionResult> Create() 18 { 19 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin); 20 if (customerClass == null) 21 { 22 return RedirectToAction("AccessDenied", "Error"); 23 } 24 var citiess = await db.CitiesObj.ToListAsync(); 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 } 25 25 26 ViewBag.Citiess = new SelectList(citiess, "id", "name"); 26 // Fetch cities for dropdown 27 var citiess = await db.CitiesObj.ToListAsync(); 28 ViewBag.Citiess = new SelectList(citiess, "id", "name"); 27 29 28 29 30 return View(); 31 } 30 32 31 // GET: Customer 32 /* public ActionResult Index() 33 { 34 return View(db.VetCentersObj.ToList()); 35 }*/ 33 public async Task<ActionResult> Index() 34 { 35 var vetCenters = await db.VetCentersObj.ToListAsync(); 36 ViewBag.isAuthenticated = User.Identity.IsAuthenticated; 36 37 37 public ActionResult Index() 38 { 39 var vetCenters = db.VetCentersObj.ToList(); 40 41 // Составување на списокот на ветеринарни центри и проверка на автентикацијата 42 ViewBag.isAuthenticated = User.Identity.IsAuthenticated; 38 // Check if the user is an admin 39 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin); 40 ViewBag.hasAccess = customerClass != null; 43 41 44 42 return View(vetCenters); 45 43 } 46 44 47 48 // GET: Customer/Details/5 49 public ActionResult Details(int? id) 45 public async Task<ActionResult> Details(int? id) 50 46 { 51 47 if (id == null) … … 53 49 return RedirectToAction("NotExist", "Error"); 54 50 } 55 VetCenter vetClass = db.VetCentersObj.Find(id); 51 52 VetCenter vetClass = await db.VetCentersObj.FindAsync(id); 56 53 if (vetClass == null) 57 54 { 58 55 return RedirectToAction("NotExist", "Error"); 59 }60 return View(vetClass);61 }62 63 // GET: Customer/Create64 /* public async Task<ActionResult> CreateAsync()65 {66 // check for permission67 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);68 if (customerClass == null)69 {70 return RedirectToAction("AccessDenied", "Error");71 }72 return View();73 }*/74 75 // POST: Customer/Create76 // To protect from overposting attacks, enable the specific properties you want to bind to, for77 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.78 [HttpPost]79 [ValidateAntiForgeryToken]80 81 82 public ActionResult Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)83 {84 if (ModelState.IsValid)85 {86 db.VetCentersObj.Add(vetClass);87 db.SaveChanges();88 return RedirectToAction("Index");89 56 } 90 57 … … 92 59 } 93 60 94 // GET: Customer/Edit/5 95 public async Task<ActionResult> EditAsync(int? id) 61 [HttpPost] 62 [ValidateAntiForgeryToken] 63 public async Task<ActionResult> Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass) 64 { 65 if (ModelState.IsValid) 66 { 67 db.VetCentersObj.Add(vetClass); 68 await db.SaveChangesAsync(); 69 return RedirectToAction("Index"); 70 } 71 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 76 return View(vetClass); 77 } 78 79 public async Task<ActionResult> Edit(int? id) 96 80 { 97 81 if (id == null) … … 99 83 return RedirectToAction("NotExist", "Error"); 100 84 } 101 VetCenter vetClass = db.VetCentersObj.Find(id); 85 86 VetCenter vetClass = await db.VetCentersObj.FindAsync(id); 102 87 if (vetClass == null) 103 88 { 104 89 return RedirectToAction("NotExist", "Error"); 105 90 } 106 // check for permission 91 92 // Check for permission 107 93 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin); 94 //UsersClass customerClass = await checkAuthorizationAsync(); 95 ViewBag.isAuthenticated = await getCrrentUser(); 108 96 if (customerClass == null) 109 97 { 110 98 return RedirectToAction("AccessDenied", "Error"); 111 99 } 100 101 // Fetch cities for dropdown 112 102 var citiess = await db.CitiesObj.ToListAsync(); 113 103 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid); 104 114 105 return View(vetClass); 115 106 } 116 107 117 // POST: Customer/Edit/5118 // To protect from overposting attacks, enable the specific properties you want to bind to, for119 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.120 108 [HttpPost] 121 109 [ValidateAntiForgeryToken] 122 /*123 public ActionResult Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)124 {125 if (ModelState.IsValid)126 {127 db.Entry(vetClass).State = EntityState.Modified;128 db.SaveChanges();129 return RedirectToAction("Index");130 }131 return View(vetClass);132 }*/133 134 // POST: VetCenter/Edit/5135 136 110 public async Task<ActionResult> Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass) 137 111 { … … 143 117 } 144 118 119 // If model is invalid, repopulate the cities for dropdown 145 120 var citiess = await db.CitiesObj.ToListAsync(); 146 121 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid); … … 149 124 } 150 125 151 152 153 // GET: Customer/Delete/5 154 public ActionResult Delete(int? id) 126 public async Task<ActionResult> Delete(int? id) 155 127 { 156 128 if (id == null) … … 158 130 return RedirectToAction("NotExist", "Error"); 159 131 } 160 VetCenter vetClass = db.VetCentersObj.Find(id); 132 133 VetCenter vetClass = await db.VetCentersObj.FindAsync(id); 161 134 if (vetClass == null) 162 135 { 163 136 return RedirectToAction("NotExist", "Error"); 164 137 } 138 165 139 return View(vetClass); 166 140 } 167 141 168 // POST: Customer/Delete/5169 142 [HttpPost, ActionName("Delete")] 170 143 [ValidateAntiForgeryToken] 171 public ActionResultDeleteConfirmed(int id)144 public async Task<ActionResult> DeleteConfirmed(int id) 172 145 { 173 VetCenter vetClass = db.VetCentersObj.Find(id);146 VetCenter vetClass = await db.VetCentersObj.FindAsync(id); 174 147 db.VetCentersObj.Remove(vetClass); 175 db.SaveChanges();148 await db.SaveChangesAsync(); 176 149 return RedirectToAction("Index"); 177 150 } … … 186 159 } 187 160 188 189 // GET: VetCenter/Search 190 public ActionResult IndexWithSearch(string searchTerm) 161 public async Task<ActionResult> IndexWithSearch(string searchTerm) 191 162 { 192 163 if (string.IsNullOrEmpty(searchTerm)) 193 164 { 194 var vetCenters = db.VetCentersObj.ToList();165 var vetCenters = await db.VetCentersObj.ToListAsync(); 195 166 return View(vetCenters); 196 167 } 197 168 else 198 169 { 199 var searchResults = db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToList();170 var searchResults = await db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToListAsync(); 200 171 return View(searchResults); 201 172 } 202 173 } 203 204 205 206 174 } 207 175 }
Note:
See TracChangeset
for help on using the changeset viewer.