Compare commits
2 Commits
be0f01bc1a
...
ad738f2c61
Author | SHA1 | Date |
---|---|---|
|
ad738f2c61 | 2 months ago |
|
2c5af3a06c | 2 months ago |
5 changed files with 83 additions and 0 deletions
@ -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; |
||||
} |
Loading…
Reference in new issue