Implement create owned machine admin page.

This commit is contained in:
2019-05-30 00:03:40 +01:00
parent e48817a5f1
commit bfa78fdba6
4 changed files with 175 additions and 105 deletions

View File

@@ -1,20 +1,30 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
namespace Cicm.Database.Models namespace Cicm.Database.Models
{ {
public class OwnedMachine : BaseModel<long> public class OwnedMachine : BaseModel<long>
{ {
public DateTime AcquisitionDate { get; set; } [DisplayName("Acquisition date")]
public DateTime? LostDate { get; set; } public DateTime AcquisitionDate { get; set; }
public StatusType Status { get; set; } [DisplayName("Date when sold, traded, or otherwise lost")]
public DateTime? LastStatusDate { get; set; } public DateTime? LostDate { get; set; }
public bool Trade { get; set; } public StatusType Status { get; set; }
public bool Boxed { get; set; } [DisplayName("Last status check date")]
public bool Manuals { get; set; } public DateTime? LastStatusDate { get; set; }
public string SerialNumber { get; set; } [DisplayName("Available for trade or sale")]
public bool SerialNumberVisible { get; set; } public bool Trade { get; set; }
public int MachineId { get; set; } [DisplayName("Has original boxes")]
public bool Boxed { get; set; }
[DisplayName("Has original manuals")]
public bool Manuals { get; set; }
[DisplayName("Serial number")]
public string SerialNumber { get; set; }
[DisplayName("Serial number visible to other users")]
public bool SerialNumberVisible { get; set; }
public int MachineId { get; set; }
public string UserId { get; set; }
public virtual ICollection<GpusByOwnedMachine> Gpus { get; set; } public virtual ICollection<GpusByOwnedMachine> Gpus { get; set; }
public virtual ICollection<MemoryByOwnedMachine> Memory { get; set; } public virtual ICollection<MemoryByOwnedMachine> Memory { get; set; }

View File

@@ -13,7 +13,7 @@ namespace cicm_web.Areas.Admin.Controllers
[Authorize] [Authorize]
public class OwnedMachineController : Controller public class OwnedMachineController : Controller
{ {
private readonly cicmContext _context; readonly cicmContext _context;
public OwnedMachineController(cicmContext context) public OwnedMachineController(cicmContext context)
{ {
@@ -23,14 +23,20 @@ namespace cicm_web.Areas.Admin.Controllers
// GET: OwnedMachine // GET: OwnedMachine
public async Task<IActionResult> Index() public async Task<IActionResult> Index()
{ {
var cicmContext = _context.OwnedMachines.Include(o => o.Machine).OrderBy(o => o.Machine.Company.Name).ThenBy(o => o.Machine.Name).ThenBy(o => o.User.UserName).ThenBy(o => o.AcquisitionDate).Select(o => new OwnedMachineViewModel IQueryable<OwnedMachineViewModel> cicmContext = _context
{ .OwnedMachines.Include(o => o.Machine)
AcquisitionDate = o.AcquisitionDate, .OrderBy(o => o.Machine.Company.Name)
Id = o.Id, .ThenBy(o => o.Machine.Name).ThenBy(o => o.User.UserName)
Machine = $"{o.Machine.Company.Name} {o.Machine.Name}", .ThenBy(o => o.AcquisitionDate)
Status = o.Status, .Select(o => new OwnedMachineViewModel
User = o.User.UserName {
}); AcquisitionDate = o.AcquisitionDate,
Id = o.Id,
Machine =
$"{o.Machine.Company.Name} {o.Machine.Name}",
Status = o.Status,
User = o.User.UserName
});
return View(await cicmContext.ToListAsync()); return View(await cicmContext.ToListAsync());
} }
@@ -38,18 +44,11 @@ namespace cicm_web.Areas.Admin.Controllers
// GET: OwnedMachine/Details/5 // GET: OwnedMachine/Details/5
public async Task<IActionResult> Details(long? id) public async Task<IActionResult> Details(long? id)
{ {
if (id == null) if(id == null) return NotFound();
{
return NotFound();
}
var ownedMachine = await _context.OwnedMachines OwnedMachine ownedMachine = await _context.OwnedMachines
.Include(o => o.Machine) .Include(o => o.Machine).FirstOrDefaultAsync(m => m.Id == id);
.FirstOrDefaultAsync(m => m.Id == id); if(ownedMachine == null) return NotFound();
if (ownedMachine == null)
{
return NotFound();
}
return View(ownedMachine); return View(ownedMachine);
} }
@@ -57,7 +56,13 @@ namespace cicm_web.Areas.Admin.Controllers
// GET: OwnedMachine/Create // GET: OwnedMachine/Create
public IActionResult Create() public IActionResult Create()
{ {
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name"); ViewData["MachineId"] =
new
SelectList(_context.Machines.OrderBy(m => m.Company.Name).ThenBy(m => m.Name).Select(m => new {m.Id, Name = $"{m.Company.Name} {m.Name}"}),
"Id", "Name");
ViewData["UserId"] =
new SelectList(_context.Users.OrderBy(u => u.UserName).Select(u => new {u.Id, u.UserName}), "Id",
"UserName");
return View(); return View();
} }
@@ -66,31 +71,36 @@ namespace cicm_web.Areas.Admin.Controllers
// more details see http://go.microsoft.com/fwlink/?LinkId=317598. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("AcquisitionDate,LostDate,Status,LastStatusDate,Trade,Boxed,Manuals,SerialNumber,SerialNumberVisible,MachineId,Id")] OwnedMachine ownedMachine) public async Task<IActionResult> Create(
[Bind(
"AcquisitionDate,LostDate,Status,LastStatusDate,Trade,Boxed,Manuals,SerialNumber,SerialNumberVisible,MachineId,UserId,Id")]
OwnedMachine ownedMachine)
{ {
if (ModelState.IsValid) if(ModelState.IsValid)
{ {
_context.Add(ownedMachine); _context.Add(ownedMachine);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));
} }
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", ownedMachine.MachineId);
ViewData["MachineId"] =
new
SelectList(_context.Machines.OrderBy(m => m.Company.Name).ThenBy(m => m.Name).Select(m => new {m.Id, Name = $"{m.Company.Name} {m.Name}"}),
"Id", "Name");
ViewData["UserId"] =
new SelectList(_context.Users.OrderBy(u => u.UserName).Select(u => new {u.Id, u.UserName}), "Id",
"UserName");
return View(ownedMachine); return View(ownedMachine);
} }
// GET: OwnedMachine/Edit/5 // GET: OwnedMachine/Edit/5
public async Task<IActionResult> Edit(long? id) public async Task<IActionResult> Edit(long? id)
{ {
if (id == null) if(id == null) return NotFound();
{
return NotFound(); OwnedMachine ownedMachine = await _context.OwnedMachines.FindAsync(id);
} if(ownedMachine == null) return NotFound();
var ownedMachine = await _context.OwnedMachines.FindAsync(id);
if (ownedMachine == null)
{
return NotFound();
}
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", ownedMachine.MachineId); ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", ownedMachine.MachineId);
return View(ownedMachine); return View(ownedMachine);
} }
@@ -100,33 +110,30 @@ namespace cicm_web.Areas.Admin.Controllers
// more details see http://go.microsoft.com/fwlink/?LinkId=317598. // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost] [HttpPost]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(long id, [Bind("AcquisitionDate,LostDate,Status,LastStatusDate,Trade,Boxed,Manuals,SerialNumber,SerialNumberVisible,MachineId,Id")] OwnedMachine ownedMachine) public async Task<IActionResult> Edit(
long id, [Bind(
"AcquisitionDate,LostDate,Status,LastStatusDate,Trade,Boxed,Manuals,SerialNumber,SerialNumberVisible,MachineId,Id")]
OwnedMachine ownedMachine)
{ {
if (id != ownedMachine.Id) if(id != ownedMachine.Id) return NotFound();
{
return NotFound();
}
if (ModelState.IsValid) if(ModelState.IsValid)
{ {
try try
{ {
_context.Update(ownedMachine); _context.Update(ownedMachine);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
} }
catch (DbUpdateConcurrencyException) catch(DbUpdateConcurrencyException)
{ {
if (!OwnedMachineExists(ownedMachine.Id)) if(!OwnedMachineExists(ownedMachine.Id)) return NotFound();
{
return NotFound(); throw;
}
else
{
throw;
}
} }
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));
} }
ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", ownedMachine.MachineId); ViewData["MachineId"] = new SelectList(_context.Machines, "Id", "Name", ownedMachine.MachineId);
return View(ownedMachine); return View(ownedMachine);
} }
@@ -134,36 +141,30 @@ namespace cicm_web.Areas.Admin.Controllers
// GET: OwnedMachine/Delete/5 // GET: OwnedMachine/Delete/5
public async Task<IActionResult> Delete(long? id) public async Task<IActionResult> Delete(long? id)
{ {
if (id == null) if(id == null) return NotFound();
{
return NotFound();
}
var ownedMachine = await _context.OwnedMachines OwnedMachine ownedMachine = await _context.OwnedMachines
.Include(o => o.Machine) .Include(o => o.Machine).FirstOrDefaultAsync(m => m.Id == id);
.FirstOrDefaultAsync(m => m.Id == id); if(ownedMachine == null) return NotFound();
if (ownedMachine == null)
{
return NotFound();
}
return View(ownedMachine); return View(ownedMachine);
} }
// POST: OwnedMachine/Delete/5 // POST: OwnedMachine/Delete/5
[HttpPost, ActionName("Delete")] [HttpPost]
[ActionName("Delete")]
[ValidateAntiForgeryToken] [ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteConfirmed(long id) public async Task<IActionResult> DeleteConfirmed(long id)
{ {
var ownedMachine = await _context.OwnedMachines.FindAsync(id); OwnedMachine ownedMachine = await _context.OwnedMachines.FindAsync(id);
_context.OwnedMachines.Remove(ownedMachine); _context.OwnedMachines.Remove(ownedMachine);
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index)); return RedirectToAction(nameof(Index));
} }
private bool OwnedMachineExists(long id) bool OwnedMachineExists(long id)
{ {
return _context.OwnedMachines.Any(e => e.Id == id); return _context.OwnedMachines.Any(e => e.Id == id);
} }
} }
} }

View File

@@ -1,3 +1,4 @@
@using Cicm.Database
@model Cicm.Database.Models.OwnedMachine @model Cicm.Database.Models.OwnedMachine
@{ @{
@@ -6,64 +7,122 @@
<h1>Create</h1> <h1>Create</h1>
<h4>OwnedMachine</h4> <h4>Owned machine</h4>
<hr /> <hr />
<div class="row"> <div class="row">
<div class="col-md-4"> <div class="col-md-4">
<form asp-action="Create"> <form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div> <div asp-validation-summary="ModelOnly"
<div class="form-group"> class="text-danger">
<label asp-for="AcquisitionDate" class="control-label"></label>
<input asp-for="AcquisitionDate" class="form-control" />
<span asp-validation-for="AcquisitionDate" class="text-danger"></span>
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="LostDate" class="control-label"></label> <label asp-for="AcquisitionDate"
<input asp-for="LostDate" class="form-control" /> class="control-label">
<span asp-validation-for="LostDate" class="text-danger"></span> </label>
<input asp-for="AcquisitionDate"
class="form-control" />
<span asp-validation-for="AcquisitionDate"
class="text-danger">
</span>
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="Status" class="control-label"></label> <label asp-for="LostDate"
<select asp-for="Status" class="form-control"></select> class="control-label">
<span asp-validation-for="Status" class="text-danger"></span> </label>
<input asp-for="LostDate"
class="form-control" />
<span asp-validation-for="LostDate"
class="text-danger">
</span>
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="LastStatusDate" class="control-label"></label> <label asp-for="Status"
<input asp-for="LastStatusDate" class="form-control" /> class="control-label">
<span asp-validation-for="LastStatusDate" class="text-danger"></span> </label>
<select asp-for="Status"
class="form-control"
asp-items="Html.GetEnumSelectList<StatusType>().OrderBy(s => s.Text)">
</select>
<span asp-validation-for="Status"
class="text-danger">
</span>
</div> </div>
<div class="form-group form-check"> <div class="form-group">
<label asp-for="LastStatusDate"
class="control-label">
</label>
<input asp-for="LastStatusDate"
class="form-control" />
<span asp-validation-for="LastStatusDate"
class="text-danger">
</span>
</div>
<div class="form-check form-group">
<label class="form-check-label"> <label class="form-check-label">
<input class="form-check-input" asp-for="Trade" /> @Html.DisplayNameFor(model => model.Trade) <input class="form-check-input"
asp-for="Trade" /> @Html.DisplayNameFor(model => model.Trade)
</label> </label>
</div> </div>
<div class="form-group form-check"> <div class="form-check form-group">
<label class="form-check-label"> <label class="form-check-label">
<input class="form-check-input" asp-for="Boxed" /> @Html.DisplayNameFor(model => model.Boxed) <input class="form-check-input"
asp-for="Boxed" /> @Html.DisplayNameFor(model => model.Boxed)
</label> </label>
</div> </div>
<div class="form-group form-check"> <div class="form-check form-group">
<label class="form-check-label"> <label class="form-check-label">
<input class="form-check-input" asp-for="Manuals" /> @Html.DisplayNameFor(model => model.Manuals) <input class="form-check-input"
asp-for="Manuals" /> @Html.DisplayNameFor(model => model.Manuals)
</label> </label>
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="SerialNumber" class="control-label"></label> <label asp-for="SerialNumber"
<input asp-for="SerialNumber" class="form-control" /> class="control-label">
<span asp-validation-for="SerialNumber" class="text-danger"></span> </label>
<input asp-for="SerialNumber"
class="form-control" />
<span asp-validation-for="SerialNumber"
class="text-danger">
</span>
</div>
<div class="form-check form-group">
<label class="form-check-label">
<input class="form-check-input"
asp-for="SerialNumberVisible"
checked /> @Html.DisplayNameFor(model => model.SerialNumberVisible)
</label>
</div> </div>
<div class="form-group"> <div class="form-group">
<label asp-for="MachineId" class="control-label"></label> <label asp-for="Machine"
<select asp-for="MachineId" class ="form-control" asp-items="ViewBag.MachineId"></select> class="control-label">
</label>
<select asp-for="MachineId"
class="form-control"
asp-items="ViewBag.MachineId">
</select>
</div> </div>
<div class="form-group"> <div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" /> <label asp-for="User"
class="control-label">
</label>
<select asp-for="UserId"
class="form-control"
asp-items="ViewBag.UserId">
</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> </div>
</form> </form>
</div> </div>
</div> </div>
<div> @section Scripts {
<a asp-action="Index">Back to List</a> @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); }
</div> }

View File

@@ -2,7 +2,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework> <TargetFramework>netcoreapp2.2</TargetFramework>
<Version>3.0.99.679</Version> <Version>3.0.99.681</Version>
<Company>Canary Islands Computer Museum</Company> <Company>Canary Islands Computer Museum</Company>
<Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright> <Copyright>Copyright © 2003-2018 Natalia Portillo</Copyright>
<Product>Canary Islands Computer Museum Website</Product> <Product>Canary Islands Computer Museum Website</Product>