-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1,2,3,4 user stories done #215
base: main
Are you sure you want to change the base?
Changes from all commits
0d8e7b1
8c7d065
4ee5f96
6ba2585
4bad20d
7644eb1
4fa0ad6
32f9866
ef8939b
c846ac9
892cab6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ coverage | |
|
||
# Local cache of Rubocop remote config | ||
.rubocop-* | ||
capybara-*.html |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,24 @@ | ||
require 'sinatra/base' | ||
require './lib/post' | ||
|
||
class Chitter < Sinatra::Base | ||
get '/test' do | ||
'Test page' | ||
get '/' do | ||
'Chitter App' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. detail - remove unused line |
||
erb :index | ||
end | ||
|
||
get '/posts' do | ||
@posts = Post.all | ||
erb :see_posts | ||
end | ||
|
||
get '/posts/new' do | ||
erb :new_post | ||
end | ||
|
||
post '/posts' do | ||
Post.create(date: params[:date], author: params[:author], message: params[:message]) | ||
redirect '/posts' | ||
end | ||
|
||
run! if app_file == $0 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE peeps ADD COLUMN author VARCHAR(60); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE peeps ADD COLUMN date VARCHAR(60); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'pg' | ||
|
||
class Post | ||
attr_reader :id, :date, :author, :message | ||
|
||
def initialize(id:, date:, author:, message:) | ||
@id = id | ||
@date = date | ||
@author = author | ||
@message = message | ||
end | ||
|
||
def self.all | ||
if ENV['ENVIRONMENT'] == 'test' | ||
connection = PG.connect(dbname: 'chitter_test') | ||
else | ||
connection = PG.connect(dbname: 'chitter') | ||
end | ||
Comment on lines
+14
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These lines are present in all your methods that access the db - they could be extracted into a database helper class :) |
||
result = connection.exec("SELECT * FROM peeps ORDER BY date DESC") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm surprised queries without |
||
result.map do |peep| | ||
Post.new(id: peep['id'], date: peep['date'], author: peep['author'], message: peep['message']) | ||
end | ||
end | ||
|
||
def self.create(date:, author:, message:) | ||
if ENV['ENVIRONMENT'] == 'test' | ||
connection = PG.connect(dbname: 'chitter_test') | ||
else | ||
connection = PG.connect(dbname: 'chitter') | ||
end | ||
|
||
result = connection.exec_params("INSERT INTO peeps (date, author, message) VALUES('#{date}', $1, $2) RETURNING id, date, author, message;", [author, message]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice that you are using exec_params to prevent sql injections here, but I'm wondering why you are not also doing that for the date, especially since the date is sent by the front-end as a param. It would be different if the date was automatically added in ruby to peeps when they are created, and not coming from the front-end. I know you form enforces a format for the input, but a malicious user could easily change the front-end html and send data in a different way, so your server should always protect itself from the front-end. |
||
Post.new(id: result[0]['id'], date: result[0]['date'], author: result[0]['author'], message: result[0]['message']) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require 'pg' | ||
|
||
def persisted_data(id:) | ||
connection = PG.connect(dbname: 'chitter_test') | ||
result = connection.query("SELECT * FROM peeps WHERE id = #{id};") | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
feature 'Adding a new post' do | ||
scenario 'a user can add a new post' do | ||
visit('/posts/new') | ||
fill_in("date", with: "2022-04-01") | ||
fill_in("author", with: "Rose") | ||
fill_in("message", with: "How are you?") | ||
click_button('Submit') | ||
|
||
expect(page).to have_content("2022-04-01") | ||
expect(page).to have_content("Rose") | ||
expect(page).to have_content("How are you?") | ||
end | ||
end |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'pg' | ||
|
||
feature 'Welcome page' do | ||
scenario 'user visits homepage ' do | ||
visit('/') | ||
expect(page).to have_content "Chitter App" | ||
expect(page).to have_link(href: "/posts") | ||
expect(page).to have_link(href: "/posts/new") | ||
end | ||
end | ||
|
||
feature 'Viewing posts' do | ||
scenario 'visiting /posts shows message' do | ||
|
||
Post.create(date: "2022-04-01", author: "Kate", message: "How are you?") | ||
|
||
visit('/posts') | ||
expect(page).to have_content("2022-04-01") | ||
expect(page).to have_content("Kate") | ||
expect(page).to have_content("How are you?") | ||
|
||
end | ||
end | ||
|
||
feature 'Viewing posts' do | ||
scenario 'visiting /posts shows new messages first' do | ||
|
||
Post.create(date: "2022-04-01", author: "Kate", message: "How are you?") | ||
Post.create(date: "2022-06-01", author: "Kate", message: "How are you?") | ||
Post.create(date: "2022-08-01", author: "Kate", message: "How are you?") | ||
|
||
visit('/posts') | ||
expect(page).to have_content("2022-08-01") | ||
expect(page).to have_content("2022-06-01") | ||
expect(page).to have_content("2022-04-01") | ||
Comment on lines
+33
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't actually check the order of the content. You could use regular expressions here to check that "2022-08-01" is above "2022-06-01", for example with something like expect(page).to match(/2022-08-01.*2022-06-01/) However, I think an even better test would check the content of the mew message comes before the content of an older one, this way the test would still pass if you decide to change the formatting of the date, given that the user story is about posts order, not dates :) |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'post' | ||
require 'pg' | ||
require 'database_helpers' | ||
|
||
RSpec.describe Post do | ||
describe '.all' do | ||
it 'returns a list of posts in a reverse chronological order' do | ||
connection = PG.connect(dbname: 'chitter_test') | ||
|
||
post = Post.create(date: "2022-04-01", author: 'Kate', message: 'This is my first post!') | ||
Post.create(date: "2022-04-02", author: 'Rose', message: 'How are you?') | ||
Post.create(date: "2022-04-03", author: 'Emma', message: 'Working from home') | ||
|
||
messages = Post.all | ||
expect(messages.length).to eq 3 | ||
expect(messages.first).to be_a Post | ||
expect(messages.first.date).to eq "2022-04-03" | ||
expect(messages.first.author).to eq 'Emma' | ||
expect(messages.first.message).to eq 'Working from home' | ||
end | ||
end | ||
|
||
describe '.create' do | ||
it 'creates a new post' do | ||
post = Post.create(date: "2022-04-01", author: 'Kate', message: 'This is my first post!') | ||
persisted_data = persisted_data(id: post.id) | ||
|
||
expect(post).to be_a Post | ||
expect(post.id).to eq persisted_data.first['id'] | ||
expect(post.date).to eq '2022-04-01' | ||
expect(post.author).to eq 'Kate' | ||
expect(post.message).to eq 'This is my first post!' | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<head> | ||
<style> | ||
body { | ||
background-color: lightblue; | ||
} | ||
h1 { | ||
font-size: 50px; | ||
color: black; | ||
text-shadow: 2px 2px white; | ||
text-align: center; | ||
} | ||
ul { | ||
margin: 0 auto; | ||
padding: 10px 10px; | ||
list-style: none; | ||
text-align: center; | ||
} | ||
li { | ||
padding: 10px 10px; | ||
} | ||
a { | ||
color: white; | ||
text-shadow: 1px 1px black; | ||
font-size: 30px; | ||
text-decoration: none; | ||
padding: 3px 5px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Chitter App</h1> | ||
<ul> | ||
<li> | ||
<a href="/posts">Click here to see all peeps</a> | ||
</li> | ||
<li> | ||
<a href="/posts/new">Click here to add a new peep</a> | ||
</li> | ||
</ul> | ||
</body> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<head> | ||
<style> | ||
body { | ||
background-color: lightblue; | ||
margin: 20px 20px; | ||
text-align: center; | ||
} | ||
h1 { | ||
font-size: 50px; | ||
} | ||
.tweet { | ||
display: inline-block; | ||
background-color: white; | ||
margin: 20px 20px; | ||
padding: 30px 60px; | ||
border: 2px solid black; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Chitter App</h1> | ||
<form action="/posts" method="post"> | ||
<div class="tweet"> | ||
<h2>Create your first peep.</h2> | ||
<input type="date" id="date" name="date" value="2022-04-01" min="2012-01-01" max="2040-01-01"> | ||
<input type="text" id="author" name="author" placeholder="Your name"><br><br> | ||
<textarea id="message" name="message" rows="5" cols="50" placeholder="Your message cannot exceed 60 characters"></textarea><br><br> | ||
<input type="submit" value="Submit" /> | ||
</div> | ||
</form> | ||
</body> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<head> | ||
<style> | ||
body { | ||
background-color: lightblue; | ||
} | ||
|
||
div { | ||
padding: 20px 300px; | ||
margin: 10px 60px; | ||
} | ||
|
||
h1 { | ||
margin-top: 40px; | ||
font-size: 80px; | ||
text-align: center; | ||
color: black; | ||
text-shadow: 2px 2px white; | ||
} | ||
h3 { | ||
padding: 30px 10px; | ||
font-size: 26px; | ||
color: navy; | ||
text-align: center; | ||
border: 2px solid black; | ||
background-color: white; | ||
} | ||
ul { | ||
list-style: none; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Recent Peeps</h1> | ||
<form> | ||
<div> | ||
<% @posts.each do |peep| %> | ||
<h3> date: <%= peep.date %><br><br> | ||
name: <%= peep.author %> <br><br> | ||
message: <%= peep.message %> </h3> | ||
<br> | ||
<% end %> | ||
</div> | ||
</form> | ||
</body> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice use of RESTful routes!