public IHttpActionResult PostTrip(TripsViewModel trip) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Trip tripdb = new Trip(); tripdb.Bus = db.Buses.First(b => b.Id == trip.busId); tripdb.Date = trip.date; tripdb.ArrivalDate = trip.arrivalDate; List <ClientTrip> clientsDb = new List <ClientTrip>(); foreach (var item in trip.clients) { ClientTrip clientTrip = new ClientTrip(); clientTrip.Client = db.Clients.First(i => i.Id == item.Id); clientTrip.From = db.Cities.First(i => i.Name == item.From); clientTrip.To = db.Cities.First(i => i.Name == item.To); clientTrip.Price = item.Price; clientTrip.IsStayInBus = item.IsStayInBus; clientTrip.HasBaggage = item.HasBaggage; clientTrip.Agent = db.Agents.First(x => x.Id == item.AgentId); clientTrip.AgentPrice = item.AgentPrice; clientTrip.AdditionalExpenses = item.AdditionalExpenses; clientsDb.Add(clientTrip); } tripdb.ClientTrip = clientsDb; tripdb.CityFrom = db.Cities.First(c => c.Id == trip.cityFrom); tripdb.CityTo = db.Cities.First(c => c.Id == trip.cityTo); tripdb.Comments = trip.comments; db.Trips.Add(tripdb); db.SaveChanges(); return(CreatedAtRoute("DefaultApi", new { id = tripdb.Id }, trip)); }
public async Task <IActionResult> Post([FromBody] TripReserveDto tripReserveDto) { await _HubContext.Clients.All.SendAsync("Driver", "Hello From Another Application"); return(Ok()); string userId = User.FindFirstValue(ClaimTypes.NameIdentifier); string userRole = User.FindFirstValue(ClaimTypes.Role); var currentUser = await this.userRepository.FindOneById(int.Parse(userId)); if (currentUser == null || currentUser.Role.ToString() != userRole.ToString()) { return(Unauthorized()); } var originPoint = new Point(tripReserveDto.OriginLocation.Lat, tripReserveDto.OriginLocation.Lng) { SRID = 4326 }; var destinationPoint = new Point(tripReserveDto.DistanceLocation.Lat, tripReserveDto.DistanceLocation.Lng) { SRID = 4326 }; var trip = await this.tripRepository.FindTripNearestByLocation(originPoint, destinationPoint); var originPlace = new Place { Location = originPoint, Name = tripReserveDto.OriginAddress }; var destantPlace = new Place { Location = destinationPoint, Name = tripReserveDto.DistantAddress }; if (trip == null) { Driver driver = await this.driverRepository.FindAnyDriver(); await _HubContext.Clients.All.SendAsync("Driver", "Hello From Another Application"); return(Ok()); if (driver == null) { return(UnprocessableEntity()); } else { var newTrip = new Trip { StartLocation = originPlace, FinalLocation = destantPlace, DriverId = driver.Id, Status = Models.enums.TripStatus.PICKING_USER, }; tripRepository.Add(newTrip); await this.tripRepository.SaveAll(); var clientInTrip = new ClientTrip() { ClientId = currentUser.Id, Status = Models.enums.ClientTripStatus.WAITING_FOR_DRIVER, FromLocation = originPlace, ToLocation = destantPlace, StartedAt = DateTime.Now, }; newTrip.Clients.Add(clientInTrip); tripRepository.Add(newTrip); await this.tripRepository.SaveAll(); var point = new ClientTripPointAtTime { ClientId = currentUser.Id, TripId = newTrip.Id, Location = originPoint, Time = DateTime.Now }; clientInTrip.Points.Add(point); clientTripRepository.Add(clientInTrip); await this.clientTripRepository.SaveAll(); var expectedPoints = TripHelper.convertFromDirectionToListOfExpectedRoutes( await getDirectionInfo(new TripCheckQuery { OriginLat = tripReserveDto.OriginLocation.Lat, OriginLng = tripReserveDto.OriginLocation.Lng, DestinationLat = tripReserveDto.DistanceLocation.Lat, DestinationLng = tripReserveDto.DistanceLocation.Lng }) ); //trip.ExpectedRoad = expectedPoints; tripRepository.Add(newTrip); if (await tripRepository.SaveAll()) { return(Ok(trip)); } else { return(StatusCode(500)); } } } else { var clientInTrip = new ClientTrip() { ClientId = currentUser.Id, Status = Models.enums.ClientTripStatus.WAITING_FOR_DRIVER, FromLocation = originPlace, ToLocation = destantPlace, StartedAt = DateTime.Now }; var point = new ClientTripPointAtTime { ClientId = currentUser.Id, TripId = trip.Id, Location = originPoint, Time = DateTime.Now }; clientInTrip.Points.Add(point); trip.Clients.Add(clientInTrip); trip.Status = Models.enums.TripStatus.ANOTHER_CLIENT; tripRepository.Update(trip); if (await tripRepository.SaveAll()) { return(Ok(trip)); } else { return(StatusCode(500)); } } }