项目作者: RitreshGirdhar

项目描述 :
Elixir Rest Api application example
高级语言: Elixir
项目地址: git://github.com/RitreshGirdhar/elixir-phoenix-rest-api.git
创建时间: 2021-06-07T13:18:10Z
项目社区:https://github.com/RitreshGirdhar/elixir-phoenix-rest-api

开源协议:

下载


elixir-phoenix-rest-api

https://www.dairon.org/2020/07/06/simple-rest-api-with-elixir-phoenix.html

Steps

1.

  1. $ mix phx.new books_api --no-html --no-webpack --binary-id && cd books_api
  2. * creating books_api/config/config.exs
  3. * creating books_api/config/dev.exs
  4. * creating books_api/config/prod.exs
  5. * creating books_api/config/prod.secret.exs
  6. * creating books_api/config/test.exs
  7. * creating books_api/lib/books_api/application.ex
  8. * creating books_api/lib/books_api.ex
  9. * creating books_api/lib/books_api_web/channels/user_socket.ex
  10. * creating books_api/lib/books_api_web/views/error_helpers.ex
  11. * creating books_api/lib/books_api_web/views/error_view.ex
  12. * creating books_api/lib/books_api_web/endpoint.ex
  13. * creating books_api/lib/books_api_web/router.ex
  14. * creating books_api/lib/books_api_web/telemetry.ex
  15. * creating books_api/lib/books_api_web.ex
  16. * creating books_api/mix.exs
  17. * creating books_api/README.md
  18. * creating books_api/.formatter.exs
  19. * creating books_api/.gitignore
  20. * creating books_api/test/support/channel_case.ex
  21. * creating books_api/test/support/conn_case.ex
  22. * creating books_api/test/test_helper.exs
  23. * creating books_api/test/books_api_web/views/error_view_test.exs
  24. * creating books_api/lib/books_api/repo.ex
  25. * creating books_api/priv/repo/migrations/.formatter.exs
  26. * creating books_api/priv/repo/seeds.exs
  27. * creating books_api/test/support/data_case.ex
  28. * creating books_api/lib/books_api_web/gettext.ex
  29. * creating books_api/priv/gettext/en/LC_MESSAGES/errors.po
  30. * creating books_api/priv/gettext/errors.pot
  31. Fetch and install dependencies? [Yn] Y
  32. * running mix deps.get
  33. * running mix deps.compile
  34. We are almost there! The following steps are missing:
  35. $ cd books_api
  36. Then configure your database in config/dev.exs and run:
  37. $ mix ecto.create
  38. Start your Phoenix app with:
  39. $ mix phx.server
  40. You can also run your app inside IEx (Interactive Elixir) as:
  41. $ iex -S mix phx.server
  1. $ cd books_api/
  2. $ mix ecto.create
  3. Compiling 11 files (.ex)
  4. Generated books_api app
  5. The database for BooksApi.Repo has already been created
  1. $ mix phx.server
  2. [info] Running BooksApiWeb.Endpoint with cowboy 2.9.0 at 0.0.0.0:4000 (http)
  3. [info] Access BooksApiWeb.Endpoint at http://localhost:4000

DB changes

  1. connect to db Image pacto
  1. drop schema
  2. $ mix ecto.drop
  3. ** (Mix) The database for BooksApi.Repo couldn't be dropped: ERROR 55006 (object_in_use) database "books_api_dev" is being accessed by other users
  4. There is 1 other session using the database.

Close postico connection

  1. $ mix ecto.drop
  2. The database for BooksApi.Repo has been dropped
  1. $ mix ecto.create
  2. The database for BooksApi.Repo has been created
  1. mix phx.gen.context Store Book books \
  2. > title:string isbn:text:unique description:text price:float authors:array:string
  3. * creating lib/books_api/store/book.ex
  4. * creating priv/repo/migrations/20210607133628_create_books.exs
  5. * creating lib/books_api/store.ex
  6. * injecting lib/books_api/store.ex
  7. * creating test/books_api/store_test.exs
  8. * injecting test/books_api/store_test.exs
  9. Remember to update your repository by running migrations:
  10. $ mix ecto.migrate
  1. $ mix ecto.migrate
  2. Compiling 2 files (.ex)
  3. Generated books_api app
  4. 19:08:46.734 [info] == Running 20210607133628 BooksApi.Repo.Migrations.CreateBooks.change/0 forward
  5. 19:08:46.739 [info] create table books
  6. 19:08:46.749 [info] create index books_isbn_index
  7. 19:08:46.755 [info] == Migrated 20210607133628 in 0.0s

TODO >>image3

  1. $ mix phx.gen.json Store Book books \
  2. > title:string isbn:text:unique description:text price:float authors:array:string --no-context --no-schema
  3. You are generating into an existing context.
  4. The BooksApi.Store context currently has 6 functions and 1 files in its directory.
  5. * It's OK to have multiple resources in the same context as long as they are closely related. But if a context grows too large, consider breaking it apart
  6. * If they are not closely related, another context probably works better
  7. The fact two entities are related in the database does not mean they belong to the same context.
  8. If you are not sure, prefer creating a new context over adding to the existing one.
  9. Would you like to proceed? [Yn] Y
  10. * creating lib/books_api_web/controllers/book_controller.ex
  11. * creating lib/books_api_web/views/book_view.ex
  12. * creating test/books_api_web/controllers/book_controller_test.exs
  13. * creating lib/books_api_web/views/changeset_view.ex
  14. * creating lib/books_api_web/controllers/fallback_controller.ex
  15. Add the resource to your :api scope in lib/books_api_web/router.ex:
  16. resources "/books", BookController, except: [:new, :edit]

change router.ex

  1. scope "/api", BooksApiWeb do
  2. pipe_through :api
  3. resources "/books", BookController, except: [:new, :edit]
  4. end
  1. $ mix phx.server
  2. Compiling 5 files (.ex)
  3. Generated books_api app
  4. [info] Running BooksApiWeb.Endpoint with cowboy 2.9.0 at 0.0.0.0:4000 (http)
  5. [info] Access BooksApiWeb.Endpoint at http://localhost:4000