List of Listings

Chapter 1. Ride the Phoenix

Listing 1.1. An example RaceCar module in Elixir

Chapter 2. Intro to Elixir

Listing 2.1. FizzBuzz implementation 1

Listing 2.2. FizzBuzz implementation 2

Listing 2.3. FizzBuzz implementation 3

Listing 2.4. Getting help from an IEx session

Listing 2.5. Using different data types

Listing 2.6. Pseudocode for making a sandwich

Listing 2.7. Creating an anonymous function and binding it to add_ingredient

Listing 2.8. Using an anonymous function

Listing 2.9. Discovering charlist gotchas

Listing 2.10. Playing with maps in IEx

Listing 2.11. Using the alternate atom key syntax for maps

Listing 2.12. Demonstrating function overloading

Listing 2.13. Example Math module

Listing 2.14. Importing functions from external modules

Listing 2.15. Deli pseudocode

Listing 2.16. Deli pseudocode with the pipe operator

Listing 2.17. Should you eat the mystery sandwich?

Chapter 3. A little Phoenix overview

Listing 3.1. The Blog.Web module (lib/blog_web/endpoint.ex), comments removed

Listing 3.2. The BlogWeb.Router module (lib/blog_web/router.ex)

Listing 3.3. mix phx.routes output

Listing 3.4. BlogWeb.PostController (lib/blog_web/controllers/post_controller .ex)

Listing 3.5. The Blog.App module (lib/blog/app/app.ex)

Listing 3.6. BlogWeb.PostView (lib/blog_web/views/post_view.ex)

Listing 3.7. show.html.eex (lib/blog_web/templates/post/show.html.eex)

Chapter 4. Phoenix is not your application

Listing 4.1. Creating the Item module

Listing 4.2. Creating defaults for a data structure

Listing 4.3. An Auction module to help retrieve data from the database

Listing 4.4. Adding a fake repo

Listing 4.5. Defining Auction.FakeRepo.get!/2

Listing 4.6. Adding get_by/1 to Auction.FakeRepo

Listing 4.7. The full Auction.FakeRepo module

Chapter 5. Elixir application structure

Listing 5.1. Letting mix tell you what it can do

Listing 5.2. Using mix help new to learn about creating a new Elixir project

Listing 5.3. Using mix new

Listing 5.4. Generating an umbrella application

Listing 5.5. Generating the Auction application

Listing 5.6. The auction_umbrella/apps/auction/mix.exs file (some comments removed)

Listing 5.7. The project function

Listing 5.8. The application function

Listing 5.9. The empty deps function

Listing 5.10. The new contents of lib/auction.ex is your Auction module

Listing 5.11. Your Auction.Item module in the lib/auction/item.ex file

Listing 5.12. Auction.FakeRepo code in the lib/auction/fake_repo.ex file

Listing 5.13. Trying out the compiled Auction application

Listing 5.14. Searching Hex for a React.js package

Listing 5.15. Searching hex.pm for Ecto

Listing 5.16. Specifying your first dependency

Listing 5.17. Your newly specified dependency list

Listing 5.18. The newly specified dependency list

Listing 5.19. Getting dependencies with mix deps.get

Listing 5.20. Temporarily adding UUID to your dependencies

Listing 5.21. Using the UUID package in your auction application

Listing 5.22. Your deps function without UUID

Chapter 6. Bring in Phoenix

Listing 6.1. The new Phoenix Mix tasks

Listing 6.2. The output of mix help phx.new.web

Listing 6.3. Configuring Phoenix in auction_umbrella/config/config.exs

Listing 6.4. Your Phoenix application is ready!

Listing 6.5. The default list of dependencies

Listing 6.6. AuctionWeb.PageController

Listing 6.7. Capturing the list of Items

Listing 6.8. Passing items to the view/template

Listing 6.9. Using the for comprehension in index.html.eex

Chapter 7. Being persistent with a database

Listing 7.1. Specifying Ecto and Postgrex as dependencies

Listing 7.2. Letting Ecto know which repos you have in your application

Listing 7.3. Configuring Auction.Repo

Listing 7.4. Setting up the Auction.Repo file

Listing 7.5. Defining Auction.Item’s schema for Ecto

Listing 7.6. Boilerplate migration code

Listing 7.7. Filling out the data for your table-creation migration

Listing 7.8. Adding your repo to the list of workers to be supervised

Listing 7.9. Adding insert_item/1 to Auction

Listing 7.10. Inserting the first Item

Listing 7.11. Listing the items in your database

Listing 7.12. Retrieving items from the database

Listing 7.13. Adding delete_item/1 to Auction

Chapter 8. Making changes with Ecto.Changeset

Listing 8.1. Attempting to use Ecto.Repo.update/2 without a changeset

Listing 8.2. Defining Auction.Item.changeset/2

Listing 8.3. Using Ecto.Changeset.cast/4 to limit the fields you accept changes for

Listing 8.4. Inspecting the return value of Auction.Item.changeset/2 in IEx

Listing 8.5. Attempting to change the title of your item

Listing 8.6. Attempting to change disallowed fields in an item

Listing 8.7. Updating Auction.Item.changeset/2 to require title

Listing 8.8. Not including a title in your Changeset

Listing 8.9. Adding length validation to title

Listing 8.10. Attempting to bypass the minimum-length restriction

Listing 8.11. Adding maximum-length validation to description

Listing 8.12. Defining your custom validator for ends_at

Listing 8.13. Adding your custom validator to the validation pipeline

Listing 8.14. Attempting to create an item with an ends_at in the past

Listing 8.15. Updating a record using your changeset

Listing 8.16. Adding Auction.update_item/2 to your public interface

Listing 8.17. Using Auction.update_item/2

Chapter 9. Transforming data in your browser

Listing 9.1. Defining a set of RESTful routes for items

Listing 9.2. Using Phoenix.Routes.resources/4

Listing 9.3. Restricting the resource route creation

Listing 9.4. Barebones AuctionWeb.ItemController implementation

Listing 9.5. Adding index/2 to AuctionWeb.ItemController

Listing 9.6. Defining AuctionWeb.ItemView

Listing 9.7. Listing the items in the database

Listing 9.8. Adding the :show action to the allowed list

Listing 9.9. Implementing show/2

Listing 9.10. Showing the details of an item

Listing 9.11. Linking to each item’s own page

Listing 9.12. Adding :new and :create to your items resource

Listing 9.13. Implementing AuctionWeb.ItemController.new/2

Listing 9.14. Implementing Auction.new_item/0

Listing 9.15. Creating a template at .../templates/item/new.html.eex

Listing 9.16. Implementing AuctionWeb.ItemController.create/2

Listing 9.17. The new Auction.insert_item/1

Listing 9.18. Matching the return value of Auction.insert_item/1

Listing 9.19. Showing errors on your form

Listing 9.20. Adding edit and update to the routes

Listing 9.21. Defining AuctionWeb.ItemController.edit/2

Listing 9.22. Defining Auction.edit_item/1

Listing 9.23. Rendering one template inside another

Listing 9.24. Creating the form template

Listing 9.25. Creating the edit.html.eex template

Listing 9.26. Defining update/2

Chapter 10. Plugs, assigns, and dealing with session data

Listing 10.1. Defining the Auction.User module

Listing 10.2. The migration to create the users table

Listing 10.3. Including comeonin and pbkdf2_elixir as deps

Listing 10.4. Defining the Auction.User changesets

Listing 10.5. Implementing the beginnings of the Auction.Password module

Listing 10.6. Trying out the changesets in IEx

Listing 10.7. Implementing the new Auction functions

Listing 10.8. Creating RESTful routes for your User resource

Listing 10.9. The AuctionWeb.UserController controller

Listing 10.10. AuctionWeb.UserView implementation

Listing 10.11. Creating new.html.eex

Listing 10.12. The user details page

Listing 10.13. The section of endpoint.ex that sets up your session

Listing 10.14. Adding new routes in .../auction_web/router.ex

Listing 10.15. Creating .../auction_web/controllers/session_controller.ex

Listing 10.16. Creating .../auction_web/views/session_view.ex

Listing 10.17. Creating .../auction_web/templates/session/new.html.eex

Listing 10.18. Implementing AuctionWeb.SessionController.create/2

Listing 10.19. Implementing Auction.get_user_by_username_and_password/2

Listing 10.20. Implementing more of Auction.Password

Listing 10.21. Creating an authentication module plug

Listing 10.22. The current browser pipeline

Listing 10.23. Adding the authenticator plug to the browser pipeline

Listing 10.24. Rendering site navigation

Listing 10.25. Logging the user out by dropping their session

Listing 10.26. Restricting a user from accessing another user’s profile

Chapter 11. Associating records and accepting bids

Listing 11.1. Creating the bid schema at apps/auction/lib/auction/bid.ex

Listing 11.2. Editing the autogenerated migration

Listing 11.3. Editing the migration to add columns, references, and indexes

Listing 11.4. The modified Auction.Bid module

Listing 11.5. Implementing Auction.Bid.changeset/2

Listing 11.6. Trying the validations and constraints in IEx

Listing 11.7. Adding insert_bid/1 to the Auction module

Listing 11.8. Adding Auction.new_bid/0

Listing 11.9. Using the new Auction.new_bid/0 function

Listing 11.10. Adding the nested bids route

Listing 11.11. Displaying all your routes

Listing 11.12. The AuctionWeb.BidController

Listing 11.13. The modified item-show template

Listing 11.14. Adding has_many :bids to Auction.Item

Listing 11.15. Adding has_many :bids to Auction.User

Listing 11.16. An example of an N+1 query in Ruby with ActiveRecord

Listing 11.17. Trying out the has_many association

Listing 11.18. Using preload to eagerly fetch the data you need

Listing 11.19. Preloading the bids and the nested users

Listing 11.20. Using Auction.get_item_with_bids/1

Listing 11.21. Displaying the bids for an item

Listing 11.22. Creating AuctionWeb.ItemView.integer_to_currency/1

Listing 11.23. The Auction.get_bids_for_user/1 function

Listing 11.24. Adding bid retrieval to the controller

Listing 11.25. Displaying a user’s recent bid activity

Listing 11.26. The modified view section of AuctionWeb

Listing 11.27. The new AuctionWeb.GlobalHelpers module

Listing 11.28. Creating formatted_datetime as a global helper

Chapter 12. Using Phoenix channels for real-time communication

Listing 12.1. The initial state of user_socket.ex

Listing 12.2. The updated AuctionWeb.UserSocket

Listing 12.3. Setting up the join handler

Listing 12.4. The socket.js file with comments removed

Listing 12.5. The edited socket.js file

Listing 12.6. The app.js file after uncommenting the appropriate line

Listing 12.7. The debug output of the Phoenix server

Listing 12.8. Setting up the message listener

Listing 12.9. Writing the handle_in/3 implementation

Listing 12.10. The code you’ll refactor

Listing 12.11. Creating auction_web/lib/auction_web/views/bid_view.ex

Listing 12.12. HTML template for a single bid

Listing 12.13. The modified code in item/show.html.eex

Listing 12.14. Modifying the controller to broadcast a successful bid

Listing 12.15. Ordering the preloaded bids

Listing 12.16. Prepending the HTML

Chapter 13. Building an API

Listing 13.1. The :browser pipeline only accepts HTML requests

Listing 13.2. The api pipeline

Listing 13.3. Phoenix generator guessing that you want an API

Listing 13.4. Adding routes to handle item API requests

Listing 13.5. The AuctionWeb.Api.ItemController implementation

Listing 13.6. Returning a map containing item attributes

Listing 13.7. Using render_one/4 in the show.json template

Listing 13.8. Implementing render("index.json", %{items: items})

Listing 13.9. The implementation of AuctionWeb.Api.BidView

Listing 13.10. Calling render_many/4 from the item_with_bids.json structure

Listing 13.11. Implementing AuctionWeb.Api.UserView

Listing 13.12. Adding the user information to AuctionWeb.Api.BidView

Chapter 14. Testing in Elixir and Phoenix

Listing 14.1. The contents of my auction_test.exs file

Listing 14.2. The new AuctionTest module

Listing 14.3. The output of my test run

Listing 14.4. A failing test

Listing 14.5. Important comments from config.exs

Listing 14.6. The new test.exs config file

Listing 14.7. The test helper file contents

Listing 14.8. Modifying the AuctionTest module

Listing 14.9. Setting up aliases for the auction application

Listing 14.10. Testing list_items/0

Listing 14.11. Output from testing /0

Listing 14.12. Changing the logging level in test.exs

Listing 14.13. The nicely cleaned-up run of tests

Listing 14.14. Testing Auction.get_item/1

Listing 14.15. Testing Auction.insert_item/1

Listing 14.16. The Auction.get_user_by_username_and_password function

Listing 14.17. Beginning documentation for get_user_by_username_and_password/2

Listing 14.18. Further documenting get_user_by_username_and_password/2

Listing 14.19. Displaying your documentation in IEx

Listing 14.20. Including ex_doc during development

Listing 14.21. Adding examples and doctests

Listing 14.22. Adding happy-path documentation

Listing 14.23. Running your doctests with your test suite

Listing 14.24. Minimal documentation for the Auction module

Listing 14.25. I get an error when running the Phoenix tests

Listing 14.26. Adding a default route to your application

Listing 14.27. The initial ItemControllerTest

Listing 14.28. The new test for your index page

Listing 14.29. Testing that an item is added to the database

Listing 14.30. Code to test redirection

Listing 14.31. Testing that no item is created on error

Listing 14.32. Ignoring database noise in auction_web/config/test.exs

Listing 14.33. Testing that the new item form is rendered

Listing 14.34. The full ItemControllerTest

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.165.246