Implement register functionality for admin in admin module

master
nicekid1 2 months ago
parent 2c5af3a06c
commit ad738f2c61
  1. 35
      src/admin/admin.controller.ts
  2. 3
      src/admin/admin.module.ts
  3. 48
      src/admin/admin.service.ts

@ -1,34 +1,17 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { AdminService } from './admin.service'; import { AdminService } from './admin.service';
import { CreateAdminDto } from './dto/create-admin.dto'; import { Injectable, UnauthorizedException } from '@nestjs/common';
import { UpdateAdminDto } from './dto/update-admin.dto'; import { InjectModel } from '@nestjs/sequelize';
import { Admin } from './entities/admin.entity';
import * as bcrypt from 'bcrypt';
import { JwtService } from '@nestjs/jwt';
@Controller('admin') @Controller('admin')
export class AdminController { export class AdminController {
constructor(private readonly adminService: AdminService) {} constructor(private readonly adminService: AdminService) {}
@Post('register')
@Post() async register(@Body() body:{email: string, password: string}):Promise<Admin>{
create(@Body() createAdminDto: CreateAdminDto) { const {email, password} = body
return this.adminService.create(createAdminDto); return this.adminService.register(email, password);
}
@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);
} }
} }

@ -1,8 +1,11 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { AdminService } from './admin.service'; import { AdminService } from './admin.service';
import { AdminController } from './admin.controller'; import { AdminController } from './admin.controller';
import { SequelizeModule } from '@nestjs/sequelize';
import { Admin } from './entities/admin.entity';
@Module({ @Module({
imports:[SequelizeModule.forFeature([Admin])],
controllers: [AdminController], controllers: [AdminController],
providers: [AdminService], providers: [AdminService],
}) })

@ -1,26 +1,40 @@
import { Injectable } from '@nestjs/common'; import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { CreateAdminDto } from './dto/create-admin.dto'; import { InjectModel } from '@nestjs/sequelize';
import { UpdateAdminDto } from './dto/update-admin.dto'; import { Admin } from './entities/admin.entity';
@Injectable() @Injectable()
export class AdminService { export class AdminService {
create(createAdminDto: CreateAdminDto) { constructor(@InjectModel(Admin) private readonly adminModel: typeof Admin){}
return 'This action adds a new admin'; async register(email: string, password: string): Promise<Admin> {
} try {
if (!email) {
throw new HttpException('Email should be entered.', HttpStatus.BAD_REQUEST);
}
findAll() { if (!password) {
return `This action returns all admin`; throw new HttpException('Password should be entered.', HttpStatus.BAD_REQUEST);
} }
findOne(id: number) { const existingAdmin = await this.adminModel.findOne({ where: { email } });
return `This action returns a #${id} admin`; if (existingAdmin) {
} throw new HttpException('Email is already registered.', HttpStatus.CONFLICT);
}
update(id: number, updateAdminDto: UpdateAdminDto) { const admin = await this.adminModel.create({
return `This action updates a #${id} admin`; email,
} password,
role: 'admin',
});
remove(id: number) { return admin;
return `This action removes a #${id} admin`; } catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
'An error occurred during registration.',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
} }
} }

Loading…
Cancel
Save