Skip to content

Commit 6449fb2

Browse files
committed
Update App timestamp every new rating
1 parent 6b251b5 commit 6449fb2

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

lib/plexus/apps.ex

+8
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,14 @@ defmodule Plexus.Apps do
5050
|> Repo.one!()
5151
end
5252

53+
@spec fetch_app(String.t()) :: {:ok, App.t()} | {:error, :not_found}
54+
def fetch_app(package) do
55+
case Repo.get(App, package) do
56+
%App{} = app -> {:ok, app}
57+
nil -> {:error, :not_found}
58+
end
59+
end
60+
5361
@spec create_app(%{
5462
optional(:icon_url) => String.t(),
5563
package: String.t(),

lib/plexus/ratings.ex

+8-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ defmodule Plexus.Ratings do
44
"""
55
import Ecto.Query
66

7+
alias Plexus.Apps
78
alias Plexus.PaginationHelpers
89
alias Plexus.QueryHelpers
910
alias Plexus.Repo
@@ -56,10 +57,13 @@ defmodule Plexus.Ratings do
5657
rating_type: atom(),
5758
score: pos_integer()
5859
}) :: {:ok, Rating.t()} | {:error, Ecto.Changeset.t()}
59-
def create_rating(params) do
60-
%Rating{}
61-
|> Rating.changeset(params)
62-
|> Repo.insert()
60+
def create_rating(%{app_package: app_package} = params) do
61+
Repo.transact(fn ->
62+
with {:ok, app} <- Apps.fetch_app(app_package),
63+
{:ok, _app} <- Apps.update_app(app, %{updated_at: DateTime.utc_now()}) do
64+
Repo.insert(Rating.changeset(%Rating{}, params))
65+
end
66+
end)
6367
|> broadcast(:app_rating_updated)
6468
end
6569

lib/plexus/schemas/app.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ defmodule Plexus.Schemas.App do
2020
@spec changeset(App.t(), map()) :: Ecto.Changeset.t()
2121
def changeset(%App{} = app, params) do
2222
app
23-
|> cast(params, [:package, :name, :icon_url])
23+
|> cast(params, [:package, :name, :icon_url, :updated_at])
2424
|> validate_required([:package, :name])
2525
|> unique_constraint(:package, name: :apps_pkey)
2626
end

test/plexus/ratings_test.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule Plexus.RatingsTest do
88
alias Plexus.Schemas.Rating
99

1010
@invalid_attrs %{
11-
app_package: nil,
11+
app_package: "",
1212
app_build_number: nil,
1313
app_version: nil,
1414
rating_type: nil,
@@ -65,7 +65,7 @@ defmodule Plexus.RatingsTest do
6565
end
6666

6767
test "invalid data returns error changeset" do
68-
assert {:error, %Ecto.Changeset{}} = Ratings.create_rating(@invalid_attrs)
68+
assert {:error, _reason} = Ratings.create_rating(@invalid_attrs)
6969
end
7070
end
7171
end

0 commit comments

Comments
 (0)