source: PostgreSqlDotnetCore/Controllers/BlogController.cs@ e9bb9d1

main
Last change on this file since e9bb9d1 was e9bb9d1, checked in by ElenaMoskova <elena.moskova99@…>, 3 months ago

Use of views

  1. Use of views in VetCenters.
  2. Ability to provide a response to a given response.
  • Property mode set to 100644
File size: 11.8 KB
RevLine 
[2aea0fd]1using Microsoft.AspNetCore.Identity;
2using Microsoft.AspNetCore.Mvc;
3using Microsoft.EntityFrameworkCore;
4using PostgreSqlDotnetCore.Data;
5using PostgreSqlDotnetCore.Models;
6using System.Net;
7
8namespace PostgreSqlDotnetCore.Controllers
9{
10 public class BlogController : BaseController
11 {
12 public BlogController(UserManager<IdentityUser> userManager) : base(userManager)
[d6040ef]13
[2aea0fd]14 {
[118e414]15
[2aea0fd]16 }
17
18 // GET: Customer
[d6040ef]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
[72b1da2]32 /* public async Task<ActionResult> Index()
33 {
34 // Проверка за автентикација
35 bool isAuthenticated = User.Identity.IsAuthenticated;
[d6040ef]36
[72b1da2]37 if (!isAuthenticated)
38 {
39 return RedirectToAction("AccessDenied", "Error");
40 }
[2aea0fd]41
[72b1da2]42 // Список на блог постови
43 var blogPosts = await db.BlogPostControllerObj.ToListAsync();
44
45 // Предавање на ViewBag за проверка на автентикација
46 ViewBag.isAuthenticated = isAuthenticated;
47
48 return View(blogPosts);
49 }*/
50
[118e414]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);
[72b1da2]67
[118e414]68 // Предавање на ViewBag за проверка на автентикација и корисничкиот ID
69 ViewBag.isAuthenticated = isAuthenticated;
70 // no access for standard user
71 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
[72b1da2]72
[118e414]73 ViewBag.CurrentUserId = customerClass?.id;
[72b1da2]74
[118e414]75 return View(blogPosts);
76 }
[72b1da2]77
78
79
[d6040ef]80
81
[2aea0fd]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);
[e9bb9d1]91 UsersClass customerClass = await getCrrentUser();
92 ViewBag.isAuthenticated = customerClass;
[2aea0fd]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
[8f8226c]101 where st.BlogPostConsultationid == blogClass.id
[2aea0fd]102 select st;
103 //elenaaa
104 var blogAnswers = query.ToList();
105 blogClass.BlogPostAnswers = blogAnswers;
[e9bb9d1]106 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
[2aea0fd]107 return View(blogClass);
108 }
109
110 // GET: Customer/Create
111 //public ActionResult Create()
112 //{
113 // return View();
114 //}
115
[57fc402]116 /* public ActionResult Create()
117 {
118 var model = new BlogPostConsultation();
119 return View(model);
120 }*/
121
122 public async Task<ActionResult> CreateAsync()
[2aea0fd]123 {
[57fc402]124
125 // check for permission
[e9bb9d1]126 //UsersClass customerClass = await checkAuthorizationAsync();
127 // ViewBag.isAuthenticated = await getCrrentUser();
128 UsersClass customerClass = await getCrrentUser();
[57fc402]129 // set if is authenticated
130 ViewBag.isAuthenticated = customerClass;
[118e414]131 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
132
[57fc402]133 return View();
[2aea0fd]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]
[72b1da2]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);
[e9bb9d1]150 //dodadeno na 25.08
151 ViewBag.isAuthenticated = await getCrrentUser();
[72b1da2]152 if (customerClass != null)
153 {
154 // Поставете users_id на идентификаторот на корисникот
155 blogClass.users_id = customerClass.id;
[57fc402]156 //blogClass.date_askes = DateOnly.FromDateTime(DateTime.UtcNow);
157 blogClass.date_askes = DateOnly.FromDateTime(DateTime.Now); // Ова ќе стави локално време
158
159 db.BlogPostControllerObj.Add(blogClass);
[72b1da2]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
[57fc402]174
175
176
[2aea0fd]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
[e9bb9d1]193 // izbriseno na 26.08
194 UsersClass customerClass = await checkAuthorizationAsync();
195 //dodadeno na 26.08
196 // UsersClass customerClass = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin);
[72b1da2]197 ViewBag.isAuthenticated = await getCrrentUser();
[2aea0fd]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 }
[118e414]212 // no access for standard user
213 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
[e9bb9d1]214 ViewBag.OnlyAdminManager1 = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
[118e414]215
[2aea0fd]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]
[72b1da2]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
[118e414]236
[72b1da2]237 public async Task<ActionResult> EditAsync(int id, [Bind(include: "id,date_askes,title,description")] BlogPostConsultation blogClass)
[2aea0fd]238 {
239 if (ModelState.IsValid)
240 {
[72b1da2]241 var existingBlogClass = await db.BlogPostControllerObj.FindAsync(id);
242 if (existingBlogClass != null)
243 {
[e9bb9d1]244
245
[72b1da2]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 }
[2aea0fd]253 }
254 return View(blogClass);
255 }
256
[72b1da2]257
258
[2aea0fd]259 // GET: Customer/Delete/5
[e9bb9d1]260
[118e414]261 public async Task<ActionResult> DeleteAsync(int? id)
262 {
263 // UsersClass customerClass = await checkAuthorizationAsync();
264
265 ViewBag.isAuthenticated = await getCrrentUser();
[72b1da2]266
[2aea0fd]267 if (id == null)
[118e414]268 {
269 return View(null);
270 //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
[2aea0fd]271 }
[118e414]272 BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
[2aea0fd]273 if (blogClass == null)
274 {
275 return View(null);
[118e414]276 //return HttpNotFound();
[2aea0fd]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 }
[118e414]294 // no access for standard user
295 ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
296
[2aea0fd]297 return View(blogClass);
298 }
299
[e9bb9d1]300
[2aea0fd]301 // POST: Customer/Delete/5
[118e414]302
[2aea0fd]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 }
[e9bb9d1]313
314 // GET: Customer/Delete/5
315 // GET: Customer/Delete/5
316 // GET: Customer/Delete/5
317
318
[2aea0fd]319
320 protected override void Dispose(bool disposing)
321 {
322 if (disposing)
323 {
324 db.Dispose();
325 }
326 base.Dispose(disposing);
327 }
328 }
329}
Note: See TracBrowser for help on using the repository browser.