Skip to content

Commit f3329b0

Browse files
committed
add rest just for files
1 parent d1d0b9c commit f3329b0

File tree

9 files changed

+102
-38
lines changed

9 files changed

+102
-38
lines changed

lib/backend/files/file.ex

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
defmodule Backend.Files.File do
2+
@moduledoc """
3+
PostgreSQL schema: `File`
4+
> Managed by *Ecto ORM*
5+
"""
6+
use Ecto.Schema
7+
import Ecto.Changeset
8+
9+
@derive {Jason.Encoder, only: [:id, :name, :content]}
10+
schema "files" do
11+
field :name, :string
12+
field :content, :string
13+
end
14+
15+
def changeset(file, attrs) do
16+
file
17+
|> cast(attrs, [:name, :content], empty_values: [:content])
18+
|> validate_required([:name])
19+
end
20+
end

lib/backend/files/files.ex

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule Backend.Files do
2+
alias Backend.Repo
3+
alias Backend.Files.File
4+
5+
def get_file(id) do
6+
Repo.get(File, id)
7+
end
8+
9+
def create_file(attrs \\ %{}) do
10+
%File{}
11+
|> File.changeset(attrs)
12+
|> Repo.insert()
13+
end
14+
15+
def save_file(file) do
16+
file
17+
|> File.changeset(%{})
18+
|> Repo.update()
19+
end
20+
21+
def delete_file(file) do
22+
file
23+
|> Repo.delete()
24+
end
25+
26+
end

lib/backend/schemas/file.ex

-12
This file was deleted.

lib/backend/schemas/user.ex

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
defmodule BackendWeb.FileController do
2+
alias Backend.Files
3+
use BackendWeb, :controller
4+
5+
def get_file(conn, %{"id" => id}) do
6+
file = Files.get_file(id)
7+
8+
case file do
9+
nil -> conn
10+
|> put_status(404)
11+
|> json(%{"message" => "File with id " <> id <> " does not exist"})
12+
_ -> conn
13+
|> put_status(200)
14+
|> json(file)
15+
end
16+
end
17+
18+
def create_file(conn, %{"name" => name}) do
19+
case Files.create_file(%{"name" => name, "content" => ""}) do
20+
{:ok, file} -> conn
21+
|> put_status(201)
22+
|> json(file)
23+
{:error, _} -> conn
24+
|> put_status(500)
25+
|> json(%{"message" => "Error occured"})
26+
end
27+
end
28+
29+
def delete_file(conn, %{"id" => id}) do
30+
file = Files.get_file(id)
31+
32+
if file == nil do
33+
conn
34+
|> put_status(404)
35+
|> json(%{"message" => "File with id " <> id <> " not found"})
36+
else
37+
case Files.delete_file(file) do
38+
{:ok, _} -> conn
39+
|> put_status(200)
40+
|> json(%{"message" => "File with id " <> id <> " has been deleted successfully"})
41+
{:error, _} -> conn
42+
|> put_status(500)
43+
|> json(%{"message" => "Error occured"})
44+
end
45+
end
46+
end
47+
48+
end

lib/backend_web/router.ex

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule BackendWeb.Router do
66
plug :fetch_session
77
plug :fetch_live_flash
88
plug :put_root_layout, html: {BackendWeb.Layouts, :root}
9-
plug :protect_from_forgery
9+
# plug :protect_from_forgery
1010
plug :put_secure_browser_headers
1111
end
1212

@@ -18,6 +18,10 @@ defmodule BackendWeb.Router do
1818
pipe_through :browser
1919

2020
get "/", PageController, :home
21+
22+
get "/file/:id", FileController, :get_file
23+
post "/file", FileController, :create_file
24+
delete "/file/:id", FileController, :delete_file
2125
end
2226

2327
# Other scopes may use custom stacks.

mix.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"finch": {:hex, :finch, "0.19.0", "c644641491ea854fc5c1bbaef36bfc764e3f08e7185e1f084e35e0672241b76d", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.6.2 or ~> 1.7", [hex: :mint, repo: "hexpm", optional: false]}, {:nimble_options, "~> 0.4 or ~> 1.0", [hex: :nimble_options, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 1.1", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "fc5324ce209125d1e2fa0fcd2634601c52a787aff1cd33ee833664a5af4ea2b6"},
2020
"floki": {:hex, :floki, "0.36.3", "1102f93b16a55bc5383b85ae3ec470f82dee056eaeff9195e8afdf0ef2a43c30", [:mix], [], "hexpm", "fe0158bff509e407735f6d40b3ee0d7deb47f3f3ee7c6c182ad28599f9f6b27a"},
2121
"gettext": {:hex, :gettext, "0.26.2", "5978aa7b21fada6deabf1f6341ddba50bc69c999e812211903b169799208f2a8", [:mix], [{:expo, "~> 0.5.1 or ~> 1.0", [hex: :expo, repo: "hexpm", optional: false]}], "hexpm", "aa978504bcf76511efdc22d580ba08e2279caab1066b76bb9aa81c4a1e0a32a5"},
22-
"heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "88ab3a0d790e6a47404cba02800a6b25d2afae50", [tag: "v2.1.1", sparse: "optimized"]},
22+
"heroicons": {:git, "https://github.com/tailwindlabs/heroicons.git", "88ab3a0d790e6a47404cba02800a6b25d2afae50", [tag: "v2.1.1", sparse: "optimized", depth: 1]},
2323
"hpax": {:hex, :hpax, "1.0.0", "28dcf54509fe2152a3d040e4e3df5b265dcb6cb532029ecbacf4ce52caea3fd2", [:mix], [], "hexpm", "7f1314731d711e2ca5fdc7fd361296593fc2542570b3105595bb0bc6d0fad601"},
2424
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
2525
"libgraph": {:hex, :libgraph, "0.16.0", "3936f3eca6ef826e08880230f806bfea13193e49bf153f93edcf0239d4fd1d07", [:mix], [], "hexpm", "41ca92240e8a4138c30a7e06466acc709b0cbb795c643e9e17174a178982d6bf"},

priv/repo/migrations/20241129212331_add_user.exs

-10
This file was deleted.

priv/repo/migrations/20241129212656_add_file.exs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ defmodule Backend.Repo.Migrations.AddFile do
33

44
def change do
55
create table(:files) do
6-
add :filename, :string
7-
add :created_at, :date
6+
add :name, :string, null: false
7+
add :content, :string
88
end
99
end
1010
end

0 commit comments

Comments
 (0)