<commit_before>defmodule CoursePlanner.Students do
  alias CoursePlanner.Repo
  alias CoursePlanner.User
  import Ecto.Query

  @students from u in User, where: u.role == "Student" and is_nil(u.deleted_at)

  def all() do
    Repo.all(@students)
  end

  def update(id, params) do
    case Repo.get(User, id) do
      student ->
        student
        |> User.changeset(params, :update)
        |> Repo.update
        |> format_error(student)
      nil -> {:error, :not_found}
    end
  end

  defp format_error({:ok, student}, _), do: {:ok, student}
  defp format_error({:error, changeset}, student), do: {:error, student, changeset}

end<commit_msg>Set timestamp when changing status.<commit_after>defmodule CoursePlanner.Students do
  alias CoursePlanner.Repo
  alias CoursePlanner.User
  import Ecto.Query
  alias Ecto.{Changeset, DateTime}

  @students from u in User, where: u.role == "Student" and is_nil(u.deleted_at)

  def all() do
    Repo.all(@students)
  end

  def update(id, params) do
    case Repo.get(User, id) do
      student ->
        student
        |> User.changeset(params, :update)
        |> IO.inspect
        |> add_timestamps()
        |> Repo.update
        |> format_error(student)
      nil -> {:error, :not_found}
    end
  end

  defp add_timestamps(%{changes: %{status: "Graduated"}} = changeset) do
    Changeset.put_change(changeset, :graduated_at, DateTime.utc())
  end

  defp add_timestamps(%{changes: %{status: "Active"}} = changeset) do
    Changeset.put_change(changeset, :activated_at, DateTime.utc())
  end

  defp add_timestamps(%{changes: %{status: "Frozen"}} = changeset) do
    Changeset.put_change(changeset, :froze_at, DateTime.utc())
  end

  defp add_timestamps(changeset), do: changeset

  defp format_error({:ok, student}, _), do: {:ok, student}
  defp format_error({:error, changeset}, student), do: {:error, student, changeset}

end
{'fraction_non_alphanumeric': 0.12903225806451613, 'fraction_numerical': 0.0, 'mean_word_length': 3.5047619047619047, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '80603', 'n_tokens_mistral': 673, 'n_tokens_neox': 643, 'n_words': 158}