Programming Phoenix LiveView - Chapter 6
Explore demographics and ratings
alias Pento.Accounts
alias Pento.Survey
alias Pento.Catalog
Create user
user_attrs = %{
email: "cassandra@grox.io",
password: "listen-to-me-plz"
}
user =
case Accounts.get_user_by_email(user_attrs.email) do
user ->
user
nil ->
{:ok, user} = Accounts.register_user(user_attrs)
user
end
Create demographics
demographic_attrs = %{
user_id: user.id,
gender: "prefer not to say",
year_of_birth: 1989
}
Survey.create_demographic(demographic_attrs)
Create demographic for my user
email = System.get_env("LB_USER_EMAIL")
my_user = Pento.Accounts.get_user_by_email(email)
attrs = %{gender: "male", year_of_birth: 1979, user_id: my_user.id}
Survey.create_demographic(attrs)
Create rating
product = Pento.Catalog.get_product_by_sku("1234567")
rating_attrs = %{
user_id: user.id,
product_id: product.id,
stars: 5
}
Survey.create_rating(rating_attrs)
Use demographic query
Survey.get_demographic_by_user(user)
Use ratings query
Catalog.list_products_with_user_ratings(user)