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