added merchants resource
This commit is contained in:
parent
d7ad5828af
commit
b5ced781c9
17 changed files with 214 additions and 80 deletions
11
bruno/Common Cents/Merchants/LOC DELETE Merchant.bru
Normal file
11
bruno/Common Cents/Merchants/LOC DELETE Merchant.bru
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
15
bruno/Common Cents/Merchants/LOC GET Merchant By ID.bru
Normal file
15
bruno/Common Cents/Merchants/LOC GET Merchant By ID.bru
Normal file
|
|
@ -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
|
||||||
|
}
|
||||||
11
bruno/Common Cents/Merchants/LOC GET Merchants.bru
Normal file
11
bruno/Common Cents/Merchants/LOC GET Merchants.bru
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
meta {
|
||||||
|
name: LOC GET Merchants
|
||||||
|
type: http
|
||||||
|
seq: 1
|
||||||
|
}
|
||||||
|
|
||||||
|
get {
|
||||||
|
url: {{localBaseUrl}}/merchants
|
||||||
|
body: none
|
||||||
|
auth: inherit
|
||||||
|
}
|
||||||
17
bruno/Common Cents/Merchants/LOC POST Merchant.bru
Normal file
17
bruno/Common Cents/Merchants/LOC POST Merchant.bru
Normal file
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
18
bruno/Common Cents/Merchants/LOC PUT Merchant.bru
Normal file
18
bruno/Common Cents/Merchants/LOC PUT Merchant.bru
Normal file
|
|
@ -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"
|
||||||
|
}
|
||||||
|
}
|
||||||
8
bruno/Common Cents/Merchants/folder.bru
Normal file
8
bruno/Common Cents/Merchants/folder.bru
Normal file
|
|
@ -0,0 +1,8 @@
|
||||||
|
meta {
|
||||||
|
name: Merchants
|
||||||
|
seq: 3
|
||||||
|
}
|
||||||
|
|
||||||
|
auth {
|
||||||
|
mode: inherit
|
||||||
|
}
|
||||||
BIN
common-cents.db
Normal file
BIN
common-cents.db
Normal file
Binary file not shown.
|
|
@ -2,22 +2,19 @@ import { Module } from '@nestjs/common';
|
||||||
import { AppController } from './app.controller';
|
import { AppController } from './app.controller';
|
||||||
import { MerchantsModule } from './merchants/merchants.module';
|
import { MerchantsModule } from './merchants/merchants.module';
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { Merchant } from './merchants/entities/merchant.entity';
|
||||||
// const config: SqliteConnectionOptions = {
|
|
||||||
// type: "sqlite",
|
|
||||||
// database: "../db",
|
|
||||||
// entities: [Post],
|
|
||||||
// synchronize: true
|
|
||||||
// };
|
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
MerchantsModule,
|
MerchantsModule,
|
||||||
TypeOrmModule.forRoot({
|
TypeOrmModule.forRoot({
|
||||||
|
type: 'sqlite',
|
||||||
|
database: 'common-cents.db',
|
||||||
|
entities: [Merchant],
|
||||||
|
// synchronize: true // schema:sync
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
controllers: [AppController],
|
controllers: [AppController],
|
||||||
providers: []
|
providers: []
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule { }
|
||||||
|
|
|
||||||
|
|
@ -1 +1,3 @@
|
||||||
export class CreateMerchantDto {}
|
export class CreateMerchantDto {
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { PartialType } from '@nestjs/mapped-types';
|
export class UpdateMerchantDto {
|
||||||
import { CreateMerchantDto } from './create-merchant.dto';
|
id: string;
|
||||||
|
name: string;
|
||||||
export class UpdateMerchantDto extends PartialType(CreateMerchantDto) {}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,10 @@
|
||||||
export class Merchant {}
|
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
export class Merchant {
|
||||||
|
@PrimaryGeneratedColumn('uuid')
|
||||||
|
id: string;
|
||||||
|
|
||||||
|
@Column()
|
||||||
|
name: string;
|
||||||
|
}
|
||||||
|
|
|
||||||
33
src/merchants/merchant-data.service.ts
Normal file
33
src/merchants/merchant-data.service.ts
Normal file
|
|
@ -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<Merchant>;
|
||||||
|
|
||||||
|
public constructor(private dataSource: DataSource) {
|
||||||
|
this.merchants = this.dataSource.getRepository(Merchant);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getAllMerchants(): Promise<Merchant[]> {
|
||||||
|
return this.merchants.find();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getMerchantById(id: string): Promise<Merchant | null> {
|
||||||
|
return await this.merchants.findOneBy({ id });
|
||||||
|
}
|
||||||
|
|
||||||
|
public async createMerchant(name: string): Promise<Merchant> {
|
||||||
|
return await this.merchants.save({ name });
|
||||||
|
}
|
||||||
|
|
||||||
|
public async updateMerchant(updateMerchant: UpdateMerchantDto): Promise<Merchant> {
|
||||||
|
return await this.merchants.save(updateMerchant);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async deleteMerchant(id: string): Promise<void> {
|
||||||
|
await this.merchants.delete({ id });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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>(MerchantsController);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(controller).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -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 { MerchantsService } from './merchants.service';
|
||||||
import { CreateMerchantDto } from './dto/create-merchant.dto';
|
import { CreateMerchantDto } from './dto/create-merchant.dto';
|
||||||
import { UpdateMerchantDto } from './dto/update-merchant.dto';
|
import { UpdateMerchantDto } from './dto/update-merchant.dto';
|
||||||
|
import { Merchant } from './entities/merchant.entity';
|
||||||
|
|
||||||
@Controller('merchants')
|
@Controller('merchants')
|
||||||
export class MerchantsController {
|
export class MerchantsController {
|
||||||
constructor(private readonly merchantsService: MerchantsService) {}
|
public constructor(private readonly merchantsService: MerchantsService) { }
|
||||||
|
|
||||||
@Post()
|
|
||||||
create(@Body() createMerchantDto: CreateMerchantDto) {
|
|
||||||
return this.merchantsService.create(createMerchantDto);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
findAll() {
|
@HttpCode(HttpStatus.OK)
|
||||||
return this.merchantsService.findAll();
|
public async findAll(): Promise<Merchant[]> {
|
||||||
|
return await this.merchantsService.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get(':id')
|
@Get(':id')
|
||||||
findOne(@Param('id') id: string) {
|
@HttpCode(HttpStatus.OK)
|
||||||
return this.merchantsService.findOne(+id);
|
public async findOne(@Param('id') id: string): Promise<Merchant> {
|
||||||
|
if (!id) {
|
||||||
|
throw new BadRequestException('No ID provided.');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await this.merchantsService.findById(id);
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
throw new NotFoundException(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Patch(':id')
|
@Post()
|
||||||
update(@Param('id') id: string, @Body() updateMerchantDto: UpdateMerchantDto) {
|
@HttpCode(HttpStatus.CREATED)
|
||||||
return this.merchantsService.update(+id, updateMerchantDto);
|
public async create(@Body() newMerchant: CreateMerchantDto): Promise<Merchant> {
|
||||||
|
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')
|
@Delete(':id')
|
||||||
remove(@Param('id') id: string) {
|
@HttpCode(HttpStatus.NO_CONTENT)
|
||||||
return this.merchantsService.remove(+id);
|
public async remove(@Param('id') id: string): Promise<void> {
|
||||||
|
return this.merchantsService.remove(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,10 @@
|
||||||
import { Module } from '@nestjs/common';
|
import { Module } from '@nestjs/common';
|
||||||
import { MerchantsService } from './merchants.service';
|
import { MerchantsService } from './merchants.service';
|
||||||
import { MerchantsController } from './merchants.controller';
|
import { MerchantsController } from './merchants.controller';
|
||||||
|
import { MerchantDataService } from './merchant-data.service';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
controllers: [MerchantsController],
|
controllers: [MerchantsController],
|
||||||
providers: [MerchantsService]
|
providers: [MerchantsService, MerchantDataService]
|
||||||
})
|
})
|
||||||
export class MerchantsModule {}
|
export class MerchantsModule {}
|
||||||
|
|
|
||||||
|
|
@ -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>(MerchantsService);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should be defined', () => {
|
|
||||||
expect(service).toBeDefined();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,26 +1,35 @@
|
||||||
import { Injectable } from '@nestjs/common';
|
import { Injectable } from '@nestjs/common';
|
||||||
import { CreateMerchantDto } from './dto/create-merchant.dto';
|
import { CreateMerchantDto } from './dto/create-merchant.dto';
|
||||||
import { UpdateMerchantDto } from './dto/update-merchant.dto';
|
import { UpdateMerchantDto } from './dto/update-merchant.dto';
|
||||||
|
import { MerchantDataService } from './merchant-data.service';
|
||||||
|
import { Merchant } from './entities/merchant.entity';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class MerchantsService {
|
export class MerchantsService {
|
||||||
create(createMerchantDto: CreateMerchantDto) {
|
public constructor(private merchantDataService: MerchantDataService) { }
|
||||||
return 'This action adds a new merchant';
|
|
||||||
|
public async findAll(): Promise<Merchant[]> {
|
||||||
|
return await this.merchantDataService.getAllMerchants();
|
||||||
}
|
}
|
||||||
|
|
||||||
findAll() {
|
public async findById(id: string): Promise<Merchant> {
|
||||||
return `This action returns all merchants`;
|
const merchant = await this.merchantDataService.getMerchantById(id);
|
||||||
|
if (!merchant) {
|
||||||
|
throw new Error('Merchant not found.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return merchant;
|
||||||
}
|
}
|
||||||
|
|
||||||
findOne(id: number) {
|
public async create(newMerchant: CreateMerchantDto): Promise<Merchant> {
|
||||||
return `This action returns a #${id} merchant`;
|
return await this.merchantDataService.createMerchant(newMerchant.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
update(id: number, updateMerchantDto: UpdateMerchantDto) {
|
public async update(updateMerchant: UpdateMerchantDto): Promise<Merchant> {
|
||||||
return `This action updates a #${id} merchant`;
|
return await this.merchantDataService.updateMerchant(updateMerchant);
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(id: number) {
|
public async remove(id: string): Promise<void> {
|
||||||
return `This action removes a #${id} merchant`;
|
await this.merchantDataService.deleteMerchant(id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue