Skip to content

Commit

Permalink
Add get version controller action
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Jul 1, 2024
1 parent 21152f8 commit c86f9c9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
9 changes: 9 additions & 0 deletions lib/polar_web/controllers/publish/version_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ defmodule PolarWeb.Publish.VersionController do
alias Polar.Repo
alias Polar.Streams
alias Polar.Streams.Product
alias Polar.Streams.Version

action_fallback PolarWeb.FallbackController

def show(conn, %{"product_id" => product_id, "id" => serial}) do
version = Repo.get_by(Version, product_id: product_id, serial: serial)

if version do
render(conn, :show, %{version: version})
end
end

def create(conn, %{"product_id" => product_id, "version" => version_params}) do
product = Repo.get(Product, product_id)

Expand Down
4 changes: 4 additions & 0 deletions lib/polar_web/controllers/publish/version_json.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
defmodule PolarWeb.Publish.VersionJSON do
def show(%{version: version}) do
%{data: %{id: version.id}}
end

def create(%{version: version}) do
%{data: %{id: version.id}}
end
Expand Down
2 changes: 1 addition & 1 deletion lib/polar_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ defmodule PolarWeb.Router do
resources "/storage", StorageController, only: [:show], singleton: true

resources "/products", ProductController, only: [:show] do
resources "/versions", VersionController, only: [:create]
resources "/versions", VersionController, only: [:show, :create]
end

resources "/versions/:version_id/events", EventController, only: [:create]
Expand Down
23 changes: 23 additions & 0 deletions test/polar_web/controllers/publish/version_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule PolarWeb.Publish.VersionControllerTest do
alias Polar.Accounts
alias Polar.Streams

import Polar.StreamsFixtures

setup do
password = Accounts.generate_automation_password()

Expand All @@ -25,6 +27,27 @@ defmodule PolarWeb.Publish.VersionControllerTest do
{:ok, conn: conn}
end

describe "GET /publish/products/:product_id/versions/:id" do
setup do
product_attributes = valid_product_attributes("alpine:3.19:amd64:default")

{:ok, product} = Streams.create_product(product_attributes)

{:ok, version} =
Streams.create_version(product, valid_version_attributes(2))

{:ok, product: product, version: version}
end

test "can fetch existing version", %{conn: conn, product: product, version: version} do
conn = get(conn, "/publish/products/#{product.id}/versions/#{version.serial}")

assert %{"data" => data} = json_response(conn, 200)

assert %{"id" => _id} = data
end
end

describe "POST /publish/products/:product_id/versions" do
setup do
product_attributes = valid_product_attributes("alpine:3.19:amd64:default")
Expand Down

0 comments on commit c86f9c9

Please sign in to comment.