so i am think about how to update a map value but about the map.update but since the map is use the
def start link
to create it what the map name should be MODULE?
and how do i edit the handle_call set price function is using Map.update right?
defmodule Sequence.Server do
use GenServer
#####
# External API
def start_link(catalogue) do
GenServer.start_link(__MODULE__, catalogue, name: __MODULE__)
end
def next_number do
GenServer.call __MODULE__, :next_number
end
def get_price(product) do
GenServer.call __MODULE__, {:get_price, product}
end
def increment_number(delta) do
GenServer.cast __MODULE__, {:increment_number, delta}
end
def show_catalogue do
GenServer.call __MODULE__, :show_catalogue
end
def set_price(product, np) do
GenServer.call __MODULE__, {:set_price, product, np}
end
#####
# GenServer implementation
def handle_call(:next_number, _from, current_number) do
{ :reply, current_number, current_number+1 }
end
def handle_call({:get_price, product}, _from(client proessid), catalogue) do
{:ok, price} = Map.fetch(catalogue,product)
{ :reply, price, catalogue }
end
def handle_cast({:increment_number, delta}, current_number) do
{ :noreply, current_number + delta}
end
def format_status(_reason, [ _pdict, state ]) do
[data: [{'State', "My current state is '#{inspect state}', and I'm happy"}]]
end
def handle_call(:show_catalogue, _from, catalogue) do
{ :reply, catalogue, catalogue}
end
def handle_call({:set_price, product, np}, _from, catalogue) do
{:ok, price} = Map.fetch(catalogue,product)
Map.update(,:product,np)
{ :reply, price, catalogue }
end
end