Compare commits
No commits in common. 'ad738f2c61b82134c084bfeda85830938267bae8' and 'be0f01bc1a5d9991804318804ec072a18902998a' have entirely different histories.
ad738f2c61
...
be0f01bc1a
5 changed files with 0 additions and 83 deletions
@ -1,17 +0,0 @@ |
|||||||
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); |
|
||||||
} |
|
||||||
} |
|
@ -1,12 +0,0 @@ |
|||||||
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 {} |
|
@ -1,40 +0,0 @@ |
|||||||
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, |
|
||||||
); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
@ -1,12 +0,0 @@ |
|||||||
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