Skip to content

Commit 7259b23

Browse files
Merge pull request #2 from LuckyOkoedion/develop
fully set up
2 parents 43173b4 + ea8cfb6 commit 7259b23

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+10214
-1440
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# rest-api-with-hapi-typescript-tsoa-prisma-and-postgresql
22

3-
This is a church office management REST api built with Hapi js, TypeScript Open API (TSOA) library, Prisma ORM and Postgresql. With Unit tests and End-toEnd tests.
3+
This is a church office management REST api built with Hapi js, TypeScript, Prisma ORM and Postgresql. With Unit tests and End-toEnd tests.
44

5-
It has an auto generated documentation with OpenAPI v3 specification, courtesy of TSOA.
5+
It is an example of how to modularize / structure hapi API implementation into controllers and services. Good separation of concerns for ease of maintenance for large projects, and type safety, courtesy of typescript.

dist/app.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"use strict";
2+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3+
if (k2 === undefined) k2 = k;
4+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5+
}) : (function(o, m, k, k2) {
6+
if (k2 === undefined) k2 = k;
7+
o[k2] = m[k];
8+
}));
9+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10+
Object.defineProperty(o, "default", { enumerable: true, value: v });
11+
}) : function(o, v) {
12+
o["default"] = v;
13+
});
14+
var __importStar = (this && this.__importStar) || function (mod) {
15+
if (mod && mod.__esModule) return mod;
16+
var result = {};
17+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18+
__setModuleDefault(result, mod);
19+
return result;
20+
};
21+
var __importDefault = (this && this.__importDefault) || function (mod) {
22+
return (mod && mod.__esModule) ? mod : { "default": mod };
23+
};
24+
Object.defineProperty(exports, "__esModule", { value: true });
25+
exports.appInstance = void 0;
26+
const Hapi = __importStar(require("@hapi/hapi"));
27+
const attendanceSumRoutes_1 = __importDefault(require("./attendance-sum/attendanceSumRoutes"));
28+
const attendanceRoutes_1 = __importDefault(require("./attendance/attendanceRoutes"));
29+
const inventoryRoutes_1 = __importDefault(require("./inventory/inventoryRoutes"));
30+
const offeringRecordsRoutes_1 = __importDefault(require("./offering-records/offeringRecordsRoutes"));
31+
const teachingsRoutes_1 = __importDefault(require("./teachings/teachingsRoutes"));
32+
const titheRecordsRoutes_1 = __importDefault(require("./tithe-records/titheRecordsRoutes"));
33+
const usersRoutes_1 = __importDefault(require("./users/usersRoutes"));
34+
class App {
35+
constructor() {
36+
}
37+
// function to initialize the server after routes have been registered
38+
init() {
39+
// set up server
40+
this.theApp = Hapi.server({
41+
port: process.env.PORT || 3000,
42+
host: process.env.HOST || '0.0.0.0',
43+
});
44+
// register routes
45+
this.theApp.register([
46+
attendanceRoutes_1.default,
47+
attendanceSumRoutes_1.default,
48+
inventoryRoutes_1.default,
49+
offeringRecordsRoutes_1.default,
50+
teachingsRoutes_1.default,
51+
titheRecordsRoutes_1.default,
52+
usersRoutes_1.default
53+
]).then(() => {
54+
console.log("Route(s) have been registered");
55+
});
56+
// initialize app with routes
57+
this.theApp.initialize().then(() => {
58+
console.log("The app has been initialized");
59+
});
60+
}
61+
// Function to start the server for the main application or for tests
62+
start() {
63+
this.theApp.start().then(() => {
64+
console.log(`Server running at: ${this.theApp.info.uri}`);
65+
});
66+
}
67+
}
68+
// create singleton for use in main app or for tests.
69+
exports.appInstance = new App();

dist/attendance-sum/attendance-sum.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
var __importDefault = (this && this.__importDefault) || function (mod) {
12+
return (mod && mod.__esModule) ? mod : { "default": mod };
13+
};
14+
Object.defineProperty(exports, "__esModule", { value: true });
15+
exports.AttendanceSumController = void 0;
16+
const attendanceSumService_1 = require("./attendanceSumService");
17+
const boom_1 = __importDefault(require("@hapi/boom"));
18+
class AttendanceSumController {
19+
create(request, h) {
20+
return __awaiter(this, void 0, void 0, function* () {
21+
try {
22+
const requestBody = request.payload;
23+
const result = yield new attendanceSumService_1.AttendanceSumService().create(requestBody);
24+
return h.response(result).code(201);
25+
}
26+
catch (error) {
27+
request.log("error", error);
28+
return boom_1.default.badImplementation(JSON.stringify(error));
29+
}
30+
});
31+
}
32+
getAll(request, h) {
33+
return __awaiter(this, void 0, void 0, function* () {
34+
try {
35+
const result = yield new attendanceSumService_1.AttendanceSumService().getAll();
36+
return h.response(result).code(200);
37+
}
38+
catch (error) {
39+
request.log("error", error);
40+
return boom_1.default.badImplementation(JSON.stringify(error));
41+
}
42+
});
43+
}
44+
getById(request, h) {
45+
return __awaiter(this, void 0, void 0, function* () {
46+
try {
47+
const id = +request.params.id;
48+
const result = yield new attendanceSumService_1.AttendanceSumService().getById(id);
49+
return h.response(result).code(200);
50+
}
51+
catch (error) {
52+
request.log("error", error);
53+
return boom_1.default.badImplementation(JSON.stringify(error));
54+
}
55+
});
56+
}
57+
update(request, h) {
58+
return __awaiter(this, void 0, void 0, function* () {
59+
try {
60+
const id = +request.params.id;
61+
const requestBody = request.payload;
62+
const result = yield new attendanceSumService_1.AttendanceSumService().update(requestBody, id);
63+
return h.response(result).code(200);
64+
}
65+
catch (error) {
66+
request.log("error", error);
67+
return boom_1.default.badImplementation(JSON.stringify(error));
68+
}
69+
});
70+
}
71+
delete(request, h) {
72+
return __awaiter(this, void 0, void 0, function* () {
73+
try {
74+
const id = +request.params.id;
75+
const result = yield new attendanceSumService_1.AttendanceSumService().delete(id);
76+
return h.response(result).code(200);
77+
}
78+
catch (error) {
79+
request.log("error", error);
80+
return boom_1.default.badImplementation(JSON.stringify(error));
81+
}
82+
});
83+
}
84+
}
85+
exports.AttendanceSumController = AttendanceSumController;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
const attendanceSumController_1 = require("./attendanceSumController");
13+
// create instance of controller
14+
const controller = new attendanceSumController_1.AttendanceSumController();
15+
// configure the routes
16+
const attendanceSumRoutes = {
17+
name: "attendance-sum",
18+
register: (server) => __awaiter(void 0, void 0, void 0, function* () {
19+
server.route([
20+
{
21+
method: 'POST',
22+
path: '/attendance-sum',
23+
handler: controller.create
24+
},
25+
{
26+
method: 'GET',
27+
path: '/attendance-sum',
28+
handler: controller.getAll
29+
},
30+
{
31+
method: 'GET',
32+
path: '/attendance-sum/{id}',
33+
handler: controller.getById
34+
},
35+
{
36+
method: 'PUT',
37+
path: '/attendance-sum/{id}',
38+
handler: controller.update
39+
},
40+
{
41+
method: 'DELETE',
42+
path: '/attendance-sum/{id}',
43+
handler: controller.delete
44+
}
45+
]);
46+
})
47+
};
48+
exports.default = attendanceSumRoutes;
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
"use strict";
2+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4+
return new (P || (P = Promise))(function (resolve, reject) {
5+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8+
step((generator = generator.apply(thisArg, _arguments || [])).next());
9+
});
10+
};
11+
Object.defineProperty(exports, "__esModule", { value: true });
12+
exports.AttendanceSumService = void 0;
13+
const client_1 = require("@prisma/client");
14+
class AttendanceSumService {
15+
constructor() {
16+
this.prisma = new client_1.PrismaClient();
17+
}
18+
create(theDto) {
19+
return __awaiter(this, void 0, void 0, function* () {
20+
try {
21+
return yield this.prisma.attendanceSum.create({
22+
data: {
23+
eventName: theDto.eventName,
24+
date: theDto.date,
25+
totalChildren: theDto.totalChildren,
26+
totalFemale: theDto.totalFemale,
27+
totalMale: theDto.totalMale
28+
}
29+
});
30+
}
31+
catch (error) {
32+
console.log(error);
33+
}
34+
finally {
35+
yield this.prisma.$disconnect();
36+
}
37+
});
38+
}
39+
getById(id) {
40+
return __awaiter(this, void 0, void 0, function* () {
41+
try {
42+
return yield this.prisma.attendanceSum.findUnique({
43+
where: {
44+
id
45+
}
46+
});
47+
}
48+
catch (error) {
49+
console.log(error);
50+
}
51+
finally {
52+
yield this.prisma.$disconnect();
53+
}
54+
});
55+
}
56+
getAll() {
57+
return __awaiter(this, void 0, void 0, function* () {
58+
try {
59+
return yield this.prisma.attendanceSum.findMany();
60+
}
61+
catch (error) {
62+
console.log(error);
63+
}
64+
finally {
65+
yield this.prisma.$disconnect();
66+
}
67+
});
68+
}
69+
update(theDto, id) {
70+
return __awaiter(this, void 0, void 0, function* () {
71+
try {
72+
return yield this.prisma.attendanceSum.update({
73+
where: {
74+
id: id
75+
},
76+
data: Object.assign({}, theDto)
77+
});
78+
}
79+
catch (error) {
80+
console.log(error);
81+
}
82+
finally {
83+
yield this.prisma.$disconnect();
84+
}
85+
});
86+
}
87+
delete(id) {
88+
return __awaiter(this, void 0, void 0, function* () {
89+
try {
90+
return yield this.prisma.attendanceSum.delete({
91+
where: {
92+
id
93+
}
94+
});
95+
}
96+
catch (error) {
97+
console.log(error);
98+
}
99+
finally {
100+
yield this.prisma.$disconnect();
101+
}
102+
});
103+
}
104+
}
105+
exports.AttendanceSumService = AttendanceSumService;

dist/attendance/attendance.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
"use strict";
2+
Object.defineProperty(exports, "__esModule", { value: true });

0 commit comments

Comments
 (0)