PlotDirector/PlotLine/Controllers/PlotLinesController.cs

65 lines
2.0 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.Data.SqlClient;
using Microsoft.AspNetCore.Authorization;
using PlotLine.Services;
using PlotLine.ViewModels;
namespace PlotLine.Controllers;
[Authorize]
public sealed class PlotLinesController(IPlotService plots) : Controller
{
public IActionResult Index(int projectId) =>
RedirectToAction("Manage", "PlotLineWorkspace", new { projectId });
public async Task<IActionResult> Create(int projectId)
{
var model = await plots.GetCreatePlotLineAsync(projectId);
return model is null ? NotFound() : View("Edit", model);
}
public async Task<IActionResult> Edit(int id)
{
var model = await plots.GetEditPlotLineAsync(id);
return model is null ? NotFound() : View(model);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Save(PlotLineEditViewModel model)
{
if (ModelState.IsValid)
{
try
{
await plots.SavePlotLineAsync(model);
return RedirectToAction(nameof(Index), new { projectId = model.ProjectID });
}
catch (SqlException exception) when (exception.Number == 51101)
{
ModelState.AddModelError(string.Empty, exception.Message);
}
}
var editModel = await plots.PopulatePlotLineFormAsync(model);
return editModel is null ? NotFound() : View("Edit", editModel);
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Archive(int id, int projectId)
{
try
{
await plots.ArchivePlotLineAsync(id);
}
catch (SqlException exception) when (exception.Number == 51101)
{
TempData["ThreadPlanningConflict"] = exception.Message;
return RedirectToAction(nameof(Edit), new { id });
}
TempData["ArchiveMessage"] = "Plot line archived. You can restore it from Archived Items.";
return RedirectToAction(nameof(Index), new { projectId });
}
}