mirror of
https://github.com/claunia/marechai.git
synced 2025-12-16 19:14:25 +00:00
Add document company creation in admin view.
This commit is contained in:
@@ -1,159 +0,0 @@
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Marechai.Areas.Admin.Models;
|
||||
using Marechai.Database.Models;
|
||||
using Marechai.ViewModels;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Rendering;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Marechai.Areas.Admin.Controllers
|
||||
{
|
||||
[Area("Admin"), Authorize]
|
||||
public class DocumentCompaniesController : Controller
|
||||
{
|
||||
readonly MarechaiContext _context;
|
||||
|
||||
public DocumentCompaniesController(MarechaiContext context) => _context = context;
|
||||
|
||||
// GET: DocumentCompanies
|
||||
public async Task<IActionResult> Index() => View(await _context.
|
||||
DocumentCompanies.OrderBy(c => c.Name).
|
||||
Select(d => new DocumentCompanyViewModel
|
||||
{
|
||||
Id = d.Id, Name = d.Name,
|
||||
Company = d.Company.Name,
|
||||
CompanyId = d.CompanyId
|
||||
}).ToListAsync());
|
||||
|
||||
// GET: DocumentCompanies/Details/5
|
||||
public async Task<IActionResult> Details(int? id)
|
||||
{
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
DocumentCompanyViewModel documentCompany =
|
||||
await _context.DocumentCompanies.Select(d => new DocumentCompanyViewModel
|
||||
{
|
||||
Id = d.Id, Name = d.Name, Company = d.Company.Name, CompanyId = d.CompanyId
|
||||
}).FirstOrDefaultAsync(m => m.Id == id);
|
||||
|
||||
if(documentCompany == null)
|
||||
return NotFound();
|
||||
|
||||
return View(documentCompany);
|
||||
}
|
||||
|
||||
// GET: DocumentCompanies/Create
|
||||
public IActionResult Create()
|
||||
{
|
||||
ViewData["CompanyId"] = new SelectList(_context.Companies.OrderBy(c => c.Name).Select(c => new
|
||||
{
|
||||
c.Id, c.Name
|
||||
}), "Id", "Name");
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
// POST: DocumentCompanies/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("Name,CompanyId,Id")] DocumentCompany documentCompany)
|
||||
{
|
||||
if(!ModelState.IsValid)
|
||||
return View(documentCompany);
|
||||
|
||||
_context.Add(documentCompany);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
// GET: DocumentCompanies/Edit/5
|
||||
public async Task<IActionResult> Edit(int? id)
|
||||
{
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
DocumentCompany documentCompany = await _context.DocumentCompanies.FindAsync(id);
|
||||
|
||||
if(documentCompany == null)
|
||||
return NotFound();
|
||||
|
||||
ViewData["CompanyId"] = new SelectList(_context.Companies.OrderBy(c => c.Name).Select(c => new
|
||||
{
|
||||
c.Id, c.Name
|
||||
}), "Id", "Name", documentCompany.CompanyId);
|
||||
|
||||
return View(documentCompany);
|
||||
}
|
||||
|
||||
// POST: DocumentCompanies/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("Name,CompanyId,Id")] DocumentCompany documentCompany)
|
||||
{
|
||||
if(id != documentCompany.Id)
|
||||
return NotFound();
|
||||
|
||||
if(ModelState.IsValid)
|
||||
{
|
||||
try
|
||||
{
|
||||
_context.Update(documentCompany);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
catch(DbUpdateConcurrencyException)
|
||||
{
|
||||
if(!DocumentCompanyExists(documentCompany.Id))
|
||||
return NotFound();
|
||||
|
||||
throw;
|
||||
}
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
ViewData["CompanyId"] = new SelectList(_context.Companies.OrderBy(c => c.Name).Select(c => new
|
||||
{
|
||||
c.Id, c.Name
|
||||
}), "Id", "Name", documentCompany.CompanyId);
|
||||
|
||||
return View(documentCompany);
|
||||
}
|
||||
|
||||
// GET: DocumentCompanies/Delete/5
|
||||
public async Task<IActionResult> Delete(int? id)
|
||||
{
|
||||
if(id == null)
|
||||
return NotFound();
|
||||
|
||||
DocumentCompanyViewModel documentCompany =
|
||||
await _context.DocumentCompanies.Select(d => new DocumentCompanyViewModel
|
||||
{
|
||||
Id = d.Id, Name = d.Name, Company = d.Company.Name, CompanyId = d.CompanyId
|
||||
}).FirstOrDefaultAsync(m => m.Id == id);
|
||||
|
||||
if(documentCompany == null)
|
||||
return NotFound();
|
||||
|
||||
return View(documentCompany);
|
||||
}
|
||||
|
||||
// POST: DocumentCompanies/Delete/5
|
||||
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
|
||||
public async Task<IActionResult> DeleteConfirmed(int id)
|
||||
{
|
||||
DocumentCompany documentCompany = await _context.DocumentCompanies.FindAsync(id);
|
||||
_context.DocumentCompanies.Remove(documentCompany);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return RedirectToAction(nameof(Index));
|
||||
}
|
||||
|
||||
bool DocumentCompanyExists(int id) => _context.DocumentCompanies.Any(e => e.Id == id);
|
||||
}
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
@model Marechai.Database.Models.DocumentCompany
|
||||
|
||||
@{
|
||||
ViewData["Title"] = "Create";
|
||||
}
|
||||
<h1>Create</h1>
|
||||
<h4>Document company</h4>
|
||||
<hr />
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
<form asp-action="Create">
|
||||
<div asp-validation-summary="ModelOnly" class="text-danger">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Name" class="control-label">
|
||||
</label>
|
||||
<input asp-for="Name" class="form-control" />
|
||||
<span asp-validation-for="Name" class="text-danger">
|
||||
</span>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label asp-for="Company" class="control-label">
|
||||
</label>
|
||||
<select asp-for="CompanyId" class="form-control" asp-items="ViewBag.CompanyId">
|
||||
<option selected value="">
|
||||
None or unknown
|
||||
</option>
|
||||
</select>
|
||||
</div>
|
||||
<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>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@section Scripts {
|
||||
@{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<Version>3.0.99.1241</Version>
|
||||
<Version>3.0.99.1243</Version>
|
||||
<Company>Canary Islands Computer Museum</Company>
|
||||
<Copyright>Copyright © 2003-2020 Natalia Portillo</Copyright>
|
||||
<Product>Canary Islands Computer Museum Website</Product>
|
||||
@@ -129,5 +129,6 @@
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\BrowserTests\Index.cshtml" />
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\News\Delete.cshtml" />
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\Companies\Create.cshtml" />
|
||||
<_ContentIncludedByDefault Remove="Areas\Admin\Views\DocumentCompanies\Create.cshtml" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -48,8 +48,7 @@ namespace Marechai.Pages.Admin.Details
|
||||
_creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/companies/create", StringComparison.InvariantCulture);
|
||||
|
||||
if(Id <= 0 &&
|
||||
!_creating)
|
||||
if(Id <= 0 && !_creating)
|
||||
return;
|
||||
|
||||
_countries = await CountriesService.GetAsync();
|
||||
|
||||
@@ -32,6 +32,7 @@
|
||||
|
||||
@page "/admin/document_companies/details/{Id:int}"
|
||||
@page "/admin/document_companies/edit/{Id:int}"
|
||||
@page "/admin/document_companies/create"
|
||||
@inherits OwningComponentBase<DocumentCompaniesService>
|
||||
@inject IStringLocalizer<DocumentCompaniesService> L
|
||||
@inject CompaniesService CompaniesService
|
||||
|
||||
@@ -11,6 +11,7 @@ namespace Marechai.Pages.Admin.Details
|
||||
public partial class DocumentCompany
|
||||
{
|
||||
List<CompanyViewModel> _companies;
|
||||
bool _creating;
|
||||
bool _editing;
|
||||
bool _loaded;
|
||||
DocumentCompanyViewModel _model;
|
||||
@@ -27,15 +28,20 @@ namespace Marechai.Pages.Admin.Details
|
||||
|
||||
_loaded = true;
|
||||
|
||||
if(Id <= 0)
|
||||
_creating = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/document_companies/create",
|
||||
StringComparison.InvariantCulture);
|
||||
|
||||
if(Id <= 0 &&
|
||||
!_creating)
|
||||
return;
|
||||
|
||||
_companies = await CompaniesService.GetAsync();
|
||||
_model = await Service.GetAsync(Id);
|
||||
_model = _creating ? new DocumentCompanyViewModel() : await Service.GetAsync(Id);
|
||||
|
||||
_editing = NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/document_companies/edit/",
|
||||
StringComparison.InvariantCulture);
|
||||
_editing = _creating || NavigationManager.ToBaseRelativePath(NavigationManager.Uri).ToLowerInvariant().
|
||||
StartsWith("admin/document_companies/edit/",
|
||||
StringComparison.InvariantCulture);
|
||||
|
||||
if(_editing)
|
||||
SetCheckboxes();
|
||||
@@ -59,7 +65,15 @@ namespace Marechai.Pages.Admin.Details
|
||||
async void OnCancelClicked()
|
||||
{
|
||||
_editing = false;
|
||||
_model = await Service.GetAsync(Id);
|
||||
|
||||
if(_creating)
|
||||
{
|
||||
NavigationManager.ToBaseRelativePath("admin/document_companies");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
_model = await Service.GetAsync(Id);
|
||||
SetCheckboxes();
|
||||
StateHasChanged();
|
||||
}
|
||||
@@ -76,9 +90,14 @@ namespace Marechai.Pages.Admin.Details
|
||||
else if(string.IsNullOrWhiteSpace(_model.Name))
|
||||
return;
|
||||
|
||||
_editing = false;
|
||||
await Service.UpdateAsync(_model);
|
||||
_model = await Service.GetAsync(Id);
|
||||
if(_creating)
|
||||
Id = await Service.CreateAsync(_model);
|
||||
else
|
||||
await Service.UpdateAsync(_model);
|
||||
|
||||
_editing = false;
|
||||
_creating = false;
|
||||
_model = await Service.GetAsync(Id);
|
||||
SetCheckboxes();
|
||||
StateHasChanged();
|
||||
}
|
||||
|
||||
@@ -42,9 +42,7 @@
|
||||
return;
|
||||
}
|
||||
<p>
|
||||
<span class="btn btn-primary">
|
||||
@L["Create new"]
|
||||
</span>
|
||||
<a class="btn btn-primary" href="/admin/document_companies/create">@L["Create new"]</a>
|
||||
</p>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
|
||||
@@ -41,6 +41,19 @@ namespace Marechai.Services
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> CreateAsync(DocumentCompanyViewModel viewModel)
|
||||
{
|
||||
var model = new DocumentCompany
|
||||
{
|
||||
CompanyId = viewModel.CompanyId, Name = viewModel.Name
|
||||
};
|
||||
|
||||
await _context.DocumentCompanies.AddAsync(model);
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return model.Id;
|
||||
}
|
||||
|
||||
public async Task DeleteAsync(int id)
|
||||
{
|
||||
DocumentCompany item = await _context.DocumentCompanies.FindAsync(id);
|
||||
|
||||
Reference in New Issue
Block a user