1 | using Microsoft.AspNetCore.Identity;
|
---|
2 | using Microsoft.AspNetCore.Mvc;
|
---|
3 | using Microsoft.EntityFrameworkCore;
|
---|
4 | using PostgreSqlDotnetCore.Models;
|
---|
5 | using System.Net;
|
---|
6 |
|
---|
7 | namespace 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 | public async Task<ActionResult> IndexAsync()
|
---|
18 | {
|
---|
19 | //return View(Enumerable.Empty<UsersClass>());
|
---|
20 | UsersClass customerClass = await getCrrentUser();
|
---|
21 |
|
---|
22 | // set if is authenticated
|
---|
23 | ViewBag.isAuthenticated = customerClass;
|
---|
24 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
25 | return View(db.BlogPostAnswersObj.ToList());
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 |
|
---|
30 |
|
---|
31 | // GET: Customer/Details/5
|
---|
32 | // public ActionResult Details(int? id)
|
---|
33 | public async Task<ActionResult> Details(int? id)
|
---|
34 | {
|
---|
35 | if (id == null)
|
---|
36 | {
|
---|
37 | return RedirectToAction("NotExist", "Error");
|
---|
38 | }
|
---|
39 | UsersClass customerClass = await getCrrentUser(); // Добијте ја тековната улога на корисникот
|
---|
40 | ViewBag.isAuthenticated = customerClass;
|
---|
41 | BlogPostAnswers answerClass = db.BlogPostAnswersObj.Find(id);
|
---|
42 | if (answerClass == null)
|
---|
43 | {
|
---|
44 | return RedirectToAction("NotExist", "Error");
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | // query
|
---|
49 | var query = from st in db.BlogPostAnswersObj
|
---|
50 | where st.parent_id == answerClass.id
|
---|
51 | select st;
|
---|
52 | //elenaaa
|
---|
53 | var answersUnderA = query.Where(x => x.id != answerClass.id).ToList();
|
---|
54 |
|
---|
55 | answerClass.blogPostAnswers = answersUnderA;
|
---|
56 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
57 | return View(answerClass);
|
---|
58 | }
|
---|
59 |
|
---|
60 | // GET: Customer/Create
|
---|
61 | //public ActionResult Create()
|
---|
62 | //{
|
---|
63 | // return View();
|
---|
64 | //}
|
---|
65 |
|
---|
66 | public async Task<ActionResult> CreateAsync()
|
---|
67 | {
|
---|
68 |
|
---|
69 | // check for permission
|
---|
70 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
71 | // UsersClass customerClass = await getCrrentUser();
|
---|
72 | // set if is authenticated
|
---|
73 | ViewBag.isAuthenticated = customerClass;
|
---|
74 | if (customerClass == null)
|
---|
75 | {
|
---|
76 | return RedirectToAction("AccessDenied", "Error");
|
---|
77 | }
|
---|
78 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
79 | return View();
|
---|
80 | }
|
---|
81 |
|
---|
82 | // POST: Customer/Create
|
---|
83 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
84 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
85 | [HttpPost]
|
---|
86 | [ValidateAntiForgeryToken]
|
---|
87 | public async Task<ActionResult> CreateAsync(int? id, int? parentId, [Bind(include: "reply")] BlogPostAnswers answerClass)
|
---|
88 | {
|
---|
89 |
|
---|
90 | //string id = Request.Query["BlogId"];
|
---|
91 | if (id == null && id > 0)
|
---|
92 | {
|
---|
93 | return RedirectToAction("NotExist", "Error");
|
---|
94 | }
|
---|
95 | //string id = Request.Query["BlogId"];
|
---|
96 | if (parentId== null && parentId > 0)
|
---|
97 | {
|
---|
98 | return RedirectToAction("NotExist", "Error");
|
---|
99 | }
|
---|
100 | if (answerClass != null && answerClass.reply.Length > 0)
|
---|
101 | {
|
---|
102 |
|
---|
103 | var user = await _userManager.GetUserAsync(User);
|
---|
104 | if (user == null)
|
---|
105 | {
|
---|
106 | return RedirectToAction("AccessDenied", "Error");
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
111 | answerClass.usersid = customerClass.id;
|
---|
112 | answerClass.BlogPostConsultationid = (int)id;
|
---|
113 | answerClass.parent_id = (int)parentId;
|
---|
114 | db.BlogPostAnswersObj.Add(answerClass);
|
---|
115 | db.SaveChanges();
|
---|
116 | //return RedirectToAction("Index");
|
---|
117 | return RedirectToAction("Details", "Blog", new { id });
|
---|
118 | }
|
---|
119 | ViewBag.OnlyAdminManager = await checkAuthorizationSpecificRoleAsync(RoleConstants.Admin) ?? await checkAuthorizationSpecificRoleAsync(RoleConstants.Manager);
|
---|
120 |
|
---|
121 | return View(answerClass);
|
---|
122 | }
|
---|
123 |
|
---|
124 | // GET: Customer/Edit/5
|
---|
125 | public async Task<ActionResult> EditAsync(int? id)
|
---|
126 | {
|
---|
127 | if (id == null)
|
---|
128 | {
|
---|
129 | return RedirectToAction("NotExist", "Error");
|
---|
130 | }
|
---|
131 | BlogPostAnswers answerClass = db.BlogPostAnswersObj.Find(id);
|
---|
132 | if (answerClass == null)
|
---|
133 | {
|
---|
134 | return RedirectToAction("NotExist", "Error");
|
---|
135 | }
|
---|
136 |
|
---|
137 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
138 | if (isAuthenticated)
|
---|
139 | {
|
---|
140 | var user = await _userManager.GetUserAsync(User);
|
---|
141 | var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
142 | if (answerClass.usersid != customerClass.id)
|
---|
143 | {
|
---|
144 | return RedirectToAction("AccessDenied", "Error");
|
---|
145 | }
|
---|
146 | }
|
---|
147 | return View(answerClass);
|
---|
148 | }
|
---|
149 |
|
---|
150 | // POST: Customer/Edit/5
|
---|
151 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
152 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
153 | [HttpPost]
|
---|
154 | [ValidateAntiForgeryToken]
|
---|
155 | public ActionResult Edit([Bind(include: "id,parent_id,reply,root_post,usersID")] BlogPostAnswers answerClass)
|
---|
156 | {
|
---|
157 |
|
---|
158 | BlogPostAnswers answerClassDB = db.BlogPostAnswersObj.Find(answerClass.id);
|
---|
159 | if (answerClassDB != null && !answerClassDB.reply.Equals(answerClass.reply))
|
---|
160 | {
|
---|
161 | answerClassDB.reply = answerClass.reply;
|
---|
162 | answerClassDB.parent_id = answerClass.parent_id;
|
---|
163 | db.Entry(answerClassDB).State = EntityState.Modified;
|
---|
164 | db.SaveChanges();
|
---|
165 | //return RedirectToAction("Index");
|
---|
166 | int id = answerClassDB.BlogPostConsultationid;
|
---|
167 | return RedirectToAction("Details", "Blog", new { id });
|
---|
168 | }
|
---|
169 | return View(answerClassDB);
|
---|
170 | }
|
---|
171 |
|
---|
172 | // GET: Customer/Delete/5
|
---|
173 | public async Task<ActionResult> DeleteAsync(int? id)
|
---|
174 | {
|
---|
175 |
|
---|
176 | if (id == null)
|
---|
177 | {
|
---|
178 | return RedirectToAction("NotExist", "Error");
|
---|
179 | }
|
---|
180 | BlogPostAnswers answerClass = db.BlogPostAnswersObj.Find(id);
|
---|
181 | if (answerClass == null)
|
---|
182 | {
|
---|
183 | return RedirectToAction("NotExist", "Error");
|
---|
184 | }
|
---|
185 |
|
---|
186 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
187 | if (isAuthenticated)
|
---|
188 | {
|
---|
189 | var user = await _userManager.GetUserAsync(User);
|
---|
190 | var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
191 | if (answerClass.usersid != customerClass.id)
|
---|
192 | {
|
---|
193 | return RedirectToAction("AccessDenied", "Error");
|
---|
194 | }
|
---|
195 | }
|
---|
196 | // return View(answerClass);
|
---|
197 | return View(answerClass);
|
---|
198 | }
|
---|
199 |
|
---|
200 | // POST: Customer/Delete/5
|
---|
201 | [HttpPost, ActionName("Delete")]
|
---|
202 | [ValidateAntiForgeryToken]
|
---|
203 | public ActionResult DeleteConfirmed(int id)
|
---|
204 | {
|
---|
205 | BlogPostAnswers answerClass = db.BlogPostAnswersObj.Find(id);
|
---|
206 | db.BlogPostAnswersObj.Remove(answerClass);
|
---|
207 | db.SaveChanges();
|
---|
208 | return RedirectToAction("Index");
|
---|
209 | }
|
---|
210 |
|
---|
211 | protected override void Dispose(bool disposing)
|
---|
212 | {
|
---|
213 | if (disposing)
|
---|
214 | {
|
---|
215 | db.Dispose();
|
---|
216 | }
|
---|
217 | base.Dispose(disposing);
|
---|
218 | }
|
---|
219 | }
|
---|
220 | }
|
---|