parent
894ab52709
commit
435083a682
10 changed files with 181 additions and 29 deletions
@ -0,0 +1,62 @@ |
|||||||
|
'use strict'; |
||||||
|
|
||||||
|
/** @type {import('sequelize-cli').Migration} */ |
||||||
|
module.exports = { |
||||||
|
async up(queryInterface, Sequelize) { |
||||||
|
await queryInterface.createTable('Users', { |
||||||
|
id: { |
||||||
|
allowNull: false, |
||||||
|
autoIncrement: true, |
||||||
|
primaryKey: true, |
||||||
|
type: Sequelize.INTEGER |
||||||
|
}, |
||||||
|
email: { |
||||||
|
type: Sequelize.STRING, |
||||||
|
unique: true, |
||||||
|
allowNull: false |
||||||
|
}, |
||||||
|
password: { |
||||||
|
type: Sequelize.STRING, |
||||||
|
allowNull: false |
||||||
|
}, |
||||||
|
role: { |
||||||
|
type: Sequelize.STRING, |
||||||
|
defaultValue: 'user' |
||||||
|
}, |
||||||
|
firstName: { |
||||||
|
type: Sequelize.STRING, |
||||||
|
allowNull: false |
||||||
|
}, |
||||||
|
lastName: { |
||||||
|
type: Sequelize.STRING, |
||||||
|
allowNull: false |
||||||
|
}, |
||||||
|
username: { |
||||||
|
type: Sequelize.STRING, |
||||||
|
unique: true, |
||||||
|
allowNull: false |
||||||
|
}, |
||||||
|
phoneNumber: { |
||||||
|
type: Sequelize.STRING, |
||||||
|
unique: true, |
||||||
|
allowNull: false |
||||||
|
}, |
||||||
|
gender: { |
||||||
|
type: Sequelize.ENUM("male", "female"), |
||||||
|
allowNull: false |
||||||
|
}, |
||||||
|
createdAt: { |
||||||
|
allowNull: false, |
||||||
|
type: Sequelize.DATE |
||||||
|
}, |
||||||
|
updatedAt: { |
||||||
|
allowNull: false, |
||||||
|
type: Sequelize.DATE |
||||||
|
} |
||||||
|
}); |
||||||
|
}, |
||||||
|
|
||||||
|
async down(queryInterface, Sequelize) { |
||||||
|
await queryInterface.dropTable('Users'); |
||||||
|
} |
||||||
|
}; |
@ -0,0 +1,30 @@ |
|||||||
|
'use strict'; |
||||||
|
const { |
||||||
|
Model |
||||||
|
} = require('sequelize'); |
||||||
|
module.exports = (sequelize, DataTypes) => { |
||||||
|
class User extends Model { |
||||||
|
/** |
||||||
|
* Helper method for defining associations. |
||||||
|
* This method is not a part of Sequelize lifecycle. |
||||||
|
* The `models/index` file will call this method automatically. |
||||||
|
*/ |
||||||
|
static associate(models) { |
||||||
|
// define association here
|
||||||
|
} |
||||||
|
} |
||||||
|
User.init({ |
||||||
|
email: DataTypes.STRING, |
||||||
|
password: DataTypes.STRING, |
||||||
|
role: DataTypes.STRING, |
||||||
|
firstName: DataTypes.STRING, |
||||||
|
lastName: DataTypes.STRING, |
||||||
|
username: DataTypes.STRING, |
||||||
|
phoneNumber: DataTypes.STRING, |
||||||
|
gender: DataTypes.ENUM |
||||||
|
}, { |
||||||
|
sequelize, |
||||||
|
modelName: 'User', |
||||||
|
}); |
||||||
|
return User; |
||||||
|
}; |
@ -1,14 +1,16 @@ |
|||||||
import { Table, Model, Column, BelongsTo, ForeignKey } from "sequelize-typescript"; |
import { Table, Model, Column, BelongsTo, ForeignKey } from "sequelize-typescript"; |
||||||
import { User } from "../../users/entities/user.entity"; |
import { User } from "../../users/entities/user.entity"; |
||||||
import { Product } from "../../products/entities/product.entity"; |
import { Product } from "../../products/entities/product.entity";
|
||||||
|
|
||||||
@Table |
@Table |
||||||
export class Invoice extends Model<Invoice> { |
export class Invoice extends Model<Invoice> { |
||||||
@ForeignKey(() => User) |
@ForeignKey(() => User) |
||||||
@Column |
@Column |
||||||
userId: number; |
userId: number; |
||||||
@BelongsTo(() => User) |
|
||||||
|
@BelongsTo(() => User, { onDelete: 'CASCADE' })
|
||||||
user: User; |
user: User; |
||||||
|
|
||||||
@Column |
@Column |
||||||
totalAmount: number; |
totalAmount: number; |
||||||
} |
} |
||||||
|
@ -1,8 +1,10 @@ |
|||||||
import { NestFactory } from '@nestjs/core'; |
import { NestFactory } from '@nestjs/core'; |
||||||
import { AppModule } from './app.module'; |
import { AppModule } from './app.module'; |
||||||
|
import { ValidationPipe } from '@nestjs/common'; |
||||||
|
|
||||||
async function bootstrap() { |
async function bootstrap() { |
||||||
const app = await NestFactory.create(AppModule); |
const app = await NestFactory.create(AppModule); |
||||||
|
app.useGlobalPipes(new ValidationPipe()); |
||||||
await app.listen(process.env.PORT ?? 3000); |
await app.listen(process.env.PORT ?? 3000); |
||||||
} |
} |
||||||
bootstrap(); |
bootstrap(); |
||||||
|
@ -0,0 +1,29 @@ |
|||||||
|
import { IsString, IsEmail, IsEnum, IsNotEmpty, IsOptional, Matches } from "class-validator"; |
||||||
|
|
||||||
|
export class CreateUserDto { |
||||||
|
@IsEmail({}, { message: "Invalid email format" }) |
||||||
|
email: string; |
||||||
|
|
||||||
|
@IsString() |
||||||
|
@IsNotEmpty({ message: "Password is required" }) |
||||||
|
password: string; |
||||||
|
|
||||||
|
@IsString() |
||||||
|
@IsNotEmpty({ message: "First name is required" }) |
||||||
|
firstName: string; |
||||||
|
|
||||||
|
@IsString() |
||||||
|
@IsNotEmpty({ message: "Last name is required" }) |
||||||
|
lastName: string; |
||||||
|
|
||||||
|
@IsString() |
||||||
|
@IsNotEmpty({ message: "Username is required" }) |
||||||
|
username: string; |
||||||
|
|
||||||
|
@IsString() |
||||||
|
@Matches(/^[0-9]{11}$/, { message: "Phone number must be 10 digits" }) |
||||||
|
phoneNumber: string; |
||||||
|
|
||||||
|
@IsEnum(["male", "female"], { message: "Gender must be 'male' or 'female'" }) |
||||||
|
gender: string; |
||||||
|
} |
@ -1,11 +1,31 @@ |
|||||||
import { Column, Table, Model } from "sequelize-typescript"; |
import { Column, Table, Model, DataType } from "sequelize-typescript"; |
||||||
|
|
||||||
@Table |
@Table |
||||||
export class User extends Model<User> { |
export class User extends Model<User> { |
||||||
@Column({ unique: true }) |
@Column({ unique: true }) |
||||||
email: string; |
email: string; |
||||||
|
|
||||||
@Column |
@Column |
||||||
password: string; |
password: string; |
||||||
|
|
||||||
@Column({ defaultValue: "user" }) |
@Column({ defaultValue: "user" }) |
||||||
role: string; |
role: string; |
||||||
|
|
||||||
|
@Column |
||||||
|
firstName: string; |
||||||
|
|
||||||
|
@Column |
||||||
|
lastName: string; |
||||||
|
|
||||||
|
@Column({ unique: true }) |
||||||
|
username: string; |
||||||
|
|
||||||
|
@Column({ unique: true }) |
||||||
|
phoneNumber: string; |
||||||
|
|
||||||
|
@Column({ |
||||||
|
type: DataType.ENUM("male", "female"), |
||||||
|
allowNull: false, |
||||||
|
}) |
||||||
|
gender: string; |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue