public ActionResult Equip(int b = 0) { var username = this.User.Identity.Name; var player = this.playersService.GetPlayerByUsername(username); var model = new EquipPartModel(); if (!player.Bikes.Any(x => x.Id == b)) { this.TempData["Error"] = "Bike does not exist!"; return(RedirectToAction(nameof(Index))); } var bike = this.bikesService.GetBikeById(b); var bikeModel = this.mapper.Map <BikeViewModel>(bike); var bikePartModels = bike.BikeParts .Select(x => this.mapper.Map <BikePartViewModel>(x)) .ToList(); model.Parts = bikePartModels; model.Bike = bikeModel; var partModels = player.Inventory.Where(x => x.IsForSale == false && x.IsUsed == false) .Select(x => this.mapper.Map <PlayerPartViewModel>(x)) .ToList(); model.AvailableParts = partModels; return(View(model)); }
public ActionResult Equip(EquipPartModel model) { var username = this.User.Identity.Name; var player = this.playersService.GetPlayerByUsername(username); if (!player.Bikes.Any(x => x.Id == model.BikeId)) { this.ModelState.AddModelError("", "Bike not found!"); } if (model.NewPlayerPartId == 0) { TempData["Error"] = "No part was selected!"; return(RedirectToAction(nameof(Equip), new { b = model.BikeId })); } if (!ModelState.IsValid) { return(View(model)); } var newPlayerPart = this.playersService.GetPlayerPartById(model.NewPlayerPartId); var bike = this.bikesService.GetBikeById(model.BikeId); var bikeParts = bike.BikeParts.Where(x => x.PlayerPart.Part.Type == newPlayerPart.Part.Type); if ((newPlayerPart.Part.Type != PartType.Other && bikeParts.Count() >= 1) || bikeParts.Count() >= 3) { var oldBikePart = bikeParts.FirstOrDefault(); this.playersService.DisusePart(oldBikePart.PlayerPartId); this.bikesService.RemoveBikePart(oldBikePart.Id); } var newBikePart = new BikeParts(); newBikePart.PlayerPart = newPlayerPart; newBikePart.Bike = bike; var addedBikePart = this.bikesService.AddBikePart(newBikePart); this.playersService.UsePart(model.NewPlayerPartId, addedBikePart.Id); return(RedirectToAction(nameof(Equip), new { b = model.BikeId })); }