Initial commit

This commit is contained in:
2020-12-12 18:38:31 +01:00
commit f877b78f33
117 changed files with 23104 additions and 0 deletions

View File

@@ -0,0 +1,99 @@
defmodule ConfientWeb.StudentController do
use ConfientWeb, :controller
alias Confient.School
alias Confient.School.Student
def index(conn, _params) do
students = School.list_students()
render(conn, "index.html", students: students)
end
def new(conn, _params) do
changeset = School.change_student(%Student{})
classes = School.list_classes() |> Enum.map(&{&1.name, &1.id})
render(conn, "new.html",
changeset: changeset,
classes: classes
)
end
def bulkform(conn, _params) do
classes = School.list_classes() |> Enum.map(&{&1.name, &1.id})
render(conn, "bulkform.html",
classes: classes,
token: get_csrf_token()
)
end
def create(conn, %{"student" => student_params}) do
case School.create_student(student_params) do
{:ok, student} ->
conn
|> put_flash(:info, "Elève créé avec succès.")
|> redirect(to: Routes.student_path(conn, :show, student))
{:error, %Ecto.Changeset{} = changeset} ->
classes = Confient.School.list_classes() |> Enum.map(&{&1.name, &1.id})
render(conn, "new.html", changeset: changeset, classes: classes)
end
end
def bulkcreate(conn, %{"file" => %{"list" => ""}}) do
conn
|> put_flash(:error, "Aucun fichier CSV.")
|> redirect(to: Routes.student_path(conn, :bulkform))
end
def bulkcreate(conn, %{"file" => %{"list" => file}, "class" => %{"class_id" => class_id}}) do
class = School.get_class!(class_id)
{:ok, students} = CSV.parse(file.path)
for {lastname, firstname} <- students do
unless School.student_exists?(lastname, firstname, class) do
School.create_student(class, %{"lastname" => lastname, "firstname" => firstname})
end
end
conn
|> put_flash(:info, "Elèves créés avec succès.")
|> redirect(to: Routes.student_path(conn, :bulkform))
end
def show(conn, %{"id" => id}) do
student = School.get_full_student!(id)
render(conn, "show.html", student: student)
end
def edit(conn, %{"id" => id}) do
student = School.get_student!(id)
changeset = School.change_student(student)
classes = School.list_classes() |> Enum.map(&{&1.name, &1.id})
render(conn, "edit.html", student: student, changeset: changeset, classes: classes)
end
def update(conn, %{"id" => id, "student" => student_params}) do
student = School.get_student!(id)
case School.update_student(student, student_params) do
{:ok, student} ->
conn
|> put_flash(:info, "Elève mis à jour avec succès.")
|> redirect(to: Routes.student_path(conn, :show, student))
{:error, %Ecto.Changeset{} = changeset} ->
render(conn, "edit.html", student: student, changeset: changeset)
end
end
def delete(conn, %{"id" => id}) do
student = School.get_student!(id)
{:ok, _student} = School.delete_student(student)
conn
|> put_flash(:info, "Elève supprimé.")
|> redirect(to: Routes.student_path(conn, :index))
end
end