Files
everything2/EveryThing/Pages/CodeTablePrePostText/AddEdit.cshtml.cs
2023-06-26 08:12:25 +02:00

182 lines
6.7 KiB
C#

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.Classes;
namespace EveryThing.Pages.CodeTablePrePostText
{
[Authorize(Roles = "Administrator,TransportThingUser,InvoicingUser")]
public class AddEditModel : PageModel
{
private readonly ApplicationDbContext _context;
private readonly UserManager<IdentityApplicationUser> _userManager;
public AddEditModel(ApplicationDbContext context, UserManager<IdentityApplicationUser> userManager)
{
_context = context;
_userManager = userManager;
}
[BindProperty]
public Models.CodeTable.CodeTablePrePostText PrePostText { get; set; }
public async Task<IActionResult> 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<IActionResult> 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("<div><br></div>", "");
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<DisplayAttribute>().ForEach(link =>
{
var insertedLink = insertedLinks.FirstOrDefault(x => (int)x.Link == link.EnumValue && x.Type == CodeTablePrePostTextLink.TypeEnum.PreText);
tableLinksPreText.Append($"<tr data-link='{link.EnumValue}' data-type='{(int)CodeTablePrePostTextLink.TypeEnum.PreText}' data-idlink='{insertedLink?.IdPrePostTextLink ?? 0}'>");
tableLinksPreText.Append($"<td><input type='checkbox' class='chb-link' {((insertedLink?.IdPrePostTextLink ?? 0) > 0 ? "checked='checked'" : "")}/></td>");
tableLinksPreText.Append($"<td>{link.EnumAttribute.Name}</td>");
tableLinksPreText.Append("</tr>");
});
var tableLinksPostText = new StringBuilder();
typeof(CodeTablePrePostTextLink.LinkEnum).GetEnumListClass<DisplayAttribute>().ForEach(link =>
{
var insertedLink = insertedLinks.FirstOrDefault(x => (int)x.Link == link.EnumValue && x.Type == CodeTablePrePostTextLink.TypeEnum.PostText);
tableLinksPostText.Append($"<tr data-link='{link.EnumValue}' data-type='{(int)CodeTablePrePostTextLink.TypeEnum.PostText}' data-idlink='{insertedLink?.IdPrePostTextLink ?? 0}'>");
tableLinksPostText.Append($"<td><input type='checkbox' class='chb-link' {((insertedLink?.IdPrePostTextLink ?? 0) > 0 ? "checked='checked'" : "")}/></td>");
tableLinksPostText.Append($"<td>{link.EnumAttribute.Name}</td>");
tableLinksPostText.Append("</tr>");
});
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 });
}
}
}