Esempio n. 1
0
        public void UpdateMileage(MileageModel model)
        {
            var currentUser = this.currentUserService.CurrentUser();
            var OwnedCar    = Context.OwnedCars.FirstOrDefault(x => x.Vin == model.Vin);

            if (OwnedCar.UserId != currentUser.Id)
            {
                throw new UnauthorizedAccessException();
            }

            if (OwnedCar == null)
            {
                throw new ArgumentException("This Vin has not been added");
            }


            if (model.RecordingDate.HasValue)
            {
                if (model.RecordingDate.Value < OwnedCar.ManufacturedDate)
                {
                    throw new ArgumentException("Recording before manufacture date");
                }
            }
            else
            {
                model.RecordingDate = DateTime.UtcNow;
            }

            var Mileage = float.Parse(model.Mileage);

            var LastMileage = Context.MileageRecordings
                              .Where(x => x.OwnedCarId == OwnedCar.Id)
                              .Where(x => x.RecordingDate < model.RecordingDate)
                              .OrderByDescending(x => x.RecordingDate)
                              .FirstOrDefault()?.Mileage ?? "0";

            if (float.Parse(LastMileage) > Mileage)
            {
                throw new ArgumentException("Mileage is too low");
            }

            var dto = new MileageRecordingDto()
            {
                Mileage       = model.Mileage,
                OwnedCarId    = OwnedCar.Id,
                RecordingDate = model.RecordingDate.Value
            };

            Context.MileageRecordings.Add(dto);
            Context.SaveChanges();
        }
Esempio n. 2
0
 public IActionResult UpdateMileage([FromBody] MileageModel model) => ReturnResult(() => this.mileageService.UpdateMileage(model));