Skip to content
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
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.3.1
1 change: 1 addition & 0 deletions backend/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ group :development do
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
gem "spring"

gem 'graphiql-rails'
# Annotate Rails classes with schema and routes info
gem "annotate"
end
Expand Down
12 changes: 12 additions & 0 deletions backend/app/controllers/api/v1/handle_file_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Api
module V1
class HandleFileController < Api::ApplicationController
include HandleFileHelper

def import
rows = set_rows_to_json(params[:file].tempfile)
render json: {file_rows: rows}, status: :ok
end
end
end
end
24 changes: 24 additions & 0 deletions backend/app/helpers/handle_file_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true
require 'csv'

module HandleFileHelper
def set_rows_to_json(file)
file_parsed = parse_file_csv(file)
set_response(file_parsed)
end

def parse_file_csv(file)
CSV.parse(file.read)
end

def encode_string(string)
string.force_encoding("ISO-8859-1").encode("UTF-8")
end

def set_response(parsed_file)
json_response = {}
parsed_file.each_with_index { |row,index| json_response.merge!("row_#{index + 1}": encode_string(row.join(','))) }

json_response
end
end
1 change: 1 addition & 0 deletions backend/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative "boot"

require "rails/all"
require "csv"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Expand Down
3 changes: 3 additions & 0 deletions backend/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@

namespace :api, defaults: { format: :json } do
namespace :v1 do
get "/public_method", to: "hello_world#public_method"
get "/private_method", to: "hello_world#private_method"
post "/import", to: "handle_file#import"
resources :users, only: %i[index create]
resources :schools, only: %i[index]
end
Expand Down
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions backend/spec/fixtures/files/valid_file.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name,age
Alice,30
Bob,25
49 changes: 49 additions & 0 deletions backend/spec/requests/api/v1/handle_file_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require "rails_helper"

RSpec.describe "HandleFiles" do
describe 'POST api/v1/import' do
context 'when CSV file is valid' do
let(:csv_file) { fixture_file_upload(Rails.root.join('spec', 'fixtures', 'files', 'valid_file.csv'), 'text/csv') }

it 'returns status ok' do
post '/api/v1/import', params: { file: csv_file }
expect(response).to have_http_status(:ok)
end

it 'returns the correct JSON response with file rows' do
post '/api/v1/import', params: { file: csv_file }
expect(JSON.parse(response.body)['file_rows']).to be_a(Hash)
end
end


context 'when CSV file is empty' do
let(:empty_csv_file) { fixture_file_upload(Rails.root.join('spec', 'fixtures', 'files', 'empty_file.csv'), 'text/csv') }

it 'returns status ok' do
post '/api/v1/import', params: { file: empty_csv_file }
expect(response).to have_http_status(:ok)
end

it 'returns empty file rows in JSON' do
post '/api/v1/import', params: { file: empty_csv_file }
expect(JSON.parse(response.body)['file_rows']).to be_empty
end
end

context "when CSV file is valid" do
let(:csv_file) { fixture_file_upload(Rails.root.join('spec', 'fixtures', 'files', 'valid_file.csv'), 'text/csv') }

it "returns status ok" do
post '/api/v1/import', params: { file: csv_file }
expect(response).to have_http_status(:ok)
end

it "returns the correct JSON response with file rows" do
post '/api/v1/import', params: { file: csv_file }
expect(response.body).to include('file_rows')
end
end
end
end

2 changes: 1 addition & 1 deletion backend/spec/requests/api/v1/hello_word_request_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

require "rails_helper"
require 'rails_helper'

RSpec.describe "HelloWorlds" do
describe "GET /api/v1/public_method" do
Expand Down
30 changes: 30 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require "sidekiq/web"

Rails.application.routes.draw do
mount GraphiQL::Rails::Engine, at: "/graphiql", graphql_path: "/graphql" if Rails.env.development?
post "/graphql", to: "graphql#execute"
mount Sidekiq::Web => "/sidekiq"
mount Rswag::Ui::Engine => "/api-docs"
mount Rswag::Api::Engine => "/api-docs"
devise_for :users
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
# root "articles#index"

get "/public_method", to: "hello_world#public_method"
get "/private_method", to: "hello_world#private_method"
get "/search", to: "hello_world#search"

namespace :api, defaults: { format: :json } do
namespace :v1 do
get "/public_method", to: "hello_world#public_method"
get "/private_method", to: "hello_world#private_method"
post "/import", to: "handle_file#import"

resources :users, only: [:index]
end
end
end