using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; 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 DocumentFormat.OpenXml.Spreadsheet; using EveryThing.Models.CodeTable; using EveryThing.Classess; namespace EveryThing.Pages.CodeTablePrePostText { [Authorize(Roles = "Administrator,TransportThingUser,InvoicingUser")] public class AddEditModel : PageModel { private readonly ApplicationDbContext _context; private readonly UserManager _userManager; public AddEditModel(ApplicationDbContext context, UserManager userManager) { _context = context; _userManager = userManager; } [BindProperty] public Models.CodeTable.CodeTablePrePostText PrePostText { get; set; } public async Task OnGetAsync(int? id) { var user = _userManager.GetUserAsync(User).Result; if (id == null) { PrePostText = new Models.CodeTable.CodeTablePrePostText { Content = "", IdCompanyFk = user.IdCompanyFk }; return Page(); } PrePostText = await _context.CodeTablePrePostText .FirstOrDefaultAsync(m => m.IdPrePostText == id && m.IdCompanyFk == user.IdCompanyFk); if (PrePostText == null) { return NotFound(); } return Page(); } public async Task OnPostOrderAsync() { System.Diagnostics.Debug.WriteLine("OnPostOrderAsync"); var user = _userManager.GetUserAsync(User).Result; if (!ModelState.IsValid) { ViewData["IdPartnerFk"] = new SelectList(_context.CodeTablePartners.Where(x => x.IdCompanyFk == user.IdCompanyFk), "IdPartner", "Title"); ViewData["IdVehicleFk"] = new SelectList(_context.Vehicles.Where(x => x.IdCompanyFk == user.IdCompanyFk), "IdVehicle", "RegistrationNumber"); return Page(); } PrePostText.Content = PrePostText.Content.Replace("

", ""); if (PrePostText.IdPrePostText > 0) { _context.Attach(PrePostText).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PrePostTextExists(PrePostText.IdPrePostText)) { return NotFound(); } else { throw; } } return Page(); } // OrderNumber and OrderYear _context.CodeTablePrePostText.Add(PrePostText); await _context.SaveChangesAsync(); return new JsonResult(new { id = PrePostText.IdPrePostText}); } private bool PrePostTextExists(int id) { return _context.CodeTablePrePostText.Any(e => e.IdPrePostText == id); } public IActionResult OnGetLinksTable(int idPrePostText) { var insertedLinks = _context.CodeTablePrePostTextLink .Where(x => x.IdPrePostTextFk == idPrePostText); var tableLinksPreText = new StringBuilder(); typeof(CodeTablePrePostTextLink.LinkEnum).GetEnumListClass().ForEach(link => { var insertedLink = insertedLinks.FirstOrDefault(x => (int)x.Link == link.EnumValue && x.Type == CodeTablePrePostTextLink.TypeEnum.PreText); tableLinksPreText.Append($""); tableLinksPreText.Append($" 0 ? "checked='checked'" : "")}/>"); tableLinksPreText.Append($"{link.EnumAttribute.Name}"); tableLinksPreText.Append(""); }); var tableLinksPostText = new StringBuilder(); typeof(CodeTablePrePostTextLink.LinkEnum).GetEnumListClass().ForEach(link => { var insertedLink = insertedLinks.FirstOrDefault(x => (int)x.Link == link.EnumValue && x.Type == CodeTablePrePostTextLink.TypeEnum.PostText); tableLinksPostText.Append($""); tableLinksPostText.Append($" 0 ? "checked='checked'" : "")}/>"); tableLinksPostText.Append($"{link.EnumAttribute.Name}"); tableLinksPostText.Append(""); }); return new JsonResult(new { tableLinksPreText = tableLinksPreText.ToString(), tableLinksPostText = tableLinksPostText.ToString(), successful = true }); } public IActionResult OnPostLinkToggle(int idPrePostText, int link, int type, int idPrePostTextLink) { var user = _userManager.GetUserAsync(User).Result; var idLink = 0; var exitingLink = _context.CodeTablePrePostTextLink.FirstOrDefault(x => x.IdPrePostTextLink == idPrePostTextLink && x.IdPrePostTextFk == idPrePostText); if (exitingLink != null) { _context.CodeTablePrePostTextLink.Remove(exitingLink); _context.SaveChanges(); } else { var newLink = new CodeTablePrePostTextLink { IdPrePostTextFk = idPrePostText, Link = (CodeTablePrePostTextLink.LinkEnum)link, Type = (CodeTablePrePostTextLink.TypeEnum)type }; _context.CodeTablePrePostTextLink.Add(newLink); _context.SaveChanges(); idLink = newLink.IdPrePostTextLink; } return new JsonResult(new { error = "", successful = true, idLink }); } } }