-
Notifications
You must be signed in to change notification settings - Fork 0
[#9] [Backend] As a user, I can upload a CSV file, [#12] [UI] As a user, I can upload a CSV file #39
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
base: develop
Are you sure you want to change the base?
[#9] [Backend] As a user, I can upload a CSV file, [#12] [UI] As a user, I can upload a CSV file #39
Changes from all commits
457bdbc
33296d1
7ad64d2
63209d9
a941293
f08d8da
dc0a701
f767e8f
183d249
7539e8b
a72413f
53cc4ff
3acfcb6
5ca8dce
60021d9
ab783db
a22fac3
c41b8f6
0b86c77
5e75a50
db8150d
6158bf1
5ea7ec4
fe8c199
1213306
27e7f72
380c5b0
f095c0a
1065845
5f732a9
46d87d2
70b5a14
b48ad3b
b974df7
fbd3d54
f3a5ea0
d51d977
f7349ba
fb1689f
035589f
6ec2e4c
3edef7e
2004806
56b8585
db940aa
9fb4021
526182b
8241db7
e8851a2
565303c
2ce0def
4925fd2
93d1548
9c29c04
4895f2e
215cc21
91e5b47
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,8 +1,38 @@ | ||||||
| # frozen_string_literal: true | ||||||
|
|
||||||
| require 'csv' | ||||||
|
|
||||||
| class SearchStatsController < ApplicationController | ||||||
| ALLOWED_MIME_TYPE = 'text/csv' | ||||||
| MAXIMUM_KEYWORDS = 1000 | ||||||
|
|
||||||
| # GET /search_stats | ||||||
| def index | ||||||
| @pagy, @search_stats = pagy(SearchStat.all) | ||||||
| end | ||||||
|
|
||||||
| def new | ||||||
| search_stat = SearchStat.new | ||||||
|
|
||||||
| render :new, locals: { search_stat: search_stat } | ||||||
| end | ||||||
|
|
||||||
| def create | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @topnimble don't just skip the warnings from the linter, the checks are imposed for a reason There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
| csv_file = params[:csv_file] | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As Long suggested, we should have a dedicated from/model/service to handle the uploaded file. |
||||||
|
|
||||||
| raise 'Invalid file type' unless csv_file.content_type == ALLOWED_MIME_TYPE | ||||||
|
|
||||||
| csv_file_content = params[:csv_file].read | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
| keywords = CSV.parse(csv_file_content).flatten.compact_blank | ||||||
|
|
||||||
| raise 'Invalid file data' unless keywords.any? | ||||||
| raise 'Too many keywords' unless keywords.count <= MAXIMUM_KEYWORDS | ||||||
|
Comment on lines
+21
to
+29
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The file validation is not the reponsibility of the controller, it usually handled in the model.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also, the validations here can be extracted to a separated validator to used in the form |
||||||
|
|
||||||
| keywords.map do |keyword| | ||||||
| search_stat = SearchStat.new({ keyword: keyword, user_id: current_user.id }) | ||||||
| search_stat.save! | ||||||
| end | ||||||
|
|
||||||
| redirect_to search_stats_path | ||||||
| end | ||||||
| end | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,5 +2,4 @@ | |
|
|
||
| class SearchStat < ApplicationRecord | ||
| validates :keyword, presence: true | ||
| validates :raw_response, presence: true | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you please point me out why we remove this validation? 🤔 |
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| <%= form_with url: '/search_stats', multipart: true do |form| %> | ||
| <%= file_field_tag :csv_file, accept: 'text/csv', class: 'form-control' %> | ||
| <%= form.submit 'Upload', class: 'btn btn-primary mt-3' %> | ||
| <% end %> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| 1st keyword | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. About the invalid type, we can specify it when attaching the file to the Fabricator instead of using a dedicated file. 😉 Rack::Test::UploadedFile.new(Rails.root.join('spec', 'fixtures', 'files', 'keywords.csv'), 'plain/text') |
||
| 2nd keyword | ||
| 3rd keyword | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| 1st keyword | ||
| 2nd keyword | ||
| 3rd keyword |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.