![]() ![]() For that, we use references: # priv/repo/migrations/xxx_create_messages.exs #. Apart from the text column, we also want this table to contain one foreign key that links to rooms (= the room that the message was written in) and one that links to users (= the user that wrote the message). Next up is the migration file for messages. It will only contain the ID of the room (plus the created_at and updated_at fields), which is added by default. Our first migration file which creates the rooms table actually doesn't need to be changed. Now we need to slightly tweak the migration files that were generated in the priv/repo/migrations-folder before we run them. Mix Chat.RoomUser room_usersĪfter you've run the commands, you should have a folder structure that looks like this. With that in mind, open a new terminal window and run the following three commands: mix Chat.Room rooms This time, it makes sense to have a Chat-context, since messages, rooms and room_users are all related to chatting in some way. We previously created an Accounts-context to hold our user schema and everything related to authentication. For that, we first need to create database tables for this data, so we'll use Mix to create three database tables: messages, rooms and room_users (which connects users to rooms).īefore we go ahead and create the schema for these tables, let's plan our folder structure a little bit. We can now display our users in JSON format, but for our Chat app to work, we also need to be able to show rooms and messages in JSON. ![]() Now, if you try to get a user that doesn't exist, you'll get this pretty error instead! Tables for our rooms and messages ![]() To make things clearer, we're going to put all of our API-related controllers in a new api-folder, which in turn is inside the default controllers-folder: # lib/messengyr_web/controllers/api/user_controller.ex defmodule MessengyrWeb.UserController do use MessengyrWeb, :controller def show(conn, %) do For now, we only want to be able to get a single user, so we'll limit the endpoint to only GET /users/:id, which is tied to the :show function.Īfter that, we create our UserController containing a show function, which will only output the user ID in raw text for now. We then use the resources function, which automatically creates RESTful endpoints for creating, getting, editing and deleting users. Resources "/users", UserController, only: Īs you can see, we pipe the request through the predefined :api pipeline, which makes sure that our request only uses JSON. # lib/messengyr_web/router.ex defmodule MessengyrWeb.Router do #. That way, we can clearly distinguish between the routes that output HTML, and the ones that output JSON. This time, we're going to create a brand new scope under the /api namespace, where we'll keep all our API routes. We want to be able to make a GET request to /api/users/1 and receive the data for the user with ID 11 in JSON format.Īs always when it comes to creating new routes, we'll start in the router.ex file. In this lesson, we're going to build an API that outputs data in JSON format, so that we can make AJAX calls to it from the Messaging page. ![]()
0 Comments
Leave a Reply. |