Skip to content
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

[API] Post Comments (for learning) #82

Closed
VictorWinberg opened this issue Jul 11, 2018 · 0 comments
Closed

[API] Post Comments (for learning) #82

VictorWinberg opened this issue Jul 11, 2018 · 0 comments
Assignees

Comments

@VictorWinberg
Copy link
Member

VictorWinberg commented Jul 11, 2018

[API] Post Comments

The post comment resource should fulfill requirements below

  • POST /api/posts/:id/comments | Create a post comment
  • GET /api/posts/:id/comments | Get a listing of post comments

The post resource should fulfill requirements below

  • GET /api/posts/:id | Get details about a post and it's comments

Generate a json post comment resource with body

Either you generate overload with gen.json or you generate less by gen.model, you choose.

Spoiler

mix phoenix.gen.json PostComment post_comments body:text post_id:references:posts
or
mix phoenix.gen.model PostComment post_comments body:text post_id:references:posts


Then associate Post-model to PostComment-model and change :on_delete to :delete_all in our migration.

Spoiler

web/models/post.ex

...
schema "posts" do
    field :title, :string
    field :body, :string
    has_many :comments, Nexpo.PostComment
  end
...

priv/repo/migrations/[TIMESTAMP]_create_post_comment.ex

...
create table(:post_comments) do
      add :body, :text
      add :post_id, references(:posts, on_delete: :delete_all)
...


Then migrate

Spoiler

Remember to update your repository by running migrations:

$ mix ecto.migrate


Then add route

Spoiler

Add the resource to your api scope in web/router.ex:

...
resources "/posts", PostController do
  resources "/comments", PostCommentController, only: [:show, :create]
end
...

Edit your json Post and PostComment resource so it fulfills requirements.

Help can be found here

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

No branches or pull requests

6 participants