Skip to content

Commit

Permalink
fixed #20 and added swagger
Browse files Browse the repository at this point in the history
  • Loading branch information
Shpendiiiii committed Jul 24, 2024
1 parent a967e5c commit 3e9ec2f
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 18 deletions.
4 changes: 4 additions & 0 deletions apps/like/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { Logger } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { setupSwagger } from '../../../libs/utils/swagger';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
const port = process.env.PORT || 3006;
if (process.env.NODE_ENV !== 'production') {
setupSwagger(app);
}
await app.listen(port);
Logger.log(`🚀 Application is running on: http://localhost:${port}`);
}
Expand Down
24 changes: 19 additions & 5 deletions apps/like/src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,30 @@ import {
Get,
Param,
ParseIntPipe,
ValidationPipe,
ValidationPipe
} from '@nestjs/common';
import { UserService } from './user.service';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';

@Controller('users')
@Controller('user')
@ApiTags('user')
export class UserController {
constructor(private userService: UserService) {}
constructor(private userService: UserService) {
}

@Get(':user_uuid/types/:type_uuid')
async handleGetUserLikes(@Param('user_uuid') user_uuid: string) {
return 'handleGetUserLikes';
@ApiOperation({ summary: 'Get user likes on type by user id'})
@ApiResponse({
status: 200,
description: 'Successfully retrieved user activities',
})
@ApiResponse({
status: 404,
description: 'Not Found',
})
async handleGetUserLikes(@Param('user_uuid') user_uuid: string, @Param('type_uuid') type_uuid: string) {
return this.userService.getUserLikesOnType(user_uuid, type_uuid);
}


}
9 changes: 9 additions & 0 deletions apps/like/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ import { PrismaService } from '../prisma/prisma.service';
export class UserService {
constructor(private prismaService: PrismaService) {}

async getUserLikesOnType(user_id_: string, type_id: string){
const user_id = Number(user_id_);
return await this.prismaService.like.findMany({
where: {user_id, type_id},
include: {
Type: true
}
})
}
// async createUser(createUserDto: CreateUserDto) {
// const existedUser = await prisma.user.findFirst({
// where: { id: createUserDto.id }
Expand Down
13 changes: 13 additions & 0 deletions libs/utils/swagger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { INestApplication } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

export function setupSwagger(app: INestApplication): void {
const options = new DocumentBuilder()
.setTitle('API')
.setVersion('2.0.0')
.addBearerAuth()
.build();

const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('docs', app, document);
}
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./apps/treetracker-like/test/jest-e2e.json"
"test:e2e": "jest --config ./apps/treetracker-like/test/jest-e2e.json",
"prisma:seed": "ts-node prisma/seed.ts"
},
"private": true,
"dependencies": {
Expand All @@ -25,6 +26,7 @@
"@nestjs/core": "^10.0.2",
"@nestjs/jwt": "^10.1.1",
"@nestjs/platform-express": "^10.0.2",
"@nestjs/swagger": "^7.4.0",
"@prisma/client": "^5.5.1",
"axios": "^1.5.1",
"bcrypt": "^5.1.1",
Expand Down
40 changes: 35 additions & 5 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
Expand All @@ -19,17 +18,48 @@ async function main() {
},
});

console.log({ type1, type2 });
// Get the ids of the created types
const treeType = await prisma.type.findUnique({
where: { name: 'tree' },
});

const speciesType = await prisma.type.findUnique({
where: { name: 'species' },
});

if (treeType && speciesType) {
// Create sample 'Like' entries
const like1 = await prisma.like.upsert({
where: { type_id_object_id_user_id: { type_id: treeType.id, object_id: 'tree1', user_id: 1 }},
update: {},
create: {
type_id: treeType.id,
object_id: 'tree1',
user_id: 1,
},
});

const like2 = await prisma.like.upsert({
where: { type_id_object_id_user_id: { type_id: speciesType.id, object_id: 'species1', user_id: 2 }},
update: {},
create: {
type_id: speciesType.id,
object_id: 'species1',
user_id: 2,
},
});

console.log({ type1, type2, like1, like2 });
} else {
console.error("Failed to retrieve the created types.");
}
}

// execute the main function
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
// close Prisma Client at the end
await prisma.$disconnect();
});

41 changes: 34 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,11 @@
semver "^7.3.5"
tar "^6.1.11"

"@microsoft/tsdoc@^0.15.0":
version "0.15.0"
resolved "https://registry.yarnpkg.com/@microsoft/tsdoc/-/tsdoc-0.15.0.tgz#f29a55df17cb6e87cfbabce33ff6a14a9f85076d"
integrity sha512-HZpPoABogPvjeJOdzCOSJsXeL/SMCBgBZMVC3X3d7YYp2gf31MfxhUoYUNwf1ERPJOnQc0wkFn9trqI6ZEdZuA==

"@nestjs/axios@^3.0.0":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@nestjs/axios/-/axios-3.0.2.tgz#0078c101a29fb46f5c566d68a4315fddabc083ed"
Expand Down Expand Up @@ -778,6 +783,11 @@
"@types/jsonwebtoken" "9.0.5"
jsonwebtoken "9.0.2"

"@nestjs/[email protected]":
version "2.0.5"
resolved "https://registry.yarnpkg.com/@nestjs/mapped-types/-/mapped-types-2.0.5.tgz#485d6b44e19779c98d04e52bd1d2bcc7001df0ea"
integrity sha512-bSJv4pd6EY99NX9CjBIyn4TVDoSit82DUZlL4I3bqNfy5Gt+gXTa86i3I/i0iIV9P4hntcGM5GyO+FhZAhxtyg==

"@nestjs/platform-express@^10.0.2":
version "10.3.10"
resolved "https://registry.yarnpkg.com/@nestjs/platform-express/-/platform-express-10.3.10.tgz#45fa006605913d5aaa31bf99073136ad639939b4"
Expand Down Expand Up @@ -810,6 +820,18 @@
jsonc-parser "3.2.0"
pluralize "8.0.0"

"@nestjs/swagger@^7.4.0":
version "7.4.0"
resolved "https://registry.yarnpkg.com/@nestjs/swagger/-/swagger-7.4.0.tgz#e61dbefdfc1d4011327a256896953c74e511c850"
integrity sha512-dCiwKkRxcR7dZs5jtrGspBAe/nqJd1AYzOBTzw9iCdbq3BGrLpwokelk6lFZPe4twpTsPQqzNKBwKzVbI6AR/g==
dependencies:
"@microsoft/tsdoc" "^0.15.0"
"@nestjs/mapped-types" "2.0.5"
js-yaml "4.1.0"
lodash "4.17.21"
path-to-regexp "3.2.0"
swagger-ui-dist "5.17.14"

"@nestjs/testing@^10.0.2":
version "10.3.10"
resolved "https://registry.yarnpkg.com/@nestjs/testing/-/testing-10.3.10.tgz#6ed3c821fddf868665cc5ddc8591ee6eaab8a916"
Expand Down Expand Up @@ -4713,6 +4735,13 @@ js-tokens@^4.0.0:
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499"
integrity sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==

[email protected], js-yaml@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602"
integrity sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==
dependencies:
argparse "^2.0.1"

js-yaml@^3.13.1:
version "3.14.1"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.14.1.tgz#dae812fdb3825fa306609a8717383c50c36a0537"
Expand All @@ -4721,13 +4750,6 @@ js-yaml@^3.13.1:
argparse "^1.0.7"
esprima "^4.0.0"

js-yaml@^4.1.0:
version "4.1.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602"
integrity sha512-wpxZs9NoxZaJESJGIZTyDEaYpl0FKSA+FB9aJiyemKhMwkxQg63h4T1KJgUGHpTqPDNRcmmYLugrRjJlBtWvRA==
dependencies:
argparse "^2.0.1"

[email protected]:
version "1.1.0"
resolved "https://registry.yarnpkg.com/jsbn/-/jsbn-1.1.0.tgz#b01307cb29b618a1ed26ec79e911f803c4da0040"
Expand Down Expand Up @@ -7176,6 +7198,11 @@ supports-preserve-symlinks-flag@^1.0.0:
resolved "https://registry.yarnpkg.com/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz#6eda4bd344a3c94aea376d4cc31bc77311039e09"
integrity sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==

[email protected]:
version "5.17.14"
resolved "https://registry.yarnpkg.com/swagger-ui-dist/-/swagger-ui-dist-5.17.14.tgz#e2c222e5bf9e15ccf80ec4bc08b4aaac09792fd6"
integrity sha512-CVbSfaLpstV65OnSjbXfVd6Sta3q3F7Cj/yYuvHMp1P90LztOLs6PfUnKEVAeiIVQt9u2SaPwv0LiH/OyMjHRw==

[email protected]:
version "4.0.0"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-4.0.0.tgz#5b425f192279e87f2f9b937ac8540d1984b39205"
Expand Down

0 comments on commit 3e9ec2f

Please sign in to comment.