Files
everything2/EveryThing/Pages/Projects/EditPart.cshtml.cs
David Štaleker 03b92525d7 Prvi commit
2023-05-12 09:00:07 +02:00

91 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using EveryThing.Data;
using EveryThing.Models;
using EveryThing.Models.Project;
using Microsoft.AspNetCore.Authorization;
namespace EveryThing.Pages.Projects
{
[Authorize(Roles = "Administrator,ProjecThingUser")]
public class EditPartModel : PageModel
{
private readonly ApplicationDbContext _context;
private readonly UserManager<IdentityApplicationUser> _userManager;
private readonly SignInManager<IdentityApplicationUser> _loginManager;
private readonly RoleManager<IdentityApplicationRole> _roleManager;
public EditPartModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager, SignInManager<IdentityApplicationUser> loginManager, RoleManager<IdentityApplicationRole> roleManager)
{
_context = context;
_userManager = userManager;
_loginManager = loginManager;
_roleManager = roleManager;
}
[BindProperty]
public ProjectPart ProjectPart { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
var user = _userManager.GetUserAsync(User).Result;
ProjectPart = await _context.ProjectParts.FirstOrDefaultAsync(m => m.IdProjectPart == id);
if (ProjectPart == null)
{
return NotFound();
}
ViewData["IdProject"] = ProjectPart.IdProjectFk;
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
_context.Attach(ProjectPart).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ProjectExists(ProjectPart.IdProjectPart))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToPage("./Edit",new { id = ProjectPart.IdProjectFk });
}
private bool ProjectExists(int id)
{
return _context.ProjectParts.Any(e => e.IdProjectPart == id);
}
}
}