public Seat(TOSeat seat, DatabaseContext _context) { Name = seat.Name; Discount = seat.Discount; Flight = _context.Flights.Find(seat.FlightId); Occupied = seat.Occupied; SeatId = seat.SeatId; Type = seat.Type; Price = seat.Price; }
public async Task <IActionResult> PutSeat(int id, TOSeat seat, [FromQuery] int version) { if (id != seat.SeatId) { return(BadRequest()); } Flight tempFlight = await _context.Flights.FindAsync(seat.FlightId); var success = false; if (tempFlight.Version != version) { return(Ok(new { success })); } tempFlight.Version++; _context.Entry(tempFlight).State = EntityState.Modified; Seat oldSeat = await _context.Seats .Include(s => s.Flight).ThenInclude(f => f.Airline) .FirstOrDefaultAsync(s => s.SeatId == seat.SeatId); Seat tempSeat = new Seat(seat, _context); if (oldSeat.Discount == 0 && tempSeat.Discount != 0) { FastTicket fastTicket = new FastTicket() { SeatId = tempSeat.SeatId, Airline = tempSeat.Flight.Airline, NewPrice = Math.Round(tempSeat.Price * (1 - (0.01 * tempSeat.Discount))) }; _context.Add(fastTicket); } else if (seat.Discount == 0 && oldSeat.Discount != 0) { FastTicket fastTicket = await _context.FastTickets.FindAsync(tempSeat.SeatId); _context.Remove(fastTicket); } else if (seat.Discount != oldSeat.Discount) { FastTicket oldFastTicket = await _context.FastTickets.FindAsync(tempSeat.SeatId); FastTicket fastTicket = new FastTicket() { Airline = oldFastTicket.Airline, SeatId = oldFastTicket.SeatId, NewPrice = Math.Round(tempSeat.Price * (1 - (0.01 * tempSeat.Discount))) }; _context.Entry(oldFastTicket).CurrentValues.SetValues(fastTicket); } _context.Entry(oldSeat).CurrentValues.SetValues(tempSeat); try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SeatExists(id)) { return(NotFound()); } else { throw; } } success = true; return(Ok(new { success })); }