source: PostgreSqlDotnetCore/Controllers/VetCenterController.cs@ 72b1da2

main
Last change on this file since 72b1da2 was 72b1da2, checked in by ElenaMoskova <elena.moskova99@…>, 5 weeks ago

Providing access

Providing access to various functionalities

  • Property mode set to 100644
File size: 6.5 KB
Line 
1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
4using PostgreSqlDotnetCore.Models;
5using Microsoft.AspNetCore.Mvc.Rendering;
6using System.Threading.Tasks;
7
8namespace PostgreSqlDotnetCore.Controllers
9{
10 public class VetCenterController : BaseController
11 {
12 public VetCenterController(UserManager<IdentityUser> userManager) : base(userManager)
13 {
14 }
15
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
26 // Fetch cities for dropdown
27 var citiess = await db.CitiesObj.ToListAsync();
28 ViewBag.Citiess = new SelectList(citiess, "id", "name");
29
30 return View();
31 }
32
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 }*/
45 public async Task<ActionResult> Index()
46 {
47 var vetCenters = await db.VetCentersObj.ToListAsync();
48 ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
49
50 // Проверете дали корисникот е администратор или менаџер
51 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
52 // ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
53
54 ViewBag.hasAccess = customerClass != null;
55
56 return View(vetCenters);
57 }
58
59
60 public async Task<ActionResult> Details(int? id)
61 {
62 if (id == null)
63 {
64 return RedirectToAction("NotExist", "Error");
65 }
66
67 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
68 if (vetClass == null)
69 {
70 return RedirectToAction("NotExist", "Error");
71 }
72
73 return View(vetClass);
74 }
75
76 [HttpPost]
77 [ValidateAntiForgeryToken]
78 public async Task<ActionResult> Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
79 {
80 if (ModelState.IsValid)
81 {
82 db.VetCentersObj.Add(vetClass);
83 await db.SaveChangesAsync();
84 return RedirectToAction("Index");
85 }
86
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
91 return View(vetClass);
92 }
93
94 public async Task<ActionResult> Edit(int? id)
95 {
96 if (id == null)
97 {
98 return RedirectToAction("NotExist", "Error");
99 }
100
101 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
102 if (vetClass == null)
103 {
104 return RedirectToAction("NotExist", "Error");
105 }
106
107 // Check for permission
108 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
109 //UsersClass customerClass = await checkAuthorizationAsync();
110 ViewBag.isAuthenticated = await getCrrentUser();
111 if (customerClass == null)
112 {
113 return RedirectToAction("AccessDenied", "Error");
114 }
115
116 // Fetch cities for dropdown
117 var citiess = await db.CitiesObj.ToListAsync();
118 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
119
120 return View(vetClass);
121 }
122
123 [HttpPost]
124 [ValidateAntiForgeryToken]
125 public async Task<ActionResult> Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
126 {
127 if (ModelState.IsValid)
128 {
129 db.Entry(vetClass).State = EntityState.Modified;
130 await db.SaveChangesAsync();
131 return RedirectToAction("Index");
132 }
133
134 // If model is invalid, repopulate the cities for dropdown
135 var citiess = await db.CitiesObj.ToListAsync();
136 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
137
138 return View(vetClass);
139 }
140
141 public async Task<ActionResult> Delete(int? id) {
142 UsersClass customerClass = await checkAuthorizationAsync();
143
144 ViewBag.isAuthenticated = await getCrrentUser();
145
146 if (id == null)
147 {
148 return RedirectToAction("NotExist", "Error");
149 }
150
151 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
152 if (vetClass == null)
153 {
154 return RedirectToAction("NotExist", "Error");
155 }
156
157 return View(vetClass);
158 }
159
160 [HttpPost, ActionName("Delete")]
161 [ValidateAntiForgeryToken]
162 public async Task<ActionResult> DeleteConfirmed(int id)
163 {
164 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
165 db.VetCentersObj.Remove(vetClass);
166 await db.SaveChangesAsync();
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
179 public async Task<ActionResult> IndexWithSearch(string searchTerm)
180 {
181 if (string.IsNullOrEmpty(searchTerm))
182 {
183 var vetCenters = await db.VetCentersObj.ToListAsync();
184 return View(vetCenters);
185 }
186 else
187 {
188 var searchResults = await db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToListAsync();
189 return View(searchResults);
190 }
191 }
192 }
193}
Note: See TracBrowser for help on using the repository browser.