parent
964867a3ab
commit
ad24c4ac05
@ -0,0 +1,13 @@ |
|||||||
|
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; |
||||||
|
|
||||||
|
@Entity() |
||||||
|
export class User { |
||||||
|
@PrimaryGeneratedColumn() |
||||||
|
id: number; |
||||||
|
|
||||||
|
@Column() |
||||||
|
name: string; |
||||||
|
|
||||||
|
@Column({ unique: true }) |
||||||
|
email: string; |
||||||
|
} |
@ -1,4 +1,33 @@ |
|||||||
import { Controller } from '@nestjs/common'; |
import { Body, Controller, Delete, Get, Param, Post, Put } from '@nestjs/common'; |
||||||
|
import { UsersService } from './users.service'; |
||||||
|
import { User } from './user.entity'; |
||||||
|
|
||||||
@Controller('users') |
@Controller('users') |
||||||
export class UsersController {} |
export class UsersController { |
||||||
|
constructor(private userService: UsersService) {} |
||||||
|
|
||||||
|
@Post() |
||||||
|
create(@Body() body: Partial<User>) { |
||||||
|
return this.userService.create(body); |
||||||
|
} |
||||||
|
|
||||||
|
@Get() |
||||||
|
findAll() { |
||||||
|
return this.userService.findAll(); |
||||||
|
} |
||||||
|
|
||||||
|
@Get(":id") |
||||||
|
findOne(@Param("id") id: string) { |
||||||
|
return this.userService.findOne(Number(id)); |
||||||
|
} |
||||||
|
|
||||||
|
@Put(":id") |
||||||
|
update(@Param("id") id: string, @Body() body: Partial<User>) { |
||||||
|
return this.userService.update(Number(id), body); |
||||||
|
} |
||||||
|
|
||||||
|
@Delete(":id") |
||||||
|
delete(@Param("id") id: string) { |
||||||
|
return this.userService.delete(Number(id)); |
||||||
|
} |
||||||
|
} |
||||||
|
@ -1,4 +1,36 @@ |
|||||||
import { Injectable } from '@nestjs/common'; |
import { Injectable, NotFoundException } from '@nestjs/common'; |
||||||
|
import { User } from './user.entity'; |
||||||
|
import { InjectRepository } from '@nestjs/typeorm'; |
||||||
|
import { Repository } from 'typeorm'; |
||||||
|
|
||||||
@Injectable() |
@Injectable() |
||||||
export class UsersService {} |
export class UsersService { |
||||||
|
constructor( @InjectRepository(User) private readonly userRepository: Repository<User>) {} |
||||||
|
|
||||||
|
async create(user: Partial<User>): Promise<User> { |
||||||
|
const newUser = this.userRepository.create(user); |
||||||
|
return this.userRepository.save(newUser); |
||||||
|
} |
||||||
|
|
||||||
|
async findAll(): Promise<User[]> { |
||||||
|
return this.userRepository.find(); |
||||||
|
} |
||||||
|
|
||||||
|
async findOne(id: number): Promise<User> { |
||||||
|
const user = await this.userRepository.findOneBy({ id }); |
||||||
|
if (!user) |
||||||
|
throw new NotFoundException(`User with id ${id} not found`); |
||||||
|
|
||||||
|
return user; |
||||||
|
} |
||||||
|
|
||||||
|
async update(id: number, update: Partial<User>): Promise<User> { |
||||||
|
const user = await this.findOne(id); |
||||||
|
Object.assign(user, update); |
||||||
|
return this.userRepository.save(user); |
||||||
|
} |
||||||
|
|
||||||
|
async delete(id: number): Promise<void> { |
||||||
|
await this.userRepository.delete(id); |
||||||
|
} |
||||||
|
} |
||||||
|
Loading…
Reference in new issue