Files
marechai/Marechai/Areas/Admin/Controllers/DocumentCompaniesController.cs

158 lines
5.7 KiB
C#
Raw Normal View History

2020-05-24 02:12:37 +01:00
using System.Linq;
2019-06-30 01:26:03 +01:00
using System.Threading.Tasks;
2020-02-10 02:20:48 +00:00
using Marechai.Areas.Admin.Models;
2020-02-10 22:44:18 +00:00
using Marechai.Database.Models;
2019-06-30 01:26:03 +01:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
2019-06-30 01:26:03 +01:00
using Microsoft.EntityFrameworkCore;
2020-02-10 02:20:48 +00:00
namespace Marechai.Areas.Admin.Controllers
2019-06-30 01:26:03 +01:00
{
2020-02-10 22:44:18 +00:00
[Area("Admin"), Authorize]
2019-06-30 01:26:03 +01:00
public class DocumentCompaniesController : Controller
{
2020-02-10 02:47:39 +00:00
readonly MarechaiContext _context;
2019-06-30 01:26:03 +01:00
2020-02-10 22:44:18 +00:00
public DocumentCompaniesController(MarechaiContext context) => _context = context;
2019-06-30 01:26:03 +01:00
// GET: DocumentCompanies
2020-02-10 22:44:18 +00:00
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());
2019-06-30 01:26:03 +01:00
// GET: DocumentCompanies/Details/5
public async Task<IActionResult> Details(int? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
2019-06-30 01:26:03 +01:00
DocumentCompanyViewModel documentCompany =
2020-02-10 22:44:18 +00:00
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();
2019-06-30 01:26:03 +01:00
return View(documentCompany);
}
// GET: DocumentCompanies/Create
public IActionResult Create()
{
2020-02-10 22:44:18 +00:00
ViewData["CompanyId"] = new SelectList(_context.Companies.OrderBy(c => c.Name).Select(c => new
{
c.Id, c.Name
}), "Id", "Name");
return View();
}
2019-06-30 01:26:03 +01:00
// POST: DocumentCompanies/Create
2020-02-10 22:44:18 +00:00
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
2019-06-30 01:26:03 +01:00
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
2020-02-10 22:44:18 +00:00
[HttpPost, ValidateAntiForgeryToken]
2019-06-30 01:26:03 +01:00
public async Task<IActionResult> Create([Bind("Name,CompanyId,Id")] DocumentCompany documentCompany)
{
2020-02-10 22:44:18 +00:00
if(!ModelState.IsValid)
return View(documentCompany);
2019-06-30 01:26:03 +01:00
_context.Add(documentCompany);
await _context.SaveChangesAsync();
2020-02-10 22:44:18 +00:00
return RedirectToAction(nameof(Index));
2019-06-30 01:26:03 +01:00
}
// GET: DocumentCompanies/Edit/5
public async Task<IActionResult> Edit(int? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
2019-06-30 01:26:03 +01:00
DocumentCompany documentCompany = await _context.DocumentCompanies.FindAsync(id);
2020-02-10 22:44:18 +00:00
if(documentCompany == null)
return NotFound();
2019-06-30 01:26:03 +01:00
2020-02-10 22:44:18 +00:00
ViewData["CompanyId"] = new SelectList(_context.Companies.OrderBy(c => c.Name).Select(c => new
{
c.Id, c.Name
}), "Id", "Name", documentCompany.CompanyId);
2019-06-30 01:26:03 +01:00
return View(documentCompany);
}
// POST: DocumentCompanies/Edit/5
2020-02-10 22:44:18 +00:00
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
2019-06-30 01:26:03 +01:00
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
2020-02-10 22:44:18 +00:00
[HttpPost, ValidateAntiForgeryToken]
2019-06-30 01:26:03 +01:00
public async Task<IActionResult> Edit(int id, [Bind("Name,CompanyId,Id")] DocumentCompany documentCompany)
{
2020-02-10 22:44:18 +00:00
if(id != documentCompany.Id)
return NotFound();
2019-06-30 01:26:03 +01:00
if(ModelState.IsValid)
{
try
{
_context.Update(documentCompany);
await _context.SaveChangesAsync();
}
catch(DbUpdateConcurrencyException)
{
2020-02-10 22:44:18 +00:00
if(!DocumentCompanyExists(documentCompany.Id))
return NotFound();
2019-06-30 01:26:03 +01:00
throw;
}
return RedirectToAction(nameof(Index));
}
2020-02-10 22:44:18 +00:00
ViewData["CompanyId"] = new SelectList(_context.Companies.OrderBy(c => c.Name).Select(c => new
{
c.Id, c.Name
}), "Id", "Name", documentCompany.CompanyId);
2019-06-30 01:26:03 +01:00
return View(documentCompany);
}
// GET: DocumentCompanies/Delete/5
public async Task<IActionResult> Delete(int? id)
{
2020-02-10 22:44:18 +00:00
if(id == null)
return NotFound();
2019-06-30 01:26:03 +01:00
DocumentCompanyViewModel documentCompany =
2020-02-10 22:44:18 +00:00
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();
2019-06-30 01:26:03 +01:00
return View(documentCompany);
}
// POST: DocumentCompanies/Delete/5
2020-02-10 22:44:18 +00:00
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
2019-06-30 01:26:03 +01:00
public async Task<IActionResult> DeleteConfirmed(int id)
{
DocumentCompany documentCompany = await _context.DocumentCompanies.FindAsync(id);
_context.DocumentCompanies.Remove(documentCompany);
await _context.SaveChangesAsync();
2020-02-10 22:44:18 +00:00
2019-06-30 01:26:03 +01:00
return RedirectToAction(nameof(Index));
}
2020-02-10 22:44:18 +00:00
bool DocumentCompanyExists(int id) => _context.DocumentCompanies.Any(e => e.Id == id);
2019-06-30 01:26:03 +01:00
}
}