Git Product home page Git Product logo

landlord's Introduction

The Landlord

Prompt:

You are a landlord in need of an app that will track your apartments and tenants.

Directions

Fork/clone this repo. Look at due dates section to figure out which parts are due when.

Due Dates

Wednesday

Friday

Ideally, you should complete the portion you haven't completed yet before starting the next one.

Schema & SQL

Create a schema file

Create a schema.sql in the db folder. It should contain the following:

  • Tenants table (with the following attributes):

    • id
    • name
    • age
    • gender
    • apartment_id
  • Apartments table (with the following attributes):

    • id
    • address
    • monthly_rent
    • sqft
    • num_beds
    • num_baths

Create your Database and Load the Schema File

  1. Create your database
  • $ createdb landlord
  1. Load the schema file
  • $ psql -d landlord < db/schema.sql
  1. Load the seed file
  • $ psql -d landlord < db/seeds.sql

Note: If at any point you need a new clean set of data, run the following commands (replacing database_name and name_of_file.sql with the actual names of the database and files you're using).

$ dropdb database_name
$ createdb database_name
$ psql -d database_name < name_of_schema_file.sql
$ psql -d database_name < name_of_seed_file.sql

Commit! Make a commit before you move on!

Active Record Exercises

Complete the AR Exercises

Look in the ar_exercises folder. Update the exercise.rb file to solve each challenge.

Commit! Make a commit before you move on!

Command Line Landlord Manager (Using ActiveRecord)

We're going to recreate our command line app, only this time we'll use ActiveRecord to store / read our data (instead of hashes and/or plain ruby objects like we did before).

Step 0 - Define Your Models

Create a models folder. Inside that, you should create models for Apartment and Tenant. Ensure you set up the correct has_many / belongs_to associations.

Hint: you can look at the top of exercise.rb for code for each model.

Step 1 - Create a Connection File

Create a db/connection.rb file. See the AR Lesson for an example of what should be in it. Hint: Make sure you update the name of the DB it's connecting to.

I like to put the following lines at the top of my connection.rb file, that way, I can just load this file elsewhere and it ensures ActiveRecord is loaded:

require "pg" # postgres db library
require "active_record" # the ORM
require "pry" # for debugging

VERIFY: Run the provided console.rb and ensure you can run commands like those below without any errors

the_bat_cave = Apartment.create(address: "123 Main St", monthly_rent: 2000, sqft: 600, num_beds: 2, num_baths: 1)
me = Tenant.create(name: "Adam", age: 30, gender: "Male", apartment: the_bat_cave)

Step 2 - Create a Seeds File

Create a db/seeds.rb file.

At the very top, ensure your seed file loads the necessary files using require_relative:

  • the connection file db/connection
  • the 2 model files

Below that, add these two lines, to ensure running the seed script clears your DB first.

Tenant.destroy_all
Apartment.destroy_all

Lastly, copy the code you wrote in exercise.rb that JUST creates ~9 tenants and ~3 apartments.

Then, run your seed file from the command line: ruby db/seeds.rb

Verify: Go look at the provided console.rb. See the commented out lines? Uncomment them and re-run console.rb. It should run without error and provide the expected output (in terms of the numbers of apartments and tenants.)

Commit! Make a commit before you move on!

Step 3 - Build out the CLI Interface

Build out a simple command line interface that provides a menu prompt and allows the user to:

  1. See a list of all apartments (include ID#, address, and monthly rent)
  2. See a list of all tenants (include name and age)
  3. See a list of all apartments and their associated tenants (just address and name)

Commit! Make a commit before you move on!

Bonus

  • extend functionality of the command line app where you, the landlord, can assign people to apartments, evict tenants, change rent and .... whatever you want!

STOP HERE FOR NOW!

Sinatra Views and Templates

Do not connect sinatra to the DB. That's friday's hw. Focus on creating the routes and views. Hardcode some sample html for each of the views.

Create the (RESTful) routes and views for the following items:

  • The homepage should list several menu options:
    • List all apartments (a link to GET /apartments)
    • View an apartment's details(a link to GET /apartments/1)
    • Add an apartment(a link to GET /apartments/new)
    • List tenants (a link to GET /apartments/1/tenants)
  • The route GET /apartments should list all apartments
    • these apartments will just be hardcoded in your app.rb or in your erb file.
  • The route GET /apartments/new should show a form for adding a new apartment
    • Make sure to get the appropriate input from the user when creating an apartment as per schema
  • The route GET /apartments/1 should show info about a single apartment
    • Tell the user the address, monthly_rent, sqft, num_beds, num_baths, and renters
  • The route GET /apartments/1/tenants should list all tenants for 1 apartment.
  • The route GET /apartments/1/tenants/new should show a form for adding a new tenant.
    • Make sure to get the appropriate input from the user to create your person as per schema

Sinatra & DB

Example of using instance variables:

In your main app.rb file, create instance variables that query the Database using active record.

In your views, replace hardcoded html with erb. For example:

<% @apartments.each do |apartment| %>
  <li><%= apartment.address %></li>
<% end %>

This will be a full single model CRUD application that connects to a database backend

You should have the following in your application:

  • Have an index route for apartments
    • should list all of the apartments
    • each apartment should link to its own show page
    • it should have a link to create a new apartment
  • have a show route for each apartment specified by the params value in the URL.
    • it should list its address, montly rent, square feet, number of bedrooms and number of bathrooms
    • it should have a link to delete the apartment
    • it should have a link to edit the apartment
    • it should show all tenants living in the apartment
  • have a new route for apartments
    • this will contain the form to create new apartments
    • when this form is submitted it will send a POST request to the create route(below) in your database.
  • have a create route for apartments
    • when there's a POST request to this route, it will create an apartment in your database
    • upon creation it will redirect to the created apartment's show route.
  • have an edit route for a single apartment
    • this will contain the form to edit an existing apartment
    • when this form is submitted it will send a PUT request to and update route(below) in your database
  • have an update route for a single apartment specified by the params value in the URL.
    • when there's a PUT request to this route, it will update an apartment in the database
    • after updating it should redirect to the updated apartments show route
  • have a delete route for a single apartment specified by the params value in the URL.
    • when there's a DELETE request to this route, it will delete the apartment specified.

BONUS

  • implement CRUD functionality for tenants
  • introduce a 3rd model
  • introduce a 4th model ...

megabonus

  • incorporate user authentication

landlord's People

Contributors

andrewsunglaekim avatar jshawl avatar jball23 avatar adambray avatar robertakarobin avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.