public ActionResult <ResponseDTOGet <GetUserResponseDTO> > Get([FromBody] GetRequestDTO getUserRequest) { using (var dbContext = new carpoolingContext()) { try { var result = (from client in dbContext.Client join auth in dbContext.Authdetail on client.AuthId equals auth.Id join session in dbContext.Sessiondetail on auth.Id equals session.AuthId where session.SessionId == getUserRequest.session_id && auth.Email == getUserRequest.email select new { name = client.Name, phone = client.Phone, rating = client.Rating, gender = client.Gender, isActive = session.IsActive, endTime = session.EndTime, isDriver = client.IsDriver }).First(); if (result == null) { throw new System.InvalidOperationException("no data found"); } if ((bool)result.isActive && (DateTime)result.endTime > DateTime.Now) { GetUserResponseDTO userData = new GetUserResponseDTO((string)result.name, (string)result.phone, (int)result.rating, (string)result.gender, (bool)result.isDriver); return(new ResponseDTOGet <GetUserResponseDTO>(200, "success", userData)); } else { throw new System.InvalidOperationException("incorrect session id"); } } catch (Exception e) { return(new ResponseDTOGet <GetUserResponseDTO>(400, e.ToString(), null)); } } }
public ActionResult <ResponseDTOGet <object> > getUser() { using (var dbContext = new carpoolingContext()) { try { var results = (from client in dbContext.Client join auth in dbContext.Authdetail on client.AuthId equals auth.Id select new { Id = client.Id, Name = client.Name, Gender = client.Gender, Phone = client.Phone, Rating = client.Rating, Email = auth.Email }).ToArray(); return(new ResponseDTOGet <object>(400, "success", results)); } catch (Exception ex) { return(new ResponseDTOGet <object>(400, ex.ToString(), null)); } } }
public ActionResult <ResponseDTO> Register([FromBody] AdminRegisterationRequestDTO registrationRequest) { using (var transaction = new TransactionScope()) { using (var dbContext = new carpoolingContext()) { try { PasswordManagment manager = new PasswordManagment(); string salt; Authdetail auth = new Authdetail { Email = registrationRequest.email, Password = manager.generateHash(registrationRequest.password, out salt), Salt = salt }; dbContext.Authdetail.Add(auth); dbContext.SaveChanges(); Administrator admin = new Administrator { AuthId = auth.Id }; dbContext.Administrator.Add(admin); dbContext.SaveChanges(); return(new ResponseDTO(200, "success")); } catch (Exception ex) { transaction.Dispose(); return(new ResponseDTO(400, ex.ToString())); } } } }
public ActionResult <ResponseDTOGet <object> > bookRide([FromBody] BookTripRequestDTO bookRideRequestDTO) { using (var transaction = new TransactionScope()) { using (var dbContext = new carpoolingContext()) { try { object user_id; using (var connection = (SqlConnection)dbContext.Database.GetDbConnection()) { connection.Open(); var command = connection.CreateCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "getUserIdFromSession"; command.Parameters.AddWithValue("@email", bookRideRequestDTO.email); command.Parameters.AddWithValue("@session_id", bookRideRequestDTO.session_id); command.Parameters.AddWithValue("@date_time", DateTime.Now.ToString()); user_id = command.ExecuteScalar(); if (user_id == null) { throw new Exception(); } Ride ride = dbContext.Ride.FirstOrDefault(ride => (bookRideRequestDTO.ride_id == ride.Id && ride.Seats >= bookRideRequestDTO.seats)); if (ride == null) { throw new Exception("Invalid input"); } ride.Seats -= bookRideRequestDTO.seats; dbContext.Ride.Update(ride); dbContext.SaveChanges(); Trip trip = new Trip() { PassengerId = (long)user_id, RideId = bookRideRequestDTO.ride_id, Time = bookRideRequestDTO.time, Seats = bookRideRequestDTO.seats }; dbContext.Trip.Add(trip); dbContext.SaveChanges(); transaction.Complete(); connection.Close(); return(new ResponseDTOGet <object>(200, "success", new { trip_id = trip.Id })); } } catch (Exception ex) { transaction.Dispose(); return(new ResponseDTOGet <object>(400, ex.ToString(), null)); } } } }
public ActionResult <ResponseDTO> cancelRide([FromBody] CancelTripRequestDTO cancelTripRequestDTO) { using (var transaction = new TransactionScope()) { using (var dbContext = new carpoolingContext()) { try { object user_id; using (var connection = (SqlConnection)dbContext.Database.GetDbConnection()) { connection.Open(); var command = connection.CreateCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "getUserIdFromSession"; command.Parameters.AddWithValue("@email", cancelTripRequestDTO.email); command.Parameters.AddWithValue("@session_id", cancelTripRequestDTO.session_id); command.Parameters.AddWithValue("@date_time", DateTime.Now.ToString()); user_id = command.ExecuteScalar(); if (user_id == null) { throw new Exception(); } Trip oldTrip = dbContext.Trip.FirstOrDefault(trip => trip.Id == cancelTripRequestDTO.tripID); if (oldTrip == null) { throw new Exception("Invalid input"); } Ride ride = dbContext.Ride.FirstOrDefault(ride => oldTrip.RideId == ride.Id); if (ride == null) { throw new Exception("Invalid input"); } ride.Seats += oldTrip.Seats; dbContext.Ride.Update(ride); dbContext.SaveChanges(); dbContext.Trip.Remove(oldTrip); dbContext.SaveChanges(); transaction.Complete(); connection.Close(); return(new ResponseDTO(200, "success")); } } catch (Exception ex) { transaction.Dispose(); return(new ResponseDTO(400, ex.ToString())); } } } }
public ActionResult <ResponseDTO> setDriver([FromBody] BecomeDriverRequestDTO driverRequestDTO) { using (var transaction = new TransactionScope()) { using (var dbContext = new carpoolingContext()) { try { object user_id; using (var connection = (SqlConnection)dbContext.Database.GetDbConnection()) { connection.Open(); var command = connection.CreateCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "getUserIdFromSession"; command.Parameters.AddWithValue("@email", driverRequestDTO.email); command.Parameters.AddWithValue("@session_id", driverRequestDTO.session_id); command.Parameters.AddWithValue("@date_time", DateTime.Now.ToString()); user_id = command.ExecuteScalar(); if (user_id == null) { throw new Exception(); } Client client = dbContext.Client.FirstOrDefault(client => client.Id == (long)user_id); client.IsDriver = true; dbContext.Client.Update(client); dbContext.SaveChanges(); Vehicle vehicle = new Vehicle { UserId = (long)user_id, Model = driverRequestDTO.model, Description = driverRequestDTO.description, Plate = driverRequestDTO.plate }; dbContext.Vehicle.Add(vehicle); dbContext.SaveChanges(); transaction.Complete(); connection.Close(); return(new ResponseDTO(200, "success")); } } catch (Exception ex) { transaction.Dispose(); return(new ResponseDTO(400, ex.ToString())); } } } }
public ActionResult <ResponseDTO> Login([FromBody] LoginRequestDTO loginRequest) { using (var transaction = new TransactionScope()) { using (var dbContext = new carpoolingContext()) { try { Authdetail authDetail = dbContext.Authdetail.FirstOrDefault(authDetail => authDetail.Email == loginRequest.email); if (authDetail == null) { throw new Exception("Email not found"); } if (!passwordManager.verifyHash(loginRequest.password, authDetail.Password, authDetail.Salt)) { throw new Exception("Incorrect password"); } string sessionId = sessioNManager.getSessionID(loginRequest.email, authDetail.Salt); Sessiondetail oldSession = dbContext.Sessiondetail.FirstOrDefault(session => (session.AuthId == authDetail.Id && session.IsActive == true)); if (oldSession != null) { oldSession.IsActive = false; dbContext.Sessiondetail.Update(oldSession); dbContext.SaveChanges(); } Sessiondetail session = new Sessiondetail { IsActive = true, StartTime = DateTime.Now, EndTime = DateTime.Now.AddMonths(6), AuthId = authDetail.Id, SessionId = sessionId }; dbContext.Sessiondetail.Add(session); dbContext.SaveChanges(); transaction.Complete(); Response.Headers.Add("session", session.SessionId); return(new ResponseDTO(200, "success")); } catch (Exception ex) { transaction.Dispose(); return(new ResponseDTO(400, ex.ToString())); } } } }
public ActionResult <ResponseDTOGet <object> > getRide([FromBody] GetRideRequestDTO rideRequestDTO) { using (var dbContext = new carpoolingContext()) { try { var results = (from ride in dbContext.Ride join vehicle in dbContext.Vehicle on ride.VehicleId equals vehicle.Id join client in dbContext.Client on vehicle.UserId equals client.Id where ride.Time >= DateTime.Now select new { ride.Id, ride.Fee, ride.EndLoc, ride.StartLoc, ride.Time, client.Gender, client.Phone, ride.Seats, vehicle.Plate, client.Name, vehicle.Model, Location = dbContext.Location.Where(location => location.RideId == ride.Id).ToArray() } ).ToArray(); List <object> rides = new List <object>(); foreach (var ride in results) { bool pickup = false; bool dropoff = false; foreach (var location in ride.Location) { double pickUp_d = HaversineMethod.getDistance(location.Lat, location.Lon, rideRequestDTO.pickUp.lat, rideRequestDTO.pickUp.lon); double dropOff_d = HaversineMethod.getDistance(location.Lat, location.Lon, rideRequestDTO.dropOff.lat, rideRequestDTO.dropOff.lon); if (pickUp_d < rideRequestDTO.radius) { pickup = true; } if (dropOff_d < rideRequestDTO.radius) { dropoff = true; } if (pickup == true && dropoff == true) { rides.Add(new { rideId = ride.Id, fee = ride.Fee, endLoc = ride.EndLoc, startLoc = ride.StartLoc, time = ride.Time, seats = ride.Seats, gender = ride.Gender, phone = ride.Phone }); break; } } } return(new ResponseDTOGet <object>(200, "success", rides)); } catch (Exception ex) { return(new ResponseDTOGet <object>(400, ex.ToString(), null)); } } }
public ActionResult <ResponseDTO> setPreference([FromBody] SetPreferenceRequestDTO preferencesRequestDTO) { using (var transaction = new TransactionScope()) { using (var dbContext = new carpoolingContext()) { try { object user_id; using (var connection = (SqlConnection)dbContext.Database.GetDbConnection()) { connection.Open(); var command = connection.CreateCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "getUserIdFromSession"; command.Parameters.AddWithValue("@email", preferencesRequestDTO.email); command.Parameters.AddWithValue("@session_id", preferencesRequestDTO.session_id); command.Parameters.AddWithValue("@date_time", DateTime.Now.ToString()); user_id = command.ExecuteScalar(); if (user_id == null) { throw new Exception("Invalid input"); } Preferences preferences = new Preferences { Gender = preferencesRequestDTO.gender, Notification = preferencesRequestDTO.notification, UserId = (long)user_id }; dbContext.Preferences.Add(preferences); dbContext.SaveChanges(); transaction.Complete(); connection.Close(); return(new ResponseDTO(200, "success")); } } catch (Exception ex) { transaction.Dispose(); return(new ResponseDTO(400, ex.ToString())); } } } }
public ActionResult <ResponseDTO> Register([FromBody] RegisterationRequestDTO registrationRequest) { using (var transaction = new TransactionScope()) { using (var dbContext = new carpoolingContext()) { try { Authdetail authDetail = dbContext.Authdetail.FirstOrDefault(authDetail => authDetail.Email == registrationRequest.email); if (authDetail != null) { throw new Exception("Email already exists"); } PasswordManagment manager = new PasswordManagment(); string salt; Authdetail auth = new Authdetail { Email = registrationRequest.email, Password = manager.generateHash(registrationRequest.password, out salt), Salt = salt }; dbContext.Authdetail.Add(auth); dbContext.SaveChanges(); Client client = new Client { AuthId = auth.Id, Gender = registrationRequest.gender, IsDriver = false, Rating = 0, Name = registrationRequest.name, Phone = registrationRequest.phone }; dbContext.Client.Add(client); dbContext.SaveChanges(); transaction.Complete(); return(new ResponseDTO(200, "success")); } catch (Exception ex) { transaction.Dispose(); return(new ResponseDTO(400, ex.ToString())); } } } }
public ActionResult <ResponseDTOGet <object> > getTrip([FromBody] GetRequestDTO tripRequestDTO) { using (var dbContext = new carpoolingContext()) { try { object user_id; using (var connection = (SqlConnection)dbContext.Database.GetDbConnection()) { connection.Open(); var command = connection.CreateCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "getUserIdFromSession"; command.Parameters.AddWithValue("@email", tripRequestDTO.email); command.Parameters.AddWithValue("@session_id", tripRequestDTO.session_id); command.Parameters.AddWithValue("@date_time", DateTime.Now.ToString()); user_id = command.ExecuteScalar(); if (user_id == null) { throw new Exception(); } var results = (from client in dbContext.Client join trip in dbContext.Trip on client.Id equals trip.PassengerId join ride in dbContext.Ride on trip.RideId equals ride.Id join vehicle in dbContext.Vehicle on ride.VehicleId equals vehicle.Id where client.Id == (long)user_id select new { trip.Id, trip.Time, trip.Seats, ride.StartLoc, ride.EndLoc, ride.Fee, vehicle.Plate }).ToList(); connection.Close(); return(new ResponseDTOGet <object>(200, "success", results)); } } catch (Exception ex) { return(new ResponseDTOGet <object>(400, ex.ToString(), null)); } } }
public ActionResult <ResponseDTOGet <object> > getTrip() { using (var dbContext = new carpoolingContext()) { try { var results = (from trip in dbContext.Trip join client in dbContext.Client on trip.PassengerId equals client.Id select new { Id = trip.Id, Time = trip.Time, Passenger_name = client.Name, Seats = trip.Seats, }).ToArray(); return(new ResponseDTOGet <object>(400, "success", results)); } catch (Exception ex) { return(new ResponseDTOGet <object>(400, ex.ToString(), null)); } } }
public ActionResult <ResponseDTOGet <object> > getRide() { using (var dbContext = new carpoolingContext()) { try { var results = (from ride in dbContext.Ride select new { Id = ride.Id, Time = ride.Time, Start_loc = ride.StartLoc, End_loc = ride.EndLoc, Seats = ride.Seats, Fees = ride.Fee }).ToArray(); return(new ResponseDTOGet <object>(400, "success", results)); } catch (Exception ex) { return(new ResponseDTOGet <object>(400, ex.ToString(), null)); } } }
public ActionResult <ResponseDTO> addRide([FromBody] AddRideRequestDTO rideRequestDTO) { using (var transaction = new TransactionScope()) { using (var dbContext = new carpoolingContext()) { try { object user_id; using (var connection = (SqlConnection)dbContext.Database.GetDbConnection()) { connection.Open(); var command = connection.CreateCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = "getUserIdFromSession"; command.Parameters.AddWithValue("@email", rideRequestDTO.email); command.Parameters.AddWithValue("@session_id", rideRequestDTO.session_id); command.Parameters.AddWithValue("@date_time", DateTime.Now.ToString()); user_id = command.ExecuteScalar(); if (user_id == null) { throw new Exception(); } var result = dbContext.Vehicle.Where(vehicle => vehicle.UserId == (long)user_id).First(); Ride ride = new Ride { VehicleId = result.Id, Seats = rideRequestDTO.seats, Fee = rideRequestDTO.fee, StartLoc = rideRequestDTO.startLocation, EndLoc = rideRequestDTO.endLocation, Time = rideRequestDTO.time, }; dbContext.Ride.Add(ride); dbContext.SaveChanges(); IList <Location> location = new List <Location>(); foreach (LocationObj _location in rideRequestDTO.locations) { location.Add(new Location() { Lat = _location.lat, Lon = _location.lon, RideId = ride.Id }); } dbContext.Location.AddRange(location); dbContext.SaveChanges(); transaction.Complete(); connection.Close(); return(new ResponseDTO(200, "success")); } } catch (Exception ex) { transaction.Dispose(); return(new ResponseDTO(400, ex.ToString())); } } } }