- Create settings table for site-wide key-value configuration - Create images table for BLOB storage of logo/header images - Add Setting schema with JSON/string/integer/boolean support - Add ThemeSettings embedded schema with all theme options - Add Settings context with get/put/update operations - Add Media context for image uploads and retrieval - Add Image schema with SVG detection and storage - Add 9 curated theme presets (gallery, studio, boutique, etc.) - Add comprehensive tests for Settings and Media contexts - Add seeds with default Studio preset - All tests passing (29 tests, 0 failures)
93 lines
1.6 KiB
Elixir
93 lines
1.6 KiB
Elixir
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
|