Skip to content

learn-academy-2023-foxtrot/apartment-app-backend-buoyant-duo-instructors

Repository files navigation

Apartment App Backend

Rails API Initial Configuration

Vocabulary

  • devise: a library in the Ruby on Rails framework that quickly adds user authentication with features like user registration, login, password reset, and etc.

  • authorization: a process that determines if a user permission to perform a specific action within an application or database.

  • authentication: a process that verifies the identity of a user, usually through a username and password.

  • user session: a temporary state that allows a user to interact with a web application. It starts when the user logs in and ends when they log out or after a period of inactivity.

  • JSON Web Token (JWT): a system used for authentication and authorization to allow data to be securely shared about a user between separate applications. A JWT consists of three parts: a header, a payload, and a signature. The header contains information about how the JWT is encoded. The payload contains information about the user. The signature is used to check if the JWT has been tampered with during transmission.

  • localStorage: a feature in web browsers that allows web applications to store small amounts of data on the user's computer like items in a shopping cart or JWTs on active user session.

Workflow

just making the initial commit on main branch

  1. Create empty github repo
  • create a team on github classroom
  • refresh to get empty github repo (no cloning)
  1. Create Rails App apartment_app_backend and ask for branch protection

  2. Add the appropriate ruby gems for rspec testing configuration

  • If adding directly to Gemfile, then run $ bundle to add appropriate dependencies for the gems
  1. Start server to ensure that the rails app was built correctly

  2. Add the appropriate ruby gems for devise to create a User model

  • Update the schema
    • If received error message, $ rails db:drop, rails db:create, rails db:migrate
  1. Add the appropriate ruby gems for jwt and cors
  • Disable authenticity token
  • Add cors.rb file to config/initializers
  1. Generate resource to add model, controller, routes for Apartment NOTE: User will have many apartments; Apartment will belong to a user, which means it will need a foreign key

  2. Establish relationships for User and Apartment models

  3. Update the schema

  4. Create seeds

  • Create users NOTE: This code---> user1 = User.where(email: '[email protected]').first_or_create(password: 'password', password_confirmation: 'password') <---attempts to find a user with the email '[email protected]'. If it finds a user with that email, it will return the existing user and won't change the password. If it doesn't find a user with that email, it will create a new user with the specified email, password, and password confirmation ('password').
  • Create apartments
  • Create a method that will save new instances to the database that are associated with each user and print out a confirmation that the instance was created
  1. Populate database with mock data from seed file

  2. Follow the file path as designated in the syllabus to modify the config directory

  3. Generate devise users sessions and registrations controllers to manage the tokens

  4. Modify the devise routes

  5. Create a JWT token NOTE: alternative way to save jwt key ---> Store the token in a variable, Close the application (VS Code), Should see confirmation ---> File encrypted and saved.
    to the Gemfile to allow our devise and jwt setup: rack-cors, devise, devise-jwt, dotenv-rails

  6. Generate jwt_denylist model to store the revoked JWT tokens NOTE: Be mindful of the singular snake_case naming conventions for the model and change method.

  7. Update schema

  8. Modify the User model to reflect this revocation strategy

  9. Exclude .DS_Store from version control NOTE: .DS_Store is a file that is automatically generated by the mac operating system. The name stands for "Desktop Services Store." They can become visible when interacting with the GUI on Finder and CLI on Git. To exclude .DS_Store files from being included in a Git repository, you can add the following line to your .gitignore file: .DS_Store

  10. Connect the two repos with the git remote code from the empty github repo

  11. Perform initial commit

  12. Ask for branch protection on main branch

API Validations

API Endpoints (Read and Create Functionality)

  1. Stub API endpoints in app/controllers/apartments_controller.rb

  2. Create tests in request spec file in spec/requests/apartments_spec.rb

  3. See it fail: $ rspec spec/requests/apartments_spec.rb

  4. Add logic in the API endpoint to make the test pass

  5. See it pass

Models (Validations)

  1. Creates tests in model spec file

  2. See it fail

  3. Add validation helpers in the model file

  4. See it pass

API Endpoints (Validations - Create Functionality)

  1. Create tests in request spec file in spec/requests/apartments_spec.rb that are missing attributes

NOTES

  • Make sure to provide a user for each apartment
  • When providing the apartment params for testing, assign a foreign key
  apartment_params = {
    apartment: {
      street: '124 Conch St',
      unit: 'A',
      city: 'Bikini Bottom',
      state: 'CA',
      square_footage: 3000,
      price: '4000',
      bedrooms: 2,
      bathrooms: 2,
      pets: 'no',
      image: 'https://images.unsplash.com/photo-1680842350641-d49b43d71025?auto=format&fit=crop&q=60&w=500&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8MTN8fHNwb25nZWJvYnxlbnwwfHwwfHx8MA%3D%3D',
      user_id: user.id
    }
  }
  • When providing the strong params, allow assignment of a foreign key
  private
  def apartment_params
    params.require(:apartment).permit(:street, :unit, :city, :state, :square_footage, :price, :bedrooms, :bathrooms, :pets, :image, :user_id)
  end

removing gitignore files from version control

  • $ git rm -rf --cached coverage

About

apartment-app-backend-buoyant-duo-instructors created by GitHub Classroom

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published