public bool RepairTaskIsChanged(RepairTaskEditInputModel repairTaskEditInputModel) { RepairTask repairTask = this.GetById(repairTaskEditInputModel.Id); List <Part> repairTaskOldRequiredParts = new List <Part>(repairTask.PartsRequired).OrderBy(x => x.Type).ToList(); List <Part> repairTaskNewRequiredParts = new List <Part>(); if (repairTaskEditInputModel.IsCarBodyPart) { repairTaskNewRequiredParts.Add(new Part { Type = PartType.CarBody, Quantity = repairTaskEditInputModel.CarBodyPartAmount }); } if (repairTaskEditInputModel.IsChassisPart) { repairTaskNewRequiredParts.Add(new Part { Type = PartType.Chassis, Quantity = repairTaskEditInputModel.ChassisPartAmount }); } if (repairTaskEditInputModel.IsElectronicPart) { repairTaskNewRequiredParts.Add(new Part { Type = PartType.Electronic, Quantity = repairTaskEditInputModel.ElectronicPartAmount }); } if (repairTaskEditInputModel.IsInteriorPart) { repairTaskNewRequiredParts.Add(new Part { Type = PartType.Interior, Quantity = repairTaskEditInputModel.InteriorPartAmount }); } repairTaskNewRequiredParts = repairTaskNewRequiredParts.OrderBy(x => x.Type).ToList(); if (repairTaskOldRequiredParts.Count != repairTaskNewRequiredParts.Count) { return(true); } else { for (int i = 0; i < repairTaskOldRequiredParts.Count; i++) { if (repairTaskOldRequiredParts[i].Quantity != repairTaskNewRequiredParts[i].Quantity) { return(true); } } } return(false); }
public IActionResult EditRepairTask(int id) { RepairTask repairTask = this.repairTaskService.GetById(id); if (repairTask.User.UserName != this.User.Identity.Name) { return(this.Unauthorized()); } //RepairTaskEditInputModel v = new RepairTaskEditInputModel { // IsCarBodyPart = repairTask.PartsRequired.Where(x => x.Type == Models.Enums.PartType.CarBody).Any(y => y.Quantity > 0) //} RepairTaskEditInputModel repairTaskEditInputModel = this.mapper.Map <RepairTaskEditInputModel>(repairTask); return(this.View(repairTaskEditInputModel)); }
public async Task CanUpdateAllRepairTaskPartsRequired() { RepairTask repairTask = new RepairTask { Id = 1, PartsRequired = new Part[] { new Part { Type = PartType.CarBody, Quantity = 10 }, new Part { Type = PartType.Interior, Quantity = 0 }, new Part { Type = PartType.Electronic, Quantity = 0 }, new Part { Type = PartType.Chassis, Quantity = 0 }, } }; this.dbContext.RepairTasks.Add(repairTask); this.dbContext.SaveChanges(); RepairTaskEditInputModel inputModel = new RepairTaskEditInputModel { IsCarBodyPart = true, CarBodyPartAmount = 5, IsChassisPart = true, ChassisPartAmount = 2, IsElectronicPart = true, ElectronicPartAmount = 3, IsInteriorPart = true, InteriorPartAmount = 1, Id = 1 }; await this.RepairTaskService.TryUpdateRepairTaskAsync(inputModel); Assert.Equal(inputModel.CarBodyPartAmount, repairTask.PartsRequired.First(x => x.Type == PartType.CarBody).Quantity); Assert.Equal(inputModel.ChassisPartAmount, repairTask.PartsRequired.First(x => x.Type == PartType.Chassis).Quantity); Assert.Equal(inputModel.ElectronicPartAmount, repairTask.PartsRequired.First(x => x.Type == PartType.Electronic).Quantity); Assert.Equal(inputModel.InteriorPartAmount, repairTask.PartsRequired.First(x => x.Type == PartType.Interior).Quantity); }
public async Task <IActionResult> EditRepairTask(RepairTaskEditInputModel repairTaskEditInputModel) { if (this.ModelState.IsValid == false) { return(this.View(repairTaskEditInputModel)); } if (this.repairTaskService.RepairTaskIsChanged(repairTaskEditInputModel) == false) { this.ModelState.AddModelError("", StringConstants.NoChangedInRepairTaskError); return(this.View(repairTaskEditInputModel)); } bool success = await this.repairTaskService.TryUpdateRepairTaskAsync(repairTaskEditInputModel); if (success == false) { this.ModelState.AddModelError("", StringConstants.RepairTaskChangesNotPossibleError); return(this.View(repairTaskEditInputModel)); } return(this.RedirectToAction(StringConstants.ActionNameRepairTaskDetails, new { repairTaskEditInputModel.Id })); }
public void CanDetectThatARepairTaskOrderIsChanged() { RepairTask repairTask = new RepairTask { Id = 1, PartsRequired = new Part[] { new Part { Type = PartType.CarBody, Quantity = 10 } } }; this.dbContext.RepairTasks.Add(repairTask); this.dbContext.SaveChanges(); RepairTaskEditInputModel inputModel = new RepairTaskEditInputModel { Id = 1, IsCarBodyPart = true, CarBodyPartAmount = 5 }; Assert.True(this.RepairTaskService.RepairTaskIsChanged(inputModel)); }
public async Task CannotLeaveARepairTaskWithNoParts() { RepairTask repairTask = new RepairTask { Id = 1, PartsRequired = new Part[] { new Part { Type = PartType.CarBody, Quantity = 10 } } }; this.dbContext.RepairTasks.Add(repairTask); this.dbContext.SaveChanges(); RepairTaskEditInputModel inputModel = new RepairTaskEditInputModel { IsCarBodyPart = true, CarBodyPartAmount = 0, Id = 1 }; Assert.False(await this.RepairTaskService.TryUpdateRepairTaskAsync(inputModel)); }
public async Task CanUpdateARepairTaskRequiredPartsQuantity() { RepairTask repairTask = new RepairTask { Id = 1, PartsRequired = new Part[] { new Part { Type = PartType.CarBody, Quantity = 10 } } }; this.dbContext.RepairTasks.Add(repairTask); this.dbContext.SaveChanges(); RepairTaskEditInputModel inputModel = new RepairTaskEditInputModel { IsCarBodyPart = true, CarBodyPartAmount = 5, Id = 1 }; await this.RepairTaskService.TryUpdateRepairTaskAsync(inputModel); Assert.Equal(inputModel.CarBodyPartAmount, repairTask.PartsRequired.First().Quantity); }
public async Task <bool> TryUpdateRepairTaskAsync(RepairTaskEditInputModel repairTaskEditInputModel) { RepairTask repairTask = this.GetById(repairTaskEditInputModel.Id); if (repairTaskEditInputModel.IsCarBodyPart) { if (this.partService.PartTypeExists(PartType.CarBody) == false) { throw new ApplicationException(); } Part part = this.GetPartByPartTypeAndRepairTaskId(PartType.CarBody, repairTask.Id); if (part == null) { part = new Part { Type = PartType.CarBody, }; repairTask.PartsRequired.Add(part); } part.Quantity = repairTaskEditInputModel.CarBodyPartAmount; } if (repairTaskEditInputModel.IsChassisPart) { if (this.partService.PartTypeExists(PartType.Chassis) == false) { throw new ArgumentNullException(); } Part part = this.GetPartByPartTypeAndRepairTaskId(PartType.Chassis, repairTask.Id); if (part == null) { part = new Part { Type = PartType.Chassis, }; repairTask.PartsRequired.Add(part); } part.Quantity = repairTaskEditInputModel.ChassisPartAmount; } if (repairTaskEditInputModel.IsElectronicPart) { if (this.partService.PartTypeExists(PartType.Electronic) == false) { throw new ArgumentNullException(); } Part part = this.GetPartByPartTypeAndRepairTaskId(PartType.Electronic, repairTask.Id); if (part == null) { part = new Part { Type = PartType.Electronic, }; repairTask.PartsRequired.Add(part); } ; part.Quantity = repairTaskEditInputModel.ElectronicPartAmount; } if (repairTaskEditInputModel.IsInteriorPart) { if (this.partService.PartTypeExists(PartType.Interior) == false) { throw new ArgumentNullException(); } Part part = this.GetPartByPartTypeAndRepairTaskId(PartType.Interior, repairTask.Id); if (part == null) { part = new Part { Type = PartType.Interior, }; repairTask.PartsRequired.Add(part); } ; part.Quantity = repairTaskEditInputModel.InteriorPartAmount; } //for(int i = 0; i < repairTask.PartsRequired.ToList().Count; i++) { // Part part = repairTask.PartsRequired.ToList()[i]; // if (part.Quantity == 0) { // repairTask.PartsRequired.Remove(part); // } //} //foreach (Part part in repairTask.PartsRequired.ToList()) { // if (part.Quantity == 0) { // repairTask.PartsRequired.Remove(part); // } //} if (repairTask.PartsRequired.All(x => x.Quantity == 0)) { return(false); } else { this.dbContext.Parts.RemoveRange(repairTask.PartsRequired.Where(x => x.Quantity == 0)); } if (await this.dbContext.SaveChangesAsync() == 0) { throw new ApplicationException(); } return(true); }