diff --git a/Steps.md b/Steps.md new file mode 100644 index 0000000..653284a --- /dev/null +++ b/Steps.md @@ -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 + diff --git a/ruby/Gemfile b/ruby/Gemfile index 0ca7b38..cfa9c1e 100644 --- a/ruby/Gemfile +++ b/ruby/Gemfile @@ -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" diff --git a/ruby/app/controllers/articles.rb b/ruby/app/controllers/articles.rb index 7a74b26..c72c51e 100644 --- a/ruby/app/controllers/articles.rb +++ b/ruby/app/controllers/articles.rb @@ -1,37 +1,43 @@ 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 @@ -39,17 +45,24 @@ def get_article(id) { 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 diff --git a/ruby/app/routes/articles.rb b/ruby/app/routes/articles.rb index 390b27c..e3647d3 100644 --- a/ruby/app/routes/articles.rb +++ b/ruby/app/routes/articles.rb @@ -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 diff --git a/ruby/app/routes/health.rb b/ruby/app/routes/health.rb index 996583e..25b81b7 100644 --- a/ruby/app/routes/health.rb +++ b/ruby/app/routes/health.rb @@ -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 diff --git a/ruby/config/.env b/ruby/config/.env index 9cf2335..90c89b6 100644 --- a/ruby/config/.env +++ b/ruby/config/.env @@ -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