defmodule Bump.Messages do alias Bump.Repo alias Bump.Messages.Message import Ecto.Query def get_newest_message(sender) do query = from m in "messages", where: m.sender == ^sender, order_by: [desc: m.timestamp], limit: 1, select: m.data Repo.one(query) end def get_all_messages_since(sender, minutes) do ago = DateTime.utc_now |> Timex.shift(minutes: -minutes) |> DateTime.truncate(:second) query = from m in "messages", where: m.sender == ^sender and m.timestamp >= ^ago, select: m.data Repo.all(query) end def remove_sender(sender) do query = from m in "messages", where: m.sender == ^sender, select: m.id Repo.delete_all(query) end def add_message(sender, message) do time = DateTime.utc_now |> DateTime.truncate(:second) Repo.insert(%Message{sender: sender, data: message, timestamp: time}) end end