Skip to content

Instantly share code, notes, and snippets.

View jorgevilaca82's full-sized avatar
🏠
Working from home

Jorge Vilaça jorgevilaca82

🏠
Working from home
View GitHub Profile
@jorgevilaca82
jorgevilaca82 / map_user_attrs.ex
Last active April 5, 2024 21:17
Elixir map user attributes to meet external service format
defmodule UserMapperFunctions do
def map_my_address(%{
address: address,
city: city,
province: province,
country: country,
postalCode: postalCode
}) do
"#{address}, #{city}, #{province}, #{country}, #{postalCode}"
end
@jorgevilaca82
jorgevilaca82 / shopping_cart_total_cost.py
Created December 13, 2023 21:31
3 ways of sum cart total cost
shopping_cart = [
{"item": "Apple", "price": 0.5, "quantity": 10},
{"item": "Milk", "price": 1.5, "quantity": 2},
{"item": "Bread", "price": 2.0, "quantity": 1}
]
def item_cost(item):
return item["price"] * item["quantity"]
sum(map(item_cost, shopping_cart))
@jorgevilaca82
jorgevilaca82 / service_module_switcher.exs
Created December 6, 2023 20:10
Elixir service module switcher
defmodule ServiceSwitcher do
defmacro __using__(opts \\ []) do
service_module_mapping = opts[:service_module_mapping]
quote do
@service_module_mapping unquote(service_module_mapping)
@services Keyword.keys(@service_module_mapping)
def get_service_module(service) when is_binary(service),
do: service |> String.to_atom() |> get_service_module()
@jorgevilaca82
jorgevilaca82 / T.ex
Last active August 15, 2023 19:41
How to rescue from Ecto.MultipleResultsError
defmodule T do
import Ecto.Query
alias MyApp.Repo
def test() do
from(s in "users", select: [:id]) |> Repo.one()
rescue
Ecto.MultipleResultsError -> IO.puts("Sorry. Things didn't go well.")
end
end
function requestNotificationPermission() {
Notification.requestPermission().then(p => {
if (p === "granted") {
console.log("Thank you")
} else {
console.log("😔")
}
}).catch(function (err) {
console.log(err)
})
@jorgevilaca82
jorgevilaca82 / utils.ex
Created January 19, 2023 14:47
check for blank values
defmodule Utils do
def is_blank(nil), do: true
def is_blank(val) when val == %{}, do: true
def is_blank(val) when val == [], do: true
def is_blank(val) when is_binary(val), do: String.trim(val) == ""
def is_blank(val), do: false
end
@jorgevilaca82
jorgevilaca82 / currency_notification.js
Created January 18, 2023 15:22
Notifys you when a target value is reached
Notification.requestPermission()
const CURRENCY = "USD-BRL"
let myTargetValue = window.prompt("Targe value")
let intervalId = null
async function checkCurrency(targeValue, currency) {
let currencyKey = currency.replace("-", "")
let response = await fetch(`https://economia.awesomeapi.com.br/last/${currency}`)
pessoas = [
{ 'nome': 'João', "idade": 64 },
{ 'nome': 'Janete', "idade": 34 },
{ 'nome': 'Eduardo', "idade": 24 },
{ 'nome': 'Sara', "idade": 64 },
{ 'nome': 'José', "idade": 32 },
{ 'nome': 'Lisa', "idade": 34 },
{ 'nome': 'Mário', "idade": 99 },
]
select pg_terminate_backend(pid)
from pg_stat_activity
where datname='dbname';
drop database if exists "dbname";
@jorgevilaca82
jorgevilaca82 / sql-server-pagination.sql
Created April 8, 2017 01:03
Páginação SQL Server
SELECT * FROM Tabela
ORDER BY (SELECT NULL)
OFFSET 3 ROWS FETCH NEXT 1 ROWS ONLY;
-- numero de registros por página
declare @PERPAGE int = 1;
select ((select count(*) from sala) / @PERPAGE) as QTD_PAG;