source: PostgreSqlDotnetCore/Controllers/VetCenterController.cs@ 6782104

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

fix authorization

implement hiding menu items

  • Property mode set to 100644
File size: 4.9 KB
Line 
1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
4using PostgreSqlDotnetCore.Models;
5using System.Data;
6using System.Net;
7
8namespace PostgreSqlDotnetCore.Controllers
9{
10 public class VetCenterController : BaseController
11 {
12 public VetCenterController(UserManager<IdentityUser> userManager) : base(userManager)
13 {
14
15 // set if is authenticated
16 ViewBag.isAuthenticated = new UsersClass();
17 }
18
19 // GET: Customer
20 public ActionResult Index()
21 {
22 return View(db.VetCentersObj.ToList());
23 }
24
25 // GET: Customer/Details/5
26 public ActionResult Details(int? id)
27 {
28 if (id == null)
29 {
30 return RedirectToAction("NotExist", "Error");
31 }
32 VetCenter vetClass = db.VetCentersObj.Find(id);
33 if (vetClass == null)
34 {
35 return RedirectToAction("NotExist", "Error");
36 }
37 return View(vetClass);
38 }
39
40 // GET: Customer/Create
41 public async Task<ActionResult> CreateAsync()
42 {
43 // check for permission
44 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
45 if (customerClass == null)
46 {
47 return RedirectToAction("AccessDenied", "Error");
48 }
49 return View();
50 }
51
52 // POST: Customer/Create
53 // To protect from overposting attacks, enable the specific properties you want to bind to, for
54 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
55 [HttpPost]
56 [ValidateAntiForgeryToken]
57 public ActionResult Create([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
58 {
59 if (ModelState.IsValid)
60 {
61 db.VetCentersObj.Add(vetClass);
62 db.SaveChanges();
63 return RedirectToAction("Index");
64 }
65
66 return View(vetClass);
67 }
68
69 // GET: Customer/Edit/5
70 public async Task<ActionResult> EditAsync(int? id)
71 {
72 if (id == null)
73 {
74 return RedirectToAction("NotExist", "Error");
75 }
76 VetCenter vetClass = db.VetCentersObj.Find(id);
77 if (vetClass == null)
78 {
79 return RedirectToAction("NotExist", "Error");
80 }
81 // check for permission
82 UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
83 if (customerClass == null)
84 {
85 return RedirectToAction("AccessDenied", "Error");
86 }
87 return View(vetClass);
88 }
89
90 // POST: Customer/Edit/5
91 // To protect from overposting attacks, enable the specific properties you want to bind to, for
92 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
93 [HttpPost]
94 [ValidateAntiForgeryToken]
95 public ActionResult Edit([Bind(include: "id,name,adress,description,workinghours,phonenumber,latitude,longitude,citiesid")] VetCenter vetClass)
96 {
97 if (ModelState.IsValid)
98 {
99 db.Entry(vetClass).State = EntityState.Modified;
100 db.SaveChanges();
101 return RedirectToAction("Index");
102 }
103 return View(vetClass);
104 }
105
106 // GET: Customer/Delete/5
107 public ActionResult Delete(int? id)
108 {
109 if (id == null)
110 {
111 return RedirectToAction("NotExist", "Error");
112 }
113 VetCenter vetClass = db.VetCentersObj.Find(id);
114 if (vetClass == null)
115 {
116 return RedirectToAction("NotExist", "Error");
117 }
118 return View(vetClass);
119 }
120
121 // POST: Customer/Delete/5
122 [HttpPost, ActionName("Delete")]
123 [ValidateAntiForgeryToken]
124 public ActionResult DeleteConfirmed(int id)
125 {
126 VetCenter vetClass = db.VetCentersObj.Find(id);
127 db.VetCentersObj.Remove(vetClass);
128 db.SaveChanges();
129 return RedirectToAction("Index");
130 }
131
132 protected override void Dispose(bool disposing)
133 {
134 if (disposing)
135 {
136 db.Dispose();
137 }
138 base.Dispose(disposing);
139 }
140
141
142 // GET: VetCenter/Search
143 public ActionResult IndexWithSearch(string searchTerm)
144 {
145 if (string.IsNullOrEmpty(searchTerm))
146 {
147 var vetCenters = db.VetCentersObj.ToList();
148 return View(vetCenters);
149 }
150 else
151 {
152 var searchResults = db.VetCentersObj.Where(vc => vc.name.Contains(searchTerm)).ToList();
153 return View(searchResults);
154 }
155 }
156
157
158
159 }
160}
Note: See TracBrowser for help on using the repository browser.