public void TestAddCarRating() { //Arrange var mock = new Mock <IGenericRepository>(); mock.Setup(x => x.Query <Car>()).Returns(tests.AsQueryable()); var carRatingService = new CarRatingService(mock.Object); var ratingCarTest = new RatingCar() { TireQuality = 3, OutsideCleanliness = 3, InsideCleanliness = 3, EngineOperation = 3, IndoorAirQuality = 3, SafetyFeatures = 3, ElectricalFunctions = 3, DeliveryExperience = 3, ProfessionalismOfOwner = 3, }; //Act var result = carRatingService.CalculateCarRating(this.car, ratingCarTest); //Assert Assert.Equal(result, 3); }
public ActionResult Share(ShareCar model) { try { var element = new RatingCar(); if (model.BodyId != -1) { element.Body = db.Body.FirstOrDefault(x => x.Id == model.BodyId); } if (model.BrandId != -1) { element.Brand = db.Brand.FirstOrDefault(x => x.Id == model.BrandId); } if (model.GenerationId != -1) { element.Generation = db.Generation.FirstOrDefault(x => x.Id == model.GenerationId); } if (model.ModelId != -1) { element.Model = db.Model.FirstOrDefault(x => x.Id == model.ModelId); } if (model.MotorId != -1) { element.Motor = db.Motor.FirstOrDefault(x => x.Id == model.MotorId); } var send = new RCViewVM(); if (element.Brand != null) { send.Brand = element.Brand.Id; } if (element.Body != null) { send.Body = element.Body.Id; } if (element.Generation != null) { send.Generation = element.Generation.Id; } if (element.Model != null) { send.Model = element.Model.Id; } if (element.Motor != null) { send.Motor = element.Motor.Id; } return(RedirectToAction("Car", send)); } catch (Exception e) { e.Data.Values.GetEnumerator(); } return(RedirectToAction("Index")); }
public IActionResult Post(int id, [FromBody] RatingCar carRating) { if (carRating.Id == 0) { _repo.AddCarRating(id, carRating); } else { _repo.UpdateCarRating(carRating); } return(Ok(carRating)); }
/// <summary> /// Method to add a car rating /// </summary> /// <param name="Id"></param> /// <param name="carRating"></param> /// <returns></returns> public decimal AddCarRating(int Id, RatingCar carRating) { var car = _repo.Query <Car>().Where(c => c.Id == Id).Include(c => c.CarRatings).FirstOrDefault(); car.CarRatings.Add(carRating); _repo.SaveChanges(); this.CalculateCarRating(car, carRating); _repo.SaveChanges(); return(car.AverageRating); }
/// <summary> /// Method to calculate the car rating for easier unit testing and separated logic /// </summary> /// <param name="car"></param> /// <param name="carRating"></param> /// <returns></returns> public decimal CalculateCarRating(Car car, RatingCar carRating) { var total = ((carRating.IndoorAirQuality) + (carRating.InsideCleanliness) + (carRating.OutsideCleanliness) + (carRating.ProfessionalismOfOwner) + (carRating.SafetyFeatures) + (carRating.TireQuality) + (carRating.ElectricalFunctions) + (carRating.EngineOperation) + (carRating.DeliveryExperience)) / 9; carRating.OverallRating = total; if (car.CarRatings.Count == 0) { car.AverageRating = (car.AverageRating * car.CarRatings.Count + total) / car.CarRatings.Count; } else { car.AverageRating = ((car.AverageRating * (car.CarRatings.Count - 1) + total)) / car.CarRatings.Count; } return(car.AverageRating); }
/// <summary> /// Method to update a car rating /// </summary> /// <param name="carRating"></param> public void UpdateCarRating(RatingCar carRating) { var originalCarRating = _repo.Query <RatingCar>().Where(c => c.Id == carRating.Id).FirstOrDefault(); originalCarRating.DeliveryExperience = carRating.DeliveryExperience; originalCarRating.ElectricalFunctions = carRating.ElectricalFunctions; originalCarRating.IndoorAirQuality = carRating.IndoorAirQuality; originalCarRating.InsideCleanliness = carRating.InsideCleanliness; originalCarRating.OutsideCleanliness = carRating.OutsideCleanliness; originalCarRating.SafetyFeatures = carRating.SafetyFeatures; originalCarRating.TireQuality = carRating.TireQuality; originalCarRating.ProfessionalismOfOwner = carRating.ProfessionalismOfOwner; originalCarRating.OverallRating = carRating.OverallRating; _repo.Update <RatingCar>(originalCarRating); }
public async Task <ActionResult> AddDescription(AddDescription AddDescription) { var email = ""; if (AddDescription.Descripton.Trim() != "") { var user = await UserManager.FindByEmailAsync(User.Identity.Name); var Brand = db.Brand.FirstOrDefault(x => x.Id == AddDescription.BrandId); var Body = db.Body.FirstOrDefault(x => x.Id == AddDescription.BodyId); var Generation = db.Generation.FirstOrDefault(x => x.Id == AddDescription.GenerationId); var Model = db.Model.FirstOrDefault(x => x.Id == AddDescription.ModelId); var Motor = db.Motor.FirstOrDefault(x => x.Id == AddDescription.MotorId); var model = new RatingCar() { Brand = Brand, Body = Body, Generation = Generation, Model = Model, Motor = Motor, Description = AddDescription.Descripton, UserId = user.Id, Conveniences = new Rating { Type = "Conveniences", Value = 0 }, Failure = new Rating { Type = "Failure", Value = 0 }, Maintenance = new Rating { Type = "Maintenance", Value = 0 } }; db.RatingCar.Add(model); db.SaveChanges(); email = user.Email; } return(Json(email, JsonRequestBehavior.AllowGet)); }
public async Task <ActionResult> Add(AddRatingCarVM model) { try { var element = new RatingCar(); var user = await UserManager.FindByEmailAsync(User.Identity.Name); element.UserId = user.Id; if (model.BodyId != -1) { element.Body = db.Body.FirstOrDefault(x => x.Id == model.BodyId); } if (model.BrandId != -1) { element.Brand = db.Brand.FirstOrDefault(x => x.Id == model.BrandId); } if (model.GenerationId != -1) { element.Generation = db.Generation.FirstOrDefault(x => x.Id == model.GenerationId); } if (model.ModelId != -1) { element.Model = db.Model.FirstOrDefault(x => x.Id == model.ModelId); } if (model.MotorId != -1) { element.Motor = db.Motor.FirstOrDefault(x => x.Id == model.MotorId); } element.Description = model.Description; element.Conveniences = model.Conveniences; element.Failure = model.Failure; element.Maintenance = model.Maintenance; db.RatingCar.Add(element); await db.SaveChangesAsync(); var send = new RCViewVM(); if (element.Brand != null) { send.Brand = element.Brand.Id; } if (element.Body != null) { send.Body = element.Body.Id; } if (element.Generation != null) { send.Generation = element.Generation.Id; } if (element.Model != null) { send.Model = element.Model.Id; } if (element.Motor != null) { send.Motor = element.Motor.Id; } return(RedirectToAction("Car", send)); } catch (Exception e) { e.Data.Values.GetEnumerator(); } return(RedirectToAction("Index")); }