diff --git a/bruno/Common Cents/Merchants/LOC DELETE Merchant.bru b/bruno/Common Cents/Merchants/LOC DELETE Merchant.bru new file mode 100644 index 0000000..cc0158a --- /dev/null +++ b/bruno/Common Cents/Merchants/LOC DELETE Merchant.bru @@ -0,0 +1,11 @@ +meta { + name: LOC DELETE Merchant + type: http + seq: 5 +} + +delete { + url: {{localBaseUrl}}/merchants/cbf30070-9ff7-419f-a567-f7d145be445b + body: none + auth: inherit +} diff --git a/bruno/Common Cents/Merchants/LOC GET Merchant By ID.bru b/bruno/Common Cents/Merchants/LOC GET Merchant By ID.bru new file mode 100644 index 0000000..55caec8 --- /dev/null +++ b/bruno/Common Cents/Merchants/LOC GET Merchant By ID.bru @@ -0,0 +1,15 @@ +meta { + name: LOC GET Merchant By ID + type: http + seq: 2 +} + +get { + url: {{localBaseUrl}}/merchants/{{merchantId}} + body: none + auth: inherit +} + +vars:pre-request { + merchantId: 1d6d2842-b271-489b-bd93-e3ceaee5a139 +} diff --git a/bruno/Common Cents/Merchants/LOC GET Merchants.bru b/bruno/Common Cents/Merchants/LOC GET Merchants.bru new file mode 100644 index 0000000..35da33e --- /dev/null +++ b/bruno/Common Cents/Merchants/LOC GET Merchants.bru @@ -0,0 +1,11 @@ +meta { + name: LOC GET Merchants + type: http + seq: 1 +} + +get { + url: {{localBaseUrl}}/merchants + body: none + auth: inherit +} diff --git a/bruno/Common Cents/Merchants/LOC POST Merchant.bru b/bruno/Common Cents/Merchants/LOC POST Merchant.bru new file mode 100644 index 0000000..e78d086 --- /dev/null +++ b/bruno/Common Cents/Merchants/LOC POST Merchant.bru @@ -0,0 +1,17 @@ +meta { + name: LOC POST Merchant + type: http + seq: 3 +} + +post { + url: {{localBaseUrl}}/merchants + body: json + auth: inherit +} + +body:json { + { + "name": "Walmart" + } +} diff --git a/bruno/Common Cents/Merchants/LOC PUT Merchant.bru b/bruno/Common Cents/Merchants/LOC PUT Merchant.bru new file mode 100644 index 0000000..e91a0e8 --- /dev/null +++ b/bruno/Common Cents/Merchants/LOC PUT Merchant.bru @@ -0,0 +1,18 @@ +meta { + name: LOC PUT Merchant + type: http + seq: 4 +} + +put { + url: {{localBaseUrl}}/merchants + body: json + auth: inherit +} + +body:json { + { + "id": "1d6d2842-b271-489b-bd93-e3ceaee5a139", + "name": "Walmart" + } +} diff --git a/bruno/Common Cents/Merchants/folder.bru b/bruno/Common Cents/Merchants/folder.bru new file mode 100644 index 0000000..caac236 --- /dev/null +++ b/bruno/Common Cents/Merchants/folder.bru @@ -0,0 +1,8 @@ +meta { + name: Merchants + seq: 3 +} + +auth { + mode: inherit +} diff --git a/common-cents.db b/common-cents.db new file mode 100644 index 0000000..448549d Binary files /dev/null and b/common-cents.db differ diff --git a/src/app.module.ts b/src/app.module.ts index 0f6f586..8f4b4f2 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -2,22 +2,19 @@ import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { MerchantsModule } from './merchants/merchants.module'; import { TypeOrmModule } from '@nestjs/typeorm'; - -// const config: SqliteConnectionOptions = { -// type: "sqlite", -// database: "../db", -// entities: [Post], -// synchronize: true -// }; +import { Merchant } from './merchants/entities/merchant.entity'; @Module({ imports: [ MerchantsModule, TypeOrmModule.forRoot({ - + type: 'sqlite', + database: 'common-cents.db', + entities: [Merchant], + // synchronize: true // schema:sync }) ], controllers: [AppController], providers: [] }) -export class AppModule {} +export class AppModule { } diff --git a/src/merchants/dto/create-merchant.dto.ts b/src/merchants/dto/create-merchant.dto.ts index 8d810ea..3d1c09d 100644 --- a/src/merchants/dto/create-merchant.dto.ts +++ b/src/merchants/dto/create-merchant.dto.ts @@ -1 +1,3 @@ -export class CreateMerchantDto {} +export class CreateMerchantDto { + name: string; +} diff --git a/src/merchants/dto/update-merchant.dto.ts b/src/merchants/dto/update-merchant.dto.ts index 7109433..b02185f 100644 --- a/src/merchants/dto/update-merchant.dto.ts +++ b/src/merchants/dto/update-merchant.dto.ts @@ -1,4 +1,4 @@ -import { PartialType } from '@nestjs/mapped-types'; -import { CreateMerchantDto } from './create-merchant.dto'; - -export class UpdateMerchantDto extends PartialType(CreateMerchantDto) {} +export class UpdateMerchantDto { + id: string; + name: string; +} diff --git a/src/merchants/entities/merchant.entity.ts b/src/merchants/entities/merchant.entity.ts index 32a6f34..0a5297f 100644 --- a/src/merchants/entities/merchant.entity.ts +++ b/src/merchants/entities/merchant.entity.ts @@ -1 +1,10 @@ -export class Merchant {} +import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; + +@Entity() +export class Merchant { + @PrimaryGeneratedColumn('uuid') + id: string; + + @Column() + name: string; +} diff --git a/src/merchants/merchant-data.service.ts b/src/merchants/merchant-data.service.ts new file mode 100644 index 0000000..ad61ae0 --- /dev/null +++ b/src/merchants/merchant-data.service.ts @@ -0,0 +1,33 @@ +import { Injectable } from '@nestjs/common'; +import { DataSource, Repository } from 'typeorm'; +import { Merchant } from './entities/merchant.entity'; +import { UpdateMerchantDto } from './dto/update-merchant.dto'; + +@Injectable() +export class MerchantDataService { + private merchants: Repository; + + public constructor(private dataSource: DataSource) { + this.merchants = this.dataSource.getRepository(Merchant); + } + + public async getAllMerchants(): Promise { + return this.merchants.find(); + } + + public async getMerchantById(id: string): Promise { + return await this.merchants.findOneBy({ id }); + } + + public async createMerchant(name: string): Promise { + return await this.merchants.save({ name }); + } + + public async updateMerchant(updateMerchant: UpdateMerchantDto): Promise { + return await this.merchants.save(updateMerchant); + } + + public async deleteMerchant(id: string): Promise { + await this.merchants.delete({ id }); + } +} \ No newline at end of file diff --git a/src/merchants/merchants.controller.spec.ts b/src/merchants/merchants.controller.spec.ts deleted file mode 100644 index aa10fa8..0000000 --- a/src/merchants/merchants.controller.spec.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { MerchantsController } from './merchants.controller'; -import { MerchantsService } from './merchants.service'; - -describe('MerchantsController', () => { - let controller: MerchantsController; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - controllers: [MerchantsController], - providers: [MerchantsService] - }).compile(); - - controller = module.get(MerchantsController); - }); - - it('should be defined', () => { - expect(controller).toBeDefined(); - }); -}); diff --git a/src/merchants/merchants.controller.ts b/src/merchants/merchants.controller.ts index 3de2630..339b9c2 100644 --- a/src/merchants/merchants.controller.ts +++ b/src/merchants/merchants.controller.ts @@ -1,34 +1,75 @@ -import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; +import { + Controller, + Get, + Post, + Put, + Delete, + Body, + Param, + HttpCode, + HttpStatus, + NotFoundException, + BadRequestException, + InternalServerErrorException +} from '@nestjs/common'; import { MerchantsService } from './merchants.service'; import { CreateMerchantDto } from './dto/create-merchant.dto'; import { UpdateMerchantDto } from './dto/update-merchant.dto'; +import { Merchant } from './entities/merchant.entity'; @Controller('merchants') export class MerchantsController { - constructor(private readonly merchantsService: MerchantsService) {} - - @Post() - create(@Body() createMerchantDto: CreateMerchantDto) { - return this.merchantsService.create(createMerchantDto); - } + public constructor(private readonly merchantsService: MerchantsService) { } @Get() - findAll() { - return this.merchantsService.findAll(); + @HttpCode(HttpStatus.OK) + public async findAll(): Promise { + return await this.merchantsService.findAll(); } @Get(':id') - findOne(@Param('id') id: string) { - return this.merchantsService.findOne(+id); + @HttpCode(HttpStatus.OK) + public async findOne(@Param('id') id: string): Promise { + if (!id) { + throw new BadRequestException('No ID provided.'); + } + + try { + return await this.merchantsService.findById(id); + } + catch (error) { + throw new NotFoundException(error); + } } - @Patch(':id') - update(@Param('id') id: string, @Body() updateMerchantDto: UpdateMerchantDto) { - return this.merchantsService.update(+id, updateMerchantDto); + @Post() + @HttpCode(HttpStatus.CREATED) + public async create(@Body() newMerchant: CreateMerchantDto): Promise { + if (!newMerchant.name) { + throw new BadRequestException('Merchant name cannot be empty.'); + } + + try { + return await this.merchantsService.create(newMerchant); + } + catch (error) { + throw new InternalServerErrorException(error); + } + } + + @Put() + @HttpCode(HttpStatus.OK) + public async update(@Body() updateMerchant: UpdateMerchantDto) { + if (!updateMerchant.id) { + return new BadRequestException('Merchant ID cannot be empty.'); + } + + return this.merchantsService.update(updateMerchant); } @Delete(':id') - remove(@Param('id') id: string) { - return this.merchantsService.remove(+id); + @HttpCode(HttpStatus.NO_CONTENT) + public async remove(@Param('id') id: string): Promise { + return this.merchantsService.remove(id); } } diff --git a/src/merchants/merchants.module.ts b/src/merchants/merchants.module.ts index c93eb11..9ce6372 100644 --- a/src/merchants/merchants.module.ts +++ b/src/merchants/merchants.module.ts @@ -1,9 +1,10 @@ import { Module } from '@nestjs/common'; import { MerchantsService } from './merchants.service'; import { MerchantsController } from './merchants.controller'; +import { MerchantDataService } from './merchant-data.service'; @Module({ controllers: [MerchantsController], - providers: [MerchantsService] + providers: [MerchantsService, MerchantDataService] }) export class MerchantsModule {} diff --git a/src/merchants/merchants.service.spec.ts b/src/merchants/merchants.service.spec.ts deleted file mode 100644 index 1034ff1..0000000 --- a/src/merchants/merchants.service.spec.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { Test, TestingModule } from '@nestjs/testing'; -import { MerchantsService } from './merchants.service'; - -describe('MerchantsService', () => { - let service: MerchantsService; - - beforeEach(async () => { - const module: TestingModule = await Test.createTestingModule({ - providers: [MerchantsService] - }).compile(); - - service = module.get(MerchantsService); - }); - - it('should be defined', () => { - expect(service).toBeDefined(); - }); -}); diff --git a/src/merchants/merchants.service.ts b/src/merchants/merchants.service.ts index 0548f69..c85ef46 100644 --- a/src/merchants/merchants.service.ts +++ b/src/merchants/merchants.service.ts @@ -1,26 +1,35 @@ import { Injectable } from '@nestjs/common'; import { CreateMerchantDto } from './dto/create-merchant.dto'; import { UpdateMerchantDto } from './dto/update-merchant.dto'; +import { MerchantDataService } from './merchant-data.service'; +import { Merchant } from './entities/merchant.entity'; @Injectable() export class MerchantsService { - create(createMerchantDto: CreateMerchantDto) { - return 'This action adds a new merchant'; + public constructor(private merchantDataService: MerchantDataService) { } + + public async findAll(): Promise { + return await this.merchantDataService.getAllMerchants(); } - findAll() { - return `This action returns all merchants`; + public async findById(id: string): Promise { + const merchant = await this.merchantDataService.getMerchantById(id); + if (!merchant) { + throw new Error('Merchant not found.'); + } + + return merchant; } - findOne(id: number) { - return `This action returns a #${id} merchant`; + public async create(newMerchant: CreateMerchantDto): Promise { + return await this.merchantDataService.createMerchant(newMerchant.name); } - update(id: number, updateMerchantDto: UpdateMerchantDto) { - return `This action updates a #${id} merchant`; + public async update(updateMerchant: UpdateMerchantDto): Promise { + return await this.merchantDataService.updateMerchant(updateMerchant); } - remove(id: number) { - return `This action removes a #${id} merchant`; + public async remove(id: string): Promise { + await this.merchantDataService.deleteMerchant(id); } }