-
Notifications
You must be signed in to change notification settings - Fork 97
Expand file tree
/
Copy pathHadith.ts
More file actions
120 lines (105 loc) · 3.29 KB
/
Hadith.ts
File metadata and controls
120 lines (105 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { Request, Response } from 'express'
import Handler from './BaseHandler'
import Hadith from '../Models/Hadith'
class HadithHandler extends Handler {
public index(req: Request, res: Response): void {
try {
const data = Hadith.available()
this.sendHttp(res, {
code: 200,
message: `${data.length} books sent.`,
data
})
} catch (err) {
this.handleHttpError(req, res, err as Error)
}
}
public getByNumber(req: Request, res: Response): void {
const { name, number } = req.params
try {
const parsedNumber = parseInt(number, 10)
const hadithName = Hadith.beautyName(name)
if (Number.isNaN(parsedNumber)) this.setHttpError({
code: 400,
message: 'Hadith number must be a number.'
})
const hadith = Hadith.getByName(name)
if (!hadith) this.setHttpError({
code: 404,
message: `${hadithName} not available.`
})
const contents = Hadith.getByNumber(hadith, parsedNumber)
if (!contents)
this.setHttpError({
code: 404,
message: `HR. ${hadithName} No. ${parsedNumber} not available.`,
});
this.sendHttp(res, {
code: 200,
message: `HR. ${hadithName} No. ${parsedNumber} sent.`,
data: {
name: `HR. ${hadithName}`,
id: name,
available: hadith.length,
contents
}
})
} catch (err) {
this.handleHttpError(req, res, err as Error)
}
}
public getByName(req: Request, res: Response): void {
const { name } = req.params
const { range = '' } = req.query
const [from, to] = (range as string).split('-')
.map(num => {
const parsed = parseInt(num, 10)
return !Number.isNaN(parsed) ? parsed : null
})
try {
const hadithName = Hadith.beautyName(name)
if (from && to) {
if (from < 1) this.setHttpError({
code: 400,
message: `Start range shouldn\'t be below 1`
})
const requestedRange = (to - from) + 1
if (requestedRange > 300) this.setHttpError({
code: 400,
message: `Reached max number of hadiths requested. Max: 300, but you request ${requestedRange}.`
})
const hadith = Hadith.getByName(name)
if (!hadith) this.setHttpError({
code: 404,
message: `${hadithName} not available.`
})
const data = Hadith.getByNumberRange(hadith, from, to)
const total = hadith.length
const totalRequested = data.length
if (to > total) this.setHttpError({
code: 400,
message: `Out of range hadith on ${hadithName}`
})
this.sendHttp(res, {
code: 200,
message: `${totalRequested} hadiths requested.`,
data: {
name: `HR. ${hadithName}`,
id: name,
available: total,
requested: totalRequested,
hadiths: data
}
})
} else {
this.setHttpError({
code: 400,
message: 'Format range doesn\'t match with {number-number}. Example: /books/bukhari?range=1-50'
})
}
} catch (err) {
this.handleHttpError(req, res, err as Error)
}
}
}
export default new HadithHandler()