Skip to content

bicycleben88/bens_bikes_backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

121 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ben's Bikes

This is a front end e-commerce web application built with React. Users can create an account, browse thru products, add items to a cart & checkout. It accesses a full CRUD API built with Ruby on Rails that follows RESTful convention.

Live Earls:

bens-bikes-frontend.vercel.app

bens-bikes-backend.herokuapp.com

Landing page to Ben's Bikes

Technologies Used

Front End:

  • React.js
  • JavaScript
  • CSS Components
  • JWT

Back End:

  • Ruby on Rails
  • PostgresQL
  • JWT
  • BCrypt

Features & Functions

Schemas

rails generate scaffold to create tables, models & controllers for Users, Items, CartItems, OrderItems & Orders

References:

  • User has many orders & has many CartItems
  • CartItems references a User & an Item
  • Orders references a User & an OrderItem
  • OrderItem belongs to an Order
  • add foreign keys

foreign keys

Controllers

Users

  • authenticates, logs in & signs up a user
  • auto_login returns a User & its CartItems

auto_login

Add (Multiple) Cart Items

  • handle POST request from front end to /cartitems
  • use .find_by to determine if a cart item with an item_id already exists
  • if so, then update the quantity of that item by 1
  • otherwise, create a new cart item with the params outlined in cartitem_params create cart  item

Stripe Credit Card Payments

Configuring Stripe

  • Create Stripe account to get API keys
  • Create file stripe.rb in /config/initializers
    • Add API keys (stored as environmental variables)
  • stripe.rb
  • Add Environmental variables
    • Create file called local_env.yml & add it to .gitignore
    • Store key/value pairs e.g. STRIPE_KEY: "api key"
  • In application.rb add the following code before loading default config
  • application.rb add env variables

Create Stripe Payment

  • Use the PaymentIntents module to create a new payment, passing the Stipe token from the request body
  • payment intent
  • update order to include the charge
  • update order with stripe payment

Pagination

Create API endpoint that returns a count of the items in the inventory

  • in routes.rb add route that runs the function 'count' when it recieves a get request from ".../items_count"
    count route

Write count function

  • in items_controller.rb use method .count on Item model to return the total number of items in the inventory count function

Add 'will_paginate' to Gemfile

  • paginate gem

Use 'will_paginate' in items index route to render a specific number of items per page

  • in items_controller.rb index route use .paginate method, passing it as arguments the page number and the number of items per page to display

    paginate index route

Activate User's Account w/ email

Add email (string) & activated (boolean) to user's model w/ migrations

email migration

Create AccountActivations controller

account activations controller

In routes.rb create named route for edit action

account activation edit route

Create & Hash a unique activation token when creating a new user

  • Add activation_digest (string) to user's model w/ migrations

activation digest migration

  • in models/user.rb convert a user's email to lowercase & make a new activation token before creating a new user

before actions

  • in same file, under private create the above functions

user email and activation token functions

Generate mailer for account_activation & password_reset

generate mailer

In application_mailer.rb change default email

In user_mailer.rb lookup user & send to its email address

user mailer

In account_activation.html.erb pass as arguments the user's activation token & email to edit_account_activation_url()

email template

Send the email w/ link to activate account using SendGrid

  • Sign up for account on SendGrid

  • In production.rb add the following configuration

send grid configurations

  • In users_controller.rb invoke account_activation() function & .deliver_now method

deliver now

Write Edit action in account_activation_controller.rb

  • Find user
  • If there's a user, & it's not already activated, & the activation tokens match (see below), then update user.activated to true

edit action

  • Match activation tokens, in models/users.rb:

decode token

Sources

Stripe

Setting up Stripe on Rails

Adding Email Verification with Rails

Contact

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors