1 | using Microsoft.AspNetCore.Identity;
|
---|
2 | using Microsoft.AspNetCore.Mvc;
|
---|
3 | using Microsoft.EntityFrameworkCore;
|
---|
4 | using PostgreSqlDotnetCore.Data;
|
---|
5 | using PostgreSqlDotnetCore.Models;
|
---|
6 | using System.Net;
|
---|
7 |
|
---|
8 | namespace PostgreSqlDotnetCore.Controllers
|
---|
9 | {
|
---|
10 | public class BlogController : BaseController
|
---|
11 | {
|
---|
12 | public BlogController(UserManager<IdentityUser> userManager) : base(userManager)
|
---|
13 |
|
---|
14 | {
|
---|
15 |
|
---|
16 | }
|
---|
17 |
|
---|
18 | // GET: Customer
|
---|
19 | /* public async Task<ActionResult> IndexAsync()
|
---|
20 | {
|
---|
21 | // check for permission
|
---|
22 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
23 | if (!isAuthenticated)
|
---|
24 | {
|
---|
25 | return RedirectToAction("AccessDenied", "Error");
|
---|
26 | }
|
---|
27 | //return View(Enumerable.Empty<UsersClass>());
|
---|
28 | return View(db.BlogPostControllerObj.ToList());
|
---|
29 | }
|
---|
30 | */
|
---|
31 |
|
---|
32 | /* public async Task<ActionResult> Index()
|
---|
33 | {
|
---|
34 | // Проверка за автентикација
|
---|
35 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
36 |
|
---|
37 | if (!isAuthenticated)
|
---|
38 | {
|
---|
39 | return RedirectToAction("AccessDenied", "Error");
|
---|
40 | }
|
---|
41 |
|
---|
42 | // Список на блог постови
|
---|
43 | var blogPosts = await db.BlogPostControllerObj.ToListAsync();
|
---|
44 |
|
---|
45 | // Предавање на ViewBag за проверка на автентикација
|
---|
46 | ViewBag.isAuthenticated = isAuthenticated;
|
---|
47 |
|
---|
48 | return View(blogPosts);
|
---|
49 | }*/
|
---|
50 |
|
---|
51 | public async Task<ActionResult> Index()
|
---|
52 | {
|
---|
53 | // Проверка за автентикација
|
---|
54 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
55 |
|
---|
56 | if (!isAuthenticated)
|
---|
57 | {
|
---|
58 | return RedirectToAction("AccessDenied", "Error");
|
---|
59 | }
|
---|
60 |
|
---|
61 | // Список на блог постови
|
---|
62 | var blogPosts = await db.BlogPostControllerObj.ToListAsync();
|
---|
63 |
|
---|
64 | // Вземи тековниот корисник
|
---|
65 | var currentUser = await _userManager.GetUserAsync(User);
|
---|
66 | var customerClass = await db.CustomerObj.SingleOrDefaultAsync(x => x.email == currentUser.Email);
|
---|
67 |
|
---|
68 | // Предавање на ViewBag за проверка на автентикација и корисничкиот ID
|
---|
69 | ViewBag.isAuthenticated = isAuthenticated;
|
---|
70 | // no access for standard user
|
---|
71 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
72 |
|
---|
73 | ViewBag.CurrentUserId = customerClass?.id;
|
---|
74 |
|
---|
75 | return View(blogPosts);
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 |
|
---|
80 |
|
---|
81 |
|
---|
82 | // GET: Customer/Details/5
|
---|
83 | public async Task<ActionResult> DetailsAsync(int? id)
|
---|
84 | {
|
---|
85 | if (id == null)
|
---|
86 | {
|
---|
87 | return View(null);
|
---|
88 | //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
---|
89 | }
|
---|
90 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
91 | UsersClass customerClass = await getCrrentUser();
|
---|
92 | ViewBag.isAuthenticated = customerClass;
|
---|
93 | if (blogClass == null)
|
---|
94 | {
|
---|
95 | return RedirectToAction("NotExist", "Error");
|
---|
96 | }
|
---|
97 | // get answers
|
---|
98 |
|
---|
99 | // query
|
---|
100 | var query = from st in db.BlogPostAnswersObj
|
---|
101 | where st.BlogPostConsultationid == blogClass.id
|
---|
102 | select st;
|
---|
103 | //elenaaa
|
---|
104 | var blogAnswers = query.ToList();
|
---|
105 | blogClass.BlogPostAnswers = blogAnswers;
|
---|
106 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
107 | return View(blogClass);
|
---|
108 | }
|
---|
109 |
|
---|
110 | // GET: Customer/Create
|
---|
111 | //public ActionResult Create()
|
---|
112 | //{
|
---|
113 | // return View();
|
---|
114 | //}
|
---|
115 |
|
---|
116 | /* public ActionResult Create()
|
---|
117 | {
|
---|
118 | var model = new BlogPostConsultation();
|
---|
119 | return View(model);
|
---|
120 | }*/
|
---|
121 |
|
---|
122 | public async Task<ActionResult> CreateAsync()
|
---|
123 | {
|
---|
124 |
|
---|
125 | // check for permission
|
---|
126 | //UsersClass customerClass = await checkAuthorizationAsync();
|
---|
127 | // ViewBag.isAuthenticated = await getCrrentUser();
|
---|
128 | UsersClass customerClass = await getCrrentUser();
|
---|
129 | // set if is authenticated
|
---|
130 | ViewBag.isAuthenticated = customerClass;
|
---|
131 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
132 |
|
---|
133 | return View();
|
---|
134 | }
|
---|
135 |
|
---|
136 | // POST: Customer/Create
|
---|
137 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
138 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
139 | [HttpPost]
|
---|
140 | [ValidateAntiForgeryToken]
|
---|
141 | public async Task<ActionResult> CreateAsync([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
|
---|
142 | {
|
---|
143 | if (ModelState.IsValid)
|
---|
144 | {
|
---|
145 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
146 | if (isAuthenticated)
|
---|
147 | {
|
---|
148 | var user = await _userManager.GetUserAsync(User);
|
---|
149 | var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
150 | //dodadeno na 25.08
|
---|
151 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
152 | if (customerClass != null)
|
---|
153 | {
|
---|
154 | // Поставете users_id на идентификаторот на корисникот
|
---|
155 | blogClass.users_id = customerClass.id;
|
---|
156 | //blogClass.date_askes = DateOnly.FromDateTime(DateTime.UtcNow);
|
---|
157 | blogClass.date_askes = DateOnly.FromDateTime(DateTime.Now); // Ова ќе стави локално време
|
---|
158 |
|
---|
159 | db.BlogPostControllerObj.Add(blogClass);
|
---|
160 | await db.SaveChangesAsync();
|
---|
161 | return RedirectToAction("Index");
|
---|
162 | }
|
---|
163 | }
|
---|
164 | else
|
---|
165 | {
|
---|
166 | return RedirectToAction("AccessDenied", "Error");
|
---|
167 | }
|
---|
168 | }
|
---|
169 |
|
---|
170 | return View(blogClass);
|
---|
171 | }
|
---|
172 |
|
---|
173 |
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 |
|
---|
179 | // GET: Customer/Edit/5
|
---|
180 | public async Task<ActionResult> EditAsync(int? id)
|
---|
181 | {
|
---|
182 | if (id == null)
|
---|
183 | {
|
---|
184 | return View(null);
|
---|
185 | //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
---|
186 | }
|
---|
187 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
188 | if (blogClass == null)
|
---|
189 | {
|
---|
190 | return RedirectToAction("NotExist", "Error");
|
---|
191 | }
|
---|
192 |
|
---|
193 | // izbriseno na 26.08
|
---|
194 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
195 | //dodadeno na 26.08
|
---|
196 | // UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
|
---|
197 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
198 | if (customerClass == null)
|
---|
199 | {
|
---|
200 |
|
---|
201 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
202 | if (isAuthenticated)
|
---|
203 | {
|
---|
204 | var user = await _userManager.GetUserAsync(User);
|
---|
205 | customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
206 | if (blogClass.users_id != customerClass.id)
|
---|
207 | {
|
---|
208 | return RedirectToAction("AccessDenied", "Error");
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 | // no access for standard user
|
---|
213 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
214 | ViewBag.OnlyAdminManager1 = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
215 |
|
---|
216 |
|
---|
217 | return View(blogClass);
|
---|
218 | }
|
---|
219 |
|
---|
220 | // POST: Customer/Edit/5
|
---|
221 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
222 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
223 | [HttpPost]
|
---|
224 | [ValidateAntiForgeryToken]
|
---|
225 | /* public ActionResult Edit([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
|
---|
226 | {
|
---|
227 | if (ModelState.IsValid)
|
---|
228 | {
|
---|
229 | db.Entry(blogClass).State = EntityState.Modified;
|
---|
230 | db.SaveChanges();
|
---|
231 | return RedirectToAction("Index");
|
---|
232 | }
|
---|
233 | return View(blogClass);
|
---|
234 | }*/
|
---|
235 |
|
---|
236 |
|
---|
237 | public async Task<ActionResult> EditAsync(int id, [Bind(include: "id,date_askes,title,description")] BlogPostConsultation blogClass)
|
---|
238 | {
|
---|
239 | if (ModelState.IsValid)
|
---|
240 | {
|
---|
241 | var existingBlogClass = await db.BlogPostControllerObj.FindAsync(id);
|
---|
242 | if (existingBlogClass != null)
|
---|
243 | {
|
---|
244 |
|
---|
245 |
|
---|
246 | // Запамтете ја старата вредност на users_id
|
---|
247 | blogClass.users_id = existingBlogClass.users_id;
|
---|
248 |
|
---|
249 | db.Entry(existingBlogClass).CurrentValues.SetValues(blogClass);
|
---|
250 | await db.SaveChangesAsync();
|
---|
251 | return RedirectToAction("Index");
|
---|
252 | }
|
---|
253 | }
|
---|
254 | return View(blogClass);
|
---|
255 | }
|
---|
256 |
|
---|
257 |
|
---|
258 |
|
---|
259 | // GET: Customer/Delete/5
|
---|
260 |
|
---|
261 | public async Task<ActionResult> DeleteAsync(int? id)
|
---|
262 | {
|
---|
263 | // UsersClass customerClass = await checkAuthorizationAsync();
|
---|
264 |
|
---|
265 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
266 |
|
---|
267 | if (id == null)
|
---|
268 | {
|
---|
269 | return View(null);
|
---|
270 | //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
---|
271 | }
|
---|
272 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
273 | if (blogClass == null)
|
---|
274 | {
|
---|
275 | return View(null);
|
---|
276 | //return HttpNotFound();
|
---|
277 | }
|
---|
278 | // check for permission
|
---|
279 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
280 | if (customerClass == null)
|
---|
281 | {
|
---|
282 |
|
---|
283 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
284 | if (isAuthenticated)
|
---|
285 | {
|
---|
286 | var user = await _userManager.GetUserAsync(User);
|
---|
287 | customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
288 | if (blogClass.users_id != customerClass.id)
|
---|
289 | {
|
---|
290 | return RedirectToAction("AccessDenied", "Error");
|
---|
291 | }
|
---|
292 | }
|
---|
293 | }
|
---|
294 | // no access for standard user
|
---|
295 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
296 |
|
---|
297 | return View(blogClass);
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | // POST: Customer/Delete/5
|
---|
302 |
|
---|
303 |
|
---|
304 | [HttpPost, ActionName("Delete")]
|
---|
305 | [ValidateAntiForgeryToken]
|
---|
306 | public ActionResult DeleteConfirmed(int id)
|
---|
307 | {
|
---|
308 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
309 | db.BlogPostControllerObj.Remove(blogClass);
|
---|
310 | db.SaveChanges();
|
---|
311 | return RedirectToAction("Index");
|
---|
312 | }
|
---|
313 |
|
---|
314 | // GET: Customer/Delete/5
|
---|
315 | // GET: Customer/Delete/5
|
---|
316 | // GET: Customer/Delete/5
|
---|
317 |
|
---|
318 |
|
---|
319 |
|
---|
320 | protected override void Dispose(bool disposing)
|
---|
321 | {
|
---|
322 | if (disposing)
|
---|
323 | {
|
---|
324 | db.Dispose();
|
---|
325 | }
|
---|
326 | base.Dispose(disposing);
|
---|
327 | }
|
---|
328 | }
|
---|
329 | }
|
---|