source: PostgreSqlDotnetCore/Controllers/BlogPostAnswersController.cs@ 2aea0fd

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

init commit Elena

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