Allow to add, or edit, company descriptions in admin view.

This commit is contained in:
2020-05-31 19:12:12 +01:00
parent efaf938752
commit d312c82e9c
13 changed files with 296 additions and 638 deletions

View File

@@ -1,191 +0,0 @@
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : CompanyDescriptionsController.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Company descriptions admin controller
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
using System.Linq;
using System.Threading.Tasks;
using Marechai.Areas.Admin.Models;
using Marechai.Database.Models;
using Markdig;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
namespace Marechai.Areas.Admin.Controllers
{
[Area("Admin"), Authorize]
public class CompanyDescriptionsController : Controller
{
readonly MarechaiContext _context;
readonly MarkdownPipeline pipeline;
public CompanyDescriptionsController(MarechaiContext context)
{
pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
_context = context;
}
// GET: CompanyDescription
public async Task<IActionResult> Index()
{
IIncludableQueryable<CompanyDescription, Company> marechaiContext =
_context.CompanyDescriptions.Include(c => c.Company);
return View(await marechaiContext.OrderBy(c => c.Company.Name).Select(c => new CompanyDescriptionViewModel
{
Id = c.Id, Company = c.Company.Name
}).ToListAsync());
}
// GET: CompanyDescription/Details/5
public async Task<IActionResult> Details(int? id)
{
if(id == null)
return NotFound();
CompanyDescription companyDescription =
await _context.CompanyDescriptions.Include(c => c.Company).FirstOrDefaultAsync(m => m.Id == id);
if(companyDescription == null)
return NotFound();
return View(companyDescription);
}
// GET: CompanyDescription/Create
public IActionResult Create()
{
ViewData["CompanyId"] = new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name");
return View();
}
// POST: CompanyDescription/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,CompanyId,Text")] CompanyDescription companyDescription)
{
if(ModelState.IsValid)
{
companyDescription.Html = Markdown.ToHtml(companyDescription.Text, pipeline);
_context.Add(companyDescription);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
ViewData["CompanyId"] = new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name",
companyDescription.CompanyId);
return View(companyDescription);
}
// GET: CompanyDescription/Edit/5
public async Task<IActionResult> Edit(int? id)
{
if(id == null)
return NotFound();
CompanyDescription companyDescription = await _context.CompanyDescriptions.FindAsync(id);
if(companyDescription == null)
return NotFound();
ViewData["CompanyId"] = new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name",
companyDescription.CompanyId);
return View(companyDescription);
}
// POST: CompanyDescription/Edit/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Id,CompanyId,Text")] CompanyDescription companyDescription)
{
if(id != companyDescription.Id)
return NotFound();
if(ModelState.IsValid)
{
try
{
companyDescription.Html = Markdown.ToHtml(companyDescription.Text, pipeline);
_context.Update(companyDescription);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
if(!CompanyDescriptionExists(companyDescription.Id))
return NotFound();
throw;
}
return RedirectToAction(nameof(Index));
}
ViewData["CompanyId"] = new SelectList(_context.Companies.OrderBy(c => c.Name), "Id", "Name",
companyDescription.CompanyId);
return View(companyDescription);
}
// GET: CompanyDescription/Delete/5
public async Task<IActionResult> Delete(int? id)
{
if(id == null)
return NotFound();
CompanyDescription companyDescription =
await _context.CompanyDescriptions.Include(c => c.Company).FirstOrDefaultAsync(m => m.Id == id);
if(companyDescription == null)
return NotFound();
return View(companyDescription);
}
// POST: CompanyDescription/Delete/5
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(int id)
{
CompanyDescription companyDescription = await _context.CompanyDescriptions.FindAsync(id);
_context.CompanyDescriptions.Remove(companyDescription);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
bool CompanyDescriptionExists(int id) => _context.CompanyDescriptions.Any(e => e.Id == id);
}
}

View File

@@ -1,77 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : Create.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Admin view create
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
}
@model Marechai.Database.Models.CompanyDescription
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>Company description</h4>
<hr />
<div class="row">
<div class="col-md-8">
<form asp-action="Create">
<div class="col-md-4">
<div asp-validation-summary="ModelOnly" class="text-danger">
</div>
<div class="form-group">
<label asp-for="Company" class="control-label">
</label>
<select asp-for="CompanyId" class="form-control" asp-items="ViewBag.CompanyId">
</select>
</div>
</div>
<div class="col-md-8">
<div class="form-group">
<label asp-for="Text" class="control-label">
</label>
<textarea asp-for="Text"
class="form-control" cols="120" rows="20"></textarea>
<span asp-validation-for="Text" class="text-danger">
</span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Create" />
<a asp-action="Index" class="btn btn-secondary">
Back to List
</a>
</div>
</div>
</form>
</div>
</div>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

View File

@@ -1,63 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : Delete.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Admin view create
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
}
@model Marechai.Database.Models.CompanyDescription
@{
ViewData["Title"] = "Delete";
}
<h1>Delete</h1>
<h3>Are you sure you want to delete this?</h3>
<div>
<h4>Company description</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Company)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Company.Name)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Html)
</dt>
<dd class="col-sm-10">
@Html.Raw(Model.Html)
</dd>
</dl>
<form asp-action="Delete">
<input type="hidden" asp-for="Id" />
<input class="btn btn-danger" type="submit" value="Delete" />
<a asp-action="Index" class="btn btn-secondary">
Back to List
</a>
</form>
</div>

View File

@@ -1,69 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : Details.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Admin view create
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
}
@model Marechai.Database.Models.CompanyDescription
@{
ViewData["Title"] = "Details";
}
<h1>Details</h1>
<div>
<h4>Company description</h4>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Company)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Company.Name)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Html)
</dt>
<dd class="col-sm-10">
@Html.Raw(Model.Html)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Text)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Text)
</dd>
</dl>
</div>
<div>
<a asp-action="Edit" asp-route-id="@Model.Id" class="btn btn-primary">
Edit
</a>
<a asp-action="Index" class="btn btn-secondary">
Back to List
</a>
</div>

View File

@@ -1,79 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : Edit.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Admin view create
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
}
@model Marechai.Database.Models.CompanyDescription
@{
ViewData["Title"] = "Edit";
}
<h1>Edit</h1>
<h4>Company description</h4>
<hr />
<div class="row">
<div class="col-md-8">
<form asp-action="Edit">
<div class="col-md-4">
<div asp-validation-summary="ModelOnly" class="text-danger">
</div>
<input type="hidden" asp-for="Id" />
<div class="form-group">
<label asp-for="Company" class="control-label">
</label>
<select asp-for="CompanyId" class="form-control" asp-items="ViewBag.CompanyId">
</select>
<span asp-validation-for="CompanyId" class="text-danger">
</span>
</div>
</div>
<div class="col-md-8">
<div class="form-group">
<label asp-for="Text" class="control-label">
</label>
<textarea asp-for="Text" class="form-control" cols="120" rows="20"> </textarea>
<span asp-validation-for="Text" class="text-danger">
</span>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<input class="btn btn-primary" type="submit" value="Save" />
<a asp-action="Index" class="btn btn-secondary">
Back to List
</a>
</div>
</div>
</form>
</div>
</div>
@section Scripts {
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
}

View File

@@ -1,73 +0,0 @@
@{
/******************************************************************************
// MARECHAI: Master repository of computing history artifacts information
// ----------------------------------------------------------------------------
//
// Filename : Index.cshtml
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// --[ Description ] ----------------------------------------------------------
//
// Admin view create
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2003-2020 Natalia Portillo
*******************************************************************************/
}
@model IEnumerable<Marechai.Areas.Admin.Models.CompanyDescriptionViewModel>
@{
ViewData["Title"] = "Company descriptions (Admin)";
}
<h1>Company description</h1>
<p>
<a asp-action="Create" class="btn btn-primary">
Create new
</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.Company)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Company)
</td>
<td>
<a asp-action="Details" asp-route-id="@item.Id" class="btn btn-primary">
Details
</a>
<a asp-action="Edit" asp-route-id="@item.Id" class="btn btn-secondary">
Edit
</a>
<a asp-action="Delete" asp-route-id="@item.Id" class="btn btn-danger">
Delete
</a>
</td>
</tr>
}
</tbody>
</table>

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.0.99.1492</Version>
<Version>3.0.99.1526</Version>
<Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product>
@@ -211,5 +211,10 @@
<_ContentIncludedByDefault Remove="Areas\Admin\Views\MachinePhotos\Details.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\MachinePhotos\Edit.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\MachinePhotos\Index.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\CompanyDescriptions\Create.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\CompanyDescriptions\Delete.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\CompanyDescriptions\Details.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\CompanyDescriptions\Edit.cshtml" />
<_ContentIncludedByDefault Remove="Areas\Admin\Views\CompanyDescriptions\Index.cshtml" />
</ItemGroup>
</Project>

View File

@@ -41,6 +41,7 @@
@inject CompanyLogosService CompanyLogosService
@inject IWebHostEnvironment Host
@inject IFileReaderService FileReaderService;
@inject IJSRuntime JSRuntime
@attribute [Authorize(Roles = "UberAdmin, Admin")]
@@ -480,11 +481,11 @@
{
<table class="table table-dark">
<thead>
<tr>
<th>@L["SVG"]</th>
<th>@L["PNG"]</th>
<th>@L["WebP"]</th>
</tr>
<tr>
<th>@L["SVG"]</th>
<th>@L["PNG"]</th>
<th>@L["WebP"]</th>
</tr>
</thead>
<tbody>
<tr>
@@ -531,4 +532,41 @@
<Button Color="Color.Success" Clicked="@ConfirmUpload" Disabled="!_uploaded || _savingLogo">@L["Save"]</Button>
</ModalFooter>
</ModalContent>
</Modal>
</Modal>
<hr/>
<div class="container">
<h4>@L["Company description"]</h4>
<hr/>
@if (_readonlyDescription &&
_description is null)
{
<Button Color="Color.Success" Clicked="@AddNewDescription">@L["Add new description"]</Button>
}
@if (!_readonlyDescription || _description != null)
{
if (_readonlyDescription)
{
<Button Color="Color.Success" Clicked="@EditDescription">@L["Edit"]</Button>
}
else
{
<Button Color="Color.Primary" Clicked="@CancelDescription">@L["Cancel"]</Button>
<Button Color="Color.Success" Clicked="@SaveDescription">@L["Save"]</Button>
}
<Tabs SelectedTab="@_selectedDescriptionTab" SelectedTabChanged="@OnSelectedDescriptionTabChanged">
<Items>
<Tab Name="markdown">Markdown</Tab>
<Tab Name="preview">Preview</Tab>
</Items>
<Content>
<TabPanel Name="markdown">
<textarea onchange="OnCompanyMarkdownChanged()" class="form-control" rows="200" id="txtCompanyDescriptionMarkdown" readonly="@_readonlyDescription">@_description.Markdown</textarea>
</TabPanel>
<TabPanel Name="preview">
@((MarkupString)_description.Html)
</TabPanel>
</Content>
</Tabs>
}
</div>

View File

@@ -11,7 +11,9 @@ using Marechai.Database.Models;
using Marechai.Helpers;
using Marechai.Shared;
using Marechai.ViewModels;
using Markdig;
using Microsoft.AspNetCore.Components;
using Microsoft.JSInterop;
using SkiaSharp;
using Svg.Skia;
@@ -19,45 +21,49 @@ namespace Marechai.Pages.Admin.Details
{
public partial class Company
{
const int _maxUploadSize = 5 * 1048576;
List<CompanyViewModel> _companies;
List<Iso31661Numeric> _countries;
bool _creating;
CompanyLogo _currentLogo;
int? _currentLogoYear;
bool _deleteInProgress;
bool _editing;
Modal _frmDelete;
Modal _frmLogoYear;
Modal _frmUpload;
ElementReference _inputUpload;
bool _loaded;
List<CompanyLogo> _logos;
CompanyViewModel _model;
double _progressValue;
bool _savingLogo;
bool _unknownAddress;
bool _unknownCity;
bool _unknownCountry;
bool _unknownFacebook;
bool _unknownFounded;
bool _unknownLogoYear;
bool _unknownPostalCode;
bool _unknownProvince;
bool _unknownSold;
bool _unknownSoldTo;
bool _unknownTwitter;
bool _unknownWebsite;
bool _uploaded;
string _uploadedPngData;
string _uploadedSvgData;
string _uploadedWebpData;
bool _uploadError;
string _uploadErrorMessage;
bool _uploading;
MemoryStream _uploadMs;
bool _yearChangeInProgress;
const int _maxUploadSize = 5 * 1048576;
bool _addingDescription;
List<CompanyViewModel> _companies;
List<Iso31661Numeric> _countries;
bool _creating;
CompanyLogo _currentLogo;
int? _currentLogoYear;
bool _deleteInProgress;
CompanyDescriptionViewModel _description;
bool _editing;
Modal _frmDelete;
Modal _frmLogoYear;
Modal _frmUpload;
ElementReference _inputUpload;
bool _loaded;
List<CompanyLogo> _logos;
CompanyViewModel _model;
MarkdownPipeline _pipeline;
double _progressValue;
bool _readonlyDescription;
bool _savingLogo;
string _selectedDescriptionTab;
bool _unknownAddress;
bool _unknownCity;
bool _unknownCountry;
bool _unknownFacebook;
bool _unknownFounded;
bool _unknownLogoYear;
bool _unknownPostalCode;
bool _unknownProvince;
bool _unknownSold;
bool _unknownSoldTo;
bool _unknownTwitter;
bool _unknownWebsite;
bool _uploaded;
string _uploadedPngData;
string _uploadedSvgData;
string _uploadedWebpData;
bool _uploadError;
string _uploadErrorMessage;
bool _uploading;
MemoryStream _uploadMs;
bool _yearChangeInProgress;
[Parameter]
public int Id { get; set; }
@@ -81,18 +87,33 @@ namespace Marechai.Pages.Admin.Details
!_creating)
return;
_countries = await CountriesService.GetAsync();
_companies = await Service.GetAsync();
_model = _creating ? new CompanyViewModel() : await Service.GetAsync(Id);
_logos = await CompanyLogosService.GetByCompany(Id);
_countries = await CountriesService.GetAsync();
_companies = await Service.GetAsync();
_model = _creating ? new CompanyViewModel() : await Service.GetAsync(Id);
_logos = await CompanyLogosService.GetByCompany(Id);
_description = await Service.GetDescriptionAsync(Id);
_selectedDescriptionTab = "markdown";
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
StartsWith("admin/companies/edit/",
StringComparison.InvariantCulture);
_pipeline = new MarkdownPipelineBuilder().UseAdvancedExtensions().Build();
if(_description?.Markdown != null)
_description.Html = Markdown.ToHtml(_description.Markdown);
if(_editing)
SetCheckboxes();
if(firstRender)
{
DotNetObjectReference<Company> dotNetReference = DotNetObjectReference.Create(this);
await JSRuntime.InvokeVoidAsync("SetDotNetClassReference", dotNetReference);
}
_readonlyDescription = true;
StateHasChanged();
}
@@ -354,7 +375,7 @@ namespace Marechai.Pages.Admin.Details
_uploadedSvgData = "";
_uploadedPngData = "";
_uploadedWebpData = "";
_savingLogo = false;
_savingLogo = false;
_frmUpload.Show();
}
@@ -371,7 +392,7 @@ namespace Marechai.Pages.Admin.Details
_uploadedSvgData = "";
_uploadedPngData = "";
_uploadedWebpData = "";
_savingLogo = false;
_savingLogo = false;
}
async Task UploadFile()
@@ -527,7 +548,9 @@ namespace Marechai.Pages.Admin.Details
{
SvgRender.RenderCompanyLogo(guid, _uploadMs, Host.WebRootPath);
var fs = new FileStream(Path.Combine(Host.WebRootPath, "assets/logos", $"{guid}.svg"), FileMode.OpenOrCreate, FileAccess.ReadWrite);
var fs = new FileStream(Path.Combine(Host.WebRootPath, "assets/logos", $"{guid}.svg"),
FileMode.OpenOrCreate, FileAccess.ReadWrite);
_uploadMs.Position = 0;
_uploadMs.WriteTo(fs);
fs.Close();
@@ -553,5 +576,60 @@ namespace Marechai.Pages.Admin.Details
// Tell we finished loading
StateHasChanged();
}
void OnSelectedDescriptionTabChanged(string name)
{
if(name == "preview" &&
!_readonlyDescription)
_description.Html = Markdown.ToHtml(_description.Markdown, _pipeline);
_selectedDescriptionTab = name;
}
[JSInvokableAttribute("OnCompanyDescriptionChangedDotnet")]
public void OnDescriptionChanged(string value)
{
if(!_readonlyDescription)
_description.Markdown = value;
}
void AddNewDescription()
{
_description = new CompanyDescriptionViewModel();
_description.CompanyId = Id;
_readonlyDescription = false;
_addingDescription = true;
}
void EditDescription() => _readonlyDescription = false;
async Task CancelDescription()
{
_description = _addingDescription ? null : await Service.GetDescriptionAsync(Id);
_readonlyDescription = true;
await JSRuntime.InvokeVoidAsync("SetCompanyDescriptionText", _description?.Markdown ?? "");
_addingDescription = false;
StateHasChanged();
}
async Task SaveDescription()
{
if(_readonlyDescription)
return;
if(string.IsNullOrWhiteSpace(_description.Markdown))
{
await CancelDescription();
return;
}
_description.Html = Markdown.ToHtml(_description.Markdown, _pipeline);
await Service.CreateOrUpdateDescriptionAsync(Id, _description);
_addingDescription = false;
await CancelDescription();
}
}
}

View File

@@ -39,7 +39,7 @@ namespace Marechai.Pages.Companies
_computers = machines.Where(m => m.Type == MachineType.Computer).ToList();
_consoles = machines.Where(m => m.Type == MachineType.Console).ToList();
_description = await Service.GetDescriptionAsync(Id);
_description = await Service.GetDescriptionTextAsync(Id);
_soldTo = await Service.GetSoldToAsync(_company.SoldToId);
_logos = await CompanyLogosService.GetByCompany(Id);

View File

@@ -43,5 +43,31 @@
<script src="_content/Blazorise/blazorise.js"></script>
<script src="_content/Blazorise.Bootstrap/blazorise.bootstrap.js"></script>
<script>
let dotNetClassReference;
function SetDotNetClassReference(reference)
{
dotNetClassReference = reference;
}
function SetCompanyDescriptionText(markdown)
{
let textArea = document.getElementById("txtCompanyDescriptionMarkdown");
if(!textArea) return;
textArea.value = markdown;
}
function OnCompanyMarkdownChanged()
{
let textArea = document.getElementById("txtCompanyDescriptionMarkdown");
if(!textArea) return;
dotNetClassReference.invokeMethodAsync('OnCompanyDescriptionChangedDotnet', textArea.value);
}
</script>
</body>
</html>

View File

@@ -49,38 +49,47 @@ namespace Marechai.Services
_l = localizer;
}
public Task<List<CompanyViewModel>> GetAsync() => _context.
Companies.Include(c => c.Logos).OrderBy(c => c.Name).
Select(c => new CompanyViewModel
{
Id = c.Id,
LastLogo = c.
Logos.OrderByDescending(l => l.Year).
FirstOrDefault().Guid,
Name = c.Name, Founded = c.Founded, Sold = c.Sold,
SoldToId = c.SoldToId, CountryId = c.CountryId,
Status = c.Status, Website = c.Website,
Twitter = c.Twitter, Facebook = c.Facebook,
Address = c.Address, City = c.City, Province = c.Province,
PostalCode = c.PostalCode, Country = c.Country.Name
}).ToListAsync();
public async Task<List<CompanyViewModel>> GetAsync() => await _context.
Companies.Include(c => c.Logos).
OrderBy(c => c.Name).
Select(c => new CompanyViewModel
{
Id = c.Id,
LastLogo = c.
Logos.
OrderByDescending(l => l.Year).
FirstOrDefault().Guid,
Name = c.Name, Founded = c.Founded,
Sold = c.Sold, SoldToId = c.SoldToId,
CountryId = c.CountryId, Status = c.Status,
Website = c.Website, Twitter = c.Twitter,
Facebook = c.Facebook, Address = c.Address,
City = c.City, Province = c.Province,
PostalCode = c.PostalCode,
Country = c.Country.Name
}).ToListAsync();
public Task<CompanyViewModel> GetAsync(int id) => _context.Companies.Where(c => c.Id == id).
Select(c => new CompanyViewModel
{
Id = c.Id,
LastLogo = c.
Logos.OrderByDescending(l => l.Year).
FirstOrDefault().Guid,
Name = c.Name, Founded = c.Founded,
Sold = c.Sold, SoldToId = c.SoldToId,
CountryId = c.CountryId, Status = c.Status,
Website = c.Website, Twitter = c.Twitter,
Facebook = c.Facebook, Address = c.Address,
City = c.City, Province = c.Province,
PostalCode = c.PostalCode,
Country = c.Country.Name
}).FirstOrDefaultAsync();
public async Task<CompanyViewModel> GetAsync(int id) => await _context.Companies.Where(c => c.Id == id).
Select(c => new CompanyViewModel
{
Id = c.Id,
LastLogo = c.
Logos.
OrderByDescending(l => l.
Year).
FirstOrDefault().Guid,
Name = c.Name, Founded = c.Founded,
Sold = c.Sold, SoldToId = c.SoldToId,
CountryId = c.CountryId,
Status = c.Status,
Website = c.Website,
Twitter = c.Twitter,
Facebook = c.Facebook,
Address = c.Address, City = c.City,
Province = c.Province,
PostalCode = c.PostalCode,
Country = c.Country.Name
}).FirstOrDefaultAsync();
public async Task UpdateAsync(CompanyViewModel viewModel)
{
@@ -129,7 +138,7 @@ namespace Marechai.Services
Id = m.Id, Name = m.Name, Type = m.Type
}).ToListAsync();
public async Task<string> GetDescriptionAsync(int id)
public async Task<string> GetDescriptionTextAsync(int id)
{
CompanyDescription description =
await _context.CompanyDescriptions.FirstOrDefaultAsync(d => d.CompanyId == id);
@@ -193,5 +202,50 @@ namespace Marechai.Services
await _context.SaveChangesAsync();
}
public async Task<CompanyDescriptionViewModel> GetDescriptionAsync(int id) => await _context.
CompanyDescriptions.
Where(d => d.CompanyId ==
id).
Select(d =>
new
CompanyDescriptionViewModel
{
Id =
d.Id,
CompanyId
= d.
CompanyId,
Html = d.
Html,
Markdown
= d.
Text
}).
FirstOrDefaultAsync();
public async Task<int> CreateOrUpdateDescriptionAsync(int id, CompanyDescriptionViewModel description)
{
CompanyDescription current = await _context.CompanyDescriptions.FirstOrDefaultAsync(d => d.CompanyId == id);
if(current is null)
{
current = new CompanyDescription
{
CompanyId = id, Html = description.Html, Text = description.Markdown
};
await _context.CompanyDescriptions.AddAsync(current);
}
else
{
current.Html = description.Html;
current.Text = description.Markdown;
}
await _context.SaveChangesAsync();
return current.Id;
}
}
}

View File

@@ -0,0 +1,9 @@
namespace Marechai.ViewModels
{
public class CompanyDescriptionViewModel : BaseViewModel<int>
{
public string Markdown { get; set; }
public string Html { get; set; }
public int CompanyId { get; set; }
}
}