adds api calls for getting, setting, removing
- using Ecto queries, the api can now get newest message or get the last message - delete all messages of a sender id - add a message (auto adding a time stamp)
This commit is contained in:
parent
de81aaaef7
commit
3b1657e18a
@ -22,3 +22,10 @@ Ready to run in production? Please [check our deployment guides](https://hexdocs
|
|||||||
* Docs: https://hexdocs.pm/phoenix
|
* Docs: https://hexdocs.pm/phoenix
|
||||||
* Forum: https://elixirforum.com/c/phoenix-forum
|
* Forum: https://elixirforum.com/c/phoenix-forum
|
||||||
* Source: https://github.com/phoenixframework/phoenix
|
* Source: https://github.com/phoenixframework/phoenix
|
||||||
|
|
||||||
|
## TODOs
|
||||||
|
|
||||||
|
[x] finish api calls in lib/bump/messages.ex
|
||||||
|
[]
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,8 +1,15 @@
|
|||||||
defmodule Bump.Messages do
|
defmodule Bump.Messages do
|
||||||
alias Bump.Repo
|
alias Bump.Repo
|
||||||
|
alias Bump.Messages.Message
|
||||||
import Ecto.Query
|
import Ecto.Query
|
||||||
|
|
||||||
def get_newest_message(_sender) do
|
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
|
end
|
||||||
|
|
||||||
def get_all_messages_since(sender, minutes) do
|
def get_all_messages_since(sender, minutes) do
|
||||||
@ -18,10 +25,21 @@ defmodule Bump.Messages do
|
|||||||
Repo.all(query)
|
Repo.all(query)
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_sender(_sender) do
|
def remove_sender(sender) do
|
||||||
|
query = from m in "messages",
|
||||||
|
where: m.sender == ^sender,
|
||||||
|
select: m.id
|
||||||
|
Repo.delete_all(query)
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_message(_sender, _message) do
|
def add_message(sender, message) do
|
||||||
|
time =
|
||||||
|
DateTime.utc_now
|
||||||
|
|> DateTime.truncate(:second)
|
||||||
|
|
||||||
|
Repo.insert(%Message{sender: sender, data: message, timestamp: time})
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -5,8 +5,7 @@ defmodule Bump.Messages.Message do
|
|||||||
schema "messages" do
|
schema "messages" do
|
||||||
field :sender, :string
|
field :sender, :string
|
||||||
field :data, :string, default: ""
|
field :data, :string, default: ""
|
||||||
field :timestamp, :utc_datetime, default: DateTime.utc_now |> DateTime.truncate(:second)
|
field :timestamp, :utc_datetime
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user