source: PostgreSqlDotnetCore/Controllers/VetCenterController.cs@ 57fc402

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

Аsync, access permission, and other fixes.

Regulation of access permissions. Which fields can be accessed by different users.

  • Property mode set to 100644
File size: 5.7 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 var vetCenters = await db.VetCentersObj.ToListAsync();
36 ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
37
38 // Check if the user is an admin
39 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
40 ViewBag.hasAccess = customerClass != null;
41
42 return View(vetCenters);
43 }
44
45 public async Task<ActionResult> Details(int? id)
46 {
47 if (id == null)
48 {
49 return RedirectToAction("NotExist", "Error");
50 }
51
52 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
53 if (vetClass == null)
54 {
55 return RedirectToAction("NotExist", "Error");
56 }
57
58 return View(vetClass);
59 }
60
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)
80 {
81 if (id == null)
82 {
83 return RedirectToAction("NotExist", "Error");
84 }
85
86 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
87 if (vetClass == null)
88 {
89 return RedirectToAction("NotExist", "Error");
90 }
91
92 // Check for permission
93 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
94 //UsersClass customerClass = await checkAuthorizationAsync();
95 ViewBag.isAuthenticated = await getCrrentUser();
96 if (customerClass == null)
97 {
98 return RedirectToAction("AccessDenied", "Error");
99 }
100
101 // Fetch cities for dropdown
102 var citiess = await db.CitiesObj.ToListAsync();
103 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
104
105 return View(vetClass);
106 }
107
108 [HttpPost]
109 [ValidateAntiForgeryToken]
110 public async Task<ActionResult> Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
111 {
112 if (ModelState.IsValid)
113 {
114 db.Entry(vetClass).State = EntityState.Modified;
115 await db.SaveChangesAsync();
116 return RedirectToAction("Index");
117 }
118
119 // If model is invalid, repopulate the cities for dropdown
120 var citiess = await db.CitiesObj.ToListAsync();
121 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
122
123 return View(vetClass);
124 }
125
126 public async Task<ActionResult> Delete(int? id)
127 {
128 if (id == null)
129 {
130 return RedirectToAction("NotExist", "Error");
131 }
132
133 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
134 if (vetClass == null)
135 {
136 return RedirectToAction("NotExist", "Error");
137 }
138
139 return View(vetClass);
140 }
141
142 [HttpPost, ActionName("Delete")]
143 [ValidateAntiForgeryToken]
144 public async Task<ActionResult> DeleteConfirmed(int id)
145 {
146 VetCenter vetClass = await db.VetCentersObj.FindAsync(id);
147 db.VetCentersObj.Remove(vetClass);
148 await db.SaveChangesAsync();
149 return RedirectToAction("Index");
150 }
151
152 protected override void Dispose(bool disposing)
153 {
154 if (disposing)
155 {
156 db.Dispose();
157 }
158 base.Dispose(disposing);
159 }
160
161 public async Task<ActionResult> IndexWithSearch(string searchTerm)
162 {
163 if (string.IsNullOrEmpty(searchTerm))
164 {
165 var vetCenters = await db.VetCentersObj.ToListAsync();
166 return View(vetCenters);
167 }
168 else
169 {
170 var searchResults = await db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToListAsync();
171 return View(searchResults);
172 }
173 }
174 }
175}
Note: See TracBrowser for help on using the repository browser.