Skip to content
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

baseline-fixes #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Steps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# installed ruby, docker
# prepared docker to run a postgres container
docker network create some-network
docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' some-postgres

# test the connection
psql -h 172.18.0.2 -U postgres

4 changes: 2 additions & 2 deletions ruby/Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
ruby '3.0.3'
ruby '3.2.4'
source 'https://rubygems.org'
gem 'rack', '~> 2.2'
gem 'rspec', '~> 3.11'
gem 'rack-test', '~> 2.0'
gem 'sinatra', '~> 2.2'
gem 'dotenv', '~> 2.8'
gem 'pg', '~> 1.4'
gem 'pg', '~> 1.5'
gem 'webrick', '~> 1.7'
gem "activerecord", "~> 7.0"
71 changes: 42 additions & 29 deletions ruby/app/controllers/articles.rb
Original file line number Diff line number Diff line change
@@ -1,55 +1,68 @@
class ArticleController
def create_article(article)
article_not_exists = ! (Article.where(:title => article['title']).empty?)

return { ok: false, msg: 'Article with given title already exists' } unless article_not_exists

new_article = Article.new(:title => article['title'], :content => article['content'], :created_at => Time.now)
new_article.save

{ ok: false, obj: article }
rescue StandardError
{ ok: false }
article_exists = Article.exists?(title: article['title'])

return { ok: false, msg: 'Article with given title already exists' } if article_exists

new_article = Article.new(title: article['title'], content: article['content'], created_at: Time.now)

if new_article.save
{ ok: true, obj: new_article }
else
{ ok: false, msg: 'Failed to save the article' }
end
rescue StandardError => e
{ ok: false, msg: e.message }
end

def update_article(id, new_data)

article = Article.where(id: id).first

return { ok: false, msg: 'Article could not be found' } unless article.nil?

article = Article.find_by(id: id)

return { ok: false, msg: 'Article could not be found' } unless article

article.title = new_data['title']
article.content = new_data['content']
article.save_changes

{ ok: true }
rescue StandardError
{ ok: false }

if article.save
{ ok: true, obj: article }
else
{ ok: false, msg: 'Failed to update the article' }
end
rescue StandardError => e
{ ok: false, msg: e.message }
end


def get_article(id)
res = Article.where(:id => id)

if res.empty?
{ ok: true, data: res }
if !res.empty?
{ ok: true, data: res[0]}
else
{ ok: false, msg: 'Article not found' }
end
rescue StandardError
{ ok: false }
end

def delete_article(_id)
delete_count = Article.delete(:id => id)

if delete_count == 0
{ ok: true }
def delete_article(id)
article = Article.find_by(id: id)
if article.nil?
{ ok: false, msg: 'Article not found' }
else
{ ok: true, delete_count: delete_count }
article.destroy
{ ok: true, delete_count: 1 }
end
rescue StandardError => e
{ ok: false, msg: e.message }
end


def get_batch

res = Article.all
{ ok: true, data: res}
rescue StandardError
{ ok: false }
end
end
36 changes: 24 additions & 12 deletions ruby/app/routes/articles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,60 @@ def initialize
content_type :json
end

get('/') do
summmary = @articleCtrl.get_batch
get('/') do
summary = @articleCtrl.get_batch

if !(summary[:ok])
{ articles: summary[:data] }.to_json
else
{ msg: 'Could not get articles.' }.to_json
end
if summary[:ok]
{ articles: summary[:data] }.to_json
else
status 500
{ msg: 'Could not get articles.' }.to_json
end
end

get('/:id') do
article_id = params["id"]
summary = @articleCtrl.get_article(article_id)

if summary[:ok]
{article: summary[:data]}.to_json
else
status 404
{ msg: 'Could not get article with id ' + article_id }.to_json
end
end

post('/') do
payload = JSON.parse(request.body.read)
summary = @articleCtrl.update_article(payload)
summary = @articleCtrl.create_article(payload)

if summary[:ok]
{ msg: 'Article updated' }.to_json
{ msg: 'Article created' }.to_json
else
{ msg: summary[:msg] }.to_json
end
end

put('/:id') do
article_id = params["id"]
payload = JSON.parse(request.body.read)
summary = @articleCtrl.uptade_article params['ids'], payload
summary = @articleCtrl.update_article(article_id, payload)

if summary[:ok]
{ msg: 'Article updated' }.to_json
else
{ msg: summary[:msg] }.to_json
end
end

delete('/:id') do
summary = self.delete_article params['id']
article_id = params["id"]
summary = @articleCtrl.delete_article(article_id)

if summary[:ok]
{ msg: 'Article deleted' }.to_json
else
{ mgs: 'Article does not exist' }.to_bson
{ msg: 'Article does not exist' }.to_json
end
end
end
2 changes: 1 addition & 1 deletion ruby/app/routes/health.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class HealthRoutes < Sinatra::Base

get('/') do
if request.env['AUTHED'] == true
if request.env['AUTHED'] = true
'App working OK'
else
status 403
Expand Down
8 changes: 4 additions & 4 deletions ruby/config/.env
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
APP_ENV=development

DB_HOST=localhost
DB_NAME=blog_db
DB_USER=applicant
DB_PASS=temp123
DB_HOST=172.18.0.2
DB_NAME=postgres
DB_USER=postgres
DB_PASS=mysecretpassword