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 { 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,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],
})

@ -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…
Cancel
Save