Skip to content

Conversation

@rosle
Copy link
Owner

@rosle rosle commented Mar 30, 2020

What happened

✅ Change the default pages controller to dashboard controller
✅ Add an upload form on the user dashboard
✅ Parse the uploaded csv

Insight

Implement the upload form which ties to the KeywordFile schema which validate for the file type

Use the CSV Library to parse the file

Proof Of Work

  • Display the upload form on the user dashboard

    Screen Shot 2563-03-31 at 09 58 49
  • When the user uploads the invalid file type, displays the error

    Screen Shot 2563-03-31 at 10 00 50
  • When the user uploads the valid csv, parses and displays the parsed content

    For example we upload the file with this content

    hotel
    animal
    cats
    restaurants
    
    Screen Shot 2563-03-31 at 10 01 11

@rosle rosle force-pushed the feature/sign-out branch from adf027d to 2fd0100 Compare March 30, 2020 03:24
@rosle rosle force-pushed the feature/parse-csv branch from d418296 to 2a073c6 Compare March 30, 2020 03:34
use Ecto.Schema
import Ecto.Changeset

embedded_schema do

Choose a reason for hiding this comment

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

It's a great idea to use embedded_schema. I just read about it last weekend. It's the closest to implement some kind of form object 💪

@rosle rosle changed the base branch from feature/sign-out to develop March 31, 2020 02:44
@rosle rosle marked this pull request as ready for review March 31, 2020 03:12
%Keyword{}

iex> get_keyword!(456)
** (Ecto.NoResultsError)

Choose a reason for hiding this comment

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

This will return nil as it's not using get!

[]
else
[file: "is not supported"]
end

Choose a reason for hiding this comment

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

How about:

defp validate_file_ext(changeset) do
  if Enum.member?(@accept_file_ext, Path.extname(file.filename)) do
    changeset
  else
	add_error(changeset, : file, "is not supported")
  end
end

The return of an empty array [] is kind of strange while returning changeset seems more expected.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I rechecked this one. I found something interesting 📝

When using the validate_change, seems like it will only validate when the value is not nil - https://hexdocs.pm/ecto/Ecto.Changeset.html#validate_change/3. So after removing it out, there is one case that if the file is nil, it will still validate and the error occurs when file.filename because the file is nil. For this, we might need to add more condition/clause function to handle this case 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants