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
14 changes: 14 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,14 @@
# frozen_string_literal: true

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
27 changes: 27 additions & 0 deletions backend/app/helpers/handle_file_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require "csv"

module HandleFileHelper
def rows_to_json(file)
file_parsed = parse_csv(file)
build_response(file_parsed)
end

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

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

def build_response(parsed_file)
json_response = {}
parsed_file.each_with_index do |row, index|
json_response["row_#{index + 1}"] = encode_string(row.join(","))
end

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"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move this to backend/app/helpers/handle_file_helper.rb


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

namespace :api, defaults: { format: :json } do
namespace :v1 do
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 @@
# frozen_string_literal: true

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(response.parsed_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(response.parsed_body["file_rows"]).to be_empty
end
end

context "when file is not a CSV" do
let(:non_csv_file) { fixture_file_upload(Rails.root.join("spec/fixtures/files/non_csv_file.txt"), "text/csv") }

it "returns status ok" do
post "/api/v1/import", params: { file: non_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: non_csv_file }
expect(response.body).to include("file_rows")
end
end
end
end
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"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this file?


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