Skip to content

Commit

Permalink
working prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
joehannis committed Jun 2, 2023
1 parent fd0b681 commit d74453e
Show file tree
Hide file tree
Showing 16 changed files with 549 additions and 18 deletions.
11 changes: 9 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

ruby '3.0.2'
ruby '3.2.1'

group :test do
gem 'rspec'
Expand All @@ -9,5 +9,12 @@ group :test do
end

group :development, :test do
gem 'rubocop', '1.20'
gem 'rubocop', '1.51'
end
gem "pg", "~> 1.3"
gem "activerecord"

gem "sinatra", "~> 3.0"
gem "sinatra-contrib", "~> 3.0"
gem "webrick", "~> 1.8"
gem "rack-test", "~> 2.1"
79 changes: 63 additions & 16 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
GEM
remote: https://rubygems.org/
specs:
activemodel (7.0.5)
activesupport (= 7.0.5)
activerecord (7.0.5)
activemodel (= 7.0.5)
activesupport (= 7.0.5)
activesupport (7.0.5)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
ansi (1.5.0)
ast (2.4.2)
concurrent-ruby (1.2.2)
diff-lcs (1.4.4)
docile (1.4.0)
parallel (1.20.1)
parser (3.0.2.0)
i18n (1.13.0)
concurrent-ruby (~> 1.0)
json (2.6.3)
minitest (5.18.0)
multi_json (1.15.0)
mustermann (3.0.0)
ruby2_keywords (~> 0.0.1)
parallel (1.23.0)
parser (3.2.2.1)
ast (~> 2.4.1)
rainbow (3.0.0)
regexp_parser (2.1.1)
pg (1.5.3)
rack (2.2.7)
rack-protection (3.0.6)
rack
rack-test (2.1.0)
rack (>= 1.3)
rainbow (3.1.1)
regexp_parser (2.8.0)
rexml (3.2.5)
rspec (3.10.0)
rspec-core (~> 3.10.0)
Expand All @@ -24,18 +48,20 @@ GEM
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.10.0)
rspec-support (3.10.2)
rubocop (1.20.0)
rubocop (1.51.0)
json (~> 2.3)
parallel (~> 1.10)
parser (>= 3.0.0.0)
parser (>= 3.2.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml
rubocop-ast (>= 1.9.1, < 2.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.28.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.11.0)
parser (>= 3.0.1.1)
ruby-progressbar (1.11.0)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.29.0)
parser (>= 3.2.1.0)
ruby-progressbar (1.13.0)
ruby2_keywords (0.0.5)
simplecov (0.21.2)
docile (~> 1.1)
simplecov-html (~> 0.11)
Expand All @@ -46,21 +72,42 @@ GEM
terminal-table
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.3)
sinatra (3.0.6)
mustermann (~> 3.0)
rack (~> 2.2, >= 2.2.4)
rack-protection (= 3.0.6)
tilt (~> 2.0)
sinatra-contrib (3.0.6)
multi_json
mustermann (~> 3.0)
rack-protection (= 3.0.6)
sinatra (= 3.0.6)
tilt (~> 2.0)
terminal-table (3.0.1)
unicode-display_width (>= 1.1.1, < 3)
unicode-display_width (2.0.0)
tilt (2.1.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.4.2)
webrick (1.8.1)

PLATFORMS
ruby

DEPENDENCIES
activerecord
pg (~> 1.3)
rack-test (~> 2.1)
rspec
rubocop (= 1.20)
rubocop (= 1.51)
simplecov
simplecov-console
sinatra (~> 3.0)
sinatra-contrib (~> 3.0)
webrick (~> 1.8)

RUBY VERSION
ruby 3.0.2p107
ruby 3.2.1p31

BUNDLED WITH
2.2.26
2.4.13
157 changes: 157 additions & 0 deletions Tables Design Template.md
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 -->
46 changes: 46 additions & 0 deletions app.rb
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

3 changes: 3 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# file: config.ru
require './app'
run Application
11 changes: 11 additions & 0 deletions lib/database_connection.rb
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
22 changes: 22 additions & 0 deletions lib/post.rb
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
Loading

0 comments on commit d74453e

Please sign in to comment.