From b5ced781c95aff58594573d91b60f9014ad51597 Mon Sep 17 00:00:00 2001 From: Joe Arndt Date: Sun, 8 Feb 2026 15:21:47 -0600 Subject: [PATCH] added merchants resource --- .../Merchants/LOC DELETE Merchant.bru | 11 +++ .../Merchants/LOC GET Merchant By ID.bru | 15 ++++ .../Merchants/LOC GET Merchants.bru | 11 +++ .../Merchants/LOC POST Merchant.bru | 17 ++++ .../Merchants/LOC PUT Merchant.bru | 18 +++++ bruno/Common Cents/Merchants/folder.bru | 8 ++ common-cents.db | Bin 0 -> 12288 bytes src/app.module.ts | 15 ++-- src/merchants/dto/create-merchant.dto.ts | 4 +- src/merchants/dto/update-merchant.dto.ts | 8 +- src/merchants/entities/merchant.entity.ts | 11 ++- src/merchants/merchant-data.service.ts | 33 ++++++++ src/merchants/merchants.controller.spec.ts | 20 ----- src/merchants/merchants.controller.ts | 73 ++++++++++++++---- src/merchants/merchants.module.ts | 3 +- src/merchants/merchants.service.spec.ts | 18 ----- src/merchants/merchants.service.ts | 29 ++++--- 17 files changed, 214 insertions(+), 80 deletions(-) create mode 100644 bruno/Common Cents/Merchants/LOC DELETE Merchant.bru create mode 100644 bruno/Common Cents/Merchants/LOC GET Merchant By ID.bru create mode 100644 bruno/Common Cents/Merchants/LOC GET Merchants.bru create mode 100644 bruno/Common Cents/Merchants/LOC POST Merchant.bru create mode 100644 bruno/Common Cents/Merchants/LOC PUT Merchant.bru create mode 100644 bruno/Common Cents/Merchants/folder.bru create mode 100644 common-cents.db create mode 100644 src/merchants/merchant-data.service.ts delete mode 100644 src/merchants/merchants.controller.spec.ts delete mode 100644 src/merchants/merchants.service.spec.ts 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 0000000000000000000000000000000000000000..448549dcbfbdb1db8817c33bc7dac91538a9cf70 GIT binary patch literal 12288 zcmeI%O-sWt7zgmAmH7e^ZysbAdU1l)b?w&m=A1o<+tk@2o<_Q+IM@rTRlNE|Jop9t z7+yX4Q9POo#fvacg7AOfY4S8FA-`MF%Ztt^lRTNF6OnP7Y!FJxA?JjUl1g1=5ma@S zibAz{y(mg#KYdJ8US+}LvJ zk#yAKSdOLojo; + + 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); } }