Skip to content

Commit

Permalink
Update App timestamp every new rating
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkonidas committed Sep 16, 2024
1 parent 6b251b5 commit 3b5ee9e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
11 changes: 10 additions & 1 deletion lib/plexus/apps.ex
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ defmodule Plexus.Apps do
|> Repo.one!()
end

@spec fetch_app(String.t()) :: {:ok, App.t()} | {:error, :not_found}
def fetch_app(package) do
case Repo.get(App, package) do
%App{} = app -> {:ok, app}
nil -> {:error, :not_found}
end
end

@spec create_app(%{
optional(:icon_url) => String.t(),
package: String.t(),
Expand All @@ -63,8 +71,9 @@ defmodule Plexus.Apps do
end

@spec update_app(App.t(), %{
optional(:updated_at) => DateTime.t(),
optional(:icon_url) => String.t(),
name: String.t()
optional(:name) => String.t()
}) :: {:ok, App.t()} | {:error, Ecto.Changeset.t()}
def update_app(%App{} = app, params) do
app
Expand Down
14 changes: 9 additions & 5 deletions lib/plexus/ratings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Plexus.Ratings do
"""
import Ecto.Query

alias Plexus.Apps
alias Plexus.PaginationHelpers
alias Plexus.QueryHelpers
alias Plexus.Repo
Expand Down Expand Up @@ -55,11 +56,14 @@ defmodule Plexus.Ratings do
installation_source: String.t(),
rating_type: atom(),
score: pos_integer()
}) :: {:ok, Rating.t()} | {:error, Ecto.Changeset.t()}
def create_rating(params) do
%Rating{}
|> Rating.changeset(params)
|> Repo.insert()
}) :: {:ok, Rating.t()} | {:error, :not_found} | {:error, Ecto.Changeset.t()}
def create_rating(%{app_package: app_package} = params) do
Repo.transact(fn ->
with {:ok, app} <- Apps.fetch_app(app_package),
{:ok, _app} <- Apps.update_app(app, %{updated_at: DateTime.utc_now()}) do
Repo.insert(Rating.changeset(%Rating{}, params))
end
end)
|> broadcast(:app_rating_updated)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/plexus/schemas/app.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ defmodule Plexus.Schemas.App do
@spec changeset(App.t(), map()) :: Ecto.Changeset.t()
def changeset(%App{} = app, params) do
app
|> cast(params, [:package, :name, :icon_url])
|> cast(params, [:package, :name, :icon_url, :updated_at])
|> validate_required([:package, :name])
|> unique_constraint(:package, name: :apps_pkey)
end
Expand Down
4 changes: 2 additions & 2 deletions test/plexus/ratings_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Plexus.RatingsTest do
alias Plexus.Schemas.Rating

@invalid_attrs %{
app_package: nil,
app_package: "",
app_build_number: nil,
app_version: nil,
rating_type: nil,
Expand Down Expand Up @@ -65,7 +65,7 @@ defmodule Plexus.RatingsTest do
end

test "invalid data returns error changeset" do
assert {:error, %Ecto.Changeset{}} = Ratings.create_rating(@invalid_attrs)
assert {:error, _reason} = Ratings.create_rating(@invalid_attrs)
end
end
end

0 comments on commit 3b5ee9e

Please sign in to comment.