From af16aedca1143e58bf05ba2694e65e00c758c0f2 Mon Sep 17 00:00:00 2001
From: Arnaud Levy <contact@arnaudlevy.com>
Date: Sat, 19 Mar 2022 11:11:06 +0100
Subject: [PATCH] Wip #267

---
 app/controllers/api/application_controller.rb |  7 +++++++
 app/controllers/api/lheo_controller.rb        |  5 +++++
 app/views/api/application/index.html.erb      |  0
 app/views/api/lheo/index.xml.erb              | 10 ++++++++++
 config/initializers/mime_types.rb             |  1 +
 config/routes.rb                              |  1 +
 config/routes/api.rb                          |  4 ++++
 7 files changed, 28 insertions(+)
 create mode 100644 app/controllers/api/application_controller.rb
 create mode 100644 app/controllers/api/lheo_controller.rb
 create mode 100644 app/views/api/application/index.html.erb
 create mode 100644 app/views/api/lheo/index.xml.erb
 create mode 100644 config/routes/api.rb

diff --git a/app/controllers/api/application_controller.rb b/app/controllers/api/application_controller.rb
new file mode 100644
index 000000000..e2a371d1a
--- /dev/null
+++ b/app/controllers/api/application_controller.rb
@@ -0,0 +1,7 @@
+class Api::ApplicationController < ApplicationController
+  layout false
+  skip_before_action :authenticate_user!
+
+  def index
+  end
+end
diff --git a/app/controllers/api/lheo_controller.rb b/app/controllers/api/lheo_controller.rb
new file mode 100644
index 000000000..5e788e8f7
--- /dev/null
+++ b/app/controllers/api/lheo_controller.rb
@@ -0,0 +1,5 @@
+class Api::LheoController < Api::ApplicationController
+  def index
+    @programs = current_university.education_programs
+  end
+end
diff --git a/app/views/api/application/index.html.erb b/app/views/api/application/index.html.erb
new file mode 100644
index 000000000..e69de29bb
diff --git a/app/views/api/lheo/index.xml.erb b/app/views/api/lheo/index.xml.erb
new file mode 100644
index 000000000..2e4453477
--- /dev/null
+++ b/app/views/api/lheo/index.xml.erb
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="utf-8"?>
+<lheo xmlns="http://lheo.gouv.fr/2.3">
+  <offres>
+    <% @programs.each do |program| %>
+      <formation>
+        <intitule-formation><![CDATA[<%= program.name %>]]></intitule-formation>
+      </formation>
+    <% end %>
+  </offres>
+</lheo>
diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb
index dc1899682..1ce3a81f7 100644
--- a/config/initializers/mime_types.rb
+++ b/config/initializers/mime_types.rb
@@ -2,3 +2,4 @@
 
 # Add new mime types for use in respond_to blocks:
 # Mime::Type.register "text/richtext", :rtf
+Mime::Type.register "application/xml", :xml, %w( text/xml application/x-xml )
diff --git a/config/routes.rb b/config/routes.rb
index 7bf2ee0b0..c6a0a9514 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -30,6 +30,7 @@ Rails.application.routes.draw do
   end
 
   draw 'extranet'
+  draw 'api'
 
   get '/media/:signed_id/:filename_with_transformations' => 'media#show', as: :medium
 
diff --git a/config/routes/api.rb b/config/routes/api.rb
new file mode 100644
index 000000000..729a8fb93
--- /dev/null
+++ b/config/routes/api.rb
@@ -0,0 +1,4 @@
+namespace :api do
+  get 'lheo' => 'lheo#index', defaults: { format: :xml }
+  root to: 'application#index'
+end
-- 
GitLab