parent
2c5af3a06c
commit
ad738f2c61
3 changed files with 43 additions and 43 deletions
@ -1,34 +1,17 @@ |
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; |
||||
import { AdminService } from './admin.service'; |
||||
import { CreateAdminDto } from './dto/create-admin.dto'; |
||||
import { UpdateAdminDto } from './dto/update-admin.dto'; |
||||
import { Injectable, UnauthorizedException } from '@nestjs/common'; |
||||
import { InjectModel } from '@nestjs/sequelize'; |
||||
import { Admin } from './entities/admin.entity'; |
||||
import * as bcrypt from 'bcrypt'; |
||||
import { JwtService } from '@nestjs/jwt'; |
||||
|
||||
@Controller('admin') |
||||
export class AdminController { |
||||
constructor(private readonly adminService: AdminService) {} |
||||
|
||||
@Post() |
||||
create(@Body() createAdminDto: CreateAdminDto) { |
||||
return this.adminService.create(createAdminDto); |
||||
} |
||||
|
||||
@Get() |
||||
findAll() { |
||||
return this.adminService.findAll(); |
||||
} |
||||
|
||||
@Get(':id') |
||||
findOne(@Param('id') id: string) { |
||||
return this.adminService.findOne(+id); |
||||
} |
||||
|
||||
@Patch(':id') |
||||
update(@Param('id') id: string, @Body() updateAdminDto: UpdateAdminDto) { |
||||
return this.adminService.update(+id, updateAdminDto); |
||||
} |
||||
|
||||
@Delete(':id') |
||||
remove(@Param('id') id: string) { |
||||
return this.adminService.remove(+id); |
||||
@Post('register') |
||||
async register(@Body() body:{email: string, password: string}):Promise<Admin>{ |
||||
const {email, password} = body |
||||
return this.adminService.register(email, password); |
||||
} |
||||
} |
||||
|
@ -1,26 +1,40 @@ |
||||
import { Injectable } from '@nestjs/common'; |
||||
import { CreateAdminDto } from './dto/create-admin.dto'; |
||||
import { UpdateAdminDto } from './dto/update-admin.dto'; |
||||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; |
||||
import { InjectModel } from '@nestjs/sequelize'; |
||||
import { Admin } from './entities/admin.entity'; |
||||
|
||||
@Injectable() |
||||
export class AdminService { |
||||
create(createAdminDto: CreateAdminDto) { |
||||
return 'This action adds a new admin'; |
||||
} |
||||
constructor(@InjectModel(Admin) private readonly adminModel: typeof Admin){} |
||||
async register(email: string, password: string): Promise<Admin> { |
||||
try { |
||||
if (!email) { |
||||
throw new HttpException('Email should be entered.', HttpStatus.BAD_REQUEST); |
||||
} |
||||
|
||||
findAll() { |
||||
return `This action returns all admin`; |
||||
} |
||||
if (!password) { |
||||
throw new HttpException('Password should be entered.', HttpStatus.BAD_REQUEST); |
||||
} |
||||
|
||||
findOne(id: number) { |
||||
return `This action returns a #${id} admin`; |
||||
} |
||||
const existingAdmin = await this.adminModel.findOne({ where: { email } }); |
||||
if (existingAdmin) { |
||||
throw new HttpException('Email is already registered.', HttpStatus.CONFLICT); |
||||
} |
||||
|
||||
update(id: number, updateAdminDto: UpdateAdminDto) { |
||||
return `This action updates a #${id} admin`; |
||||
} |
||||
const admin = await this.adminModel.create({ |
||||
email, |
||||
password, |
||||
role: 'admin', |
||||
}); |
||||
|
||||
remove(id: number) { |
||||
return `This action removes a #${id} admin`; |
||||
return admin; |
||||
} catch (error) { |
||||
if (error instanceof HttpException) { |
||||
throw error;
|
||||
} |
||||
throw new HttpException( |
||||
'An error occurred during registration.', |
||||
HttpStatus.INTERNAL_SERVER_ERROR, |
||||
); |
||||
} |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue