source: PostgreSqlDotnetCore/Controllers/CityController.cs@ 72b1da2

main
Last change on this file since 72b1da2 was 57fc402, checked in by ElenaMoskova <elena.moskova99@…>, 3 months 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: 4.5 KB
Line 
1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
4using PostgreSqlDotnetCore.Controllers;
5using PostgreSqlDotnetCore.Models;
6using System.Net;
7
8namespace PostgreSqlDotnetCore.Controllers
9{
10 public class CityController : BaseController
11 {
12 public CityController(UserManager<IdentityUser> userManager) : base(userManager)
13 {
14 }
15
16 // GET: Customer
17 public async Task<ActionResult> IndexAsync()
18 {
19 // check for permission
20 UsersClass customerClass = await checkAuthorizationAsync();
21
22
23 if (customerClass == null)
24 {
25 return RedirectToAction("AccessDenied", "Error");
26 }
27
28 var citiess = await db.CitiesObj.ToListAsync();
29
30 return View(citiess);
31 }
32
33
34
35
36 /* public async Task<ActionResult> IndexAsync()
37 {
38 // check for permission
39 UsersClass customerClass = await checkAuthorizationAsync();
40 if (customerClass == null)
41 {
42 return RedirectToAction("AccessDenied", "Error");
43 }
44 //return View(Enumerable.Empty<UsersClass>());
45 return View(db.CitiesObj.ToList());
46 }
47 */
48
49
50
51 // GET: Customer/Details/5
52 public ActionResult Details(int? id)
53 {
54 if (id == null)
55 {
56 return RedirectToAction("NotExist", "Error");
57 }
58 CitiesClass cityClass = db.CitiesObj.Find(id);
59 if (cityClass == null)
60 {
61 return RedirectToAction("NotExist", "Error");
62 }
63 return View(cityClass);
64 }
65
66 // GET: Customer/Create
67 //public ActionResult Create()
68 //{
69 // return View();
70 //}
71
72 public ActionResult Create()
73 {
74
75 return View();
76 }
77
78 // POST: Customer/Create
79 // To protect from overposting attacks, enable the specific properties you want to bind to, for
80 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
81 [HttpPost]
82 [ValidateAntiForgeryToken]
83 public ActionResult Create([Bind(include: "id,name")] CitiesClass cityClass)
84 {
85 if (ModelState.IsValid)
86 {
87 db.CitiesObj.Add(cityClass);
88 db.SaveChanges();
89 return RedirectToAction("Index");
90 }
91
92 return View(cityClass);
93 }
94
95 // GET: Customer/Edit/5
96 public ActionResult Edit(int? id)
97 {
98 if (id == null)
99 {
100 return RedirectToAction("NotExist", "Error");
101 }
102 CitiesClass cityClass = db.CitiesObj.Find(id);
103 if (cityClass == null)
104 {
105 return RedirectToAction("NotExist", "Error");
106 }
107 return View(cityClass);
108 }
109
110 // POST: Customer/Edit/5
111 // To protect from overposting attacks, enable the specific properties you want to bind to, for
112 // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
113 [HttpPost]
114 [ValidateAntiForgeryToken]
115 public ActionResult Edit([Bind(include: "id,name")] CitiesClass cityClass)
116 {
117 if (ModelState.IsValid)
118 {
119 db.Entry(cityClass).State = EntityState.Modified;
120 db.SaveChanges();
121 return RedirectToAction("Index");
122 }
123 return View(cityClass);
124 }
125
126 // GET: Customer/Delete/5
127 public ActionResult Delete(int? id)
128 {
129 if (id == null)
130 {
131 return RedirectToAction("NotExist", "Error");
132 }
133 CitiesClass cityClass = db.CitiesObj.Find(id);
134 if (cityClass == null)
135 {
136 return RedirectToAction("NotExist", "Error");
137 }
138 return View(cityClass);
139 }
140
141 // POST: Customer/Delete/5
142 [HttpPost, ActionName("Delete")]
143 [ValidateAntiForgeryToken]
144 public ActionResult DeleteConfirmed(int id)
145 {
146 CitiesClass cityClass = db.CitiesObj.Find(id);
147 db.CitiesObj.Remove(cityClass);
148 db.SaveChanges();
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
162
163
164 }
165}
Note: See TracBrowser for help on using the repository browser.