Skip to content

Commit

Permalink
Merge branch 'release/0.1.9'
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Nov 28, 2024
2 parents 8e72dba + 2339c25 commit 06f2429
Show file tree
Hide file tree
Showing 38 changed files with 415 additions and 258 deletions.
8 changes: 7 additions & 1 deletion lib/opsmaru/cache.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
defmodule Opsmaru.Cache do
use Nebulex.Cache,
otp_app: :opsmaru,
adapter: Nebulex.Adapters.Replicated
adapter: Nebulex.Adapters.Replicated,
default_key_generator: __MODULE__

@behaviour Nebulex.Caching.KeyGenerator

@impl true
def generate(mod, fun, args), do: :erlang.phash2({mod, fun, args})
end
10 changes: 5 additions & 5 deletions lib/opsmaru/content.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Opsmaru.Content do

alias __MODULE__.Technology

defdelegate list_technologies(options \\ [end_index: 5]),
defdelegate list_technologies(options \\ []),
to: Technology.Manager,
as: :list

Expand All @@ -43,7 +43,7 @@ defmodule Opsmaru.Content do
to: Post.Manager,
as: :featured

defdelegate posts_feed,
defdelegate posts_feed(options \\ []),
to: Post.Manager,
as: :feed

Expand All @@ -59,19 +59,19 @@ defmodule Opsmaru.Content do

alias __MODULE__.Movie

defdelegate show_movie(slug),
defdelegate show_movie(slug, options \\ []),
to: Movie.Manager,
as: :show

alias __MODULE__.Product

defdelegate list_products,
defdelegate list_products(options \\ []),
to: Product.Manager,
as: :list

alias __MODULE__.Page

defdelegate show_page(slug),
defdelegate show_page(slug, options \\ []),
to: Page.Manager,
as: :show

Expand Down
8 changes: 6 additions & 2 deletions lib/opsmaru/content/course/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ defmodule Opsmaru.Content.Course.Manager do
use Nebulex.Caching
import Opsmaru.Sanity

alias Opsmaru.Sanity.Response

alias Opsmaru.Cache
alias Opsmaru.Content.Course

@ttl :timer.hours(1)

@decorate cacheable(cache: Cache, key: {:course, slug}, opts: [ttl: @ttl])
@spec show(String.t(), Keyword.t()) :: %{data: %Course{}, perspective: String.t()}
@decorate cacheable(cache: Cache, match: &sanity_cache?/1, opts: [ttl: @ttl])
def show(slug, options \\ []) do
perspective = Keyword.get(options, :perspective, "published")

Expand Down Expand Up @@ -38,6 +41,7 @@ defmodule Opsmaru.Content.Course.Manager do

course = Course.parse(course)
full_overview = Req.get!(course.overview).body
%{course | overview: full_overview}

%Response{data: %{course | overview: full_overview}, perspective: perspective}
end
end
16 changes: 11 additions & 5 deletions lib/opsmaru/content/logo/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@ defmodule Opsmaru.Content.Logo.Manager do
use Nebulex.Caching
import Opsmaru.Sanity

alias Opsmaru.Sanity.Response

alias Opsmaru.Cache
alias Opsmaru.Content.Logo

@decorate cacheable(cache: Cache, key: :logos, opts: [ttl: :timer.hours(1)])
def list(_options \\ []) do
@decorate cacheable(cache: Cache, match: &sanity_cache?/1, opts: [ttl: :timer.hours(1)])
def list(options \\ []) do
perspective = Keyword.get(options, :perspective, "published")

query = ~s"""
*[_type == "logo"] | order(name asc){..., "image": image.asset -> url}
"""

%Sanity.Response{body: %{"result" => logos}} =
data =
query
|> Sanity.query(%{}, perspective: "published")
|> Sanity.query(%{}, perspective: perspective)
|> Sanity.request!(sanity_request_opts())
|> Sanity.result!()
|> Enum.map(&Logo.parse/1)

Enum.map(logos, &Logo.parse/1)
%Response{data: data, perspective: perspective}
end
end
16 changes: 11 additions & 5 deletions lib/opsmaru/content/movie/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ defmodule Opsmaru.Content.Movie.Manager do
use Nebulex.Caching
import Opsmaru.Sanity

alias Opsmaru.Sanity.Response

alias Opsmaru.Cache
alias Opsmaru.Content.Movie

@spec show(String.t()) :: %Movie{}
@decorate cacheable(cache: Cache, key: {:movie, slug}, opts: [ttl: :timer.hours(1)])
def show(slug) do
@decorate cacheable(cache: Cache, match: &sanity_cache?/1, opts: [ttl: :timer.hours(1)])
def show(slug, options \\ []) do
perspective = Keyword.get(options, :perspective, "published")

query = ~S"""
*[_type == "movie" && slug.current == $slug][0]{
...,
Expand All @@ -19,11 +23,13 @@ defmodule Opsmaru.Content.Movie.Manager do
}
"""

%Sanity.Response{body: %{"result" => movie_params}} =
data =
query
|> Sanity.query(%{slug: slug}, perspective: "published")
|> Sanity.query(%{slug: slug}, perspective: perspective)
|> Sanity.request!(sanity_request_opts())
|> Sanity.result!()
|> Movie.parse()

Movie.parse(movie_params)
%Response{data: data, perspective: perspective}
end
end
25 changes: 14 additions & 11 deletions lib/opsmaru/content/navigation/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule Opsmaru.Content.Navigation.Manager do
use Nebulex.Caching
import Opsmaru.Sanity

alias Opsmaru.Sanity.Response

alias Opsmaru.Content.Navigation

@ttl :timer.hours(24)
Expand All @@ -15,17 +17,18 @@ defmodule Opsmaru.Content.Navigation.Manager do
}
"""

@decorate cacheable(cache: Opsmaru.Cache, key: :navigations, opts: [ttl: @ttl])
def list(_options \\ []) do
@base_query
|> Sanity.query(%{}, perspective: "published")
|> Sanity.request!(sanity_request_opts())
|> case do
%Sanity.Response{body: %{"result" => navigations}} ->
Enum.map(navigations, &Navigation.parse/1)
@spec list(Keyword.t()) :: %{data: [%Navigation{}], perspective: String.t()}
@decorate cacheable(cache: Opsmaru.Cache, opts: [ttl: @ttl])
def list(options \\ []) do
perspective = Keyword.get(options, :perspective, "published")

data =
@base_query
|> Sanity.query(%{}, perspective: perspective)
|> Sanity.request!(sanity_request_opts())
|> Sanity.result!()
|> Enum.map(&Navigation.parse/1)

error ->
error
end
%Response{data: data, perspective: perspective}
end
end
56 changes: 28 additions & 28 deletions lib/opsmaru/content/page/manager.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,45 @@ defmodule Opsmaru.Content.Page.Manager do
use Nebulex.Caching
import Opsmaru.Sanity

alias Opsmaru.Sanity.Response

alias Opsmaru.Cache
alias Opsmaru.Content.Page

@ttl :timer.hours(1)

@decorate cacheable(cache: Cache, key: {:pages, slug}, opts: [ttl: @ttl])
def show(slug) do
~S"""
*[_type == "page" && slug.current == $slug][0]{
...,
"sections": *[ _type == "pageSection" && references(^._id) ]{
@spec show(String.t(), Keyword.t()) :: %{data: %Page{}, perspective: String.t()}
@decorate cacheable(cache: Cache, match: &sanity_cache?/1, opts: [ttl: @ttl])
def show(slug, options \\ []) do
perspective = Keyword.get(options, :perspective, "published")

page =
~S"""
*[_type == "page" && slug.current == $slug][0]{
...,
"contents": *[ _type == "pageContent" && references(^._id) ]{
...,
"markdown": markdown.asset -> url
},
"cards": *[ _type == "pageCard" && references(^._id) ]{
"sections": *[ _type == "pageSection" && references(^._id) ]{
...,
card -> {..., "cover": {"url": cover.asset -> url, "alt": cover.alt}}
"contents": *[ _type == "pageContent" && references(^._id) ]{
...,
"markdown": markdown.asset -> url
},
"cards": *[ _type == "pageCard" && references(^._id) ]{
...,
card -> {..., "cover": {"url": cover.asset -> url, "alt": cover.alt}}
}
}
}
}
"""
|> Sanity.query(%{"slug" => slug}, perspective: "published")
|> Sanity.request!(sanity_request_opts())
|> Sanity.result!()
|> case do
nil ->
nil

page ->
page = Page.parse(page)
"""
|> Sanity.query(%{"slug" => slug}, perspective: perspective)
|> Sanity.request!(sanity_request_opts())
|> Sanity.result!()
|> Page.parse()

sections =
page.sections
|> Enum.map(&process_section/1)
sections =
page.sections
|> Enum.map(&process_section/1)

%{page | sections: sections}
end
%Response{data: %{page | sections: sections}, perspective: perspective}
end

defp process_section(section) do
Expand Down
Loading

0 comments on commit 06f2429

Please sign in to comment.