Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 17 additions & 51 deletions stdlib/examples/produce-exchange/serverActor.as
Original file line number Diff line number Diff line change
Expand Up @@ -51,63 +51,30 @@ actor server = {
----------------------
Register a new user, who may play several roles in the exchange.

The given `user_name` must be unique to the exchange; the operation fails otherwise.

*/

registrarAddUser(
short_name_: Text,
description_: Text,
region_: RegionId,
isDeveloper_: Bool,
user_name: Text,
public_key: Text,
description: Text,
region: RegionId,
isDeveloper: Bool,
isProducer: Bool,
isRetailer: Bool,
isTransporter: Bool
) : async ?UserId {

let prId = if isProducer { getModel().producerTable.addInfoGetId(
func(id_:ProducerId):ProducerInfo {
shared {
id=id_:ProducerId;
short_name=short_name_;
description=description_;
region=region_;
inventory=[];
reserved=[];
}
}) } else null;

let trId = if isTransporter { getModel().transporterTable.addInfoGetId(
func(id_:TransporterId):TransporterInfo {
shared {
id=id_:TransporterId;
short_name=short_name_;
description=description_;
routes=[];
reserved=[];
}
}) } else null;

let rrId = if isRetailer { getModel().retailerTable.addInfoGetId(
func(id_:RetailerId):RetailerInfo {
shared {
id=id_;
short_name=short_name_;
description=description_;
region=region_:RegionId
}
}) } else null;

getModel().userTable.addInfoGetId(
func (id_: UserId) : UserInfo =
shared {
id = id_;
short_name = short_name_;
description = description_;
region = region_;
producerId = prId;
transporterId = trId;
retailerId = rrId;
isDeveloper = isDeveloper_;
})
getModel().addUser(
user_name,
public_key,
description,
region,
isDeveloper,
isProducer,
isRetailer,
isTransporter
)
};

/**
Expand Down Expand Up @@ -900,4 +867,3 @@ been processed
With the following closing brace, the interface of the `Server` is thusly defined.
*/
}; // end: actor class `Server`

125 changes: 122 additions & 3 deletions stdlib/examples/produce-exchange/serverModel.as
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class Model() {

private idIsEq(x:Nat,y:Nat):Bool { x == y };

private textIsEq(x:Text,y:Text):Bool { x == y };

private idPairIsEq(x:(Nat,Nat),y:(Nat,Nat)):Bool { x.0 == y.0 and x.1 == y.1 };

private idHash(x:Nat):Hash { Hash.hashOfInt(x) };
Expand All @@ -61,6 +63,10 @@ class Model() {
new { key = (x,y) ; hash = idPairHash(x,y) }
};

private keyOfText(x:Text):Key<Text> {
new { key = x ; hash = Hash.hashOfText(x) }
};

/**
Misc counters
==================
Expand Down Expand Up @@ -124,7 +130,8 @@ secondary maps.
idHash,
func(doc:UserDoc):UserInfo = shared {
id=doc.id;
short_name=doc.short_name;
user_name=doc.user_name;
public_key=doc.public_key;
description=doc.description;
region=doc.region;
producerId=doc.producerId;
Expand All @@ -134,7 +141,8 @@ secondary maps.
},
func(info:UserInfo):?UserDoc = ?(new {
id=info.id;
short_name=info.short_name;
user_name=info.user_name;
public_key=info.public_key;
description=info.description;
region=info.region;
producerId=info.producerId;
Expand Down Expand Up @@ -483,6 +491,13 @@ secondary maps.
}}
);

/**
Indexing by `UserName`
=====================================
*/

private var usersByUserName
: UserNameMap = null;

/**

Expand Down Expand Up @@ -584,6 +599,109 @@ than the MVP goals, however.

*/

/**

`User`-oriented operations
==========================================

*/


/**

`addUser`
---------

The given `user_name` must be unique to the exchange; the operation fails otherwise.

*/
addUser(
user_name_: Text,
public_key_: Text,
description_: Text,
region_: RegionId,
isDeveloper_: Bool,
isProducer: Bool,
isRetailer: Bool,
isTransporter: Bool
) : ?UserId {

/**- Fail immediately if the user name is already taken: */
switch (Trie.find<UserName,UserId>(usersByUserName, keyOfText(user_name_), textIsEq)) {
case null {};
case (?_) { return null };
};

/**- Fail immediately if the region Id is invalid: */
switch (regionTable.getDoc(region_)) {
case null { return null };
case (?_) {};
};

/** Input is valid: All subsequent operations will succeed: */

/**- Create a producer role for the user: */
let prId = if isProducer { producerTable.addInfoGetId(
func(id_:ProducerId):ProducerInfo {
shared {
id=id_:ProducerId;
short_name=user_name_;
description=description_;
region=region_;
inventory=[];
reserved=[];
}
}) } else null;

/**- Create a transporter role for the user: */
let trId = if isTransporter { transporterTable.addInfoGetId(
func(id_:TransporterId):TransporterInfo {
shared {
id=id_:TransporterId;
short_name=user_name_;
description=description_;
routes=[];
reserved=[];
}
}) } else null;

/**- Create a retailer role for the user: */
let rrId = if isRetailer { retailerTable.addInfoGetId(
func(id_:RetailerId):RetailerInfo {
shared {
id=id_;
short_name=user_name_;
description=description_;
region=region_:RegionId;
}
}) } else null;

/**- Record the user information: */
let id = userTable.addInfoGetId(
func (id_: UserId) : UserInfo =
shared {
id = id_;
user_name = user_name_;
public_key = public_key_;
description = description_;
region = region_;
producerId = prId;
transporterId = trId;
retailerId = rrId;
isDeveloper = isDeveloper_;
});

/**- Record the mapping from user-chosen name to exchange-chosen id: */
usersByUserName :=
Trie.insertFresh<UserName,UserId>(
usersByUserName,
keyOfText(user_name_), textIsEq,
unwrap<UserId>(id)
);

/**- return the id */
id
};


/**
Expand Down Expand Up @@ -1033,7 +1151,8 @@ than the MVP goals, however.

*/
isCompatibleTruckType(tt:TruckTypeDoc, produce:ProduceDoc) : Bool {
nyi()
// todo
true
};

/**
Expand Down
Loading