Skip to content

Commit

Permalink
Add improvements to the petdesk sample application
Browse files Browse the repository at this point in the history
  • Loading branch information
ShanChathusanda93 committed Apr 2, 2024
1 parent c4507f3 commit 54cbea8
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
10 changes: 8 additions & 2 deletions petcare-sample/b2c/web-app/petdesk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
```
!!! Note
By default, the services store the data in memory.
> [!NOTE]
> By default, the services store the data in memory. They can be connected to MySQL databases. Create a `Config.toml` file in the root folder of the service component and add the relevant DB configurations to the `Config.toml` files
> dbHost = "<DB_HOST>"
> dbUsername = "<DB_USERNAME>"
> dbPassword = "<DB_USER_PASSWORD>"
> dbDatabase = "<DB_NAME>"
> dbPort = "<DB_PORT>"
> Create a MySQL database tables using the schemas located in `<PROJECT_HOME>/petcare-sample/b2c/web-app/petdesk/dbscripts directory`.
## Create an Application in WSO2 Identity Server
1. Create a Single-Page Application named `Pet Desk App` in root organization.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function dbAddPet(Pet pet) returns Pet|error {
Vaccination[]? vacs = pet.vaccinations;
sql:ExecutionResult[]|sql:Error batchResult = [];

if vacs != null {
if vacs != null && vacs.length() > 0 {

foreach Vaccination vac in vacs {
if vac.enableAlerts == null {
Expand Down Expand Up @@ -157,7 +157,7 @@ function dbUpdatePet(Pet pet) returns Pet|error {
Vaccination[]? vacs = pet.vaccinations;
sql:ExecutionResult[]|sql:Error batchResult = [];

if vacs != null {
if vacs != null && vacs.length() > 0 {

foreach Vaccination vac in vacs {
if vac.enableAlerts == null {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ballerina/sql;
import ballerina/log;
import ballerinax/mysql;

configurable string dbHost = "localhost";
configurable string dbHost = "";
configurable string dbUsername = "admin";
configurable string dbPassword = "admin";
configurable string dbDatabase = "PET_DB";
Expand All @@ -19,7 +19,7 @@ map<Thumbnail> thumbnailMap = {};

function init() returns error? {

if dbHost != "localhost" && dbHost != "" {
if dbHost != "" {
useDB = true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import ballerina/mime;

# A service representing a network-accessible API
# bound to port `9090`.
@http:ServiceConfig {
cors: {
allowOrigins: ["*"]
}
}
service / on new http:Listener(9090) {

# Get all pets
Expand Down Expand Up @@ -197,6 +202,17 @@ function getOwner(http:Headers headers) returns string|error {

var jwtHeader = headers.getHeader("x-jwt-assertion");
if jwtHeader is http:HeaderNotFoundError {
var authHeader = headers.getHeader("Authorization");
if authHeader is http:HeaderNotFoundError {
return authHeader;
} else {
if (authHeader.startsWith("Bearer ")) {
jwtHeader = authHeader.substring(7);
}
}
}

if (jwtHeader is http:HeaderNotFoundError) {
return jwtHeader;
}

Expand All @@ -208,6 +224,17 @@ function getOwnerWithEmail(http:Headers headers) returns [string, string]|error

var jwtHeader = headers.getHeader("x-jwt-assertion");
if jwtHeader is http:HeaderNotFoundError {
var authHeader = headers.getHeader("Authorization");
if authHeader is http:HeaderNotFoundError {
return authHeader;
} else {
if (authHeader.startsWith("Bearer ")) {
jwtHeader = authHeader.substring(7);
}
}
}

if (jwtHeader is http:HeaderNotFoundError) {
return jwtHeader;
}

Expand Down
32 changes: 32 additions & 0 deletions petcare-sample/b2c/web-app/petdesk/dbscripts/pet-management.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CREATE TABLE Pet (
id VARCHAR(255) PRIMARY KEY,
name VARCHAR(255) NOT NULL,
breed VARCHAR(255) NOT NULL,
dateOfBirth VARCHAR(255) NOT NULL,
owner VARCHAR(255) NOT NULL
);

CREATE TABLE Vaccination (
id INT AUTO_INCREMENT PRIMARY KEY,
petId VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
lastVaccinationDate VARCHAR(255) NOT NULL,
nextVaccinationDate VARCHAR(255),
enableAlerts BOOLEAN NOT NULL DEFAULT 0,
FOREIGN KEY (petId) REFERENCES Pet(id) ON DELETE CASCADE
);

CREATE TABLE Thumbnail (
id INT AUTO_INCREMENT PRIMARY KEY,
fileName VARCHAR(255) NOT NULL,
content MEDIUMBLOB NOT NULL,
petId VARCHAR(255) NOT NULL,
FOREIGN KEY (petId) REFERENCES Pet(id) ON DELETE CASCADE
);

CREATE TABLE Settings (
owner VARCHAR(255) NOT NULL,
notifications_enabled BOOLEAN NOT NULL,
notifications_emailAddress VARCHAR(255),
PRIMARY KEY (owner)
);

0 comments on commit 54cbea8

Please sign in to comment.