using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.AspNetCore.Mvc.Rendering; using EveryThing.Data; using EveryThing.Models; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Authorization; namespace EveryThing.Pages.Invoices { [Authorize(Roles = "Administrator,InvoicingUser,ProjecThingUser")] public class CreateItemModel : PageModel { private readonly ApplicationDbContext _context; private readonly UserManager _userManager; private readonly SignInManager _loginManager; private readonly RoleManager _roleManager; public CreateItemModel(ApplicationDbContext context, UserManager userManager, SignInManager loginManager, RoleManager roleManager) { _context = context; _userManager = userManager; _loginManager = loginManager; _roleManager = roleManager; } public IActionResult OnGet(int idInvoice) { var user = _userManager.GetUserAsync(User).Result; ViewData["IdInvoice"] = idInvoice; ViewData["Items"] = new SelectList(_context.CodeTableItems.Where(x => x.IdCompanyFk == user.IdCompanyFk && x.Active), "IdItem", "Title"); return Page(); } [BindProperty] public Models.Invoice.InvoiceItem InvoiceItem { get; set; } public async Task OnPostAsync(int idInvoice) { if (!ModelState.IsValid) { return Page(); } var user = _userManager.GetUserAsync(User).Result; InvoiceItem.IdInvoiceFk = idInvoice; _context.InvoiceItems.Add(InvoiceItem); await _context.SaveChangesAsync(); return RedirectToPage("./Edit", new { id = idInvoice }); } } }