public async Task <FETrip> Create(FETrip newTrip) { Trip tripToAdd = new Trip(); UpdateTrip(ref tripToAdd, newTrip); appDbContext.Trips.Add(tripToAdd); await appDbContext.SaveChangesAsync(); newTrip.ID = tripToAdd.TripId; return(newTrip); }
public async Task <ActionResult <FETrip> > Post([FromBody] FETrip newTrip) //new instance of an object everytime { try { newTrip = await tripsRepository.Create(newTrip); return(CreatedAtAction(nameof(GetAll), new { id = newTrip.ID }, newTrip)); // return all trips after; } catch (Exception e) { return(StatusCode(500, e)); } }
void UpdateTrip(ref Trip tripToUpdate, FETrip newTrip) { tripToUpdate.DriverId = newTrip.DriverID; tripToUpdate.Origin = newTrip.Destination == "Vilnius" ? "Kaunas" : "Vilnius"; tripToUpdate.Destination = newTrip.Destination; tripToUpdate.LeavingDate = newTrip.LeavingDate; tripToUpdate.ReturnDate = newTrip.ReturnDate; tripToUpdate.MaxSeats = newTrip.MaxSeats; tripToUpdate.CarPlate = newTrip.CarType != "Rental" ? newTrip.CarPlate : null; tripToUpdate.CarType = newTrip.CarType; tripToUpdate.Status = newTrip.Status; tripToUpdate.Comments = newTrip.Comments; }
public async Task <ActionResult <FETrip> > Put([FromBody] FETrip newTrip, [FromQuery] UpdateTripRequest updateTripRequest) //same result with same request { try { return(await tripsRepository.Update(newTrip, updateTripRequest)); } catch (InvalidOperationException e) { return(BadRequest(e)); } catch (Exception e) { return(StatusCode(500, e)); } }
public async Task <FETrip> Update(FETrip newTrip, UpdateTripRequest updateTripRequest) { var passengers = GetListOfPassengers(newTrip.ID.Value); if (updateTripRequest != null) { if (updateTripRequest.PassengerId != null) { if (updateTripRequest.IsLeaving.Value) { var passengerToRemove = newTrip.Passengers.FirstOrDefault(passenger => passenger.UserId == updateTripRequest.PassengerId); newTrip.Passengers.Remove(passengerToRemove); RemovePassenger(passengerToRemove); } else { if (!passengers.Exists(p => p.UserId == updateTripRequest.PassengerId)) { if (newTrip.DriverID == updateTripRequest.PassengerId.Value) { throw new InvalidOperationException(ErrorMessages.JoinOwnTrip); } if (newTrip.LeavingDate < DateTime.Now) { throw new InvalidOperationException(ErrorMessages.JoinPastTrip); } if (newTrip.MaxSeats <= passengers.Count) { throw new InvalidOperationException(ErrorMessages.NoAvailableSeatsLeft); } FEPassenger passengerToAdd = new FEPassenger { TripId = (int)newTrip.ID, UserId = (int)updateTripRequest.PassengerId, FullName = appDbContext.Users.FirstOrDefault(user => user.UserId == updateTripRequest.PassengerId).FullName }; newTrip.Passengers.Add(passengerToAdd); AddPassenger(passengerToAdd, newTrip.ID.Value); } } } } if (newTrip.ID != null) { int index = (int)newTrip.ID; Trip oldTrip = (from t in appDbContext.Trips where t.TripId == index select t).SingleOrDefault(); Trip updatedTrip = new Trip(); updatedTrip = oldTrip; if (oldTrip != null) { UpdateTrip(ref updatedTrip, newTrip); appDbContext.Entry(oldTrip).State = EntityState.Detached; appDbContext.Entry(updatedTrip).State = EntityState.Modified; await appDbContext.SaveChangesAsync(); } else { newTrip = await Create(newTrip); } } else { newTrip = await Create(newTrip); } return(newTrip); }