diff --git a/src/admin/admin.controller.ts b/src/admin/admin.controller.ts index 367bac5..075bbe2 100644 --- a/src/admin/admin.controller.ts +++ b/src/admin/admin.controller.ts @@ -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{ + const {email, password} = body + return this.adminService.register(email, password); } } diff --git a/src/admin/admin.module.ts b/src/admin/admin.module.ts index 2926ee8..8f141d0 100644 --- a/src/admin/admin.module.ts +++ b/src/admin/admin.module.ts @@ -1,8 +1,11 @@ import { Module } from '@nestjs/common'; import { AdminService } from './admin.service'; import { AdminController } from './admin.controller'; +import { SequelizeModule } from '@nestjs/sequelize'; +import { Admin } from './entities/admin.entity'; @Module({ + imports:[SequelizeModule.forFeature([Admin])], controllers: [AdminController], providers: [AdminService], }) diff --git a/src/admin/admin.service.ts b/src/admin/admin.service.ts index f4dbcf1..7340136 100644 --- a/src/admin/admin.service.ts +++ b/src/admin/admin.service.ts @@ -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 { + 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, + ); + } } }