feat: add context methods to mutate rooms
Also, improve user input validation.
This commit is contained in:
parent
c6ee2bbf0b
commit
32350431f8
@ -8,6 +8,18 @@ defmodule Slax.Chat do
|
||||
alias Slax.Chat.Room
|
||||
alias Slax.Repo
|
||||
|
||||
def create_room(attrs) do
|
||||
%Room{}
|
||||
|> Room.changeset(attrs)
|
||||
|> Repo.insert()
|
||||
end
|
||||
|
||||
def update_room(%Room{} = room, attrs) do
|
||||
room
|
||||
|> Room.changeset(attrs)
|
||||
|> Repo.update()
|
||||
end
|
||||
|
||||
def get_first_room! do
|
||||
query = from(r in Room, limit: 1, order_by: [asc: :name])
|
||||
Repo.one!(query)
|
||||
|
@ -13,6 +13,11 @@ defmodule Slax.Chat.Room do
|
||||
def changeset(room, attrs) do
|
||||
room
|
||||
|> cast(attrs, [:name, :topic])
|
||||
|> validate_required([:name, :topic])
|
||||
|> validate_required([:name])
|
||||
|> validate_length(:name, max: 80)
|
||||
|> validate_format(:name, ~r/\A[a-z0-9-]+\z/,
|
||||
message: "can only contain lowercase letters, numbers, and dashes"
|
||||
)
|
||||
|> validate_length(:topic, max: 200)
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user