Add project files.
This commit is contained in:
104
DiaryApp/Controllers/DiaryEntriesController.cs
Normal file
104
DiaryApp/Controllers/DiaryEntriesController.cs
Normal file
@ -0,0 +1,104 @@
|
||||
using DiaryApp.Data;
|
||||
using DiaryApp.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||
|
||||
namespace DiaryApp.Controllers
|
||||
{
|
||||
public class DiaryEntriesController : Controller
|
||||
{
|
||||
private readonly ApplicationDbContext _db;
|
||||
|
||||
public DiaryEntriesController(ApplicationDbContext db) {
|
||||
_db = db;
|
||||
}
|
||||
public IActionResult Index()
|
||||
{
|
||||
List<DiaryEntry> objDiaryEntryList = _db.DiaryEntries.ToList();
|
||||
|
||||
return View(objDiaryEntryList);
|
||||
}
|
||||
|
||||
public IActionResult Create()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Create(DiaryEntry obj)
|
||||
{
|
||||
if (obj != null && obj.Title.Length < 3)
|
||||
{
|
||||
ModelState.AddModelError("Title", "The Title must be at least 3 characters long.");
|
||||
}
|
||||
if(ModelState.IsValid)
|
||||
{
|
||||
_db.DiaryEntries.Add(obj);
|
||||
_db.SaveChanges();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
return View(obj);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Edit(int? id)
|
||||
{
|
||||
if(id== null || id == 0)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
|
||||
DiaryEntry? diaryEntry = _db.DiaryEntries.Find(id);
|
||||
if (diaryEntry == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return View(diaryEntry);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Edit(DiaryEntry obj)
|
||||
{
|
||||
if (obj != null && obj.Title.Length < 3)
|
||||
{
|
||||
ModelState.AddModelError("Title", "The Title must be at least 3 characters long.");
|
||||
}
|
||||
if (ModelState.IsValid)
|
||||
{
|
||||
_db.DiaryEntries.Update(obj);
|
||||
_db.SaveChanges();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
return View(obj);
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult Delete(int? id)
|
||||
{
|
||||
if (id == null || id == 0)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
|
||||
DiaryEntry? diaryEntry = _db.DiaryEntries.Find(id);
|
||||
if (diaryEntry == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
return View(diaryEntry);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public IActionResult Delete(DiaryEntry obj)
|
||||
{
|
||||
_db.DiaryEntries.Remove(obj);
|
||||
_db.SaveChanges();
|
||||
return RedirectToAction("Index");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
30
DiaryApp/Controllers/HomeController.cs
Normal file
30
DiaryApp/Controllers/HomeController.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using System.Diagnostics;
|
||||
using DiaryApp.Models;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace DiaryApp.Controllers
|
||||
{
|
||||
public class HomeController : Controller
|
||||
{
|
||||
public IActionResult Index()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
public IActionResult MyPage()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
public IActionResult Privacy()
|
||||
{
|
||||
return View();
|
||||
}
|
||||
|
||||
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
|
||||
public IActionResult Error()
|
||||
{
|
||||
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user