Skip to content

Commit f042f41

Browse files
committed
iterate
1 parent 3592aec commit f042f41

File tree

3 files changed

+55
-10
lines changed

3 files changed

+55
-10
lines changed

apps/cf_graphql/lib/resolvers/videos.ex

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ defmodule CF.Graphql.Resolvers.Videos do
3737
end
3838
end
3939

40+
def search(_root, %{url: url}, _info) do
41+
case CF.Videos.get_video_by_url(url) do
42+
nil -> {:ok, nil}
43+
video -> {:ok, video}
44+
end
45+
end
46+
4047
# Deprecated: Use paginated_list/3 instead
4148
# Keeping for backward compatibility with deprecated all_videos field
4249
def list(_root, args, _info) do
@@ -102,6 +109,27 @@ defmodule CF.Graphql.Resolvers.Videos do
102109
|> Enum.group_by(& &1.video_id)
103110
end
104111

112+
def create(_root, %{url: url, unlisted: unlisted}, %{
113+
context: %{user: user}
114+
}) do
115+
case CF.Videos.get_video_by_url(url) do
116+
nil ->
117+
case CF.Videos.create!(user, url, unlisted: unlisted) do
118+
{:ok, video} ->
119+
{:ok, video}
120+
121+
{:error, error} when is_binary(error) ->
122+
{:error, error}
123+
124+
{:error, _reason} ->
125+
{:error, "Failed to create video"}
126+
end
127+
128+
existing_video ->
129+
{:ok, existing_video}
130+
end
131+
end
132+
105133
def start_automatic_statements_extraction(_root, %{video_id: video_id}, %{
106134
context: %{user: user}
107135
}) do

apps/cf_graphql/lib/schema/schema.ex

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ defmodule CF.Graphql.Schema do
123123
arg(:statement_id, non_null(:id))
124124
resolve(&Resolvers.History.statement_history_actions/3)
125125
end
126+
127+
@desc "Search for a video by URL. Returns the video if it exists, null otherwise."
128+
field :search_video, :video do
129+
arg(:url, non_null(:string))
130+
resolve(&Resolvers.Videos.search/3)
131+
end
126132
end
127133

128134
# Mutation API
@@ -160,6 +166,17 @@ defmodule CF.Graphql.Schema do
160166
resolve(&Resolvers.Videos.start_automatic_statements_extraction/3)
161167
end
162168

169+
@desc "Create a new video. If it already exists, returns the existing video."
170+
field :create_video, :video do
171+
middleware(Middleware.RequireAuthentication)
172+
middleware(Middleware.RequireReputation, 75)
173+
174+
arg(:url, non_null(:string))
175+
arg(:unlisted, non_null(:boolean))
176+
177+
resolve(&Resolvers.Videos.create/3)
178+
end
179+
163180
field :edit_video, :video do
164181
middleware(Middleware.RequireAuthentication)
165182
# MIN_REPUTATION_UPDATE_VIDEO

apps/cf_rest_api/lib/router.ex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ defmodule CF.RestApi.Router do
2323

2424
# ---- Public endpoints ----
2525
get("/", ApiInfoController, :get)
26-
get("/videos", VideoController, :index)
27-
get("/speakers/:slug_or_id", SpeakerController, :show)
28-
post("/search/video", VideoController, :search)
29-
get("/videos/:video_id/statements", StatementController, :get)
26+
# get("/videos", VideoController, :index)
27+
# get("/speakers/:slug_or_id", SpeakerController, :show)
28+
# post("/search/video", VideoController, :search)
29+
# get("/videos/:video_id/statements", StatementController, :get)
3030
get("/newsletter/unsubscribe/:token", UserController, :newsletter_unsubscribe)
3131

3232
# ---- Authenticathed endpoints ----
@@ -43,7 +43,7 @@ defmodule CF.RestApi.Router do
4343
# Users
4444
scope "/users" do
4545
post("/", UserController, :create)
46-
post("/request_invitation", UserController, :request_invitation)
46+
# post("/request_invitation", UserController, :request_invitation)
4747
get("/username/:username", UserController, :show)
4848

4949
scope "/reset_password" do
@@ -65,12 +65,12 @@ defmodule CF.RestApi.Router do
6565
end
6666
end
6767

68-
# Videos
69-
post("/videos", VideoController, :get_or_create)
68+
# # Videos
69+
# post("/videos", VideoController, :get_or_create)
7070

71-
# Moderation
72-
get("/moderation/random", ModerationController, :random)
73-
post("/moderation/feedback", ModerationController, :post_feedback)
71+
# # Moderation
72+
# get("/moderation/random", ModerationController, :random)
73+
# post("/moderation/feedback", ModerationController, :post_feedback)
7474
end
7575
end
7676

0 commit comments

Comments
 (0)