File tree Expand file tree Collapse file tree 2 files changed +32
-404
lines changed Expand file tree Collapse file tree 2 files changed +32
-404
lines changed Original file line number Diff line number Diff line change @@ -12,12 +12,42 @@ app.use(cors());
12
12
router . get ( "/users/all" , async ( req , res ) => {
13
13
try {
14
14
console . log ( `${ new Date ( ) . toISOString ( ) } - All users request hit!` ) ;
15
+ let { page, limit } = req . query ;
15
16
16
- const users = await prisma . user . findMany ( { } ) ;
17
+ if ( ! page && ! limit ) {
18
+ page = 1 ;
19
+ limit = 5 ;
20
+ }
21
+
22
+ if ( page <= 0 ) {
23
+ return res . status ( HTTP_STATUS . UNPROCESSABLE_ENTITY ) . send ( {
24
+ success : false ,
25
+ message : "Page value must be 1 or more" ,
26
+ data : null ,
27
+ } ) ;
28
+ }
29
+
30
+ if ( limit <= 0 ) {
31
+ return res . status ( HTTP_STATUS . UNPROCESSABLE_ENTITY ) . send ( {
32
+ success : false ,
33
+ message : "Limit value must be 1 or more" ,
34
+ data : null ,
35
+ } ) ;
36
+ }
37
+
38
+ const users = await prisma . user . findMany ( {
39
+ skip : Number ( page - 1 ) * Number ( limit ) ,
40
+ take : Number ( limit ) ,
41
+ } ) ;
42
+
43
+ const total = await prisma . user . count ( ) ;
17
44
return res . status ( HTTP_STATUS . OK ) . send ( {
18
45
success : true ,
19
46
message : "Successfully received all users" ,
20
- data : users ,
47
+ data : {
48
+ users : users ,
49
+ total : total ,
50
+ } ,
21
51
} ) ;
22
52
} catch ( error ) {
23
53
console . log ( error ) ;
You can’t perform that action at this time.
0 commit comments