Skip to content

Instantly share code, notes, and snippets.

@IMS94
Last active January 26, 2022 09:30
Show Gist options
  • Save IMS94/d1dd3ff7d1189273e2dd082f5c7a6c53 to your computer and use it in GitHub Desktop.
Save IMS94/d1dd3ff7d1189273e2dd082f5c7a6c53 to your computer and use it in GitHub Desktop.
An in memory CRUD service written in Ballerina
import ballerina/http;
import ballerina/log;
import ballerina/uuid;
# Represents a product
#
# + id - Product ID
# + name - Name of the product
# + description - Product description
# + price - Product price
public type Product record {|
string id?;
string name;
string description;
Price price;
|};
# An enum to represent currencies
public enum Currency {
USD,
LKR,
SGD,
GBP
}
# Represents price
public type Price record {|
Currency currency;
float amount;
|};
# Represents an error
public type Error record {|
string code;
string message;
|};
public type ErrorResponse record {|
Error 'error;
|};
# Bad request response
public type BadRequest record {|
*http:BadRequest;
ErrorResponse body;
|};
# Represents headers of created response
public type Headers record {|
string location;
|};
# HTTP Created response
public type Created record {|
*http:Created;
Headers headers;
anydata body = null;
|};
# The product service
service /products on new http:Listener(8080) {
private map<Product> products = {};
# List all products
# + return - List of products
resource function get .() returns Product[] {
return self.products.toArray();
}
# Add a new product
#
# + product - Product to be added
# + return - http created or bad request
resource function post .(@http:Payload Product product) returns Created|BadRequest {
if product.name.length() == 0 || product.description.length() == 0 {
log:printWarn("Product name or description is not present", product = product);
return <BadRequest>{
body: {
'error: {
code: "INVALID_NAME",
message: "Product name and description are required"
}
}
};
}
if product.price.amount < 0.0 {
log:printWarn("Product price cannot be negative", product = product);
return <BadRequest>{
body: {
'error: {
code: "INVALID_PRICE",
message: "Product price cannot be negative"
}
}
};
}
log:printDebug("Adding new product", product = product);
product.id = uuid:createType1AsString();
self.products[<string>product.id] = product;
log:printInfo("Added new product", product = product);
string productUrl = string `/products/${<string>product.id}`;
return <Created>{
headers: {
location: productUrl
}
};
}
# Update a product
#
# + product - Updated product
# + return - Error if product is invalid
resource function put .(@http:Payload Product product) returns BadRequest? {
if product.id is () || !self.products.hasKey(<string>product.id) {
log:printWarn("Invalid product provided for update", product = product);
return {
body: {
'error: {
code: "INVALID_PRODUCT",
message: "Invalid product"
}
}
};
}
log:printInfo("Updating product", product = product);
self.products[<string>product.id] = product;
return;
}
# Deletes a product
#
# + id - Product ID
# + return - Ok or bad request
resource function delete [string id]() returns BadRequest? {
if !self.products.hasKey(<string>id) {
log:printWarn("Invalid product ID to be deleted", id = id);
return {
body: {
'error: {
code: "INVALID_ID",
message: "Invalud product id"
}
}
};
}
log:printDebug("Deleting product", id = id);
Product removed = self.products.remove(id);
log:printDebug("Deleted product", product = removed);
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment