Klavzule
This commit is contained in:
169
EveryThing/Pages/CodeTablePrePostText/AddEdit.cshtml.cs
Normal file
169
EveryThing/Pages/CodeTablePrePostText/AddEdit.cshtml.cs
Normal file
@@ -0,0 +1,169 @@
|
||||
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<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 tableLinks = new StringBuilder();
|
||||
|
||||
typeof(CodeTablePrePostTextLink.LinkEnum).GetEnumListClass<DisplayAttribute>().ForEach(link =>
|
||||
{
|
||||
var insertedLink = insertedLinks.FirstOrDefault(x => (int)x.Link == link.EnumValue);
|
||||
tableLinks.Append($"<tr data-link='{link.EnumValue}' data-idlink='{insertedLink?.IdPrePostTextLink ?? 0}'>");
|
||||
tableLinks.Append($"<td><input type='checkbox' class='chb-link' {((insertedLink?.IdPrePostTextLink ?? 0) > 0 ? "checked='checked'" : "")}/></td>");
|
||||
tableLinks.Append($"<td>{link.EnumAttribute.Name}</td>");
|
||||
tableLinks.Append("</tr>");
|
||||
});
|
||||
|
||||
return new JsonResult(new { tableLinks = tableLinks.ToString(), successful = true });
|
||||
}
|
||||
|
||||
public IActionResult OnPostLinkToggle(int idPrePostText, int link, 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
|
||||
};
|
||||
|
||||
_context.CodeTablePrePostTextLink.Add(newLink);
|
||||
_context.SaveChanges();
|
||||
idLink = newLink.IdPrePostTextLink;
|
||||
}
|
||||
|
||||
return new JsonResult(new { error = "", successful = true, idLink });
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user