public static List <Lease> GetAvailableLeasesByCustomer(int CustomerId)
        {
            List <Lease> leases = new List <Lease>(); // Empty list

            SqlConnection connection = MarinaDB.GetConnection();

            // Gets all information to build the leases. Need to build a slip, dock, and customer to build a lease object
            string query = "SELECT l.ID AS LeaseId, s.ID AS SlipId, s.Width, s.Length, d.ID AS DockId, d.Name, d.WaterService, d.ElectricalService, c.ID AS CustomerId, c.FirstName, c.LastName, c.Phone, c.City   " +
                           "FROM Lease l JOIN Slip s " +
                           "ON l.SlipId = s.ID " +
                           "JOIN Customer c " +
                           "ON l.CustomerID = c.ID " +
                           "JOIN Dock d " +
                           "ON s.DockId = d.ID " +
                           "WHERE c.ID = @CustomerId ; ";

            SqlCommand cmd = new SqlCommand(query, connection);

            cmd.Parameters.AddWithValue("@CustomerId", CustomerId);

            connection.Open();
            SqlDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);

            while (reader.Read())
            {
                // Create dock (for slip) , slip, and customer assigned to lease
                Dock dock = new Dock(
                    (int)reader["DockId"],
                    reader["Name"].ToString(),
                    ((int)reader["WaterService"]) == 1,
                    ((int)reader["ElectricalService"]) == 1);
                Slip slip = new Slip(
                    (int)reader["SlipId"],
                    (int)reader["Width"],
                    (int)reader["Length"],
                    dock);

                Customer newcustomer = new Customer(
                    (int)reader["CustomerId"],
                    reader["FirstName"].ToString(),
                    reader["LastName"].ToString(),
                    reader["Phone"].ToString(),
                    reader["City"].ToString());

                // Add new lease to list
                leases.Add(new Lease((int)reader["LeaseId"], slip, newcustomer));
            }
            reader.Close();
            return(leases);
        }
Example #2
0
        }                                      // Customer who bought lease

        /// <summary>
        /// Represents a lease.
        /// </summary>
        /// <param name="leaseId">Lease id</param>
        /// <param name="slip">Slip assigned to customer</param>
        /// <param name="customer">Customer who leased slip</param>
        public Lease(int leaseId, Slip slip, Customer customer)
        {
            LeaseId  = leaseId;
            Slip     = slip ?? throw new ArgumentNullException(nameof(slip));
            Customer = customer ?? throw new ArgumentNullException(nameof(customer));
        }