Slide 82
Slide 82 text
defmodule AutoMaxx.VehicleInspectionRatingController do
def update(conn, %{
user_id: user_id, vehicle_id: vehicle_id, rating: new_rating
}) do
with user <- Repo.get_by(User, user_id),
vehicle <-
Repo.get_by(Vehicle, vehicle_id)
|> Repo.preload(:rating),
:ok <- InspectionRatingPolicy.editable_by?(vehicle, user) do
inspection_rating =
vehicle.rating
|> rating.changeset(%{value: new_rating})
|> Repo.insert!()
render(conn, "show.html", inspection_rating: inspection_rating)
else
render(conn, "error.html", message: "Oops!")
end
end
end 65 / 128