2018-11-06 09:30:27 +00:00
|
|
|
defmodule MobilizonWeb.Resolvers.Category do
|
2019-01-03 13:59:59 +00:00
|
|
|
@moduledoc """
|
|
|
|
Handles the category-related GraphQL calls
|
|
|
|
"""
|
2018-11-06 09:30:27 +00:00
|
|
|
require Logger
|
2018-11-12 22:30:47 +00:00
|
|
|
alias Mobilizon.Actors.User
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2018-12-14 16:41:55 +00:00
|
|
|
###
|
|
|
|
# TODO : Refactor this into MobilizonWeb.API.Categories when a standard AS category is defined
|
|
|
|
###
|
2018-12-14 10:23:36 +00:00
|
|
|
def list_categories(_parent, %{page: page, limit: limit}, _resolution) do
|
2018-11-06 09:30:27 +00:00
|
|
|
categories =
|
2018-12-14 10:23:36 +00:00
|
|
|
Mobilizon.Events.list_categories(page, limit)
|
2018-11-06 09:30:27 +00: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 22:30:47 +00:00
|
|
|
context: %{current_user: %User{} = _user}
|
2018-11-06 09:30:27 +00: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 07:34:25 +00:00
|
|
|
|
2018-11-12 22:30:47 +00:00
|
|
|
def create_category(_parent, _args, %{}) do
|
2018-11-07 07:34:25 +00:00
|
|
|
{:error, "You are not allowed to create a category if not connected"}
|
|
|
|
end
|
2018-11-06 09:30:27 +00:00
|
|
|
end
|