-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: categories crud was added, ticket creation was finished
- Loading branch information
Showing
21 changed files
with
352 additions
and
63 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/migrations/1634866392817-cascade-was-added-to-all-relationships.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.