defmodule SimpleshopTheme.Media do @moduledoc """ The Media context for managing images and file uploads. """ import Ecto.Query, warn: false alias SimpleshopTheme.Repo alias SimpleshopTheme.Media.Image @doc """ Uploads an image and stores it in the database. ## Examples iex> upload_image(%{image_type: "logo", filename: "logo.png", ...}) {:ok, %Image{}} """ def upload_image(attrs) do %Image{} |> Image.changeset(attrs) |> Repo.insert() end @doc """ Gets a single image by ID. ## Examples iex> get_image(id) %Image{} iex> get_image("nonexistent") nil """ def get_image(id) do Repo.get(Image, id) end @doc """ Gets the current logo image. ## Examples iex> get_logo() %Image{} """ def get_logo do Repo.one(from i in Image, where: i.image_type == "logo", order_by: [desc: i.inserted_at], limit: 1) end @doc """ Gets the current header image. ## Examples iex> get_header() %Image{} """ def get_header do Repo.one(from i in Image, where: i.image_type == "header", order_by: [desc: i.inserted_at], limit: 1) end @doc """ Deletes an image. ## Examples iex> delete_image(image) {:ok, %Image{}} """ def delete_image(%Image{} = image) do Repo.delete(image) end @doc """ Lists all images of a specific type. ## Examples iex> list_images_by_type("logo") [%Image{}, ...] """ def list_images_by_type(type) do Repo.all(from i in Image, where: i.image_type == ^type, order_by: [desc: i.inserted_at]) end end