update
This commit is contained in:
parent
e2967f68b2
commit
ef2a6c41b4
39 changed files with 2349 additions and 30 deletions
132
lib/zoeyscomputer/api_keys.ex
Normal file
132
lib/zoeyscomputer/api_keys.ex
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
defmodule Zoeyscomputer.ApiKeys do
|
||||
@moduledoc """
|
||||
The ApiKeys context.
|
||||
"""
|
||||
require Logger
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias Hex.API.User
|
||||
alias Zoeyscomputer.ApiKeys.ApiKey
|
||||
alias Zoeyscomputer.Repo
|
||||
alias Zoeyscomputer.Users.User
|
||||
|
||||
alias Zoeyscomputer.ApiKeys.ApiKey
|
||||
|
||||
@doc """
|
||||
Returns the list of api_keys.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_api_keys()
|
||||
[%ApiKey{}, ...]
|
||||
|
||||
"""
|
||||
def list_api_keys do
|
||||
Repo.all(ApiKey)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single api_key.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Api key does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_api_key!(123)
|
||||
%ApiKey{}
|
||||
|
||||
iex> get_api_key!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_api_key!(id), do: Repo.get!(ApiKey, id)
|
||||
|
||||
@doc """
|
||||
Gets an API key by its token
|
||||
"""
|
||||
def get_api_key_by_token(token) do
|
||||
Repo.get_by(ApiKey, token: token)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates an API key for a user.
|
||||
"""
|
||||
def create_api_key(user, attrs \\ %{}) do
|
||||
# Convert attrs to string keys and add user_id with string key
|
||||
attrs = Map.put(attrs, "user_id", user.id)
|
||||
|
||||
result =
|
||||
%ApiKey{}
|
||||
|> ApiKey.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
|
||||
result
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a api_key.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_api_key(api_key, %{field: new_value})
|
||||
{:ok, %ApiKey{}}
|
||||
|
||||
iex> update_api_key(api_key, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_api_key(%ApiKey{} = api_key, attrs) do
|
||||
api_key
|
||||
|> ApiKey.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
List all API Keys for a given user
|
||||
"""
|
||||
def list_api_keys(%User{} = user) do
|
||||
ApiKey
|
||||
|> where([a], a.user_id == ^user.id)
|
||||
|> Repo.all()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a api_key.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_api_key(api_key)
|
||||
{:ok, %ApiKey{}}
|
||||
|
||||
iex> delete_api_key(api_key)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_api_key(%ApiKey{} = api_key) do
|
||||
Repo.delete(api_key)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking api_key changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_api_key(api_key)
|
||||
%Ecto.Changeset{data: %ApiKey{}}
|
||||
|
||||
"""
|
||||
def change_api_key(%ApiKey{} = api_key, attrs \\ %{}) do
|
||||
ApiKey.changeset(api_key, attrs)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Validates an API key token and returns associated user.
|
||||
Returns nil if token is invalid
|
||||
"""
|
||||
def authenticate_api_key(token) when is_binary(token) do
|
||||
case get_api_key_by_token(token) do
|
||||
%ApiKey{} = key -> Repo.preload(key, :user).user
|
||||
nil -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
36
lib/zoeyscomputer/api_keys/api_key.ex
Normal file
36
lib/zoeyscomputer/api_keys/api_key.ex
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
defmodule Zoeyscomputer.ApiKeys.ApiKey do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
alias Zoeyscomputer.Users.User
|
||||
|
||||
schema "api_keys" do
|
||||
field :name, :string
|
||||
field :token, :string
|
||||
belongs_to :user, User
|
||||
|
||||
timestamps(type: :utc_datetime)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(api_key, attrs) do
|
||||
api_key
|
||||
# Make sure both fields are in cast
|
||||
|> cast(attrs, [:name, :user_id])
|
||||
|> validate_required([:name, :user_id])
|
||||
# This needs to happen before validation
|
||||
|> put_token()
|
||||
# Add token to required fields
|
||||
|> validate_required([:token])
|
||||
end
|
||||
|
||||
defp put_token(changeset) do
|
||||
case changeset do
|
||||
%Ecto.Changeset{valid?: true} ->
|
||||
token = :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false)
|
||||
put_change(changeset, :token, token)
|
||||
|
||||
_ ->
|
||||
changeset
|
||||
end
|
||||
end
|
||||
end
|
||||
125
lib/zoeyscomputer/images.ex
Normal file
125
lib/zoeyscomputer/images.ex
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
defmodule Zoeyscomputer.Images do
|
||||
@moduledoc """
|
||||
The Images context.
|
||||
"""
|
||||
|
||||
import Ecto.Query, warn: false
|
||||
alias Ecto.Repo
|
||||
alias Zoeyscomputer.Users.User
|
||||
alias Zoeyscomputer.Repo
|
||||
|
||||
alias Zoeyscomputer.Images.Image
|
||||
|
||||
@doc """
|
||||
Returns the list of images.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> list_images()
|
||||
[%Image{}, ...]
|
||||
|
||||
"""
|
||||
def list_images do
|
||||
Repo.all(Image)
|
||||
end
|
||||
|
||||
@doc """
|
||||
List Images uploaded by user
|
||||
"""
|
||||
|
||||
def list_images_by_user(%User{} = user) do
|
||||
Image
|
||||
|> where([a], a.user_id == ^user.id)
|
||||
|> Repo.all()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a single image.
|
||||
|
||||
Raises `Ecto.NoResultsError` if the Image does not exist.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> get_image!(123)
|
||||
%Image{}
|
||||
|
||||
iex> get_image!(456)
|
||||
** (Ecto.NoResultsError)
|
||||
|
||||
"""
|
||||
def get_image!(id), do: Repo.get!(Image, id)
|
||||
|
||||
def get_image_by!(file) do
|
||||
Image
|
||||
|> Repo.get_by!(file: file)
|
||||
|> Repo.preload(:user)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Creates a image.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> create_image(%{field: value})
|
||||
{:ok, %Image{}}
|
||||
|
||||
iex> create_image(%{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def create_image(attrs \\ %{}) do
|
||||
# attrs = Map.put(attrs, "user_id", user.id)
|
||||
# attrs = Map.put(attrs, "s3_key", s3_key)
|
||||
|
||||
%Image{}
|
||||
|> Image.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Updates a image.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> update_image(image, %{field: new_value})
|
||||
{:ok, %Image{}}
|
||||
|
||||
iex> update_image(image, %{field: bad_value})
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def update_image(%Image{} = image, attrs) do
|
||||
image
|
||||
|> Image.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Deletes a image.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> delete_image(image)
|
||||
{:ok, %Image{}}
|
||||
|
||||
iex> delete_image(image)
|
||||
{:error, %Ecto.Changeset{}}
|
||||
|
||||
"""
|
||||
def delete_image(%Image{} = image) do
|
||||
Repo.delete(image)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns an `%Ecto.Changeset{}` for tracking image changes.
|
||||
|
||||
## Examples
|
||||
|
||||
iex> change_image(image)
|
||||
%Ecto.Changeset{data: %Image{}}
|
||||
|
||||
"""
|
||||
def change_image(%Image{} = image, attrs \\ %{}) do
|
||||
Image.changeset(image, attrs)
|
||||
end
|
||||
end
|
||||
20
lib/zoeyscomputer/images/image.ex
Normal file
20
lib/zoeyscomputer/images/image.ex
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
defmodule Zoeyscomputer.Images.Image do
|
||||
use Ecto.Schema
|
||||
import Ecto.Changeset
|
||||
|
||||
@derive {Jason.Encoder, only: [:id, :file, :inserted_at, :updated_at]}
|
||||
|
||||
schema "images" do
|
||||
field(:file, :string)
|
||||
belongs_to :user, Zoeyscomputer.Users.User
|
||||
|
||||
timestamps(type: :utc_datetime)
|
||||
end
|
||||
|
||||
@doc false
|
||||
def changeset(image, attrs) do
|
||||
image
|
||||
|> cast(attrs, [:file, :user_id])
|
||||
|> validate_required([:file, :user_id])
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue