-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathproject_controller.ex
More file actions
94 lines (69 loc) · 2.48 KB
/
project_controller.ex
File metadata and controls
94 lines (69 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
defmodule LightningWeb.API.ProjectController do
@moduledoc """
API controller for project management.
Provides read access to projects for authenticated users and API tokens.
Users can list projects they have access to and retrieve individual project details.
## Query Parameters (index)
- `page` - Page number (default: 1)
- `page_size` - Number of items per page (default: 10)
## Examples
GET /api/projects?page=1&page_size=20
GET /api/projects/a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d
"""
use LightningWeb, :controller
alias Lightning.Policies.Permissions
alias Lightning.Policies.ProjectUsers
alias Lightning.Projects
alias LightningWeb.API.Helpers
action_fallback LightningWeb.FallbackController
@doc """
Lists all projects accessible to the authenticated user.
Returns a paginated list of projects that the current user or API token
has access to.
## Parameters
- `conn` - The Plug connection struct with the current resource assigned
- `params` - Map of query parameters for pagination
## Returns
- Renders JSON with paginated list of projects
## Examples
GET /api/projects
GET /api/projects?page=2&page_size=50
"""
@spec index(Plug.Conn.t(), map()) :: Plug.Conn.t()
def index(conn, params) do
pagination_attrs = Map.take(params, ["page_size", "page"])
page =
Projects.projects_for_user_query(conn.assigns.current_resource)
|> Lightning.Repo.paginate(pagination_attrs)
render(conn, "index.json", page: page, conn: conn)
end
@doc """
Retrieves a specific project by ID.
Returns detailed information about a single project if the authenticated
user has access to it.
## Parameters
- `conn` - The Plug connection struct with the current resource assigned
- `params` - Map containing:
- `id` - Project UUID (required)
## Returns
- `200 OK` with project JSON on success
- `404 Not Found` if project doesn't exist
- `403 Forbidden` if user lacks access to the project
## Examples
GET /api/projects/a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d
"""
@spec show(Plug.Conn.t(), map()) :: Plug.Conn.t()
def show(conn, %{"id" => id}) do
with :ok <- Helpers.validate_uuid(id),
project <- Projects.get_project(id),
:ok <-
ProjectUsers
|> Permissions.can(
:access_project,
conn.assigns.current_resource,
project
) do
render(conn, "show.json", project: project, conn: conn)
end
end
end