This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp find_user(params) do | |
if params["login_id"] || params["device_id"] || params["prism_id"] do | |
query = Portmeirion.User.find_query(params) | |
{:ok, result} = Ecto.Adapters.SQL.query(Repo, query, []) | |
row = List.first(result.rows) | |
if row do | |
id = row |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp where_clause(query, params) do | |
allowed_params = ~w(login_id facebook_id device_id prism_id) | |
key_values = params | |
|> Enum.filter(fn({key, _}) -> Enum.member?(allowed_params, key) end) | |
|> Enum.map(fn({key, value}) -> {String.to_existing_atom(key), cast_param(key, value)} end) # or_where expects list of tuples | |
|> Enum.sort # Ensure device_id is always first if present | |
where_fragment(query, key_values) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT warehouse_id | |
FROM users as u | |
FULL JOIN devices AS d ON d.user_id = u.id | |
WHERE login_id = ? OR device_id = ? OR prism_id = ? | |
ORDER BY u.login_id, d.device_id, u.prism_id | |
LIMIT 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defp deps do | |
[{:phoenix, "~> 1.2"}, | |
{:phoenix_ecto, "~> 3.0"}, | |
... | |
{:ecto, "~> 2.1.0-rc.3", override: true}, | |
] | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iex(3)> where_string = "device_id = AAA OR login_id = 123" | |
"device_id = AAA OR login_id = 123" | |
iex(4)> query = from u in Portmeirion.User, limit: 1, select: %{warehouse_id: u.warehouse_id}, where: fragment(^where_string) | |
** (ArgumentError) to prevent sql injection, only a keyword list may be interpolated as the first argument to `fragment/1` with the `^` operator, got `"device_id = AAA OR login_id = 123"` | |
(ecto) lib/ecto/query/builder.ex:512: Ecto.Query.Builder.keyword!/1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
"a" => %{"r" => %{"s" => %{"e" => %{"n" => %{"i" => %{"c" => :leaf}}}}}}, | |
"b" => %{ | |
"l" => %{"o" => %{"o" => %{"m" => %{"e" => %{"r" => %{"s" => :leaf}}}}}}, | |
"o" => %{ | |
"s" => %{"o" => %{"m" => :leaf}}, | |
"u" => %{"n" => %{"d" => %{"e" => %{"r" => :leaf}}}} | |
} | |
}, | |
"f" => %{"u" => %{"s" => %{"t" => %{"i" => %{"l" => %{"u" => %{"g" => %{"s" => :leaf}}}}}}}}, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def build_tree(word_list) do | |
Enum.reduce(word_list, %{}, fn(word, tree) -> | |
word | |
|> String.codepoints | |
|> place_chars(tree) | |
end) | |
end | |
def place_chars([], current_node), do: current_node | |
def place_chars([h | t], current_node) do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def valid_word?("", _), do: true | |
def valid_word?(word, tree) do | |
chars = String.codepoints(word) | |
case check_char(chars, tree) do | |
false -> | |
false | |
_ -> | |
[_ | t] = chars | |
valid_word?(Enum.join(t), tree) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def check_char(["3" | t], current_node) do | |
if check_char(["b" | t], current_node) do | |
check_char(["e" | t], current_node) | |
else | |
false | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<feed xmlns="http://www.w3.org/2005/Atom"> | |
<title>BleacherReport articles</title> | |
<link href="http://bleacherreport.com/"/> | |
<link rel="self" href="http://feed.bleacherreport.com/articles"/> | |
<updated>2018-07-24T18:30:02Z</updated> | |
<id>urn:uuid:6ccdb64c-9ee8-47c5-bae4-9e2fb9fae183</id> | |
<category term="Sport"/> |