-
-
Save motephyr/e50ffe86f95c229b4ddac49d21a01d5d to your computer and use it in GitHub Desktop.
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
defmodule BidhouseWeb.HouseBidController do | |
use BidhouseWeb, :controller | |
alias Bidhouse.Origins | |
alias Bidhouse.Origins.HouseBid | |
alias Bidhouse.Coherence.User | |
alias BidhouseWeb.HouseEmail | |
alias BidhouseWeb.Coherence.Mailer | |
plug Coherence.Authentication.Session, [protected: true] when action in [:create] | |
import Bidhouse.HouseAuthorization | |
plug :find_house, "house_id" when action in [:create] | |
plug :add_interest_house when action in [:create] | |
import Bidhouse.HouseBidAuthorization | |
plug :password_match when action in [:create] | |
plug :house_not_my_and_publish_and_start when action in [:create] | |
plug :find_this_house_maxbid when action in [:create] | |
plug :find_this_house_mymaxbid when action in [:create] | |
# plug :price_more_then_start_price when action in [:create] | |
plug :price_more_then_minimum_bid_amount when action in [:create] | |
plug :maxbid_more_then_mymaxbid when action in [:create] | |
plug :find_bid_user_id_prices when action in [:create] | |
def create(conn, %{"house_id" => id,"house_bid" => house_bid_params}) do | |
house = conn.assigns[:house] | |
maxbid = conn.assigns[:maxbid] | |
maxbid_price = if maxbid, do: maxbid.price, else: house.start_price | |
params = house_bid_params |> Map.put("price", maxbid_price + String.to_integer( house_bid_params["add_price"]) ) | |
changeset = HouseBid.mybid_changeset(conn, id, params) | |
case Repo.insert(changeset) do | |
{:ok, house_bid} -> | |
other_bid_user_id_prices = conn.assigns[:bid_user_id_prices] |> Enum.reject(&Enum.at(&1,0) == Coherence.current_user(conn).id) | |
for n <- other_bid_user_id_prices do | |
other_bid_user_id = n |> Enum.at(0) | |
other_bid_price = n |> Enum.at(1) | |
other_bid_user = Ecto.Query.from(u in User, where: u.id == ^other_bid_user_id) |> Repo.one | |
more_price = house_bid.price - other_bid_price | |
HouseEmail.object_be_outbid_send_to_tenant(other_bid_user, house_url(conn, :show, house), house.title, more_price) |> Mailer.deliver | |
end | |
HouseEmail.object_get_bid_send_to_landlord(house.user, house_url(conn, :show, house), house.title, house_bid.price) | |
|> Mailer.deliver | |
conn | |
|> put_flash(:info, "出價成功!") | |
|> redirect(to: house_path(conn, :show, house)) | |
{:error, changeset} -> | |
render(conn, "new.html", changeset: changeset) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment