2020-09-17 13:37:11 +00:00
using System.Linq ;
using System.Threading.Tasks ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.AspNetCore.Mvc.RazorPages ;
using Microsoft.EntityFrameworkCore ;
using Snootalogue.Data ;
using Snootalogue.Models ;
using Snootalogue.ViewModels ;
namespace Snootalogue.Pages.Documents {
public class EditModel : PageModel {
private readonly SnootalogueContext _context ;
[BindProperty]
public EditDocument ed { get ; set ; }
public EditModel ( SnootalogueContext context ) {
_context = context ;
}
2020-09-20 03:43:08 +00:00
public async Task < IActionResult > OnGetAsync ( string id ) {
2020-09-17 13:37:11 +00:00
if ( id = = null ) {
return NotFound ( ) ;
}
var Document = await _context . Document . FirstOrDefaultAsync ( d = > d . ID = = id ) ;
if ( Document = = null ) {
return NotFound ( ) ;
}
ed = new EditDocument {
ID = Document . ID ,
Title = Document . Title ,
2020-09-18 05:35:20 +00:00
Authors = string . Join ( "," , Document . Authors ) ,
2020-09-17 13:37:11 +00:00
Category = Document . Category ,
2020-09-18 05:35:20 +00:00
Tags = Document . Tags = = null ? "" : string . Join ( "," , Document . Tags ) ,
2020-09-17 13:37:11 +00:00
Read = Document . Read
} ;
return Page ( ) ;
}
public async Task < IActionResult > OnPostAsync ( ) {
if ( ! ModelState . IsValid ) {
return Page ( ) ;
}
Document d = _context . Document . First ( d = > d . ID = = ed . ID ) ;
d . Title = ed . Title ;
2020-09-18 05:35:20 +00:00
d . Authors = ed . Authors . Split ( "," ) . ToList < string > ( ) ;
2020-09-17 13:37:11 +00:00
d . Category = ed . Category ;
2020-09-18 05:35:20 +00:00
d . Tags = string . IsNullOrWhiteSpace ( ed . Tags ) ? null : ed . Tags . Split ( "," ) . ToList < string > ( ) ;
2020-09-17 13:37:11 +00:00
d . Notes = ed . Notes ;
d . Read = ed . Read ;
_context . Attach ( d ) . State = EntityState . Modified ;
try {
await _context . SaveChangesAsync ( ) ;
}
catch ( DbUpdateConcurrencyException e ) {
// TODO: handle it like https://github.com/dotnet/AspNetCore.Docs/blob/master/aspnetcore/tutorials/razor-pages/razor-pages-start/sample/RazorPagesMovie30/Pages/Movies/Edit.cshtml.cs#L56
throw e ;
}
return RedirectToPage ( "/Index" ) ;
}
}
}