From 57e8c4d1ea97ada38d0f52e90b138a1f17cfcebc Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Wed, 23 Aug 2023 15:57:52 +0200
Subject: [PATCH] server api

---
 .../api/osuny/communication/websites_controller.rb  |  6 ------
 .../api/osuny/communication_controller.rb           |  1 -
 .../api/osuny/server/websites_controller.rb         | 13 +++++++++++++
 app/controllers/api/osuny/server_controller.rb      |  5 +++++
 app/controllers/api/osuny_controller.rb             |  1 -
 app/views/api/dashboard/index.html.erb              |  2 +-
 app/views/api/osuny/index.json.jbuilder             |  3 +++
 app/views/api/osuny/server/index.json.jbuilder      |  3 +++
 .../api/osuny/server/websites/index.json.jbuilder   |  4 ++++
 .../websites/theme_released.json.jbuilder           |  0
 config/routes/api.rb                                |  4 ++++
 11 files changed, 33 insertions(+), 9 deletions(-)
 create mode 100644 app/controllers/api/osuny/server/websites_controller.rb
 create mode 100644 app/controllers/api/osuny/server_controller.rb
 create mode 100644 app/views/api/osuny/server/index.json.jbuilder
 create mode 100644 app/views/api/osuny/server/websites/index.json.jbuilder
 rename app/views/api/osuny/{communication => server}/websites/theme_released.json.jbuilder (100%)

diff --git a/app/controllers/api/osuny/communication/websites_controller.rb b/app/controllers/api/osuny/communication/websites_controller.rb
index a23cb12bb..a6d4a4809 100644
--- a/app/controllers/api/osuny/communication/websites_controller.rb
+++ b/app/controllers/api/osuny/communication/websites_controller.rb
@@ -4,10 +4,4 @@ class Api::Osuny::Communication::WebsitesController < Api::ApplicationController
     @websites = current_university.communication_websites.in_production
   end
 
-  def theme_released
-    render_forbidden unless params[:secret_key].present? && params[:secret_key] == ENV['OSUNY_API_AUTOUPDATE_THEME_KEY']
-    Communication::Website.with_automatic_update.find_each do |website|
-      website.update_theme_version
-    end
-  end
 end
diff --git a/app/controllers/api/osuny/communication_controller.rb b/app/controllers/api/osuny/communication_controller.rb
index 9be1e00a7..597ec060f 100644
--- a/app/controllers/api/osuny/communication_controller.rb
+++ b/app/controllers/api/osuny/communication_controller.rb
@@ -1,5 +1,4 @@
 class Api::Osuny::CommunicationController < Api::ApplicationController
   def index
-    @websites = current_university.communication_websites.in_production
   end
 end
diff --git a/app/controllers/api/osuny/server/websites_controller.rb b/app/controllers/api/osuny/server/websites_controller.rb
new file mode 100644
index 000000000..bc5c1f1f8
--- /dev/null
+++ b/app/controllers/api/osuny/server/websites_controller.rb
@@ -0,0 +1,13 @@
+class Api::Osuny::Server::WebsitesController < Api::ApplicationController
+
+  def index
+    @websites = Communication::Website.in_production
+  end
+
+  def theme_released
+    render_forbidden unless params[:secret_key].present? && params[:secret_key] == ENV['OSUNY_API_AUTOUPDATE_THEME_KEY']
+    Communication::Website.with_automatic_update.find_each do |website|
+      website.update_theme_version
+    end
+  end
+end
diff --git a/app/controllers/api/osuny/server_controller.rb b/app/controllers/api/osuny/server_controller.rb
new file mode 100644
index 000000000..914600ac9
--- /dev/null
+++ b/app/controllers/api/osuny/server_controller.rb
@@ -0,0 +1,5 @@
+class Api::Osuny::ServerController < Api::ApplicationController
+  def index
+    @websites = current_university.communication_websites.in_production
+  end
+end
diff --git a/app/controllers/api/osuny_controller.rb b/app/controllers/api/osuny_controller.rb
index 0412db3f9..242733459 100644
--- a/app/controllers/api/osuny_controller.rb
+++ b/app/controllers/api/osuny_controller.rb
@@ -1,5 +1,4 @@
 class Api::OsunyController < Api::ApplicationController
   def index
-    @websites = current_university.communication_websites.in_production
   end
 end
diff --git a/app/views/api/dashboard/index.html.erb b/app/views/api/dashboard/index.html.erb
index 7d58b0d40..52a32b899 100644
--- a/app/views/api/dashboard/index.html.erb
+++ b/app/views/api/dashboard/index.html.erb
@@ -7,7 +7,7 @@
       L'API Osuny permet d'interagir avec une instance.
       L'implémentation est très rudimentaire, n'hésitez pas à ouvrir <a href="https://github.com/noesya/osuny/issues" target="_blank" rel="noreferrer">une issue sur Github</a> pour signaler un manque.
     </p>
-    <%= link_to 'API', api_osuny_root_path, class: 'btn btn-primary' %>
+    <%= link_to 'API', api_osuny_path, class: 'btn btn-primary' %>
   </div>
   <div class="col-lg-6 mb-5">
     <h2>LHÉO</h2>
diff --git a/app/views/api/osuny/index.json.jbuilder b/app/views/api/osuny/index.json.jbuilder
index 51849c5d0..933e295d1 100644
--- a/app/views/api/osuny/index.json.jbuilder
+++ b/app/views/api/osuny/index.json.jbuilder
@@ -1,3 +1,6 @@
 json.communication do 
   json.url api_osuny_communication_path
 end
+json.server do 
+  json.url api_osuny_server_path
+end
diff --git a/app/views/api/osuny/server/index.json.jbuilder b/app/views/api/osuny/server/index.json.jbuilder
new file mode 100644
index 000000000..e14674d74
--- /dev/null
+++ b/app/views/api/osuny/server/index.json.jbuilder
@@ -0,0 +1,3 @@
+json.websites do
+  json.url api_osuny_server_websites_path
+end
diff --git a/app/views/api/osuny/server/websites/index.json.jbuilder b/app/views/api/osuny/server/websites/index.json.jbuilder
new file mode 100644
index 000000000..1cec4fd71
--- /dev/null
+++ b/app/views/api/osuny/server/websites/index.json.jbuilder
@@ -0,0 +1,4 @@
+json.array! @websites.each do |website|
+  json.name website.name
+  json.url website.url
+end
diff --git a/app/views/api/osuny/communication/websites/theme_released.json.jbuilder b/app/views/api/osuny/server/websites/theme_released.json.jbuilder
similarity index 100%
rename from app/views/api/osuny/communication/websites/theme_released.json.jbuilder
rename to app/views/api/osuny/server/websites/theme_released.json.jbuilder
diff --git a/config/routes/api.rb b/config/routes/api.rb
index 0429915c6..45b8a1269 100644
--- a/config/routes/api.rb
+++ b/config/routes/api.rb
@@ -5,6 +5,10 @@ namespace :api do
     get 'communication' => 'communication#index'
     namespace :communication do
       get 'websites' => 'websites#index'
+    end
+    get 'server' => 'server#index'
+    namespace :server do
+      get 'websites' => 'websites#index'
       post 'websites/theme-released' => 'websites#theme_released'
     end
   end
-- 
GitLab