Refactor and reorganize code.

This commit is contained in:
2020-02-10 22:44:18 +00:00
parent ed5aacd34e
commit da352de481
345 changed files with 15117 additions and 20198 deletions

View File

@@ -39,7 +39,7 @@ namespace Marechai.Controllers
{
public class CompanyController : Controller
{
readonly MarechaiContext _context;
readonly MarechaiContext _context;
readonly IHostingEnvironment hostingEnvironment;
public CompanyController(IHostingEnvironment env, MarechaiContext context)
@@ -51,18 +51,24 @@ namespace Marechai.Controllers
public IActionResult ByLetter(char id)
{
// ToUpper()
if(id >= 'a' && id <= 'z') id -= (char)32;
if(id >= 'a' &&
id <= 'z')
id -= (char)32;
// Check if not letter
if(id < 'A' || id > 'Z') id = '\0';
if(id < 'A' ||
id > 'Z')
id = '\0';
ViewBag.Letter = id;
ViewBag.WebRootPath = hostingEnvironment.WebRootPath;
if(id == '\0') return RedirectToAction(nameof(Index));
if(id == '\0')
return RedirectToAction(nameof(Index));
return View(_context.Companies.Include(c => c.Logos).Where(c => c.Name.StartsWith(id)).OrderBy(c => c.Name)
.Select(c => new CompanyViewModel
return View(_context.Companies.Include(c => c.Logos).Where(c => c.Name.StartsWith(id)).OrderBy(c => c.Name).
Select(c => new CompanyViewModel
{
Id = c.Id,
LastLogo = c.Logos.OrderByDescending(l => l.Year).FirstOrDefault().Guid,
@@ -75,7 +81,8 @@ namespace Marechai.Controllers
ViewBag.WebRootPath = hostingEnvironment.WebRootPath;
Company company = _context.Companies.FirstOrDefault(c => c.Id == id);
if(company == null) return Index();
if(company == null)
return Index();
ViewBag.CompanyDescription = company.Description?.Html ?? company.Description?.Text;
@@ -86,11 +93,13 @@ namespace Marechai.Controllers
{
ViewBag.Iso3166 = _context.Iso31661Numeric.FirstOrDefault(i => i.Id == id);
if(ViewBag.Iso3166 is null) RedirectToAction(nameof(Index));
if(ViewBag.Iso3166 is null)
RedirectToAction(nameof(Index));
ViewBag.WebRootPath = hostingEnvironment.WebRootPath;
return View(_context.Companies.Include(c => c.Logos).Where(c => c.CountryId == id).OrderBy(c => c.Name)
.Select(c => new CompanyViewModel
return View(_context.Companies.Include(c => c.Logos).Where(c => c.CountryId == id).OrderBy(c => c.Name).
Select(c => new CompanyViewModel
{
Id = c.Id,
LastLogo = c.Logos.OrderByDescending(l => l.Year).FirstOrDefault().Guid,
@@ -104,14 +113,7 @@ namespace Marechai.Controllers
return View(_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
Id = c.Id, LastLogo = c.Logos.OrderByDescending(l => l.Year).FirstOrDefault().Guid, Name = c.Name
}).ToList());
}
}

View File

@@ -38,7 +38,7 @@ namespace Marechai.Controllers
{
public class ComputerController : Controller
{
readonly MarechaiContext _context;
readonly MarechaiContext _context;
readonly IHostingEnvironment hostingEnvironment;
public ComputerController(IHostingEnvironment env, MarechaiContext context)
@@ -50,12 +50,14 @@ namespace Marechai.Controllers
public IActionResult Index()
{
ViewBag.ItemCount = _context.Machines.Count(m => m.Type == MachineType.Computer);
ViewBag.MinYear = _context
.Machines.Where(t => t.Type == MachineType.Computer &&
ViewBag.MinYear = _context.
Machines.Where(t => t.Type == MachineType.Computer &&
t.Introduced.HasValue &&
t.Introduced.Value.Year > 1000).Min(t => t.Introduced.Value.Year);
ViewBag.MaxYear = _context
.Machines.Where(t => t.Type == MachineType.Computer &&
ViewBag.MaxYear = _context.
Machines.Where(t => t.Type == MachineType.Computer &&
t.Introduced.HasValue &&
t.Introduced.Value.Year > 1000).Max(t => t.Introduced.Value.Year);
@@ -65,16 +67,28 @@ namespace Marechai.Controllers
public IActionResult ByLetter(char id)
{
// ToUpper()
if(id >= 'a' && id <= 'z') id -= (char)32;
if(id >= 'a' &&
id <= 'z')
id -= (char)32;
// Check if not letter
if(id < 'A' || id > 'Z') id = '\0';
if(id < 'A' ||
id > 'Z')
id = '\0';
ViewBag.Letter = id;
return View(id == '\0'
? _context.Machines.Where(m => m.Type == MachineType.Computer).ToArray()
: _context.Machines.Where(m => m.Type == MachineType.Computer && m.Name.StartsWith(id))
.ToArray());
return View(id == '\0' ? _context.Machines.Where(m => m.Type == MachineType.Computer).ToArray() : _context.
Machines.
Where(m =>
m.
Type ==
MachineType.
Computer &&
m.
Name.
StartsWith(id)).
ToArray());
}
public IActionResult ByYear(int id)

View File

@@ -38,7 +38,7 @@ namespace Marechai.Controllers
{
public class ConsoleController : Controller
{
readonly MarechaiContext _context;
readonly MarechaiContext _context;
readonly IHostingEnvironment hostingEnvironment;
public ConsoleController(IHostingEnvironment env, MarechaiContext context)
@@ -50,12 +50,14 @@ namespace Marechai.Controllers
public IActionResult Index()
{
ViewBag.ItemCount = _context.Machines.Count(m => m.Type == MachineType.Console);
ViewBag.MinYear = _context
.Machines.Where(t => t.Type == MachineType.Console &&
ViewBag.MinYear = _context.
Machines.Where(t => t.Type == MachineType.Console &&
t.Introduced.HasValue &&
t.Introduced.Value.Year > 1000).Min(t => t.Introduced.Value.Year);
ViewBag.MaxYear = _context
.Machines.Where(t => t.Type == MachineType.Console &&
ViewBag.MaxYear = _context.
Machines.Where(t => t.Type == MachineType.Console &&
t.Introduced.HasValue &&
t.Introduced.Value.Year > 1000).Max(t => t.Introduced.Value.Year);
@@ -65,16 +67,28 @@ namespace Marechai.Controllers
public IActionResult ByLetter(char id)
{
// ToUpper()
if(id >= 'a' && id <= 'z') id -= (char)32;
if(id >= 'a' &&
id <= 'z')
id -= (char)32;
// Check if not letter
if(id < 'A' || id > 'Z') id = '\0';
if(id < 'A' ||
id > 'Z')
id = '\0';
ViewBag.Letter = id;
return View(id == '\0'
? _context.Machines.Where(m => m.Type == MachineType.Console).ToArray()
: _context.Machines.Where(m => m.Type == MachineType.Console && m.Name.StartsWith(id))
.ToArray());
return View(id == '\0' ? _context.Machines.Where(m => m.Type == MachineType.Console).ToArray() : _context.
Machines.
Where(m =>
m.
Type ==
MachineType.
Console &&
m.
Name.
StartsWith(id)).
ToArray());
}
public IActionResult ByYear(int id)

View File

@@ -41,7 +41,7 @@ namespace Marechai.Controllers
{
public class HomeController : Controller
{
readonly MarechaiContext _context;
readonly MarechaiContext _context;
readonly IHostingEnvironment hostingEnvironment;
public HomeController(IHostingEnvironment env, MarechaiContext context)
@@ -60,47 +60,56 @@ namespace Marechai.Controllers
{
Machine machine = _context.Machines.Find(@new.AddedId);
if(machine is null) continue;
if(machine is null)
continue;
switch(@new.Type)
{
case NewsType.NewComputerInDb:
news.Add(new NewsModel(@new.AddedId, "New computer in database", @new.Date, "Machine", "View",
$"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.NewConsoleInDb:
news.Add(new NewsModel(@new.AddedId, "New console in database", @new.Date, "Machine", "View",
$"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.NewComputerInCollection:
news.Add(new NewsModel(@new.AddedId, "New computer in collection", @new.Date, "Machine", "View",
$"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.NewConsoleInCollection:
news.Add(new NewsModel(@new.AddedId, "New console in collection", @new.Date, "Machine", "View",
$"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.UpdatedComputerInDb:
news.Add(new NewsModel(@new.AddedId, "Updated computer in database", @new.Date, "Machine",
"View", $"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.UpdatedConsoleInDb:
news.Add(new NewsModel(@new.AddedId, "Updated console in database", @new.Date, "Machine",
"View", $"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.UpdatedComputerInCollection:
news.Add(new NewsModel(@new.AddedId, "Updated computer in collection", @new.Date, "Machine",
"View", $"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.UpdatedConsoleInCollection:
news.Add(new NewsModel(@new.AddedId, "Updated console in collection", @new.Date, "Machine",
"View", $"{machine.Company.Name} {machine.Name}"));
break;
case NewsType.NewMoneyDonation:
@@ -118,7 +127,9 @@ namespace Marechai.Controllers
public IActionResult Contact() => View();
public IActionResult Error() =>
View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
public IActionResult Error() => View(new ErrorViewModel
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier
});
}
}

View File

@@ -37,7 +37,7 @@ namespace Marechai.Controllers
{
public class MachineController : Controller
{
readonly MarechaiContext _context;
readonly MarechaiContext _context;
readonly IHostingEnvironment hostingEnvironment;
public MachineController(IHostingEnvironment env, MarechaiContext context)