Skip to content

Commit 8223d6e

Browse files
authored
Update BlogController.cs
1 parent e195362 commit 8223d6e

1 file changed

Lines changed: 49 additions & 52 deletions

File tree

Controller/BlogController.cs

Lines changed: 49 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Microsoft.AspNetCore.Mvc;
2-
using Microsoft.EntityFrameworkCore; // Required for FindAsync and other EF methods
2+
using Microsoft.EntityFrameworkCore;
33
using MyBlog.Data;
44
using MyBlog.Models;
55

@@ -15,97 +15,94 @@ public BlogController(ApplicationDbContext context)
1515
}
1616

1717
// READ: Display all posts
18-
public IActionResult Index()
18+
public async Task<IActionResult> Index()
1919
{
20-
var posts = _context.BlogPosts.OrderByDescending(p => p.CreatedAt).ToList();
20+
// OPTIMIZATION: Changed ToList() to ToListAsync()
21+
var posts = await _context.BlogPosts.OrderByDescending(p => p.CreatedAt).ToListAsync();
2122
return View(posts);
2223
}
23-
24+
2425
// READ: Display a single post (Details)
2526
public async Task<IActionResult> Details(int? id)
2627
{
27-
if (id == null)
28-
{
29-
return NotFound();
30-
}
28+
if (id == null) return NotFound();
3129

32-
// Find the post by ID
3330
var post = await _context.BlogPosts.FindAsync(id);
34-
35-
if (post == null)
36-
{
37-
return NotFound();
38-
}
31+
32+
if (post == null) return NotFound();
3933

4034
return View(post);
4135
}
4236

43-
// CREATE: GET action to display the creation form
37+
// CREATE: GET
4438
[HttpGet]
4539
public IActionResult Create()
4640
{
4741
return View();
4842
}
4943

50-
// CREATE: POST action to handle form submission
44+
// CREATE: POST
5145
[HttpPost]
5246
[ValidateAntiForgeryToken]
53-
public IActionResult Create(BlogPost post)
47+
public async Task<IActionResult> Create(BlogPost post)
5448
{
5549
if (ModelState.IsValid)
5650
{
5751
post.CreatedAt = DateTime.UtcNow;
5852

5953
_context.BlogPosts.Add(post);
60-
_context.SaveChanges();
54+
// OPTIMIZATION: Changed SaveChanges() to SaveChangesAsync()
55+
await _context.SaveChangesAsync();
6156

6257
return RedirectToAction(nameof(Index));
6358
}
6459

6560
return View(post);
6661
}
6762

68-
// UPDATE: GET action to display the edit form
63+
// UPDATE: GET
6964
[HttpGet]
7065
public async Task<IActionResult> Edit(int? id)
7166
{
72-
if (id == null)
73-
{
74-
return NotFound();
75-
}
76-
67+
if (id == null) return NotFound();
68+
7769
var post = await _context.BlogPosts.FindAsync(id);
78-
79-
if (post == null)
80-
{
81-
return NotFound();
82-
}
83-
70+
71+
if (post == null) return NotFound();
72+
8473
return View(post);
8574
}
8675

87-
// UPDATE: POST action to handle save changes
76+
// UPDATE: POST
8877
[HttpPost]
8978
[ValidateAntiForgeryToken]
9079
public async Task<IActionResult> Edit(int id, BlogPost post)
9180
{
92-
if (id != post.Id)
93-
{
94-
return NotFound();
95-
}
81+
if (id != post.Id) return NotFound();
9682

9783
if (ModelState.IsValid)
9884
{
9985
try
10086
{
101-
// Mark the post as modified and save changes
102-
_context.Update(post);
87+
// FIX: Prevent overwriting CreatedAt with default date
88+
// 1. Get the existing post from DB (AsNoTracking is not needed here as we want to update it)
89+
var existingPost = await _context.BlogPosts.FindAsync(id);
90+
91+
if (existingPost == null) return NotFound();
92+
93+
// 2. Update only the editable fields
94+
existingPost.Title = post.Title;
95+
existingPost.Content = post.Content;
96+
// Add other fields here if you have them (e.g. ImageUrl)
97+
98+
// Note: We do NOT update existingPost.CreatedAt
99+
100+
_context.Update(existingPost);
103101
await _context.SaveChangesAsync();
104102
}
105103
catch (DbUpdateConcurrencyException)
106104
{
107-
// Optional: Add logic here to handle concurrent updates if needed
108-
if (!_context.BlogPosts.Any(e => e.Id == id))
105+
if (!PostExists(post.Id))
109106
{
110107
return NotFound();
111108
}
@@ -119,39 +116,39 @@ public async Task<IActionResult> Edit(int id, BlogPost post)
119116
return View(post);
120117
}
121118

122-
// DELETE: GET action to show the confirmation screen
119+
// DELETE: GET
123120
[HttpGet]
124121
public async Task<IActionResult> Delete(int? id)
125122
{
126-
if (id == null)
127-
{
128-
return NotFound();
129-
}
123+
if (id == null) return NotFound();
130124

131125
var post = await _context.BlogPosts.FindAsync(id);
132-
133-
if (post == null)
134-
{
135-
return NotFound();
136-
}
126+
127+
if (post == null) return NotFound();
137128

138129
return View(post);
139130
}
140131

141-
// DELETE: POST action to execute the deletion
132+
// DELETE: POST
142133
[HttpPost, ActionName("Delete")]
143134
[ValidateAntiForgeryToken]
144135
public async Task<IActionResult> DeleteConfirmed(int id)
145136
{
146137
var post = await _context.BlogPosts.FindAsync(id);
147-
138+
148139
if (post != null)
149140
{
150141
_context.BlogPosts.Remove(post);
151142
await _context.SaveChangesAsync();
152143
}
153-
144+
154145
return RedirectToAction(nameof(Index));
155146
}
147+
148+
// Helper method to check existence
149+
private bool PostExists(int id)
150+
{
151+
return _context.BlogPosts.Any(e => e.Id == id);
152+
}
156153
}
157154
}

0 commit comments

Comments
 (0)