コード例 #1
0
 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));
     }
 }
コード例 #2
0
        public async Task <IActionResult> Update([FromRoute] Guid tripId, [FromBody] UpdateTripRequest request)
        {
            var trip = new Trip
            {
                Id   = tripId,
                Name = request.Name
            };

            var updated = await _tripService.UpdateTripAsync(trip);

            if (updated)
            {
                return(Ok(trip));
            }

            return(NotFound());
        }
コード例 #3
0
        public async Task <IActionResult> UpdateTrip(int tripId, [FromBody] UpdateTripRequest request)
        {
            await Mediator.Send(new UpdateTripCommand(tripId, request));

            return(Ok());
        }
コード例 #4
0
        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);
        }