using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; using EveryThing.Data; using EveryThing.Models; using EveryThing.Models.CodeTable; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Authorization; using Microsoft.VisualStudio.Debugger.Contracts; using System.Globalization; namespace EveryThing.Pages.CodeTableOperations { [Authorize(Roles = "Administrator,InvoicingUser,ProjecThingUser")] public class IndexModel : PageModel { public class AddEditCodeTableOperation { public CodeTableOperation Operation { get; set; } public bool Edit { get; set; } public int IdCodeTableOperation { get; set; } } private readonly ApplicationDbContext _context; private readonly UserManager _userManager; private readonly SignInManager _loginManager; private readonly RoleManager _roleManager; public IndexModel(ApplicationDbContext context, UserManager userManager, SignInManager loginManager, RoleManager roleManager) { _context = context; _userManager = userManager; _loginManager = loginManager; _roleManager = roleManager; } public IList Operation { get; set; } public async Task OnGetAsync(string searchString) { var user = _userManager.GetUserAsync(User).Result; ViewData["SearchString"] = searchString ?? ""; var search = !string.IsNullOrEmpty(searchString); var query = _context.CodeTableOperations.AsQueryable(); if (search) { query = query.Where(x => EF.Functions.Like(x.Title, $"%{searchString}%")); } Operation = await query .OrderByDescending(x => x.Id) .Take(100) .ToListAsync(); } public IActionResult OnGetCodeTableOperationModal(bool edit, int idCodeTableOperation) { CodeTableOperation operation = null; if (edit) { operation = _context.CodeTableOperations .FirstOrDefault(x => x.Id == idCodeTableOperation); } operation ??= new CodeTableOperation(); return Partial("AddEditOperationModal", new AddEditCodeTableOperation { Operation = operation, Edit = edit, IdCodeTableOperation = idCodeTableOperation }); } public IActionResult OnPostCodeTableOperation(string title, bool defaultValue, bool edit, int idCodeTableOperation) { var successful = true; var error = ""; if (edit) { var operation = _context.CodeTableOperations .FirstOrDefault(x => x.Id == idCodeTableOperation); if (operation != null) { operation.Title = title; operation.Default = defaultValue; _context.SaveChanges(); } else { successful = false; error = $"Code-table operation with ID: {idCodeTableOperation} not found"; } } else { var operation = new CodeTableOperation { Title = title, Default = defaultValue }; _context.CodeTableOperations.Add(operation); _context.SaveChanges(); idCodeTableOperation = operation.Id; } return new JsonResult(new { idCodeTableOperation, error, successful }); } public IActionResult OnGetCodeTableOperation(int idCodeTableOperation) { var successful = true; var error = ""; var inUse = false; var operation = _context.CodeTableOperations .Include(x => x.ProjectPartItemOperation) .FirstOrDefault(x => x.Id == idCodeTableOperation); if (operation == null) { successful = false; error = $"Code-table operation with ID: {idCodeTableOperation} not found"; } else { inUse = operation.ProjectPartItemOperation != null && operation.ProjectPartItemOperation.Count > 0; operation.ProjectPartItemOperation = null; } return new JsonResult(new { operation, error, successful, inUse }); } public IActionResult OnDeleteCodeTableOperation(int idCodeTableOperation) { var successful = true; var error = ""; var operation = _context.CodeTableOperations .FirstOrDefault(x => x.Id == idCodeTableOperation); if (operation != null) { _context.CodeTableOperations.Remove(operation); _context.SaveChanges(); } else { successful = false; error = $"Code-table operation with ID: {idCodeTableOperation} not found"; } return new JsonResult(new { idCodeTableOperation, error, successful }); } } }