feat(back): backend example CRUD generated

This commit is contained in:
2024-07-24 00:22:40 +02:00
parent 94ce4f548e
commit 017995656e
9 changed files with 165 additions and 2 deletions

View File

@@ -5,14 +5,18 @@ import { AuthModule } from './auth/auth.module';
import { UsersModule } from './users/users.module';
import { ThrottlerModule } from '@nestjs/throttler';
import { TypeOrmModule } from '@nestjs/typeorm';
import { User } from './users/entities/user.entity';
import { ConfigModule } from '@nestjs/config';
import { ExampleModule } from './example/example.module';
import { User } from './users/entities/user.entity';
import { Example } from './example/entities/example.entity';
@Module({
imports: [
ConfigModule.forRoot(),
AuthModule,
UsersModule,
ExampleModule,
ThrottlerModule.forRoot([
{
ttl: 10000,
@@ -26,7 +30,7 @@ import { ConfigModule } from '@nestjs/config';
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_MAIN,
entities: [User],
entities: [User, Example],
synchronize: false,
connectTimeout: 20000,
}),

View File

@@ -0,0 +1 @@
export class CreateExampleDto {}

View File

@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateExampleDto } from './create-example.dto';
export class UpdateExampleDto extends PartialType(CreateExampleDto) {}

View File

@@ -0,0 +1,19 @@
import { Column, Entity, Generated, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class Example {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column()
description: string;
@Column()
image: string;
@Column()
created_at: string;
}

View File

@@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ExampleController } from './example.controller';
import { ExampleService } from './example.service';
describe('ExampleController', () => {
let controller: ExampleController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [ExampleController],
providers: [ExampleService],
}).compile();
controller = module.get<ExampleController>(ExampleController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

View File

@@ -0,0 +1,49 @@
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
} from '@nestjs/common';
import { ExampleService } from './example.service';
import { CreateExampleDto } from './dto/create-example.dto';
import { UpdateExampleDto } from './dto/update-example.dto';
import { Auth } from 'src/auth/auth.decorator';
import { Role } from 'src/users/roles/role.enum';
@Controller('example')
export class ExampleController {
constructor(private readonly exampleService: ExampleService) {}
@Auth(Role.Admin)
@Post()
create(@Body() createExampleDto: CreateExampleDto) {
return this.exampleService.create(createExampleDto);
}
@Auth(Role.Public)
@Get()
findAll() {
return this.exampleService.findAll();
}
@Auth(Role.User)
@Get(':id')
findOne(@Param('id') id: string) {
return this.exampleService.findOne(+id);
}
@Auth(Role.Admin)
@Patch(':id')
update(@Param('id') id: string, @Body() updateExampleDto: UpdateExampleDto) {
return this.exampleService.update(+id, updateExampleDto);
}
@Auth(Role.Admin)
@Delete(':id')
remove(@Param('id') id: string) {
return this.exampleService.remove(+id);
}
}

View File

@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { ExampleService } from './example.service';
import { ExampleController } from './example.controller';
import { Example } from './entities/example.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [TypeOrmModule.forFeature([Example])],
controllers: [ExampleController],
providers: [ExampleService],
})
export class ExampleModule {}

View File

@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ExampleService } from './example.service';
describe('ExampleService', () => {
let service: ExampleService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ExampleService],
}).compile();
service = module.get<ExampleService>(ExampleService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

View File

@@ -0,0 +1,36 @@
import { Injectable } from '@nestjs/common';
import { CreateExampleDto } from './dto/create-example.dto';
import { UpdateExampleDto } from './dto/update-example.dto';
import { Example } from './entities/example.entity';
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
import { DataSource, Repository } from 'typeorm';
@Injectable()
export class ExampleService {
constructor(
@InjectRepository(Example)
private exampleRepository: Repository<Example>,
@InjectDataSource()
private dataSource: DataSource,
) {}
create(createExampleDto: CreateExampleDto) {
return this.exampleRepository.create(createExampleDto);
}
findAll() {
return this.dataSource.query('select * from example');
}
findOne(id: number) {
return this.exampleRepository.findOneBy({ id });
}
update(id: number, updateExampleDto: UpdateExampleDto) {
return this.exampleRepository.update({ id }, updateExampleDto);
}
remove(id: number) {
return this.exampleRepository.delete(id);
}
}