1 | using Microsoft.AspNetCore.Identity;
|
---|
2 | using Microsoft.AspNetCore.Mvc;
|
---|
3 | using Microsoft.EntityFrameworkCore;
|
---|
4 | using PostgreSqlDotnetCore.Models;
|
---|
5 | using Microsoft.AspNetCore.Mvc.Rendering;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 | using Npgsql;
|
---|
8 | using PostgreSqlDotnetCore.Data;
|
---|
9 |
|
---|
10 | namespace PostgreSqlDotnetCore.Controllers
|
---|
11 | {
|
---|
12 | public class VetCenterController : BaseController
|
---|
13 | {
|
---|
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)
|
---|
22 | {
|
---|
23 | db = context ?? throw new ArgumentNullException(nameof(context));
|
---|
24 | }
|
---|
25 |
|
---|
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 | }
|
---|
35 | // no access for standard user
|
---|
36 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
37 |
|
---|
38 | // Fetch cities for dropdown
|
---|
39 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
40 | ViewBag.Citiess = new SelectList(citiess, "id", "name");
|
---|
41 |
|
---|
42 | return View();
|
---|
43 | }
|
---|
44 |
|
---|
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 | }*/
|
---|
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 | */
|
---|
73 | public async Task<ActionResult> Index()
|
---|
74 | {
|
---|
75 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
76 | ViewBag.hasAccess = await checkAuthorizationAsync();
|
---|
77 | var vetCenters = await db.VetCentersWithCity.ToListAsync();
|
---|
78 | // ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
|
---|
79 | // no access for standard user
|
---|
80 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
81 |
|
---|
82 |
|
---|
83 | UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
84 |
|
---|
85 | ViewBag.hasAccess = customerClass != null;
|
---|
86 |
|
---|
87 | return View(vetCenters);
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 |
|
---|
92 | /*public async Task<ActionResult> Details(int? id)
|
---|
93 | {
|
---|
94 | if (id == null)
|
---|
95 | {
|
---|
96 | return RedirectToAction("NotExist", "Error");
|
---|
97 | }
|
---|
98 |
|
---|
99 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
100 | UsersClass customerClass = await getCrrentUser();
|
---|
101 | ViewBag.isAuthenticated = customerClass;
|
---|
102 | if (vetClass == null)
|
---|
103 | {
|
---|
104 | return RedirectToAction("NotExist", "Error");
|
---|
105 | }
|
---|
106 | // no access for standard user
|
---|
107 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
108 |
|
---|
109 | return View(vetClass);
|
---|
110 | }
|
---|
111 |
|
---|
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 |
|
---|
152 | [HttpPost]
|
---|
153 | [ValidateAntiForgeryToken]
|
---|
154 | public async Task<ActionResult> Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
|
---|
155 | {
|
---|
156 | if (ModelState.IsValid)
|
---|
157 | {
|
---|
158 | db.VetCentersObj.Add(vetClass);
|
---|
159 | await db.SaveChangesAsync();
|
---|
160 | return RedirectToAction("Index");
|
---|
161 | }
|
---|
162 |
|
---|
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 |
|
---|
167 | return View(vetClass);
|
---|
168 | }
|
---|
169 |
|
---|
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 |
|
---|
212 | public async Task<ActionResult> Edit(int? id)
|
---|
213 | {
|
---|
214 | if (id == null)
|
---|
215 | {
|
---|
216 | return RedirectToAction("NotExist", "Error");
|
---|
217 | }
|
---|
218 |
|
---|
219 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
220 | if (vetClass == null)
|
---|
221 | {
|
---|
222 | return RedirectToAction("NotExist", "Error");
|
---|
223 | }
|
---|
224 |
|
---|
225 | // Check for permission
|
---|
226 | UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
227 | //UsersClass customerClass = await checkAuthorizationAsync();
|
---|
228 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
229 | if (customerClass == null)
|
---|
230 | {
|
---|
231 | return RedirectToAction("AccessDenied", "Error");
|
---|
232 | }
|
---|
233 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
234 |
|
---|
235 |
|
---|
236 | // Fetch cities for dropdown
|
---|
237 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
238 | ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
|
---|
239 |
|
---|
240 | return View(vetClass);
|
---|
241 | }
|
---|
242 |
|
---|
243 | [HttpPost]
|
---|
244 | [ValidateAntiForgeryToken]
|
---|
245 | public async Task<ActionResult> Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
|
---|
246 | {
|
---|
247 | if (ModelState.IsValid)
|
---|
248 | {
|
---|
249 | db.Entry(vetClass).State = EntityState.Modified;
|
---|
250 | await db.SaveChangesAsync();
|
---|
251 | return RedirectToAction("Index");
|
---|
252 | }
|
---|
253 |
|
---|
254 | // If model is invalid, repopulate the cities for dropdown
|
---|
255 | var citiess = await db.CitiesObj.ToListAsync();
|
---|
256 | ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
|
---|
257 |
|
---|
258 | return View(vetClass);
|
---|
259 | }
|
---|
260 |
|
---|
261 | public async Task<ActionResult> Delete(int? id)
|
---|
262 | {
|
---|
263 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
264 |
|
---|
265 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
266 |
|
---|
267 | if (id == null)
|
---|
268 | {
|
---|
269 | return RedirectToAction("NotExist", "Error");
|
---|
270 | }
|
---|
271 |
|
---|
272 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
273 | if (vetClass == null)
|
---|
274 | {
|
---|
275 | return RedirectToAction("NotExist", "Error");
|
---|
276 | }
|
---|
277 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
278 |
|
---|
279 |
|
---|
280 | return View(vetClass);
|
---|
281 | }
|
---|
282 |
|
---|
283 | [HttpPost, ActionName("Delete")]
|
---|
284 | [ValidateAntiForgeryToken]
|
---|
285 | public async Task<ActionResult> DeleteConfirmed(int id)
|
---|
286 | {
|
---|
287 | VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
|
---|
288 | db.VetCentersObj.Remove(vetClass);
|
---|
289 | await db.SaveChangesAsync();
|
---|
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 |
|
---|
302 | public async Task<ActionResult> IndexWithSearch(string searchTerm)
|
---|
303 | {
|
---|
304 | if (string.IsNullOrEmpty(searchTerm))
|
---|
305 | {
|
---|
306 | var vetCenters = await db.VetCentersObj.ToListAsync();
|
---|
307 | return View(vetCenters);
|
---|
308 | }
|
---|
309 | else
|
---|
310 | {
|
---|
311 | var searchResults = await db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToListAsync();
|
---|
312 | return View(searchResults);
|
---|
313 | }
|
---|
314 | }
|
---|
315 | }
|
---|
316 | }
|
---|