public void FromTO(TOVehicle toVehicle, RentACar rentACar) { Brand = toVehicle.Brand; IsOnSale = toVehicle.IsOnSale; Location = toVehicle.Location; NumOfSeats = toVehicle.NumOfSeats; PricePerDay = toVehicle.PricePerDay; Rating = toVehicle.Rating; RentACar = rentACar; Type = toVehicle.Type; UnavailableDates = new List <UnavailableDate>(); toVehicle.UnavailableDates.ToList().ForEach(date => { DateTime newDate = DateTime.Parse(date.Value.ToString()); UnavailableDates.Add(new UnavailableDate() { DateId = 0, Date = newDate, Vehicle = this }); }); VehicleId = toVehicle.VehicleId; Year = toVehicle.Year; Version = toVehicle.Version; }
public async Task <IActionResult> PutVehicle(int id, TOVehicle toVehicle) { string role = User.Claims.First(c => c.Type == "Roles").Value; if (role != "racAdmin" && role != "racAdminNew") { return(BadRequest("You are not authorised to do this action")); } //Iscitaj vozilo i proveri verziju bool success = false; Vehicle vehicle = await _context.Vehicles.FindAsync(id); if (vehicle.Version != toVehicle.Version) { return(Ok(new { success })); } //Vehicle vehicle = new Vehicle(); var rentACar = _context.RentACars.FirstOrDefault(r => r.Name == toVehicle.RentACar); vehicle.FromTO(toVehicle, rentACar); if (id != vehicle.VehicleId) { return(BadRequest()); } vehicle.Version++; _context.Entry(vehicle).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!VehicleExists(id)) { return(NotFound()); } else { throw; } } success = true; return(Ok(new { success })); }
public async Task <ActionResult <TOVehicle> > PostVehicle(TOVehicle toVehicle) { string role = User.Claims.First(c => c.Type == "Roles").Value; if (role != "racAdmin" && role != "racAdminNew") { return(BadRequest("You are not authorised to do this action")); } Vehicle vehicle = new Vehicle(); var rentACar = _context.RentACars.FirstOrDefault(r => r.Name == toVehicle.RentACar); vehicle.FromTO(toVehicle, rentACar); vehicle.Version = 0; _context.Vehicles.Add(vehicle); await _context.SaveChangesAsync(); return(CreatedAtAction("GetVehicle", new { id = vehicle.VehicleId }, vehicle)); }
public TOVehicle ToTO() { TOVehicle toVehicle = new TOVehicle(); toVehicle.Brand = Brand; toVehicle.IsOnSale = IsOnSale; toVehicle.Location = Location; toVehicle.NumOfSeats = NumOfSeats; toVehicle.PricePerDay = PricePerDay; double ratingSum = 0; if (Ratings.Count != 0) { Ratings.ToList().ForEach(rating => ratingSum += rating.VehicleRatingValue); toVehicle.Rating = ratingSum / Ratings.Count; } else { toVehicle.Rating = 0; } toVehicle.RentACar = RentACar.Name; toVehicle.Type = Type; toVehicle.UnavailableDates = new List <TOPrimaryObject>(); UnavailableDates.ToList().ForEach(date => toVehicle.UnavailableDates.Add( new TOPrimaryObject() { Id = 0, Value = date.Date.ToShortDateString(), Reference = this })); toVehicle.VehicleId = VehicleId; toVehicle.Year = Year; toVehicle.Version = Version; return(toVehicle); }