source: PostgreSqlDotnetCore/Controllers/VetCenterController.cs@ d6040ef

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

Аccess permission

  • Property mode set to 100644
File size: 6.5 KB
RevLine 
[2aea0fd]1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
4using PostgreSqlDotnetCore.Models;
[d6040ef]5using Microsoft.AspNetCore.Mvc.Rendering;
[2aea0fd]6using System.Data;
7using System.Net;
8
9namespace PostgreSqlDotnetCore.Controllers
10{
11 public class VetCenterController : BaseController
12 {
13 public VetCenterController(UserManager<IdentityUser> userManager) : base(userManager)
14 {
15 }
16
[d6040ef]17 public async Task<ActionResult> Create()
18 {
19 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
20 if (customerClass == null)
21 {
22 return RedirectToAction("AccessDenied", "Error");
23 }
24 var citiess = await db.CitiesObj.ToListAsync();
25
26 ViewBag.Citiess = new SelectList(citiess, "id", "name");
27
28 return View();
29 }
30
[2aea0fd]31 // GET: Customer
[d6040ef]32 /* public ActionResult Index()
33 {
34 return View(db.VetCentersObj.ToList());
35 }*/
36
[2aea0fd]37 public ActionResult Index()
38 {
[d6040ef]39 var vetCenters = db.VetCentersObj.ToList();
40
41 // Составување на списокот на ветеринарни центри и проверка на автентикацијата
42 ViewBag.isAuthenticated = User.Identity.IsAuthenticated;
43
44 return View(vetCenters);
[2aea0fd]45 }
46
[d6040ef]47
[2aea0fd]48 // GET: Customer/Details/5
49 public ActionResult Details(int? id)
50 {
51 if (id == null)
52 {
53 return RedirectToAction("NotExist", "Error");
54 }
55 VetCenter vetClass = db.VetCentersObj.Find(id);
56 if (vetClass == null)
57 {
58 return RedirectToAction("NotExist", "Error");
59 }
60 return View(vetClass);
61 }
62
63 // GET: Customer/Create
[d6040ef]64 /* public async Task<ActionResult> CreateAsync()
65 {
66 // check for permission
67 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
68 if (customerClass == null)
69 {
70 return RedirectToAction("AccessDenied", "Error");
71 }
72 return View();
73 }*/
[2aea0fd]74
75 // POST: Customer/Create
76 // To protect from overposting attacks, enable the specific properties you want to bind to, for
77 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
78 [HttpPost]
79 [ValidateAntiForgeryToken]
[d6040ef]80
81
[2aea0fd]82 public ActionResult Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
83 {
84 if (ModelState.IsValid)
85 {
86 db.VetCentersObj.Add(vetClass);
87 db.SaveChanges();
88 return RedirectToAction("Index");
89 }
90
91 return View(vetClass);
92 }
93
94 // GET: Customer/Edit/5
95 public async Task<ActionResult> EditAsync(int? id)
96 {
97 if (id == null)
98 {
99 return RedirectToAction("NotExist", "Error");
100 }
101 VetCenter vetClass = db.VetCentersObj.Find(id);
102 if (vetClass == null)
103 {
104 return RedirectToAction("NotExist", "Error");
105 }
106 // check for permission
107 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
108 if (customerClass == null)
109 {
110 return RedirectToAction("AccessDenied", "Error");
111 }
[d6040ef]112 var citiess = await db.CitiesObj.ToListAsync();
113 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
[2aea0fd]114 return View(vetClass);
115 }
116
117 // POST: Customer/Edit/5
118 // To protect from overposting attacks, enable the specific properties you want to bind to, for
119 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
120 [HttpPost]
121 [ValidateAntiForgeryToken]
[d6040ef]122 /*
123 public ActionResult Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
124 {
125 if (ModelState.IsValid)
126 {
127 db.Entry(vetClass).State = EntityState.Modified;
128 db.SaveChanges();
129 return RedirectToAction("Index");
130 }
131 return View(vetClass);
132 }*/
133
134 // POST: VetCenter/Edit/5
135
136 public async Task<ActionResult> Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
[2aea0fd]137 {
138 if (ModelState.IsValid)
139 {
140 db.Entry(vetClass).State = EntityState.Modified;
[d6040ef]141 await db.SaveChangesAsync();
[2aea0fd]142 return RedirectToAction("Index");
143 }
[d6040ef]144
145 var citiess = await db.CitiesObj.ToListAsync();
146 ViewBag.Citiess = new SelectList(citiess, "id", "name", vetClass.citiesid);
147
[2aea0fd]148 return View(vetClass);
149 }
150
[d6040ef]151
152
[2aea0fd]153 // GET: Customer/Delete/5
154 public ActionResult Delete(int? id)
155 {
156 if (id == null)
157 {
158 return RedirectToAction("NotExist", "Error");
159 }
160 VetCenter vetClass = db.VetCentersObj.Find(id);
161 if (vetClass == null)
162 {
163 return RedirectToAction("NotExist", "Error");
164 }
165 return View(vetClass);
166 }
167
168 // POST: Customer/Delete/5
169 [HttpPost, ActionName("Delete")]
170 [ValidateAntiForgeryToken]
171 public ActionResult DeleteConfirmed(int id)
172 {
173 VetCenter vetClass = db.VetCentersObj.Find(id);
174 db.VetCentersObj.Remove(vetClass);
175 db.SaveChanges();
176 return RedirectToAction("Index");
177 }
178
179 protected override void Dispose(bool disposing)
180 {
181 if (disposing)
182 {
183 db.Dispose();
184 }
185 base.Dispose(disposing);
186 }
187
188
189 // GET: VetCenter/Search
190 public ActionResult IndexWithSearch(string searchTerm)
191 {
192 if (string.IsNullOrEmpty(searchTerm))
193 {
194 var vetCenters = db.VetCentersObj.ToList();
195 return View(vetCenters);
196 }
197 else
198 {
199 var searchResults = db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToList();
200 return View(searchResults);
201 }
202 }
203
204
205
206 }
[d6040ef]207}
Note: See TracBrowser for help on using the repository browser.