Skip to content

Commit

Permalink
Change EmployeeTask to Task
Browse files Browse the repository at this point in the history
  • Loading branch information
mindula committed Nov 16, 2023
1 parent 5ad4e3c commit 8d0b8ad
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 59 deletions.
14 changes: 7 additions & 7 deletions data-integration/data-seeds.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ INSERT INTO `Employee` (id, name, age, phone, email, department) VALUES (2, 'Tho
INSERT INTO `Employee` (id, name, age, phone, email, department) VALUES (3, 'Mary Taylor', 24, '9876543210', 'marytaylor@gmail.com', 'IT');

-- Add Employee task data to the database
INSERT INTO `EmployeeTask` (taskId, taskName, description, status, employeeId) VALUES (1001, 'Update Server Security', 'Implement the latest security patches and configurations', 'IN_PROGRESS', 1);
INSERT INTO `EmployeeTask` (taskId, taskName, description, status, employeeId) VALUES (1002, 'Network Optimization', 'Analyze network performance', 'NOT_STARTED', 1);
INSERT INTO `EmployeeTask` (taskId, taskName, description, status, employeeId) VALUES (1003, 'Database Migration', 'Migrate database from MySQL to PostgreSQL', 'COMPLETED', 1);
INSERT INTO `EmployeeTask` (taskId, taskName, description, status, employeeId) VALUES (1004, 'Update Server Security', 'Conduct a comprehensive audit of software licenses', 'COMPLETED', 2);
INSERT INTO `EmployeeTask` (taskId, taskName, description, status, employeeId) VALUES (1005, 'Firewall Configuration Review', 'Review and update firewall rules to enhance security', 'NOT_STARTED', 2);
INSERT INTO `EmployeeTask` (taskId, taskName, description, status, employeeId) VALUES (1006, 'Network Optimization', 'Analyze network performance', 'IN_PROGRESS', 3);
INSERT INTO `EmployeeTask` (taskId, taskName, description, status, employeeId) VALUES (1007, 'Mobile App Testing', 'Perform thorough testing and quality assurance checks', 'NOT_STARTED', 3);
INSERT INTO `Task` (taskId, taskName, description, status, employeeId) VALUES (1001, 'Update Server Security', 'Implement the latest security patches and configurations', 'IN_PROGRESS', 1);
INSERT INTO `Task` (taskId, taskName, description, status, employeeId) VALUES (1002, 'Network Optimization', 'Analyze network performance', 'NOT_STARTED', 1);
INSERT INTO `Task` (taskId, taskName, description, status, employeeId) VALUES (1003, 'Database Migration', 'Migrate database from MySQL to PostgreSQL', 'COMPLETED', 1);
INSERT INTO `Task` (taskId, taskName, description, status, employeeId) VALUES (1004, 'Update Server Security', 'Conduct a comprehensive audit of software licenses', 'COMPLETED', 2);
INSERT INTO `Task` (taskId, taskName, description, status, employeeId) VALUES (1005, 'Firewall Configuration Review', 'Review and update firewall rules to enhance security', 'NOT_STARTED', 2);
INSERT INTO `Task` (taskId, taskName, description, status, employeeId) VALUES (1006, 'Network Optimization', 'Analyze network performance', 'IN_PROGRESS', 3);
INSERT INTO `Task` (taskId, taskName, description, status, employeeId) VALUES (1007, 'Mobile App Testing', 'Perform thorough testing and quality assurance checks', 'NOT_STARTED', 3);
44 changes: 22 additions & 22 deletions data-integration/generated/store/persist_client.bal
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import ballerinax/mysql.driver as _;
import ballerinax/persist.sql as psql;

const EMPLOYEE = "employees";
const EMPLOYEE_TASK = "employeetasks";
const TASK = "tasks";

public isolated client class Client {
*persist:AbstractPersistClient;
Expand All @@ -30,18 +30,18 @@ public isolated client class Client {
phone: {columnName: "phone"},
email: {columnName: "email"},
department: {columnName: "department"},
"employeeTask[].taskId": {relation: {entityName: "employeeTask", refField: "taskId"}},
"employeeTask[].taskName": {relation: {entityName: "employeeTask", refField: "taskName"}},
"employeeTask[].description": {relation: {entityName: "employeeTask", refField: "description"}},
"employeeTask[].status": {relation: {entityName: "employeeTask", refField: "status"}},
"employeeTask[].employeeId": {relation: {entityName: "employeeTask", refField: "employeeId"}}
"tasks[].taskId": {relation: {entityName: "tasks", refField: "taskId"}},
"tasks[].taskName": {relation: {entityName: "tasks", refField: "taskName"}},
"tasks[].description": {relation: {entityName: "tasks", refField: "description"}},
"tasks[].status": {relation: {entityName: "tasks", refField: "status"}},
"tasks[].employeeId": {relation: {entityName: "tasks", refField: "employeeId"}}
},
keyFields: ["id"],
joinMetadata: {employeeTask: {entity: EmployeeTask, fieldName: "employeeTask", refTable: "EmployeeTask", refColumns: ["employeeId"], joinColumns: ["id"], 'type: psql:MANY_TO_ONE}}
joinMetadata: {tasks: {entity: Task, fieldName: "tasks", refTable: "Task", refColumns: ["employeeId"], joinColumns: ["id"], 'type: psql:MANY_TO_ONE}}
},
[EMPLOYEE_TASK] : {
entityName: "EmployeeTask",
tableName: "EmployeeTask",
[TASK] : {
entityName: "Task",
tableName: "Task",
fieldMetadata: {
taskId: {columnName: "taskId"},
taskName: {columnName: "taskName"},
Expand All @@ -68,7 +68,7 @@ public isolated client class Client {
self.dbClient = dbClient;
self.persistClients = {
[EMPLOYEE] : check new (dbClient, self.metadata.get(EMPLOYEE), psql:MYSQL_SPECIFICS),
[EMPLOYEE_TASK] : check new (dbClient, self.metadata.get(EMPLOYEE_TASK), psql:MYSQL_SPECIFICS)
[TASK] : check new (dbClient, self.metadata.get(TASK), psql:MYSQL_SPECIFICS)
};
}

Expand Down Expand Up @@ -111,40 +111,40 @@ public isolated client class Client {
return result;
}

isolated resource function get employeetasks(EmployeeTaskTargetType targetType = <>) returns stream<targetType, persist:Error?> = @java:Method {
isolated resource function get tasks(TaskTargetType targetType = <>) returns stream<targetType, persist:Error?> = @java:Method {
'class: "io.ballerina.stdlib.persist.sql.datastore.MySQLProcessor",
name: "query"
} external;

isolated resource function get employeetasks/[int taskId](EmployeeTaskTargetType targetType = <>) returns targetType|persist:Error = @java:Method {
isolated resource function get tasks/[int taskId](TaskTargetType targetType = <>) returns targetType|persist:Error = @java:Method {
'class: "io.ballerina.stdlib.persist.sql.datastore.MySQLProcessor",
name: "queryOne"
} external;

isolated resource function post employeetasks(EmployeeTaskInsert[] data) returns int[]|persist:Error {
isolated resource function post tasks(TaskInsert[] data) returns int[]|persist:Error {
psql:SQLClient sqlClient;
lock {
sqlClient = self.persistClients.get(EMPLOYEE_TASK);
sqlClient = self.persistClients.get(TASK);
}
_ = check sqlClient.runBatchInsertQuery(data);
return from EmployeeTaskInsert inserted in data
return from TaskInsert inserted in data
select inserted.taskId;
}

isolated resource function put employeetasks/[int taskId](EmployeeTaskUpdate value) returns EmployeeTask|persist:Error {
isolated resource function put tasks/[int taskId](TaskUpdate value) returns Task|persist:Error {
psql:SQLClient sqlClient;
lock {
sqlClient = self.persistClients.get(EMPLOYEE_TASK);
sqlClient = self.persistClients.get(TASK);
}
_ = check sqlClient.runUpdateQuery(taskId, value);
return self->/employeetasks/[taskId].get();
return self->/tasks/[taskId].get();
}

isolated resource function delete employeetasks/[int taskId]() returns EmployeeTask|persist:Error {
EmployeeTask result = check self->/employeetasks/[taskId].get();
isolated resource function delete tasks/[int taskId]() returns Task|persist:Error {
Task result = check self->/tasks/[taskId].get();
psql:SQLClient sqlClient;
lock {
sqlClient = self.persistClients.get(EMPLOYEE_TASK);
sqlClient = self.persistClients.get(TASK);
}
_ = check sqlClient.runDeleteQuery(taskId);
return result;
Expand Down
16 changes: 8 additions & 8 deletions data-integration/generated/store/persist_types.bal
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public type EmployeeOptionalized record {|

public type EmployeeWithRelations record {|
*EmployeeOptionalized;
EmployeeTaskOptionalized[] employeeTask?;
TaskOptionalized[] tasks?;
|};

public type EmployeeTargetType typedesc<EmployeeWithRelations>;
Expand All @@ -44,32 +44,32 @@ public type EmployeeUpdate record {|
string department?;
|};

public type EmployeeTask record {|
public type Task record {|
readonly int taskId;
string taskName;
string description;
TaskStatus status;
int employeeId;
|};

public type EmployeeTaskOptionalized record {|
public type TaskOptionalized record {|
int taskId?;
string taskName?;
string description?;
TaskStatus status?;
int employeeId?;
|};

public type EmployeeTaskWithRelations record {|
*EmployeeTaskOptionalized;
public type TaskWithRelations record {|
*TaskOptionalized;
EmployeeOptionalized employee?;
|};

public type EmployeeTaskTargetType typedesc<EmployeeTaskWithRelations>;
public type TaskTargetType typedesc<TaskWithRelations>;

public type EmployeeTaskInsert EmployeeTask;
public type TaskInsert Task;

public type EmployeeTaskUpdate record {|
public type TaskUpdate record {|
string taskName?;
string description?;
TaskStatus status?;
Expand Down
4 changes: 2 additions & 2 deletions data-integration/generated/store/script.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
-- This file is an auto-generated file by Ballerina persistence layer for model.
-- Please verify the generated scripts and execute them against the target DB server.

DROP TABLE IF EXISTS `EmployeeTask`;
DROP TABLE IF EXISTS `Task`;
DROP TABLE IF EXISTS `Employee`;

CREATE TABLE `Employee` (
Expand All @@ -16,7 +16,7 @@ CREATE TABLE `Employee` (
PRIMARY KEY(`id`)
);

CREATE TABLE `EmployeeTask` (
CREATE TABLE `Task` (
`taskId` INT NOT NULL,
`taskName` VARCHAR(191) NOT NULL,
`description` VARCHAR(191) NOT NULL,
Expand Down
18 changes: 9 additions & 9 deletions data-integration/main.bal
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ service / on new http:Listener(port) {
return http:CREATED;
}

resource function post task(store:EmployeeTaskInsert task)
resource function post task(store:TaskInsert task)
returns http:Created|http:Conflict|http:InternalServerError {
int[]|persist:Error result = dbClient->/employeetasks.post([task]);
int[]|persist:Error result = dbClient->/tasks.post([task]);
if result is persist:AlreadyExistsError {
return http:CONFLICT;
}
Expand All @@ -32,7 +32,7 @@ service / on new http:Listener(port) {
}

resource function get task/[int taskId]() returns http:Ok|http:NotFound|http:InternalServerError {
store:EmployeeTask|persist:Error task = dbClient->/employeetasks/[taskId];
store:Task|persist:Error task = dbClient->/tasks/[taskId];
if task is persist:Error {
if task is persist:NotFoundError {
return http:NOT_FOUND;
Expand All @@ -43,8 +43,8 @@ service / on new http:Listener(port) {
}

resource function get employeetasks/[int empId]() returns http:Ok|http:NotFound|http:InternalServerError {
store:EmployeeTask[]|persist:Error tasks = from store:EmployeeTask task
in dbClient->/employeetasks(store:EmployeeTask)
store:Task[]|persist:Error tasks = from store:Task task
in dbClient->/tasks(store:Task)
where task.employeeId == empId
select task;
if tasks is persist:Error {
Expand All @@ -67,9 +67,9 @@ service / on new http:Listener(port) {
return <http:Ok>{body: employee};
}

resource function put task/[int taskId](store:EmployeeTaskUpdate emp)
resource function put task/[int taskId](store:TaskUpdate emp)
returns http:Ok|http:InternalServerError {
store:EmployeeTask|persist:Error result = dbClient->/employeetasks/[taskId].put(emp);
store:Task|persist:Error result = dbClient->/tasks/[taskId].put(emp);
if result is persist:Error {
return <http:InternalServerError>{body: result.message()};
}
Expand All @@ -78,7 +78,7 @@ service / on new http:Listener(port) {

resource function delete task/[int taskId]()
returns http:NoContent|http:NotFound|http:InternalServerError {
store:EmployeeTask|persist:Error task = dbClient->/employeetasks/[taskId];
store:Task|persist:Error task = dbClient->/tasks/[taskId];
if task is persist:Error {
if task is persist:NotFoundError {
return http:NOT_FOUND;
Expand All @@ -88,7 +88,7 @@ service / on new http:Listener(port) {
if task.status != store:COMPLETED {
return <http:InternalServerError>{body: "Task is not completed yet"};
}
store:EmployeeTask|persist:Error deleteResult = dbClient->/employeetasks/[taskId].delete;
store:Task|persist:Error deleteResult = dbClient->/tasks/[taskId].delete;
if deleteResult is persist:Error {
return <http:InternalServerError>{body: deleteResult.message()};
}
Expand Down
4 changes: 2 additions & 2 deletions data-integration/persist/model.bal
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ type Employee record {|
string phone;
string email;
string department;
EmployeeTask[] employeeTask;
Task[] tasks;
|};

type EmployeeTask record {|
type Task record {|
readonly int taskId;
string taskName;
string description;
Expand Down
18 changes: 9 additions & 9 deletions data-integration/tests/test.bal
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function testEmployee() returns error? {

@test:Config
function testTask() returns error? {
store:EmployeeTask task = {
store:Task task = {
taskId: 1008,
taskName: "IT Training Workshop'",
description: "Organize a workshop",
Expand All @@ -39,7 +39,7 @@ function testTask() returns error? {
http:Response res = check cl->/task.post(task);
test:assertEquals(res.statusCode, http:STATUS_CREATED);

store:EmployeeTask|persist:Error empTask = dbClient->/employeetasks/[1008];
store:Task|persist:Error empTask = dbClient->/tasks/[1008];
if empTask is persist:Error {
test:assertFail(empTask.message());
}
Expand All @@ -63,8 +63,8 @@ function testGetEmployee() returns error? {

@test:Config
function testGetTask() returns error? {
store:EmployeeTask res = check cl->/task/[1001];
store:EmployeeTask expectedRes = {
store:Task res = check cl->/task/[1001];
store:Task expectedRes = {
taskId: 1001,
taskName: "Update Server Security",
description: "Implement the latest security patches and configurations",
Expand All @@ -77,8 +77,8 @@ function testGetTask() returns error? {

@test:Config
function testGetEmployeeTasks() returns error? {
store:EmployeeTask[] res = check cl->/employeetasks/[1];
store:EmployeeTask[] expectedRes = [
store:Task[] res = check cl->/employeetasks/[1];
store:Task[] expectedRes = [
{
taskId: 1001,
taskName: "Update Server Security",
Expand Down Expand Up @@ -107,14 +107,14 @@ function testGetEmployeeTasks() returns error? {

@test:Config
function testPutEmployee() returns error? {
store:EmployeeTask res = check cl->/task/[1001];
store:Task res = check cl->/task/[1001];
test:assertEquals(res.status, "IN_PROGRESS");

store:EmployeeTaskUpdate updatedTask = {
store:TaskUpdate updatedTask = {
status: store:COMPLETED
};

store:EmployeeTask updatedRes = check cl->/task/[1001].put(updatedTask);
store:Task updatedRes = check cl->/task/[1001].put(updatedTask);
test:assertEquals(updatedRes.status, "COMPLETED");
}

Expand Down

0 comments on commit 8d0b8ad

Please sign in to comment.