70 lines
2.4 KiB
C#
70 lines
2.4 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 CreateModel : PageModel
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly UserManager<IdentityApplicationUser> _userManager;
|
|
private readonly SignInManager<IdentityApplicationUser> _loginManager;
|
|
private readonly RoleManager<IdentityApplicationRole> _roleManager;
|
|
|
|
public CreateModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager, SignInManager<IdentityApplicationUser> loginManager, RoleManager<IdentityApplicationRole> roleManager)
|
|
{
|
|
_context = context;
|
|
_userManager = userManager;
|
|
_loginManager = loginManager;
|
|
_roleManager = roleManager;
|
|
}
|
|
|
|
public IActionResult OnGet()
|
|
{
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
ViewData["IdPartnerFk"] = new SelectList(_context.CodeTablePartners.Where(x => x.IdCompanyFk == user.IdCompanyFk && x.Active), "IdPartner", "Title");
|
|
|
|
return Page();
|
|
}
|
|
|
|
[BindProperty]
|
|
public Models.Project.Project Project { get; set; }
|
|
|
|
public async Task<IActionResult> OnPostAsync()
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
return Page();
|
|
}
|
|
|
|
var user = _userManager.GetUserAsync(User).Result;
|
|
|
|
Project.IdCompanyFk = user.IdCompanyFk;
|
|
Project.Status = ProjectStatus.Opened;
|
|
|
|
Project.ProjectYear = DateTime.Now.Year;
|
|
var items = _context.Projects
|
|
.Where(x => x.IdCompanyFk == user.IdCompanyFk
|
|
&& x.ProjectYear == Project.ProjectYear).ToList();
|
|
|
|
Project.ProjectNumber = items.Count <= 0 ? 1 : items.Max(x => x.ProjectNumber) + 1;
|
|
|
|
_context.Projects.Add(Project);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return RedirectToPage("./Edit", new { id = Project.IdProject });
|
|
}
|
|
}
|
|
}
|