Skip to content

Commit efcaf80

Browse files
committed
feat: added return only specific fields in findAll() and findOne() functions
1 parent ccdf0c2 commit efcaf80

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

src/modules/users/users.service.ts

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,24 @@ export class UsersService {
1212
}
1313

1414
async findAll() {
15-
return this.prisma.user.findMany({ where: { deletedAt: null } });
15+
return this.prisma.user.findMany({
16+
where: { deletedAt: null },
17+
select: {
18+
name: true,
19+
function: true,
20+
},
21+
});
1622
}
1723

1824
async findOne(id: number) {
19-
const user = await this.prisma.user.findUnique({ where: { id } });
25+
const user = await this.prisma.user.findUnique({
26+
where: { id },
27+
select: {
28+
name: true,
29+
function: true,
30+
posts: { where: { isPublished: true }, select: { title: true } },
31+
},
32+
});
2033

2134
if (!user) {
2235
throw new NotFoundException('User not found');
@@ -26,12 +39,21 @@ export class UsersService {
2639
}
2740

2841
async update(id: number, data: UpdateUserDto) {
29-
await this.findOne(id);
42+
const findUser = await this.findOne(id);
43+
44+
if (!findUser) {
45+
throw new NotFoundException('User not found');
46+
}
47+
3048
return this.prisma.user.update({ where: { id }, data });
3149
}
3250

3351
async remove(id: number) {
34-
await this.findOne(id);
52+
const findUser = await this.findOne(id);
53+
54+
if (!findUser) {
55+
throw new NotFoundException('User not found');
56+
}
3557

3658
await this.prisma.user.delete({ where: { id } });
3759

0 commit comments

Comments
 (0)