Skip to content

Commit

Permalink
feat: categories crud was added, ticket creation was finished
Browse files Browse the repository at this point in the history
  • Loading branch information
GiovannaK committed Oct 22, 2021
1 parent c40cf41 commit fa2cbd9
Show file tree
Hide file tree
Showing 21 changed files with 352 additions and 63 deletions.
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"@nestjs/config": "^1.0.2",
"@nestjs/core": "^8.0.0",
"@nestjs/jwt": "^8.0.0",
"@nestjs/mapped-types": "^1.0.0",
"@nestjs/passport": "^8.0.1",
"@nestjs/platform-express": "^8.0.0",
"@nestjs/typeorm": "^8.0.2",
Expand Down
19 changes: 15 additions & 4 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Body, Controller, Get, Post, Res, UseGuards } from '@nestjs/common';
import {
Body,
Controller,
Get,
Param,
Post,
Res,
UseGuards,
} from '@nestjs/common';
import { Response } from 'express';
import { Role } from 'src/user/entities/role/role.enum';
import { AuthDto } from './auth.dto';
Expand All @@ -12,9 +20,12 @@ import { RolesGuard } from './roles.guard';
export class AuthController {
constructor(private readonly authService: AuthService) {}

@Post('authorize')
async validateUser(@Body() authDto: AuthDto, @Res() response: Response) {
const getToken = await this.authService.validateUser(authDto);
@Get(':authToken')
async validateUser(
@Param('authToken') authToken: string,
@Res() response: Response,
) {
const getToken = await this.authService.validateUser(authToken);

const convertCookieSecureEnvVariable = () => {
if (process.env.COOKIE_SECURE.toLocaleLowerCase() == 'true') {
Expand Down
7 changes: 4 additions & 3 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ export class AuthService {
return updatedUser;
}

async validateUser(authDto: AuthDto) {
const { loginToken } = authDto;
const user = await this.userRepository.findOne({ where: { loginToken } });
async validateUser(authToken: string) {
const user = await this.userRepository.findOne({
where: { loginToken: authToken },
});

if (!user) {
throw new NotFoundException('Cannot found user');
Expand Down
45 changes: 43 additions & 2 deletions src/category/category.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,45 @@
import { Controller } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Put,
} from '@nestjs/common';
import { CategoryService } from './category.service';
import { CreateCategoryDto } from './dto/create-category.dto';
import { UpdateCategoryDto } from './dto/update-category.dto';

@Controller('category')
export class CategoryController {}
export class CategoryController {
constructor(private readonly categoryService: CategoryService) {}

@Post()
async createCategory(@Body() createCategoryDto: CreateCategoryDto) {
return await this.categoryService.createCategory(createCategoryDto);
}

@Get(':id')
async findCategoryById(@Param('id') id: string) {
return await this.categoryService.findCategoryById(id);
}

@Get()
async findAllCategories() {
return await this.categoryService.findAllCategories();
}

@Put(':id')
async updateCategory(
@Param('id') id: string,
@Body() updateCategoryDto: UpdateCategoryDto,
) {
return await this.categoryService.updateCategory(id, updateCategoryDto);
}

@Delete(':id')
async deleteCategory(@Param('id') id: string) {
return await this.categoryService.deleteCategory(id);
}
}
5 changes: 4 additions & 1 deletion src/category/category.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { CategoryController } from './category.controller';
import { CategoryService } from './category.service';
import { Category } from './entities/category.entity';

@Module({
controllers: [CategoryController],
providers: [CategoryService]
providers: [CategoryService],
imports: [TypeOrmModule.forFeature([Category])],
})
export class CategoryModule {}
72 changes: 70 additions & 2 deletions src/category/category.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,72 @@
import { Injectable } from '@nestjs/common';
import {
Injectable,
InternalServerErrorException,
NotFoundException,
} from '@nestjs/common';

import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Category } from './entities/category.entity';
import { CreateCategoryDto } from './dto/create-category.dto';
import { UpdateCategoryDto } from './dto/update-category.dto';

@Injectable()
export class CategoryService {}
export class CategoryService {
constructor(
@InjectRepository(Category)
private readonly categoryRepository: Repository<Category>,
) {}

async createCategory(createCategoryDto: CreateCategoryDto) {
const category = await this.categoryRepository.create({
...createCategoryDto,
});

if (!category) {
throw new InternalServerErrorException('Cannot create category');
}

return this.categoryRepository.save(category);
}

async findCategoryById(id: string) {
const category = await this.categoryRepository.findOne(id);

if (!category) {
throw new NotFoundException('Category not found, try a different id');
}

return category;
}

async findAllCategories() {
const categories = await this.categoryRepository.find();
return categories;
}

async updateCategory(id: string, updateCategoryDto: UpdateCategoryDto) {
const category = await this.findCategoryById(id);

await this.categoryRepository.update(category, {
...updateCategoryDto,
});

const updatedCategory = this.categoryRepository.create({
...category,
...updateCategoryDto,
});

await this.categoryRepository.save(updatedCategory);

return updatedCategory;
}

async deleteCategory(id: string) {
const category = await this.findCategoryById(id);

if (!category) {
new InternalServerErrorException('Cannot delete category');
}
return this.categoryRepository.remove(category);
}
}
7 changes: 7 additions & 0 deletions src/category/dto/create-category.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { IsString, Length } from 'class-validator';

export class CreateCategoryDto {
@IsString()
@Length(1, 200)
readonly title: string;
}
4 changes: 4 additions & 0 deletions src/category/dto/update-category.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateCategoryDto } from './create-category.dto';

export class UpdateCategoryDto extends PartialType(CreateCategoryDto) {}
3 changes: 3 additions & 0 deletions src/category/entities/category.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export class Category {
@OneToMany(
() => Ticket,
(categoryTicket: Ticket) => categoryTicket.ticketCategory,
{
cascade: true,
},
)
categoriesTickets: Ticket[];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {MigrationInterface, QueryRunner} from "typeorm";

export class cascadeWasAddedToAllRelationships1634866392817 implements MigrationInterface {
name = 'cascadeWasAddedToAllRelationships1634866392817'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "order" DROP CONSTRAINT "FK_88f2277956386ca318b787d6d73"`);
await queryRunner.query(`ALTER TABLE "order" DROP CONSTRAINT "FK_34c2e7cb66c553a5ceb127a072e"`);
await queryRunner.query(`ALTER TABLE "order" DROP CONSTRAINT "FK_92bb963a31edbbc5fc5e53ce87f"`);
await queryRunner.query(`ALTER TABLE "ticket" DROP CONSTRAINT "FK_aa44ac36bbefb899205fbf58a41"`);
await queryRunner.query(`ALTER TABLE "ticket" DROP CONSTRAINT "FK_9e406f239bb3d886067cc5fb6e3"`);
await queryRunner.query(`ALTER TABLE "ticket" ADD "imageUrl" character varying`);
await queryRunner.query(`ALTER TABLE "order" ADD CONSTRAINT "FK_88f2277956386ca318b787d6d73" FOREIGN KEY ("ticketsOrderId") REFERENCES "ticket"("id") ON DELETE CASCADE ON UPDATE CASCADE`);
await queryRunner.query(`ALTER TABLE "order" ADD CONSTRAINT "FK_34c2e7cb66c553a5ceb127a072e" FOREIGN KEY ("sellerIdId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE`);
await queryRunner.query(`ALTER TABLE "order" ADD CONSTRAINT "FK_92bb963a31edbbc5fc5e53ce87f" FOREIGN KEY ("customerIdId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE`);
await queryRunner.query(`ALTER TABLE "ticket" ADD CONSTRAINT "FK_aa44ac36bbefb899205fbf58a41" FOREIGN KEY ("sellerIdId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE CASCADE`);
await queryRunner.query(`ALTER TABLE "ticket" ADD CONSTRAINT "FK_9e406f239bb3d886067cc5fb6e3" FOREIGN KEY ("ticketCategoryId") REFERENCES "category"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "ticket" DROP CONSTRAINT "FK_9e406f239bb3d886067cc5fb6e3"`);
await queryRunner.query(`ALTER TABLE "ticket" DROP CONSTRAINT "FK_aa44ac36bbefb899205fbf58a41"`);
await queryRunner.query(`ALTER TABLE "order" DROP CONSTRAINT "FK_92bb963a31edbbc5fc5e53ce87f"`);
await queryRunner.query(`ALTER TABLE "order" DROP CONSTRAINT "FK_34c2e7cb66c553a5ceb127a072e"`);
await queryRunner.query(`ALTER TABLE "order" DROP CONSTRAINT "FK_88f2277956386ca318b787d6d73"`);
await queryRunner.query(`ALTER TABLE "ticket" DROP COLUMN "imageUrl"`);
await queryRunner.query(`ALTER TABLE "ticket" ADD CONSTRAINT "FK_9e406f239bb3d886067cc5fb6e3" FOREIGN KEY ("ticketCategoryId") REFERENCES "category"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "ticket" ADD CONSTRAINT "FK_aa44ac36bbefb899205fbf58a41" FOREIGN KEY ("sellerIdId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "order" ADD CONSTRAINT "FK_92bb963a31edbbc5fc5e53ce87f" FOREIGN KEY ("customerIdId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "order" ADD CONSTRAINT "FK_34c2e7cb66c553a5ceb127a072e" FOREIGN KEY ("sellerIdId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "order" ADD CONSTRAINT "FK_88f2277956386ca318b787d6d73" FOREIGN KEY ("ticketsOrderId") REFERENCES "ticket"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
}

}
2 changes: 2 additions & 0 deletions src/stripe/stripe.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ export class StripeService {
stripeAccountId: account.id,
});

await this.userRepository.save(updatedSeller);

return {
user: updatedSeller,
account,
Expand Down
68 changes: 68 additions & 0 deletions src/ticket/dto/create-ticket.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {
IsBoolean,
IsDate,
IsInt,
IsNotEmpty,
IsNumber,
isNumber,
IsString,
Length,
} from 'class-validator';
import { Category } from 'src/category/entities/category.entity';
import { isFloat64Array } from 'util/types';

export class CreateTicketDto {
@IsString()
@Length(1, 200)
readonly title: string;

@IsString()
@Length(1, 5000)
readonly description: string;

@IsNotEmpty()
@IsString()
readonly date: Date;

@IsNotEmpty()
@IsString()
readonly hour: string;

@IsBoolean()
readonly isActive: boolean;

@IsBoolean()
readonly hasVariation: boolean;

@IsBoolean()
readonly isOnline: boolean;

@IsNumber()
readonly priceStandard: number;

@IsNumber()
readonly pricePremium: number;

@IsNumber()
readonly price: number;

@IsString()
readonly link?: string;

@IsString()
readonly address: string;

readonly imageUrl: string | undefined;

readonly latitude: number | undefined;

readonly longitude: number | undefined;

@IsInt()
readonly quantity: number;

readonly sellerId?: string | undefined;

@IsNotEmpty()
readonly category: Category;
}
15 changes: 12 additions & 3 deletions src/ticket/entities/order.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ export class Order {
@Column({ nullable: true, type: 'float' })
total: number;

@ManyToOne(() => Ticket, (ticketOrder: Ticket) => ticketOrder.orderTicket)
@ManyToOne(() => Ticket, (ticketOrder: Ticket) => ticketOrder.orderTicket, {
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
})
ticketsOrder: Ticket;

@ManyToOne(() => User, (sellerId: User) => sellerId.tickets)
@ManyToOne(() => User, (sellerId: User) => sellerId.tickets, {
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
})
sellerId: User;

@ManyToOne(() => User, (customerId: User) => customerId.order)
@ManyToOne(() => User, (customerId: User) => customerId.order, {
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
})
customerId: User;

@CreateDateColumn()
Expand Down
Loading

0 comments on commit fa2cbd9

Please sign in to comment.