-
Notifications
You must be signed in to change notification settings - Fork 908
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
743 additions
and
279 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { JsonController, Get, Post, Put, Param, Delete, Body, OnUndefined, Authorized } from 'routing-controllers'; | ||
import { PetService } from '../services/PetService'; | ||
import { Pet } from '../models/Pet'; | ||
import { PetNotFoundError } from '../errors/PetNotFoundError'; | ||
|
||
|
||
@Authorized() | ||
@JsonController('/pets') | ||
export class PetController { | ||
|
||
constructor( | ||
private petService: PetService | ||
) { } | ||
|
||
@Get() | ||
public find(): Promise<Pet[]> { | ||
return this.petService.find(); | ||
} | ||
|
||
@Get('/:id') | ||
@OnUndefined(PetNotFoundError) | ||
public one( @Param('id') id: string): Promise<Pet | undefined> { | ||
return this.petService.findOne(id); | ||
} | ||
|
||
@Post() | ||
public create( @Body() pet: Pet): Promise<Pet> { | ||
return this.petService.create(pet); | ||
} | ||
|
||
@Put('/:id') | ||
public update( @Param('id') id: string, @Body() pet: Pet): Promise<Pet> { | ||
return this.petService.update(id, pet); | ||
} | ||
|
||
@Delete('/:id') | ||
public delete( @Param('id') id: string): Promise<void> { | ||
return this.petService.delete(id); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { HttpError } from 'routing-controllers'; | ||
|
||
export class PetNotFoundError extends HttpError { | ||
constructor() { | ||
super(404, 'Pet not found!'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne } from 'typeorm'; | ||
import { IsNotEmpty } from 'class-validator'; | ||
import { User } from './User'; | ||
|
||
|
||
@Entity() | ||
export class Pet { | ||
|
||
@PrimaryGeneratedColumn('uuid') | ||
public id: string; | ||
|
||
@IsNotEmpty() | ||
@Column() | ||
public name: string; | ||
|
||
@IsNotEmpty() | ||
@Column() | ||
public age: number; | ||
|
||
@ManyToOne(type => User, user => user.pets) | ||
public user: User; | ||
|
||
public toString(): string { | ||
return `${this.name}`; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Repository, EntityRepository } from 'typeorm'; | ||
import { Pet } from '../models/Pet'; | ||
|
||
@EntityRepository(Pet) | ||
export class PetRepository extends Repository<Pet> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { Service } from 'typedi'; | ||
import { OrmRepository } from 'typeorm-typedi-extensions'; | ||
import { PetRepository } from '../repositories/PetRepository'; | ||
import { Pet } from '../models/Pet'; | ||
import { events } from '../subscribers/events'; | ||
import { EventDispatcher, EventDispatcherInterface } from '../../decorators/EventDispatcher'; | ||
import { Logger, LoggerInterface } from '../../decorators/Logger'; | ||
|
||
|
||
@Service() | ||
export class PetService { | ||
|
||
constructor( | ||
@OrmRepository() private petRepository: PetRepository, | ||
@EventDispatcher() private eventDispatcher: EventDispatcherInterface, | ||
@Logger(__filename) private log: LoggerInterface | ||
) { } | ||
|
||
public find(): Promise<Pet[]> { | ||
this.log.info('Find all pets'); | ||
return this.petRepository.find(); | ||
} | ||
|
||
public findOne(id: string): Promise<Pet | undefined> { | ||
this.log.info('Find all pets'); | ||
return this.petRepository.findOne({ id }); | ||
} | ||
|
||
public async create(pet: Pet): Promise<Pet> { | ||
this.log.info('Create a new pet => ', pet.toString()); | ||
const newPet = await this.petRepository.save(pet); | ||
this.eventDispatcher.dispatch(events.pet.created, newPet); | ||
return newPet; | ||
} | ||
|
||
public update(id: string, pet: Pet): Promise<Pet> { | ||
this.log.info('Update a pet'); | ||
pet.id = id; | ||
return this.petRepository.save(pet); | ||
} | ||
|
||
public delete(id: string): Promise<void> { | ||
this.log.info('Delete a pet'); | ||
return this.petRepository.removeById(id); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,7 @@ export const events = { | |
user: { | ||
created: 'onUserCreate', | ||
}, | ||
pet: { | ||
created: 'onPetCreate', | ||
}, | ||
}; |
Oops, something went wrong.