source: PostgreSqlDotnetCore/Controllers/BlogPostAnswersController.cs@ a850333

main
Last change on this file since a850333 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: 7.8 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 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 if (ModelState.IsValid)
158 {
159 db.Entry(answerClass).State = EntityState.Modified;
160 db.SaveChanges();
161 return RedirectToAction("Index");
162 }
163 return View(answerClass);
164 }
165
166 // GET: Customer/Delete/5
167 public async Task<ActionResult> DeleteAsync(int? id)
168 {
169
170 if (id == null)
171 {
172 return RedirectToAction("NotExist", "Error");
173 }
174 BlogPostAnswers answerClass = db.BlogPostAnswersObj.Find(id);
175 if (answerClass == null)
176 {
177 return RedirectToAction("NotExist", "Error");
178 }
179
180 bool isAuthenticated = User.Identity.IsAuthenticated;
181 if (isAuthenticated)
182 {
183 var user = await _userManager.GetUserAsync(User);
184 var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
185 if (answerClass.usersid != customerClass.id)
186 {
187 return RedirectToAction("AccessDenied", "Error");
188 }
189 }
190 // return View(answerClass);
191 return View(answerClass);
192 }
193
194 // POST: Customer/Delete/5
195 [HttpPost, ActionName("Delete")]
196 [ValidateAntiForgeryToken]
197 public ActionResult DeleteConfirmed(int id)
198 {
199 BlogPostAnswers answerClass = db.BlogPostAnswersObj.Find(id);
200 db.BlogPostAnswersObj.Remove(answerClass);
201 db.SaveChanges();
202 return RedirectToAction("Index");
203 }
204
205 protected override void Dispose(bool disposing)
206 {
207 if (disposing)
208 {
209 db.Dispose();
210 }
211 base.Dispose(disposing);
212 }
213 }
214}
Note: See TracBrowser for help on using the repository browser.