neiam
/cartosnap_ex
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
Log in to mark this repository.
basic trip-photo linkage support with ui for assigning/unassiging
5dd4046 · gmorell · 2023-08-22 04:04
Files changed
modified
lib/cartosnap_app/photos.ex
+179
−5
| 25 | 25 | Repo.all(Trip) |
| 26 | 26 | end |
| 27 | 27 | |
| 28 | + def list_trip_ids(user) do | |
| 29 | + q = from t in Trip, | |
| 30 | + where: t.user == ^user, | |
| 31 | + select: {t.name, t.id} | |
| 32 | + Repo.all(q) | |
| 33 | + end | |
| 34 | + | |
| 28 | 35 | @doc """ |
| 29 | 36 | Gets a single trip. |
| 30 | 37 |
| 209 | 216 | ** (Ecto.NoResultsError) |
| 210 | 217 | |
| 211 | 218 | """ |
| 219 | + | |
| 212 | 220 | def get_photo(id), do: Repo.get(Photo, id) |
| 213 | 221 | def get_photo!(id), do: Repo.get!(Photo, id) |
| 214 | 222 | |
| 223 | + def get_photo(id, :trip) do | |
| 224 | + q = from p in Photo, | |
| 225 | + left_join: pt in "photo_photo_trips", | |
| 226 | + on: pt.photo == p.id, | |
| 227 | + left_join: t in "photo_trips", | |
| 228 | + on: pt.trip == t.id, | |
| 229 | + select: %{p: p, trip_id: pt.trip, trip_name: t.name}, | |
| 230 | + where: p.id == ^id | |
| 231 | + Repo.one(q) | |
| 232 | + end | |
| 233 | + | |
| 234 | + def get_photo!(id, :trip) do | |
| 235 | + Repo.get!(Photo, id) | |
| 236 | + q = from p in Photo, | |
| 237 | + left_join: pt in "photo_photo_trips", | |
| 238 | + on: pt.photo == p.id, | |
| 239 | + left_join: t in "photo_trips", | |
| 240 | + on: pt.trip == t.id, | |
| 241 | + select: %{p: p, trip_id: pt.trip, trip_name: t.name}, | |
| 242 | + where: p.id == ^id, | |
| 243 | + limit: 1 | |
| 244 | + Repo.one(q) | |
| 245 | + end | |
| 246 | + | |
| 215 | 247 | def get_next_photo(:photo, %Photo{} = ph) do |
| 216 | 248 | q = from p in Photo, |
| 217 | 249 | where: p.date > ^ph.date, |
| 218 | 250 | order_by: [asc: p.date], |
| 219 | − limit: 1 | |
| 251 | + limit: 1, | |
| 252 | + left_join: pt in "photo_photo_trips", | |
| 253 | + on: pt.photo == p.id, | |
| 254 | + left_join: t in "photo_trips", | |
| 255 | + on: pt.trip == t.id, | |
| 256 | + select: %{p: p, trip_id: pt.trip, trip_name: t.name} | |
| 220 | 257 | q |> Repo.one |
| 221 | 258 | end |
| 222 | 259 | def get_next_photo(:trip, %Photo{} = ph) do |
| 227 | 264 | q = from p in Photo, |
| 228 | 265 | where: p.date > ^ph.date and p.promoted == true, |
| 229 | 266 | order_by: [asc: p.date], |
| 230 | − limit: 1 | |
| 267 | + limit: 1, | |
| 268 | + left_join: pt in "photo_photo_trips", | |
| 269 | + on: pt.photo == p.id, | |
| 270 | + left_join: t in "photo_trips", | |
| 271 | + on: pt.trip == t.id, | |
| 272 | + select: %{p: p, trip_id: pt.trip, trip_name: t.name} | |
| 231 | 273 | q |> Repo.one |
| 232 | 274 | end |
| 233 | 275 |
| 235 | 277 | q = from p in Photo, |
| 236 | 278 | where: p.date < ^ph.date, |
| 237 | 279 | order_by: [desc: p.date], |
| 238 | − limit: 1 | |
| 280 | + limit: 1, | |
| 281 | + left_join: pt in "photo_photo_trips", | |
| 282 | + on: pt.photo == p.id, | |
| 283 | + left_join: t in "photo_trips", | |
| 284 | + on: pt.trip == t.id, | |
| 285 | + select: %{p: p, trip_id: pt.trip, trip_name: t.name} | |
| 239 | 286 | q |> Repo.one |
| 240 | 287 | end |
| 241 | 288 |
| 247 | 294 | q = from p in Photo, |
| 248 | 295 | where: p.date < ^ph.date and p.promoted == true, |
| 249 | 296 | order_by: [desc: p.date], |
| 250 | − limit: 1 | |
| 297 | + limit: 1, | |
| 298 | + left_join: pt in "photo_photo_trips", | |
| 299 | + on: pt.photo == p.id, | |
| 300 | + left_join: t in "photo_trips", | |
| 301 | + on: pt.trip == t.id, | |
| 302 | + select: %{p: p, trip_id: pt.trip, trip_name: t.name} | |
| 251 | 303 | q |> Repo.one |
| 252 | 304 | end |
| 253 | 305 | def get_next_photo(:photo, id) do |
| 256 | 308 | ph -> q = from p in Photo, |
| 257 | 309 | where: p.date > ^ph.date, |
| 258 | 310 | order_by: [asc: p.date], |
| 259 | − limit: 1 | |
| 311 | + limit: 1, | |
| 312 | + left_join: pt in "photo_photo_trips", | |
| 313 | + on: pt.photo == p.id, | |
| 314 | + left_join: t in "photo_trips", | |
| 315 | + on: pt.trip == t.id, | |
| 316 | + select: %{p: p, trip_id: pt.trip, trip_name: t.name} | |
| 260 | 317 | q |> Repo.one |
| 261 | 318 | end |
| 262 | 319 | end |
| 306 | 363 | q |> Repo.one |
| 307 | 364 | end |
| 308 | 365 | end |
| 366 | + | |
| 309 | 367 | @doc """ |
| 310 | 368 | Creates a photo. |
| 311 | 369 |
| 394 | 452 | def change_photo(%Photo{} = photo, attrs \\ %{}) do |
| 395 | 453 | Photo.changeset(photo, attrs) |
| 396 | 454 | end |
| 455 | + | |
| 456 | + alias CartosnapApp.Photos.PhotoTrip | |
| 457 | + | |
| 458 | + @doc """ | |
| 459 | + Returns the list of photo_photo_trips. | |
| 460 | + | |
| 461 | + ## Examples | |
| 462 | + | |
| 463 | + iex> list_photo_photo_trips() | |
| 464 | + [%PhotoTrip{}, ...] | |
| 465 | + | |
| 466 | + """ | |
| 467 | + def list_photo_photo_trips do | |
| 468 | + Repo.all(PhotoTrip) | |
| 469 | + end | |
| 470 | + | |
| 471 | + @doc """ | |
| 472 | + Gets a single photo_trip. | |
| 473 | + | |
| 474 | + Raises `Ecto.NoResultsError` if the Photo trip does not exist. | |
| 475 | + | |
| 476 | + ## Examples | |
| 477 | + | |
| 478 | + iex> get_photo_trip!(123) | |
| 479 | + %PhotoTrip{} | |
| 480 | + | |
| 481 | + iex> get_photo_trip!(456) | |
| 482 | + ** (Ecto.NoResultsError) | |
| 483 | + | |
| 484 | + """ | |
| 485 | + def get_photo_trip!(id), do: Repo.get!(PhotoTrip, id) | |
| 486 | + | |
| 487 | + @doc """ | |
| 488 | + Creates a photo_trip. | |
| 489 | + | |
| 490 | + ## Examples | |
| 491 | + | |
| 492 | + iex> create_photo_trip(%{field: value}) | |
| 493 | + {:ok, %PhotoTrip{}} | |
| 494 | + | |
| 495 | + iex> create_photo_trip(%{field: bad_value}) | |
| 496 | + {:error, %Ecto.Changeset{}} | |
| 497 | + | |
| 498 | + """ | |
| 499 | + def create_photo_trip(attrs \\ %{}) do | |
| 500 | + %PhotoTrip{} | |
| 501 | + |> PhotoTrip.changeset(attrs) | |
| 502 | + |> Repo.insert() | |
| 503 | + end | |
| 504 | + | |
| 505 | + @doc """ | |
| 506 | + Updates a photo_trip. | |
| 507 | + | |
| 508 | + ## Examples | |
| 509 | + | |
| 510 | + iex> update_photo_trip(photo_trip, %{field: new_value}) | |
| 511 | + {:ok, %PhotoTrip{}} | |
| 512 | + | |
| 513 | + iex> update_photo_trip(photo_trip, %{field: bad_value}) | |
| 514 | + {:error, %Ecto.Changeset{}} | |
| 515 | + | |
| 516 | + """ | |
| 517 | + def update_photo_trip(%PhotoTrip{} = photo_trip, attrs) do | |
| 518 | + photo_trip | |
| 519 | + |> PhotoTrip.changeset(attrs) | |
| 520 | + |> Repo.update() | |
| 521 | + end | |
| 522 | + | |
| 523 | + @doc """ | |
| 524 | + Deletes a photo_trip. | |
| 525 | + | |
| 526 | + ## Examples | |
| 527 | + | |
| 528 | + iex> delete_photo_trip(photo_trip) | |
| 529 | + {:ok, %PhotoTrip{}} | |
| 530 | + | |
| 531 | + iex> delete_photo_trip(photo_trip) | |
| 532 | + {:error, %Ecto.Changeset{}} | |
| 533 | + | |
| 534 | + """ | |
| 535 | + def delete_photo_trip(%PhotoTrip{} = photo_trip) do | |
| 536 | + Repo.delete(photo_trip) | |
| 537 | + end | |
| 538 | + | |
| 539 | + @doc """ | |
| 540 | + Returns an `%Ecto.Changeset{}` for tracking photo_trip changes. | |
| 541 | + | |
| 542 | + ## Examples | |
| 543 | + | |
| 544 | + iex> change_photo_trip(photo_trip) | |
| 545 | + %Ecto.Changeset{data: %PhotoTrip{}} | |
| 546 | + | |
| 547 | + """ | |
| 548 | + def change_photo_trip(%PhotoTrip{} = photo_trip, attrs \\ %{}) do | |
| 549 | + PhotoTrip.changeset(photo_trip, attrs) | |
| 550 | + end | |
| 551 | + | |
| 552 | + def set_photo_trip(photo_id, trip_id) do | |
| 553 | +# clear_photo_trip(photo_id) | |
| 554 | + create_photo_trip(%{photo: photo_id, trip: trip_id}) | |
| 555 | + end | |
| 556 | + | |
| 557 | + def clear_photo_trip(photo_id) do | |
| 558 | + from(pt in PhotoTrip, where: pt.photo == ^photo_id) |> Repo.delete_all | |
| 559 | + end | |
| 560 | + | |
| 561 | + def get_trip_random_photo(trip_id) do | |
| 562 | + q = from pt in PhotoTrip, | |
| 563 | + where: pt.trip == ^trip_id, | |
| 564 | + left_join: p in "photo_photos", | |
| 565 | + on: pt.photo == p.id, | |
| 566 | + select: p.url_preview, | |
| 567 | + limit: 1, | |
| 568 | + order_by: fragment("RANDOM()") | |
| 569 | + q |> Repo.one | |
| 570 | + end | |
| 397 | 571 | end |
added
lib/cartosnap_app/photos/photo_trip.ex
+23
−0
| 1 | +defmodule CartosnapApp.Photos.PhotoTrip do | |
| 2 | + use Ecto.Schema | |
| 3 | + import Ecto.Changeset | |
| 4 | + | |
| 5 | + @primary_key {:id, :binary_id, autogenerate: true} | |
| 6 | + @foreign_key_type :binary_id | |
| 7 | + schema "photo_photo_trips" do | |
| 8 | + | |
| 9 | + field :photo, :binary_id | |
| 10 | + field :trip, :binary_id | |
| 11 | +# belongs_to :photo, :binary_id | |
| 12 | +# belongs_to :trip, :binary_id | |
| 13 | + | |
| 14 | + timestamps() | |
| 15 | + end | |
| 16 | + | |
| 17 | + @doc false | |
| 18 | + def changeset(photo_trip, attrs) do | |
| 19 | + photo_trip | |
| 20 | + |> cast(attrs, [:photo, :trip]) | |
| 21 | + |> validate_required([:photo, :trip]) | |
| 22 | + end | |
| 23 | +end |
modified
lib/cartosnap_app_web/components/live_edit.ex
+10
−0
| 12 | 12 | |
| 13 | 13 | {:noreply, assign(socket, :ple_mode, "anchor")} |
| 14 | 14 | end |
| 15 | + | |
| 16 | + def handle_event("set", data, socket) do | |
| 17 | + # send message to view | |
| 18 | + socket | |
| 19 | + |> Map.get(:assigns) | |
| 20 | + |> Map.get(:ple_viewpid) | |
| 21 | + |> send({:set, data |> Map.put("id", socket.assigns.photo_id)}) | |
| 22 | + | |
| 23 | + {:noreply, assign(socket, :ple_mode, "anchor")} | |
| 24 | + end | |
| 15 | 25 | end |
modified
lib/cartosnap_app_web/components/ple_components.ex
+5
−5
| 59 | 59 | phx-click="ple-focus" |
| 60 | 60 | phx-target={@myself} |
| 61 | 61 | > |
| 62 | − <span style="color: #111; font-style: italic;">Untitled</span> | |
| 62 | + <span style="color: #111; font-style: italic;">Unset</span> | |
| 63 | 63 | </span> |
| 64 | 64 | """ |
| 65 | 65 | end |
| 85 | 85 | phx-blur={@ple_action} |
| 86 | 86 | phx-target={@myself} |
| 87 | 87 | phx-click-away={@ple_action} |
| 88 | − phx-value-target="myfield" | |
| 88 | + phx-value-target="select" | |
| 89 | 89 | > |
| 90 | − <%= select(:ok, :ord, @ple_options, phx_change: "reorder", phx_target: @myself) %> | |
| 90 | + <%= select(:ok, :selected, @ple_options, phx_change: "set", phx_target: @myself) %> | |
| 91 | 91 | </form> |
| 92 | 92 | </div> |
| 93 | 93 | """ |
| 103 | 103 | phx-target={@myself} |
| 104 | 104 | phx-click-away={@ple_action} |
| 105 | 105 | > |
| 106 | − <%= select(:ok, :ord, @ple_options, | |
| 107 | − phx_change: "reorder", | |
| 106 | + <%= select(:ok, :selected, @ple_options, | |
| 107 | + phx_change: "set", | |
| 108 | 108 | phx_target: @myself, |
| 109 | 109 | selected: @ple_data |
| 110 | 110 | ) %> |
modified
lib/cartosnap_app_web/live/photo_live/form_component.html.heex
+0
−2
| 8 | 8 | phx-submit="save" |
| 9 | 9 | > |
| 10 | 10 | <.input field={@form[:name]} type="text" label="Name" /> |
| 11 | − <.input field={@form[:date]} type="date" label="Date" /> | |
| 12 | − <.input field={@form[:geo_point]} type="text" label="Point" /> | |
| 13 | 11 | <.input field={@form[:promoted]} type="checkbox" label="Promoted" /> |
| 14 | 12 | <:actions> |
| 15 | 13 | <.button phx-disable-with="Saving...">Save Test</.button> |
modified
lib/cartosnap_app_web/live/photo_live/index.html.heex
+1
−1
Click to load diff…
modified
lib/cartosnap_app_web/live/photo_live/show.ex
+26
−2
Click to load diff…
modified
lib/cartosnap_app_web/live/photo_live/show.html.heex
+41
−30
Click to load diff…
added
lib/cartosnap_app_web/live/photo_trip_live/form_component.ex
+90
−0
Click to load diff…
added
lib/cartosnap_app_web/live/photo_trip_live/index.ex
+47
−0
Click to load diff…
added
lib/cartosnap_app_web/live/photo_trip_live/index.html.heex
+40
−0
Click to load diff…
added
lib/cartosnap_app_web/live/photo_trip_live/show.ex
+21
−0
Click to load diff…
added
lib/cartosnap_app_web/live/photo_trip_live/show.html.heex
+25
−0
Click to load diff…
modified
lib/cartosnap_app_web/live/trip_live/index.ex
+13
−2
Click to load diff…
modified
lib/cartosnap_app_web/live/trip_live/index.html.heex
+20
−51
Click to load diff…
modified
lib/cartosnap_app_web/router.ex
+8
−0
Click to load diff…
added
priv/repo/migrations/20230822014350_create_photo_photo_trips.exs
+16
−0
Click to load diff…
modified
test/cartosnap_app/photos_test.exs
+52
−0
Click to load diff…
added
test/cartosnap_app_web/live/photo_trip_live_test.exs
+108
−0
Click to load diff…
modified
test/support/fixtures/photos_fixtures.ex
+14
−0
Click to load diff…
Parents: f059361