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 <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 <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> 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())); } } } }