diff --git a/README.md b/README.md index feb6166..7cda2ba 100644 --- a/README.md +++ b/README.md @@ -1164,6 +1164,10 @@ defmodule UsersTable do Pockets.put(@table, person_id, %{stripe_id: stripe_id, status: status}) end + def fetch_user(person_id) do + Pockets.get(@table, person_id) + end + end ``` @@ -1182,6 +1186,8 @@ If the file already exists, we open the file. - `create_user/3` receives a `stripe_id`, `person_id` and `status` (pertaining to whether the payment has been made or not) and creates a user object. +- `fetch_user/1` retrieves the persisted user +according to the given` person_id`. Let's make use of some of these functions. We want to setup the `DETS` table on the process startup. @@ -1199,6 +1205,12 @@ UsersTable.init() Awesome! + + +# todo +ver todo no checkout_session_controller.ex +remove swoosh, mailer, postgres, ecto + # Thanks! Thanks for learning about payment processing with us! diff --git a/lib/app/users.ex b/lib/app/users.ex index de4b75e..7a8055c 100644 --- a/lib/app/users.ex +++ b/lib/app/users.ex @@ -21,4 +21,8 @@ defmodule UsersTable do Pockets.put(@table, person_id, %{stripe_id: stripe_id, status: status}) end + def fetch_user(person_id) do + Pockets.get(@table, person_id) + end + end diff --git a/lib/app_web/controllers/app_controller.ex b/lib/app_web/controllers/app_controller.ex index eb95642..d4bc469 100644 --- a/lib/app_web/controllers/app_controller.ex +++ b/lib/app_web/controllers/app_controller.ex @@ -2,8 +2,14 @@ defmodule AppWeb.AppController do use AppWeb, :controller def home(conn, _params) do - # The home page is often custom made, - # so skip the default app layout. - render(conn, :app, layout: false) + + person_id = conn.assigns.person.id + case UsersTable.fetch_user(person_id) do + nil -> + conn |> redirect(to: ~p"/") + + _ -> + render(conn, :app, layout: false) + end end end diff --git a/lib/app_web/controllers/page_controller.ex b/lib/app_web/controllers/page_controller.ex index 4f9d9ec..30ddb3a 100644 --- a/lib/app_web/controllers/page_controller.ex +++ b/lib/app_web/controllers/page_controller.ex @@ -2,15 +2,17 @@ defmodule AppWeb.PageController do use AppWeb, :controller def home(conn, _params) do - # The home page is often custom made, - # so skip the default app layout. render(conn, :home, layout: false) end def success(conn, %{"session_id" => session_id}) do case Stripe.Session.retrieve(session_id) do - {:ok, _session} -> + {:ok, session} -> + + person_id = conn.assigns.person.id + UsersTable.create_user(%{person_id: person_id, stripe_id: session.customer, status: true}) + render(conn, :success, layout: false) {:error, _error} ->