diff --git a/Marechai/Areas/Admin/Controllers/CompanyDescriptionsController.cs b/Marechai/Areas/Admin/Controllers/CompanyDescriptionsController.cs deleted file mode 100644 index a659f3ed..00000000 --- a/Marechai/Areas/Admin/Controllers/CompanyDescriptionsController.cs +++ /dev/null @@ -1,191 +0,0 @@ -/****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : CompanyDescriptionsController.cs -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// 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 Index() - { - IIncludableQueryable 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 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 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 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 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 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 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); - } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/CompanyDescriptions/Create.cshtml b/Marechai/Areas/Admin/Views/CompanyDescriptions/Create.cshtml deleted file mode 100644 index c2fae0d9..00000000 --- a/Marechai/Areas/Admin/Views/CompanyDescriptions/Create.cshtml +++ /dev/null @@ -1,77 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Create.cshtml -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// Copyright © 2003-2020 Natalia Portillo -*******************************************************************************/ -} -@model Marechai.Database.Models.CompanyDescription - -@{ - ViewData["Title"] = "Create"; -} -

Create

-

Company description

-
-
-
-
-
-
-
-
- - -
-
-
-
- - - - -
-
- -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/CompanyDescriptions/Delete.cshtml b/Marechai/Areas/Admin/Views/CompanyDescriptions/Delete.cshtml deleted file mode 100644 index 700f1d13..00000000 --- a/Marechai/Areas/Admin/Views/CompanyDescriptions/Delete.cshtml +++ /dev/null @@ -1,63 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Delete.cshtml -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// Copyright © 2003-2020 Natalia Portillo -*******************************************************************************/ -} -@model Marechai.Database.Models.CompanyDescription - -@{ - ViewData["Title"] = "Delete"; -} -

Delete

-

Are you sure you want to delete this?

-
-

Company description

-
-
-
- @Html.DisplayNameFor(model => model.Company) -
-
- @Html.DisplayFor(model => model.Company.Name) -
-
- @Html.DisplayNameFor(model => model.Html) -
-
- @Html.Raw(Model.Html) -
-
-
- - - - Back to List - -
-
\ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/CompanyDescriptions/Details.cshtml b/Marechai/Areas/Admin/Views/CompanyDescriptions/Details.cshtml deleted file mode 100644 index ec8cffcc..00000000 --- a/Marechai/Areas/Admin/Views/CompanyDescriptions/Details.cshtml +++ /dev/null @@ -1,69 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Details.cshtml -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// Copyright © 2003-2020 Natalia Portillo -*******************************************************************************/ -} -@model Marechai.Database.Models.CompanyDescription - -@{ - ViewData["Title"] = "Details"; -} -

Details

-
-

Company description

-
-
-
- @Html.DisplayNameFor(model => model.Company) -
-
- @Html.DisplayFor(model => model.Company.Name) -
-
- @Html.DisplayNameFor(model => model.Html) -
-
- @Html.Raw(Model.Html) -
-
- @Html.DisplayNameFor(model => model.Text) -
-
- @Html.DisplayFor(model => model.Text) -
-
-
- \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/CompanyDescriptions/Edit.cshtml b/Marechai/Areas/Admin/Views/CompanyDescriptions/Edit.cshtml deleted file mode 100644 index 83f22389..00000000 --- a/Marechai/Areas/Admin/Views/CompanyDescriptions/Edit.cshtml +++ /dev/null @@ -1,79 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Edit.cshtml -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// Copyright © 2003-2020 Natalia Portillo -*******************************************************************************/ -} -@model Marechai.Database.Models.CompanyDescription - -@{ - ViewData["Title"] = "Edit"; -} -

Edit

-

Company description

-
-
-
-
-
-
-
- -
- - - - -
-
-
-
- - - - -
-
- -
-
-
- -@section Scripts { - @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } -} \ No newline at end of file diff --git a/Marechai/Areas/Admin/Views/CompanyDescriptions/Index.cshtml b/Marechai/Areas/Admin/Views/CompanyDescriptions/Index.cshtml deleted file mode 100644 index 9a77b591..00000000 --- a/Marechai/Areas/Admin/Views/CompanyDescriptions/Index.cshtml +++ /dev/null @@ -1,73 +0,0 @@ -@{ - /****************************************************************************** -// MARECHAI: Master repository of computing history artifacts information -// ---------------------------------------------------------------------------- -// -// Filename : Index.cshtml -// Author(s) : Natalia Portillo -// -// --[ 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 . -// -// ---------------------------------------------------------------------------- -// Copyright © 2003-2020 Natalia Portillo -*******************************************************************************/ -} -@model IEnumerable - -@{ - ViewData["Title"] = "Company descriptions (Admin)"; -} -

Company description

-

- - Create new - -

- - - - - - - - - @foreach (var item in Model) - { - - - - - } - -
- @Html.DisplayNameFor(model => model.Company) -
- @Html.DisplayFor(modelItem => item.Company) - - - Details - - - Edit - - - Delete - -
\ No newline at end of file diff --git a/Marechai/Marechai.csproj b/Marechai/Marechai.csproj index 9da69171..44c12427 100644 --- a/Marechai/Marechai.csproj +++ b/Marechai/Marechai.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 3.0.99.1492 + 3.0.99.1526 Canary Islands Computer Museum Copyright © 2003-2020 Natalia Portillo Canary Islands Computer Museum Website @@ -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" /> \ No newline at end of file diff --git a/Marechai/Pages/Admin/Details/Company.razor b/Marechai/Pages/Admin/Details/Company.razor index c5498d48..78acaa7d 100644 --- a/Marechai/Pages/Admin/Details/Company.razor +++ b/Marechai/Pages/Admin/Details/Company.razor @@ -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 @@ { - - - - - + + + + + @@ -531,4 +532,41 @@ - \ No newline at end of file + + +
+
+

@L["Company description"]

+
+ @if (_readonlyDescription && + _description is null) + { + + } + @if (!_readonlyDescription || _description != null) + { + if (_readonlyDescription) + { + + } + else + { + + + } + + + Markdown + Preview + + + + + + + @((MarkupString)_description.Html) + + + + } +
diff --git a/Marechai/Pages/Admin/Details/Company.razor.cs b/Marechai/Pages/Admin/Details/Company.razor.cs index 8864d975..e9dc9918 100644 --- a/Marechai/Pages/Admin/Details/Company.razor.cs +++ b/Marechai/Pages/Admin/Details/Company.razor.cs @@ -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 _companies; - List _countries; - bool _creating; - CompanyLogo _currentLogo; - int? _currentLogoYear; - bool _deleteInProgress; - bool _editing; - Modal _frmDelete; - Modal _frmLogoYear; - Modal _frmUpload; - ElementReference _inputUpload; - bool _loaded; - List _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 _companies; + List _countries; + bool _creating; + CompanyLogo _currentLogo; + int? _currentLogoYear; + bool _deleteInProgress; + CompanyDescriptionViewModel _description; + bool _editing; + Modal _frmDelete; + Modal _frmLogoYear; + Modal _frmUpload; + ElementReference _inputUpload; + bool _loaded; + List _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 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(); + } } } \ No newline at end of file diff --git a/Marechai/Pages/Companies/View.razor.cs b/Marechai/Pages/Companies/View.razor.cs index 153407f6..edd2d5cf 100644 --- a/Marechai/Pages/Companies/View.razor.cs +++ b/Marechai/Pages/Companies/View.razor.cs @@ -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); diff --git a/Marechai/Pages/_Host.cshtml b/Marechai/Pages/_Host.cshtml index 39ad6cbf..eac3cae8 100644 --- a/Marechai/Pages/_Host.cshtml +++ b/Marechai/Pages/_Host.cshtml @@ -43,5 +43,31 @@ + \ No newline at end of file diff --git a/Marechai/Services/CompaniesService.cs b/Marechai/Services/CompaniesService.cs index b6f730ae..a8cd0c3b 100644 --- a/Marechai/Services/CompaniesService.cs +++ b/Marechai/Services/CompaniesService.cs @@ -49,38 +49,47 @@ namespace Marechai.Services _l = localizer; } - public Task> 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> 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 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 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 GetDescriptionAsync(int id) + public async Task 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 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 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; + } } } \ No newline at end of file diff --git a/Marechai/ViewModels/CompanyDescriptionViewModel.cs b/Marechai/ViewModels/CompanyDescriptionViewModel.cs new file mode 100644 index 00000000..bc8107fb --- /dev/null +++ b/Marechai/ViewModels/CompanyDescriptionViewModel.cs @@ -0,0 +1,9 @@ +namespace Marechai.ViewModels +{ + public class CompanyDescriptionViewModel : BaseViewModel + { + public string Markdown { get; set; } + public string Html { get; set; } + public int CompanyId { get; set; } + } +} \ No newline at end of file
@L["SVG"]@L["PNG"]@L["WebP"]
@L["SVG"]@L["PNG"]@L["WebP"]