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 | ViewBag.CurrentUserId = customerClass?.id;
|
---|
71 |
|
---|
72 | return View(blogPosts);
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 |
|
---|
77 |
|
---|
78 |
|
---|
79 | // GET: Customer/Details/5
|
---|
80 | public async Task<ActionResult> DetailsAsync(int? id)
|
---|
81 | {
|
---|
82 | if (id == null)
|
---|
83 | {
|
---|
84 | return View(null);
|
---|
85 | //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
---|
86 | }
|
---|
87 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
88 | if (blogClass == null)
|
---|
89 | {
|
---|
90 | return RedirectToAction("NotExist", "Error");
|
---|
91 | }
|
---|
92 | // get answers
|
---|
93 |
|
---|
94 | // query
|
---|
95 | var query = from st in db.BlogPostAnswersObj
|
---|
96 | where st.BlogPostConsultationid == blogClass.id
|
---|
97 | select st;
|
---|
98 | //elenaaa
|
---|
99 | var blogAnswers = query.ToList();
|
---|
100 | blogClass.BlogPostAnswers = blogAnswers;
|
---|
101 | return View(blogClass);
|
---|
102 | }
|
---|
103 |
|
---|
104 | // GET: Customer/Create
|
---|
105 | //public ActionResult Create()
|
---|
106 | //{
|
---|
107 | // return View();
|
---|
108 | //}
|
---|
109 |
|
---|
110 | /* public ActionResult Create()
|
---|
111 | {
|
---|
112 | var model = new BlogPostConsultation();
|
---|
113 | return View(model);
|
---|
114 | }*/
|
---|
115 |
|
---|
116 | public async Task<ActionResult> CreateAsync()
|
---|
117 | {
|
---|
118 |
|
---|
119 | // check for permission
|
---|
120 | UsersClass customerClass = await getCrrentUser();
|
---|
121 | // set if is authenticated
|
---|
122 | ViewBag.isAuthenticated = customerClass;
|
---|
123 | return View();
|
---|
124 | }
|
---|
125 |
|
---|
126 | // POST: Customer/Create
|
---|
127 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
128 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
129 | [HttpPost]
|
---|
130 | [ValidateAntiForgeryToken]
|
---|
131 | public async Task<ActionResult> CreateAsync([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
|
---|
132 | {
|
---|
133 | if (ModelState.IsValid)
|
---|
134 | {
|
---|
135 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
136 | if (isAuthenticated)
|
---|
137 | {
|
---|
138 | var user = await _userManager.GetUserAsync(User);
|
---|
139 | var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
140 | if (customerClass != null)
|
---|
141 | {
|
---|
142 | // Поставете users_id на идентификаторот на корисникот
|
---|
143 | blogClass.users_id = customerClass.id;
|
---|
144 | //blogClass.date_askes = DateOnly.FromDateTime(DateTime.UtcNow);
|
---|
145 | blogClass.date_askes = DateOnly.FromDateTime(DateTime.Now); // Ова ќе стави локално време
|
---|
146 |
|
---|
147 | db.BlogPostControllerObj.Add(blogClass);
|
---|
148 | await db.SaveChangesAsync();
|
---|
149 | return RedirectToAction("Index");
|
---|
150 | }
|
---|
151 | }
|
---|
152 | else
|
---|
153 | {
|
---|
154 | return RedirectToAction("AccessDenied", "Error");
|
---|
155 | }
|
---|
156 | }
|
---|
157 |
|
---|
158 | return View(blogClass);
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 |
|
---|
163 |
|
---|
164 |
|
---|
165 |
|
---|
166 |
|
---|
167 | // GET: Customer/Edit/5
|
---|
168 | public async Task<ActionResult> EditAsync(int? id)
|
---|
169 | {
|
---|
170 | if (id == null)
|
---|
171 | {
|
---|
172 | return View(null);
|
---|
173 | //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
---|
174 | }
|
---|
175 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
176 | if (blogClass == null)
|
---|
177 | {
|
---|
178 | return RedirectToAction("NotExist", "Error");
|
---|
179 | }
|
---|
180 |
|
---|
181 | // check for permission
|
---|
182 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
183 | //dodadeno na 23.08
|
---|
184 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
185 | if (customerClass == null)
|
---|
186 | {
|
---|
187 |
|
---|
188 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
189 | if (isAuthenticated)
|
---|
190 | {
|
---|
191 | var user = await _userManager.GetUserAsync(User);
|
---|
192 | customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
193 | if (blogClass.users_id != customerClass.id)
|
---|
194 | {
|
---|
195 | return RedirectToAction("AccessDenied", "Error");
|
---|
196 | }
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | return View(blogClass);
|
---|
201 | }
|
---|
202 |
|
---|
203 | // POST: Customer/Edit/5
|
---|
204 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
205 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
206 | [HttpPost]
|
---|
207 | [ValidateAntiForgeryToken]
|
---|
208 | /* public ActionResult Edit([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
|
---|
209 | {
|
---|
210 | if (ModelState.IsValid)
|
---|
211 | {
|
---|
212 | db.Entry(blogClass).State = EntityState.Modified;
|
---|
213 | db.SaveChanges();
|
---|
214 | return RedirectToAction("Index");
|
---|
215 | }
|
---|
216 | return View(blogClass);
|
---|
217 | }*/
|
---|
218 |
|
---|
219 |
|
---|
220 | public async Task<ActionResult> EditAsync(int id, [Bind(include: "id,date_askes,title,description")] BlogPostConsultation blogClass)
|
---|
221 | {
|
---|
222 | if (ModelState.IsValid)
|
---|
223 | {
|
---|
224 | var existingBlogClass = await db.BlogPostControllerObj.FindAsync(id);
|
---|
225 | if (existingBlogClass != null)
|
---|
226 | {
|
---|
227 | // Запамтете ја старата вредност на users_id
|
---|
228 | blogClass.users_id = existingBlogClass.users_id;
|
---|
229 |
|
---|
230 | db.Entry(existingBlogClass).CurrentValues.SetValues(blogClass);
|
---|
231 | await db.SaveChangesAsync();
|
---|
232 | return RedirectToAction("Index");
|
---|
233 | }
|
---|
234 | }
|
---|
235 | return View(blogClass);
|
---|
236 | }
|
---|
237 |
|
---|
238 |
|
---|
239 |
|
---|
240 | // GET: Customer/Delete/5
|
---|
241 | public async Task<ActionResult> DeleteAsync(int? id) {
|
---|
242 | // UsersClass customerClass = await checkAuthorizationAsync();
|
---|
243 |
|
---|
244 | ViewBag.isAuthenticated = await getCrrentUser();
|
---|
245 |
|
---|
246 | if (id == null)
|
---|
247 | {
|
---|
248 | return View(null);
|
---|
249 | //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
---|
250 | }
|
---|
251 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
252 | if (blogClass == null)
|
---|
253 | {
|
---|
254 | return View(null);
|
---|
255 | //return HttpNotFound();
|
---|
256 | }
|
---|
257 | // check for permission
|
---|
258 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
259 | if (customerClass == null)
|
---|
260 | {
|
---|
261 |
|
---|
262 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
263 | if (isAuthenticated)
|
---|
264 | {
|
---|
265 | var user = await _userManager.GetUserAsync(User);
|
---|
266 | customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
267 | if (blogClass.users_id != customerClass.id)
|
---|
268 | {
|
---|
269 | return RedirectToAction("AccessDenied", "Error");
|
---|
270 | }
|
---|
271 | }
|
---|
272 | }
|
---|
273 | return View(blogClass);
|
---|
274 | }
|
---|
275 |
|
---|
276 | // POST: Customer/Delete/5
|
---|
277 |
|
---|
278 |
|
---|
279 | [HttpPost, ActionName("Delete")]
|
---|
280 | [ValidateAntiForgeryToken]
|
---|
281 | public ActionResult DeleteConfirmed(int id)
|
---|
282 | {
|
---|
283 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
284 | db.BlogPostControllerObj.Remove(blogClass);
|
---|
285 | db.SaveChanges();
|
---|
286 | return RedirectToAction("Index");
|
---|
287 | }
|
---|
288 |
|
---|
289 | protected override void Dispose(bool disposing)
|
---|
290 | {
|
---|
291 | if (disposing)
|
---|
292 | {
|
---|
293 | db.Dispose();
|
---|
294 | }
|
---|
295 | base.Dispose(disposing);
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|