public async Task <IActionResult> Create(PCBindingModel model) { if (!ModelState.IsValid) { TempData["Error"] = "Invalid form."; return(RedirectToAction("Create")); } if (context.PCs.Any(p => p.BuildName == model.BuildName)) { TempData["Error"] = "Build name already exists."; return(RedirectToAction("Create")); } if (model.SolidStateDrive == null && model.HardDiskDrive == null) { TempData["Error"] = "Select at least one drive."; return(RedirectToAction("Create")); } PC pc = await BuildPC(model); context.PCs.Add(pc); await context.SaveChangesAsync(); return(RedirectToAction("Details", new { id = pc.Id })); }
private async Task <PC> BuildPC(PCBindingModel model) { var user = await this.userManager.GetUserAsync(this.User); var pcCase = context.Cases.FirstOrDefault(c => $"{c.Manufacturer} {c.ModelName}" == model.Case); var processor = context.Processors.FirstOrDefault(p => $"{p.Manufacturer} {p.ModelName}" == model.Processor); var graphicsCard = context.GraphicsCards.FirstOrDefault(gc => $"{gc.Manufacturer} {gc.ModelName}" == model.GraphicsCard); var motherboard = context.Motherboards.FirstOrDefault(m => $"{m.Manufacturer} {m.ModelName}" == model.Motherboard); var memory = context.Memory.FirstOrDefault(m => $"{m.Manufacturer} {m.ModelName} {m.Amount} GB" == model.Memory); var powerSupply = context.PowerSupplies.FirstOrDefault(ps => $"{ps.Manufacturer} {ps.ModelName}" == model.PowerSupply); HardDiskDrive hardDiskDrive = context.HardDiskDrives.FirstOrDefault(hdd => $"{hdd.Manufacturer} {hdd.ModelName} {hdd.MemoryCapacity} GB" == model.HardDiskDrive); SolidStateDrive solidStateDrive = context.SolidStateDrives.FirstOrDefault(ssd => $"{ssd.Manufacturer} {ssd.ModelName} {ssd.MemoryCapacity} GB" == model.SolidStateDrive); var pc = new PC() { BuildName = model.BuildName, Case = pcCase, GraphicsCard = graphicsCard, Processor = processor, Memory = memory, Motherboard = motherboard, PowerSupply = powerSupply, User = user, NumberOfMemorySticks = model.NumberOfMemorySticks, TotalPrice = pcCase.Price + graphicsCard.Price + processor.Price + (memory.Price * model.NumberOfMemorySticks) + motherboard.Price + powerSupply.Price }; if (hardDiskDrive != null) { pc.HardDiskDrive = hardDiskDrive; pc.TotalPrice += hardDiskDrive.Price; if (solidStateDrive != null) { pc.SolidStateDrive = solidStateDrive; pc.TotalPrice += solidStateDrive.Price; } else { pc.MaxSystemRating -= 1; } } else { pc.SolidStateDrive = solidStateDrive; pc.TotalPrice += solidStateDrive.Price; pc.MaxSystemRating -= 1; } return(pc); }