2018-11-06 10:30:27 +01:00
|
|
|
defmodule MobilizonWeb.Resolvers.Category do
|
|
|
|
require Logger
|
2018-11-12 23:30:47 +01:00
|
|
|
alias Mobilizon.Actors.User
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2018-12-14 17:41:55 +01:00
|
|
|
###
|
|
|
|
# TODO : Refactor this into MobilizonWeb.API.Categories when a standard AS category is defined
|
|
|
|
###
|
2018-12-14 11:23:36 +01:00
|
|
|
def list_categories(_parent, %{page: page, limit: limit}, _resolution) do
|
2018-11-06 10:30:27 +01:00
|
|
|
categories =
|
2018-12-14 11:23:36 +01:00
|
|
|
Mobilizon.Events.list_categories(page, limit)
|
2018-11-06 10:30:27 +01:00
|
|
|
|> Enum.map(fn category ->
|
|
|
|
urls = MobilizonWeb.Uploaders.Category.urls({category.picture, category})
|
|
|
|
Map.put(category, :picture, %{url: urls.original, url_thumbnail: urls.thumb})
|
|
|
|
end)
|
|
|
|
|
|
|
|
{:ok, categories}
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_category(_parent, %{title: title, picture: picture, description: description}, %{
|
2018-11-12 23:30:47 +01:00
|
|
|
context: %{current_user: %User{} = _user}
|
2018-11-06 10:30:27 +01:00
|
|
|
}) do
|
|
|
|
with {:ok, category} <-
|
|
|
|
Mobilizon.Events.create_category(%{
|
|
|
|
title: title,
|
|
|
|
description: description,
|
|
|
|
picture: picture
|
|
|
|
}),
|
|
|
|
urls <- MobilizonWeb.Uploaders.Category.urls({category.picture, category}) do
|
|
|
|
Logger.info("Created category " <> title)
|
|
|
|
{:ok, Map.put(category, :picture, %{url: urls.original, url_thumbnail: urls.thumb})}
|
|
|
|
else
|
|
|
|
{:error, %Ecto.Changeset{errors: errors} = _changeset} ->
|
|
|
|
# This is pretty ridiculous for changeset to error
|
|
|
|
errors =
|
|
|
|
Enum.into(errors, %{})
|
|
|
|
|> Enum.map(fn {key, {value, _}} -> Atom.to_string(key) <> ": " <> value end)
|
|
|
|
|
|
|
|
{:error, errors}
|
|
|
|
end
|
|
|
|
end
|
2018-11-07 08:34:25 +01:00
|
|
|
|
2018-11-12 23:30:47 +01:00
|
|
|
def create_category(_parent, _args, %{}) do
|
2018-11-07 08:34:25 +01:00
|
|
|
{:error, "You are not allowed to create a category if not connected"}
|
|
|
|
end
|
2018-11-06 10:30:27 +01:00
|
|
|
end
|