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] Posts (for learning) #81

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

[API] Posts (for learning) #81

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

Comments

@VictorWinberg
Copy link
Member

VictorWinberg commented Jul 11, 2018

[API] Posts

The post resource should fulfill requirements below

  • POST /api/posts | Create a post
  • GET /api/posts | Get a listing of posts
  • GET /api/posts/:id | Get details about a post
  • PUT /api/posts/:id | Update a post
  • DELETE /api/posts/:id | Delete a post

Generate a json post resource with title and body

Hint: phoenix doc

Spoiler

mix phoenix.gen.json Post posts title:string body:text


Then add route and migrate

Spoiler

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

resources "/posts", PostController, except: [:new, :edit]

Remember to update your repository by running migrations:

$ mix ecto.migrate

Edit your json post resource as needed to fulfill requirements

Change ur route to be able to fulfill the requirements

Spoiler

In web/routes.ex, remove the except: [:new, :edit].


Try to use ur newly created json post resource by sending POST requests to your server api.

Spoiler

Send the following as body in Postman:

{
  "post": {
    "title": "First Post",
    "body": "Content"
  }
}


Tip: Download Postman.

Further notes:

In web/controllers/post_controller.ex:

def create(conn, %{"post" => post_params}) do

expects a POST request as:

{
  "post": {
    "title": "First Post",
    "body": "Content"
  }
}

in contrast to:

def create(conn, post_params) do

which expects a POST request as:

{
  "title": "First Post",
  "body": "Content"
}
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