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