Files
everything/EveryThing/Pages/Projects/CreatePart.cshtml.cs
David Štaleker db0cc8d3de prvi
2025-07-18 05:33:16 +02:00

70 lines
2.2 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 EveryThing.Data;
using EveryThing.Models;
using EveryThing.Models.Project;
using Microsoft.AspNetCore.Authorization;
namespace EveryThing.Pages.Projects
{
[Authorize(Roles = "Administrator,ProjecThingUser")]
public class CreatePartModel : PageModel
{
private readonly ApplicationDbContext _context;
private readonly UserManager<IdentityApplicationUser> _userManager;
private readonly SignInManager<IdentityApplicationUser> _loginManager;
private readonly RoleManager<IdentityApplicationRole> _roleManager;
public CreatePartModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager, SignInManager<IdentityApplicationUser> loginManager, RoleManager<IdentityApplicationRole> roleManager)
{
_context = context;
_userManager = userManager;
_loginManager = loginManager;
_roleManager = roleManager;
}
public IActionResult OnGet(int idProject)
{
ViewData["IdProject"] = idProject;
ProjectPart = new ProjectPart
{
IdProjectFk = idProject
};
return Page();
}
[BindProperty]
public Models.Project.ProjectPart ProjectPart { get; set; }
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
var user = _userManager.GetUserAsync(User).Result;
ProjectPart.Status = ProjectPartStatus.Opened;
var items = _context.ProjectParts
.Where(x => x.IdProjectFk == ProjectPart.IdProjectFk).ToList();
ProjectPart.ProjectPartNumber = items.Count <= 0 ? 1 : items.Max(x => x.ProjectPartNumber) + 1;
_context.ProjectParts.Add(ProjectPart);
await _context.SaveChangesAsync();
return RedirectToPage("./Edit", new { id = ProjectPart.IdProjectFk });
}
}
}