Compare commits

...

2 Commits

  1. 17
      src/admin/admin.controller.ts
  2. 12
      src/admin/admin.module.ts
  3. 40
      src/admin/admin.service.ts
  4. 12
      src/admin/entities/admin.entity.ts
  5. 2
      src/app.module.ts

@ -0,0 +1,17 @@
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { AdminService } from './admin.service';
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('register')
async register(@Body() body:{email: string, password: string}):Promise<Admin>{
const {email, password} = body
return this.adminService.register(email, password);
}
}

@ -0,0 +1,12 @@
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],
})
export class AdminModule {}

@ -0,0 +1,40 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { Admin } from './entities/admin.entity';
@Injectable()
export class AdminService {
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);
}
if (!password) {
throw new HttpException('Password should be entered.', HttpStatus.BAD_REQUEST);
}
const existingAdmin = await this.adminModel.findOne({ where: { email } });
if (existingAdmin) {
throw new HttpException('Email is already registered.', HttpStatus.CONFLICT);
}
const admin = await this.adminModel.create({
email,
password,
role: 'admin',
});
return admin;
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException(
'An error occurred during registration.',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

@ -0,0 +1,12 @@
import { Model, Table, Column } from "sequelize-typescript";
@Table
export class Admin extends Model<Admin> {
@Column
email: string;
@Column
password: string;
@Column
role: string;
}

@ -9,6 +9,7 @@ import { ProductsModule } from './products/products.module';
import { CartModule } from './cart/cart.module';
import { WalletModule } from './wallet/wallet.module';
import { InvoiceModule } from './invoice/invoice.module';
import { AdminModule } from './admin/admin.module';
@Module({
imports: [
@ -21,6 +22,7 @@ import { InvoiceModule } from './invoice/invoice.module';
CartModule,
WalletModule,
InvoiceModule,
AdminModule,
],
controllers: [AppController],
providers: [AppService],

Loading…
Cancel
Save