Changeset 72b1da2 for PostgreSqlDotnetCore/Controllers/BlogController.cs
- Timestamp:
- 08/23/24 03:03:32 (3 months ago)
- Branches:
- main
- Children:
- 118e414
- Parents:
- 57fc402
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
PostgreSqlDotnetCore/Controllers/BlogController.cs
r57fc402 r72b1da2 30 30 */ 31 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 } 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 50 79 // GET: Customer/Details/5 51 80 public async Task<ActionResult> DetailsAsync(int? id) … … 100 129 [HttpPost] 101 130 [ValidateAntiForgeryToken] 102 103 104 105 106 107 108 109 110 111 112 113 114 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; 115 144 //blogClass.date_askes = DateOnly.FromDateTime(DateTime.UtcNow); 116 145 blogClass.date_askes = DateOnly.FromDateTime(DateTime.Now); // Ова ќе стави локално време 117 146 118 147 db.BlogPostControllerObj.Add(blogClass); 119 await db.SaveChangesAsync(); 120 return RedirectToAction("Index"); 121 } 122 } 123 else 124 { 125 return RedirectToAction("AccessDenied", "Error"); 126 } 127 } 128 129 return View(blogClass); 130 } 131 132 133 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 134 164 135 165 … … 151 181 // check for permission 152 182 UsersClass customerClass = await checkAuthorizationAsync(); 183 //dodadeno na 23.08 184 ViewBag.isAuthenticated = await getCrrentUser(); 153 185 if (customerClass == null) 154 186 { … … 174 206 [HttpPost] 175 207 [ValidateAntiForgeryToken] 176 public ActionResult Edit([Bind(include: "id,date_askes,title,description,users_id")] BlogPostConsultation blogClass) 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) 177 221 { 178 222 if (ModelState.IsValid) 179 223 { 180 db.Entry(blogClass).State = EntityState.Modified; 181 db.SaveChanges(); 182 return RedirectToAction("Index"); 183 } 184 return View(blogClass); 185 } 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 186 239 187 240 // GET: Customer/Delete/5 188 public async Task<ActionResult> DeleteAsync(int? id) 189 { 241 public async Task<ActionResult> DeleteAsync(int? id) { 242 // UsersClass customerClass = await checkAuthorizationAsync(); 243 244 ViewBag.isAuthenticated = await getCrrentUser(); 245 190 246 if (id == null) 191 247 {
Note:
See TracChangeset
for help on using the changeset viewer.