forked from makersacademy/chitter-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
549 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
# Two Tables Design Recipe Template | ||
|
||
_Copy this recipe template to design and create two related database tables from a specification._ | ||
|
||
## 1. Extract nouns from the user stories or specification | ||
|
||
``` | ||
STRAIGHT UP | ||
As a Maker | ||
So that I can let people know what I am doing | ||
I want to post a message (peep) to chitter | ||
As a maker | ||
So that I can see what others are saying | ||
I want to see all peeps in reverse chronological order | ||
As a Maker | ||
So that I can better appreciate the context of a peep | ||
I want to see the time at which it was made | ||
As a Maker | ||
So that I can post messages on Chitter as me | ||
I want to sign up for Chitter | ||
HARDER | ||
As a Maker | ||
So that only I can post messages on Chitter as me | ||
I want to log in to Chitter | ||
As a Maker | ||
So that I can avoid others posting messages on Chitter as me | ||
I want to log out of Chitter | ||
ADVANCED | ||
As a Maker | ||
So that I can stay constantly tapped in to the shouty box of Chitter | ||
I want to receive an email if I am tagged in a Peep | ||
``` | ||
Nouns: | ||
|
||
posts, message, time, signup, login, logout | ||
``` | ||
## 2. Infer the Table Name and Columns | ||
Put the different nouns in this table. Replace the example with your own nouns. | ||
| Record | Properties | | ||
| --------------------- | ------------------ | | ||
| Users | user_id, username, email, password | ||
| Posts | posts_id, timestamp, message | ||
1. Name of the first table (always plural): `users` | ||
Column names: `user_id, username, email, password' | ||
2. Name of the second table (always plural): `posts | ||
Column names: `posts_id`, `timestamp`, 'message' | ||
## 3. Decide the column types. | ||
[Here's a full documentation of PostgreSQL data types](https://www.postgresql.org/docs/current/datatype.html). | ||
Most of the time, you'll need either `text`, `int`, `bigint`, `numeric`, or `boolean`. If you're in doubt, do some research or ask your peers. | ||
Remember to **always** have the primary key `id` as a first column. Its type will always be `SERIAL`. | ||
``` | ||
# EXAMPLE: | ||
|
||
Table: users | ||
id: SERIAL | ||
username: text | ||
email: text | ||
password: text | ||
|
||
|
||
Table: posts | ||
id: SERIAL | ||
timestamp: timestasmp | ||
message: text | ||
``` | ||
## 4. Decide on The Tables Relationship | ||
Most of the time, you'll be using a **one-to-many** relationship, and will need a **foreign key** on one of the two tables. | ||
To decide on which one, answer these two questions: | ||
1. Can one [TABLE ONE] have many [TABLE TWO]? (yes) | ||
2. Can one [TABLE TWO] have many [TABLE ONE]? (no) | ||
You'll then be able to say that: | ||
1. **[A] has many [B]** | ||
2. And on the other side, **[B] belongs to [A]** | ||
3. In that case, the foreign key is in the table [B] | ||
Replace the relevant bits in this example with your own: | ||
``` | ||
Therefore the foreign key is on comments (post_id) | ||
|
||
``` | ||
*If you can answer YES to the two questions, you'll probably have to implement a Many-to-Many relationship, which is more complex and needs a third table (called a join table).* | ||
## 4. Write the SQL. | ||
```sql | ||
-- EXAMPLE | ||
-- file: albums_table.sql | ||
-- Replace the table name, columm names and types. | ||
-- Create the table without the foreign key first. | ||
CREATE TABLE users ( | ||
id SERIAL PRIMARY KEY, | ||
username text, | ||
email text, | ||
password text | ||
); | ||
-- Then the table with the foreign key first. | ||
CREATE TABLE posts ( | ||
id SERIAL PRIMARY KEY, | ||
timestamp timestamp, | ||
message text | ||
-- The foreign key name is always {other_table_singular}_id | ||
user_id int, | ||
constraint fk_post foreign key(user_id) | ||
references users(id) | ||
on delete cascade | ||
); | ||
``` | ||
|
||
## 5. Create the tables. | ||
|
||
```bash | ||
psql -h 127.0.0.1 database_name < albums_table.sql | ||
``` | ||
|
||
|
||
<!-- BEGIN GENERATED SECTION DO NOT EDIT --> | ||
|
||
--- | ||
|
||
**How was this resource?** | ||
[😫](https://airtable.com/shrUJ3t7KLMqVRFKR?prefill_Repository=makersacademy%2Fdatabases&prefill_File=resources%2Ftwo_table_design_recipe_template.md&prefill_Sentiment=😫) [😕](https://airtable.com/shrUJ3t7KLMqVRFKR?prefill_Repository=makersacademy%2Fdatabases&prefill_File=resources%2Ftwo_table_design_recipe_template.md&prefill_Sentiment=😕) [😐](https://airtable.com/shrUJ3t7KLMqVRFKR?prefill_Repository=makersacademy%2Fdatabases&prefill_File=resources%2Ftwo_table_design_recipe_template.md&prefill_Sentiment=😐) [🙂](https://airtable.com/shrUJ3t7KLMqVRFKR?prefill_Repository=makersacademy%2Fdatabases&prefill_File=resources%2Ftwo_table_design_recipe_template.md&prefill_Sentiment=🙂) [😀](https://airtable.com/shrUJ3t7KLMqVRFKR?prefill_Repository=makersacademy%2Fdatabases&prefill_File=resources%2Ftwo_table_design_recipe_template.md&prefill_Sentiment=😀) | ||
Click an emoji to tell us. | ||
|
||
<!-- END GENERATED SECTION DO NOT EDIT --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require 'active_record' | ||
require 'sinatra' | ||
require "sinatra/reloader" | ||
require_relative 'lib/database_connection' | ||
require_relative 'lib/post' | ||
require_relative 'lib/user' | ||
|
||
class Application < Sinatra::Base | ||
configure :development do | ||
register Sinatra::Reloader | ||
also_reload 'lib/post' | ||
also_reload 'lib/user' | ||
end | ||
|
||
get '/' do | ||
p "Printing from app.rb line 16" | ||
p Post.all_peeps | ||
@posts = Post.all_peeps | ||
@posts = @posts.reverse | ||
return erb(:index) | ||
end | ||
post '/' do | ||
# def invalid_request_parameters? | ||
# # Are the params nil? | ||
# return true if params[:message] == nil | ||
|
||
# # Are they empty strings? | ||
# return true if params[:message] | ||
|
||
# return false | ||
# end | ||
# if invalid_request_parameters? | ||
# status 400 | ||
|
||
# return 'Please enter a message' | ||
# end | ||
|
||
# Parameters are valid, | ||
# the rest of the route can execute. | ||
Post.create(time: Time.now, message: params[:message], user_id: 1) | ||
redirect '/' | ||
|
||
end | ||
|
||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# file: config.ru | ||
require './app' | ||
run Application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# file: lib/database_connection.rb | ||
require 'active_record' | ||
|
||
def establish_database_connection | ||
ActiveRecord::Base.establish_connection( | ||
adapter: 'postgresql', | ||
host: 'localhost', | ||
port: '5432', | ||
database: 'chitter_test', | ||
) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'active_record' | ||
require_relative './database_connection' | ||
establish_database_connection | ||
|
||
class Post < ActiveRecord::Base | ||
belongs_to :user | ||
@posts = [] | ||
def self.all_peeps | ||
Post.joins(:user).map do |post| | ||
@posts << "#{post.time} #{post.user.username} #{post.message}" | ||
end | ||
p "we are in al_peeps function" | ||
p @posts | ||
end | ||
|
||
def self.create_post(time, message, user_id) | ||
post = Post.new(time: time, message: message, user_id: user_id) | ||
post.save | ||
post | ||
end | ||
|
||
end |
Oops, something went wrong.