Skip to content

Lession06 #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/pet/controllers/admin/manage-pet-category.controller.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PetCategoryInjectionKey } from './../../providers/pet-category.provider';
import {
Body,
Controller,
Expand All @@ -7,13 +8,15 @@ import {
Post,
Redirect,
Render,
Inject,
} from '@nestjs/common';
import { CreatePetCategoryDto } from 'src/pet/dtos/pet-dto';
import { plainToInstance } from 'class-transformer';
import { validate, ValidationError } from 'class-validator';
import { FormDataRequest } from 'nestjs-form-data';
import { PetCategory } from 'src/pet/models/pet-category.model';
import { Response } from 'express';
import { PetCategoryRepository } from 'src/pet/repositories/pet-category.repository';

const transformError = (error: ValidationError) => {
const { property, constraints } = error;
Expand All @@ -24,10 +27,16 @@ const transformError = (error: ValidationError) => {
};
@Controller('admin/pet-categories')
export class ManagePetCategoryController {
constructor(
private petCategoryRepository: PetCategoryRepository,
@Inject(PetCategoryInjectionKey)
private defaultPetCategoryRepository: typeof PetCategory,
) {}
@Get('')
@Render('pet/admin/manage-pet-category/list')
async getList() {
const petCategories = await PetCategory.findAll();
// const petCategories = await this.petCategoryRepository.findAll();
const petCategories = await this.defaultPetCategoryRepository.findAll();
return {
petCategories,
};
Expand Down
3 changes: 3 additions & 0 deletions src/pet/pet.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { ManagePetController } from './controllers/admin/manage-pet.controller';
import { ManagePetCategoryController } from './controllers/admin/manage-pet-category.controller';
import { ManagePetAttributeController } from './controllers/admin/manage-pet-attribute.controller';
import { NestjsFormDataModule } from 'nestjs-form-data';
import { PetCategoryRepository } from './repositories/pet-category.repository';
import { PetCategoryProvider } from './providers/pet-category.provider';
@Module({
imports: [NestjsFormDataModule],
controllers: [
Expand All @@ -14,5 +16,6 @@ import { NestjsFormDataModule } from 'nestjs-form-data';
ManagePetCategoryController,
ManagePetAttributeController,
],
providers: [PetCategoryRepository, PetCategoryProvider],
})
export class PetModule {}
7 changes: 7 additions & 0 deletions src/pet/providers/pet-category.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { PetCategory } from '../models/pet-category.model';

export const PetCategoryInjectionKey = 'Pet_Category';
export const PetCategoryProvider = {
provide: PetCategoryInjectionKey,
useValue: PetCategory,
};
9 changes: 9 additions & 0 deletions src/pet/repositories/pet-category.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Injectable } from '@nestjs/common';
import { PetCategory } from '../models/pet-category.model';

@Injectable()
export class PetCategoryRepository {
findAll() {
return PetCategory.findAll();
}
}