source: PostgreSqlDotnetCore/Controllers/VetCenterController.cs@ e90ba32

main
Last change on this file since e90ba32 was e9bb9d1, checked in by ElenaMoskova <elena.moskova99@…>, 4 weeks ago

Use of views

  1. Use of views in VetCenters.
  2. Ability to provide a response to a given response.
  • Property mode set to 100644
File size: 11.2 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;
7using Npgsql;
8using PostgreSqlDotnetCore.Data;
9
10namespace 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 var vetCenters = await db.VetCentersWithCity.ToListAsync();
76 ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
77 // no access for standard user
78 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
79
80
81 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
82
83 ViewBag.hasAccess = customerClass != null;
84
85 return View(vetCenters);
86 }
87
88
89
90 /*public async Task<ActionResult> Details(int? id)
91 {
92 if (id == null)
93 {
94 return RedirectToAction("NotExist", "Error");
95 }
96
97 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
98 UsersClass customerClass = await getCrrentUser();
99 ViewBag.isAuthenticated = customerClass;
100 if (vetClass == null)
101 {
102 return RedirectToAction("NotExist", "Error");
103 }
104 // no access for standard user
105 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
106
107 return View(vetClass);
108 }
109
110
111 */
112 public async Task<IActionResult> Details(int? id)
113 {
114 if (id == null)
115 {
116 return RedirectToAction("NotExist", "Error");
117 }
118
119 // Логирајте го ID-то за дебугирање
120 Console.WriteLine($"ID: {id}");
121
122 // Обидете се да најдете запис во view
123 VetCenterWithCity vetClass = await db.VetCentersWithCity
124 .Where(v => v.id == id)
125 .FirstOrDefaultAsync();
126 if (vetClass == null)
127 {
128 return RedirectToAction("NotExist", "Error");
129 }
130
131
132 UsersClass customerClass = await getCrrentUser();
133 ViewBag.isAuthenticated = customerClass;
134
135
136 return View(vetClass);
137 }
138
139
140
141
142
143
144
145
146
147
148
149
150 [HttpPost]
151 [ValidateAntiForgeryToken]
152 public async Task<ActionResult> Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
153 {
154 if (ModelState.IsValid)
155 {
156 db.VetCentersObj.Add(vetClass);
157 await db.SaveChangesAsync();
158 return RedirectToAction("Index");
159 }
160
161 // If model is invalid, repopulate the cities for dropdown
162 var citiess = await db.CitiesObj.ToListAsync();
163 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
164
165 return View(vetClass);
166 }
167
168
169
170 /*public async Task<ActionResult> Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
171 {
172 // Логирање на параметрите
173 Console.WriteLine($"Parameters: {vetClass.name}, {vetClass.adress}, {vetClass.description}, {vetClass.workinghours}, {vetClass.phonenumber}, {vetClass.latitude}, {vetClass.longitude}, {vetClass.citiesid}");
174
175 if (ModelState.IsValid)
176 {
177 // Повик на складираната процедура
178 var parameters = new[]
179 {
180 new NpgsqlParameter("@name", vetClass.name),
181 new NpgsqlParameter("@adress", vetClass.adress),
182 new NpgsqlParameter("@description", vetClass.description),
183 new NpgsqlParameter("@workinghours", vetClass.workinghours),
184 new NpgsqlParameter("@phonenumber", vetClass.phonenumber),
185 new NpgsqlParameter("@latitude", (decimal)vetClass.latitude),
186 new NpgsqlParameter("@longitude", (decimal)vetClass.longitude),
187 new NpgsqlParameter("@citiesid", vetClass.citiesid)
188 };
189
190 await db.Database.ExecuteSqlRawAsync("CALL project.AddVetCenter(@name, @adress, @description, @workinghours, @phonenumber, @latitude, @longitude, @citiesid)", parameters);
191
192 return RedirectToAction("Index");
193 }
194
195 // Ако моделот не е валиден, повторно пополнете ги градовите за паѓачкиот мени
196 var citiess = await db.CitiesObj.ToListAsync();
197 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
198
199 return View(vetClass);
200 }
201
202
203
204
205 */
206
207
208
209
210 public async Task<ActionResult> Edit(int? id)
211 {
212 if (id == null)
213 {
214 return RedirectToAction("NotExist", "Error");
215 }
216
217 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
218 if (vetClass == null)
219 {
220 return RedirectToAction("NotExist", "Error");
221 }
222
223 // Check for permission
224 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
225 //UsersClass customerClass = await checkAuthorizationAsync();
226 ViewBag.isAuthenticated = await getCrrentUser();
227 if (customerClass == null)
228 {
229 return RedirectToAction("AccessDenied", "Error");
230 }
231 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
232
233
234 // Fetch cities for dropdown
235 var citiess = await db.CitiesObj.ToListAsync();
236 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
237
238 return View(vetClass);
239 }
240
241 [HttpPost]
242 [ValidateAntiForgeryToken]
243 public async Task<ActionResult> Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
244 {
245 if (ModelState.IsValid)
246 {
247 db.Entry(vetClass).State = EntityState.Modified;
248 await db.SaveChangesAsync();
249 return RedirectToAction("Index");
250 }
251
252 // If model is invalid, repopulate the cities for dropdown
253 var citiess = await db.CitiesObj.ToListAsync();
254 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
255
256 return View(vetClass);
257 }
258
259 public async Task<ActionResult> Delete(int? id)
260 {
261 UsersClass customerClass = await checkAuthorizationAsync();
262
263 ViewBag.isAuthenticated = await getCrrentUser();
264
265 if (id == null)
266 {
267 return RedirectToAction("NotExist", "Error");
268 }
269
270 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
271 if (vetClass == null)
272 {
273 return RedirectToAction("NotExist", "Error");
274 }
275 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
276
277
278 return View(vetClass);
279 }
280
281 [HttpPost, ActionName("Delete")]
282 [ValidateAntiForgeryToken]
283 public async Task<ActionResult> DeleteConfirmed(int id)
284 {
285 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
286 db.VetCentersObj.Remove(vetClass);
287 await db.SaveChangesAsync();
288 return RedirectToAction("Index");
289 }
290
291 protected override void Dispose(bool disposing)
292 {
293 if (disposing)
294 {
295 db.Dispose();
296 }
297 base.Dispose(disposing);
298 }
299
300 public async Task<ActionResult> IndexWithSearch(string searchTerm)
301 {
302 if (string.IsNullOrEmpty(searchTerm))
303 {
304 var vetCenters = await db.VetCentersObj.ToListAsync();
305 return View(vetCenters);
306 }
307 else
308 {
309 var searchResults = await db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToListAsync();
310 return View(searchResults);
311 }
312 }
313 }
314}
Note: See TracBrowser for help on using the repository browser.