コード例 #1
0
        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));
                }
            }
        }
コード例 #2
0
        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));
                }
            }
        }