[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)
|
---|
| 13 | {
|
---|
| 14 | }
|
---|
| 15 |
|
---|
| 16 | // GET: Customer
|
---|
| 17 | public async Task<ActionResult> IndexAsync()
|
---|
| 18 | {
|
---|
| 19 | // check for permission
|
---|
| 20 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 21 | if (!isAuthenticated)
|
---|
| 22 | {
|
---|
| 23 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 24 | }
|
---|
| 25 | //return View(Enumerable.Empty<UsersClass>());
|
---|
| 26 | return View(db.BlogPostControllerObj.ToList());
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | // GET: Customer/Details/5
|
---|
| 30 | public async Task<ActionResult> DetailsAsync(int? id)
|
---|
| 31 | {
|
---|
| 32 | if (id == null)
|
---|
| 33 | {
|
---|
| 34 | return View(null);
|
---|
| 35 | //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
---|
| 36 | }
|
---|
| 37 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
| 38 | if (blogClass == null)
|
---|
| 39 | {
|
---|
| 40 | return RedirectToAction("NotExist", "Error");
|
---|
| 41 | }
|
---|
| 42 | // get answers
|
---|
| 43 |
|
---|
| 44 | // query
|
---|
| 45 | var query = from st in db.BlogPostAnswersObj
|
---|
| 46 | where st.blogpostconsultationid == blogClass.id
|
---|
| 47 | select st;
|
---|
| 48 | //elenaaa
|
---|
| 49 | var blogAnswers = query.ToList();
|
---|
| 50 | blogClass.BlogPostAnswers = blogAnswers;
|
---|
| 51 | return View(blogClass);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | // GET: Customer/Create
|
---|
| 55 | //public ActionResult Create()
|
---|
| 56 | //{
|
---|
| 57 | // return View();
|
---|
| 58 | //}
|
---|
| 59 |
|
---|
| 60 | public ActionResult Create()
|
---|
| 61 | {
|
---|
[2639fab] | 62 | var model = new BlogPostConsultation();
|
---|
[2aea0fd] | 63 | return View(model);
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | // POST: Customer/Create
|
---|
| 67 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
| 68 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
| 69 | [HttpPost]
|
---|
| 70 | [ValidateAntiForgeryToken]
|
---|
| 71 | public async Task<ActionResult> CreateAsync([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
|
---|
| 72 | {
|
---|
| 73 | if (ModelState.IsValid)
|
---|
| 74 | {
|
---|
| 75 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 76 | if (isAuthenticated)
|
---|
| 77 | {
|
---|
| 78 | var user = await _userManager.GetUserAsync(User);
|
---|
| 79 | var customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
| 80 | blogClass.users_id = customerClass.id;
|
---|
| 81 | db.BlogPostControllerObj.Add(blogClass);
|
---|
| 82 | db.SaveChanges();
|
---|
| 83 | return RedirectToAction("Index");
|
---|
| 84 | }
|
---|
| 85 | else
|
---|
| 86 | {
|
---|
| 87 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 88 | }
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 | return View(blogClass);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | // GET: Customer/Edit/5
|
---|
| 95 | public async Task<ActionResult> EditAsync(int? id)
|
---|
| 96 | {
|
---|
| 97 | if (id == null)
|
---|
| 98 | {
|
---|
| 99 | return View(null);
|
---|
| 100 | //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
---|
| 101 | }
|
---|
| 102 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
| 103 | if (blogClass == null)
|
---|
| 104 | {
|
---|
| 105 | return RedirectToAction("NotExist", "Error");
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | // check for permission
|
---|
| 109 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
| 110 | if (customerClass == null)
|
---|
| 111 | {
|
---|
| 112 |
|
---|
| 113 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 114 | if (isAuthenticated)
|
---|
| 115 | {
|
---|
| 116 | var user = await _userManager.GetUserAsync(User);
|
---|
| 117 | customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
| 118 | if (blogClass.users_id != customerClass.id)
|
---|
| 119 | {
|
---|
| 120 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | return View(blogClass);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | // POST: Customer/Edit/5
|
---|
| 129 | // To protect from overposting attacks, enable the specific properties you want to bind to, for
|
---|
| 130 | // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
|
---|
| 131 | [HttpPost]
|
---|
| 132 | [ValidateAntiForgeryToken]
|
---|
| 133 | public ActionResult Edit([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass)
|
---|
| 134 | {
|
---|
| 135 | if (ModelState.IsValid)
|
---|
| 136 | {
|
---|
| 137 | db.Entry(blogClass).State = EntityState.Modified;
|
---|
| 138 | db.SaveChanges();
|
---|
| 139 | return RedirectToAction("Index");
|
---|
| 140 | }
|
---|
| 141 | return View(blogClass);
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | // GET: Customer/Delete/5
|
---|
| 145 | public async Task<ActionResult> DeleteAsync(int? id)
|
---|
| 146 | {
|
---|
| 147 | if (id == null)
|
---|
| 148 | {
|
---|
| 149 | return View(null);
|
---|
| 150 | //return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
---|
| 151 | }
|
---|
| 152 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
| 153 | if (blogClass == null)
|
---|
| 154 | {
|
---|
| 155 | return View(null);
|
---|
| 156 | //return HttpNotFound();
|
---|
| 157 | }
|
---|
| 158 | // check for permission
|
---|
| 159 | UsersClass customerClass = await checkAuthorizationAsync();
|
---|
| 160 | if (customerClass == null)
|
---|
| 161 | {
|
---|
| 162 |
|
---|
| 163 | bool isAuthenticated = User.Identity.IsAuthenticated;
|
---|
| 164 | if (isAuthenticated)
|
---|
| 165 | {
|
---|
| 166 | var user = await _userManager.GetUserAsync(User);
|
---|
| 167 | customerClass = db.CustomerObj.SingleOrDefault(x => x.email == user.Email);
|
---|
| 168 | if (blogClass.users_id != customerClass.id)
|
---|
| 169 | {
|
---|
| 170 | return RedirectToAction("AccessDenied", "Error");
|
---|
| 171 | }
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 | return View(blogClass);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | // POST: Customer/Delete/5
|
---|
| 178 |
|
---|
| 179 |
|
---|
| 180 | [HttpPost, ActionName("Delete")]
|
---|
| 181 | [ValidateAntiForgeryToken]
|
---|
| 182 | public ActionResult DeleteConfirmed(int id)
|
---|
| 183 | {
|
---|
| 184 | BlogPostConsultation blogClass = db.BlogPostControllerObj.Find(id);
|
---|
| 185 | db.BlogPostControllerObj.Remove(blogClass);
|
---|
| 186 | db.SaveChanges();
|
---|
| 187 | return RedirectToAction("Index");
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | protected override void Dispose(bool disposing)
|
---|
| 191 | {
|
---|
| 192 | if (disposing)
|
---|
| 193 | {
|
---|
| 194 | db.Dispose();
|
---|
| 195 | }
|
---|
| 196 | base.Dispose(disposing);
|
---|
| 197 | }
|
---|
| 198 | }
|
---|
| 199 | }
|
---|