-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
154 lines (126 loc) · 3.11 KB
/
server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require 'sinatra'
require 'sinatra/reloader'
require 'pry'
require 'pg'
def db_connection
begin
connection = PG.connect(dbname: 'slacker_news')
yield(connection)
ensure
connection.close
end
end
def get_urls
query = 'SELECT articles.url FROM articles;'
urls = db_connection do |conn|
conn.exec(query)
end
urls
end
def valid_title?(title)
title != nil
end
def url_fixer(url)
if url[0..6] != "http://"
url = "http://" + url
end
url
end
def valid_description?(description)
description.length > 20
end
def valid_url?(url)
url = url_fixer(url)
urls = get_urls
urls.each do |hash|
if hash['url'] == (url)
return false
end
end
true
end
def write_article(title, url, description)
article = 'INSERT INTO articles (title, url, description)
VALUES ($1, $2, $3)'
db_connection do |conn|
conn.exec_params(article, [title, url, description])
end
end
def valid_input?(title, url, description)
(valid_title?(title) && valid_url?(url) && valid_description?(description))
end
def get_article(id)
query = 'SELECT articles.id, articles.title, articles.url, articles.description
FROM articles
WHERE articles.id = $1'
article = db_connection do |conn|
conn.exec_params(query, [id])
end
article
end
def valid_comment?(username, comment)
(username.length < 50) && (comment.length > 0 && comment.length < 2000)
end
def get_comments(article_id)
query = 'SELECT comments.username, comments.comment
FROM comments
WHERE article_id = $1'
comments = db_connection do |conn|
conn.exec_params(query, [article_id])
end
comments
end
def write_comment(username, comment, article_id)
input = 'INSERT INTO comments (username, comment, article_id)
VALUES ($1, $2, $3)'
db_connection do |conn|
conn.exec_params(input, [username, comment, article_id])
end
end
get '/' do
redirect '/articles'
end
get '/articles' do
query = 'SELECT articles.id, articles.title, articles.url, articles.description
FROM articles'
@articles = db_connection do |conn|
conn.exec(query)
end
erb :'articles/index'
end
get '/new' do
erb :'new/index'
end
post '/new' do
if valid_input?(params["title"], params["url"], params["description"])
title = params["title"]
url = url_fixer(params["url"])
description = params["description"]
write_article(title, url, description)
redirect '/articles'
else
erb :'new/index'
end
end
get '/articles/:article_id/comments' do
article_id = params[:article_id].to_i
@comments = get_comments(article_id)
@article = {}
article = (get_article(article_id)).to_a
@article['url'] = article[0]['url']
@article['title'] = article[0]['title']
@article['description'] = article[0]['description']
@article['id'] = article[0]['id']
erb :'comments/index'
end
post '/articles/:article_id/comments' do
id = params[:article_id].to_i
if valid_comment?(params["username"], params["comment"])
username = params["username"]
comment = params["comment"]
write_comment(username, comment, id)
redirect "/articles/#{params[:article_id]}/comments"
else
erb :'comments/index'
end
end