PlotDirector/PlotLine/Controllers/BooksController.cs
2026-06-01 09:03:59 +01:00

48 lines
1.3 KiB
C#

using Microsoft.AspNetCore.Mvc;
using PlotLine.Services;
using PlotLine.ViewModels;
namespace PlotLine.Controllers;
public sealed class BooksController(IBookService books) : Controller
{
public async Task<IActionResult> Details(int id)
{
var model = await books.GetDetailAsync(id);
return model is null ? NotFound() : View(model);
}
public async Task<IActionResult> Create(int projectId)
{
var model = await books.GetCreateAsync(projectId);
return model is null ? NotFound() : View("Edit", model);
}
public async Task<IActionResult> Edit(int id)
{
var model = await books.GetEditAsync(id);
return model is null ? NotFound() : View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Save(BookEditViewModel model)
{
if (!ModelState.IsValid)
{
return View("Edit", model);
}
var bookId = await books.SaveAsync(model);
return RedirectToAction(nameof(Details), new { id = bookId });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Archive(int id, int projectId)
{
await books.ArchiveAsync(id);
return RedirectToAction("Details", "Projects", new { id = projectId });
}
}