Skip to content

Commit 06605bf

Browse files
committed
updated
added UserService and RoleService
1 parent ce201d7 commit 06605bf

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

src/model/service/RoleService.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// RoleService.js
3+
// PureMVC JS Demo - EmployeeAdmin Microservice
4+
//
5+
// Copyright(c) 2023 Saad Shams <[email protected]>
6+
// Your reuse is governed by the Creative Commons Attribution 3.0 License
7+
//
8+
9+
export class RoleService {
10+
11+
// See UserService
12+
constructor() {
13+
}
14+
15+
}

src/model/service/UserService.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// UserService.js
3+
// PureMVC JS Demo - EmployeeAdmin Microservice
4+
//
5+
// Copyright(c) 2023 Saad Shams <[email protected]>
6+
// Your reuse is governed by the Creative Commons Attribution 3.0 License
7+
//
8+
9+
import https from "node:https";
10+
11+
export class UserService {
12+
13+
constructor() {}
14+
15+
// Simple Use Case:
16+
// If the data is sourced externally, the Proxy first checks the database (UserData) to see if the data is
17+
// available. If available, it returns the cached data to the Mediator. Otherwise, it contacts the UserService.
18+
// Service classes retrieve data from external sources, manage network requests and parsing, and return data to
19+
// the Proxy, which then caches it in the database for future use before returning it to the Mediator.
20+
21+
// Complex Use Case:
22+
// A Command will come into play if the data retrieval involves multiple proxies and data sources,
23+
// typically if the logic spans different domains.
24+
// The Command encapsulates business logic, manages data retrieval, performs data transformation, and
25+
// enforces business rules before returning the final data to the Mediator and its component.
26+
findAllUsers() {
27+
return new Promise((resolve, reject) => {
28+
https.request({
29+
method: "GET", hostname: "https://jsonplaceholder.typicode.com", path: "/users"
30+
}, response => {
31+
let buffers = [];
32+
response.on("data", data => buffers.push(data));
33+
response.on("end", () => {
34+
try {
35+
if(response.statusCode === 200)
36+
resolve(JSON.parse(Buffer.concat(buffers).toString()));
37+
else
38+
reject(JSON.parse(Buffer.concat(buffers).toString()));
39+
} catch(error) { reject(error); }
40+
});
41+
}).on("error", reject).end();
42+
});
43+
}
44+
45+
}

0 commit comments

Comments
 (0)