Add TypeORM DB and Resources #2

Merged
joe merged 8 commits from add-sqlite into master 2026-02-09 00:10:26 +00:00
17 changed files with 214 additions and 80 deletions
Showing only changes of commit b5ced781c9 - Show all commits

View 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
}

View 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
}

View file

@ -0,0 +1,11 @@
meta {
name: LOC GET Merchants
type: http
seq: 1
}
get {
url: {{localBaseUrl}}/merchants
body: none
auth: inherit
}

View 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"
}
}

View 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"
}
}

View file

@ -0,0 +1,8 @@
meta {
name: Merchants
seq: 3
}
auth {
mode: inherit
}

BIN
common-cents.db Normal file

Binary file not shown.

View file

@ -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 { }

View file

@ -1 +1,3 @@
export class CreateMerchantDto {}
export class CreateMerchantDto {
name: string;
}

View file

@ -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;
}

View file

@ -1 +1,10 @@
export class Merchant {}
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class Merchant {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column()
name: string;
}

View 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 });
}
}

View file

@ -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();
});
});

View file

@ -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<Merchant[]> {
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<Merchant> {
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<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')
remove(@Param('id') id: string) {
return this.merchantsService.remove(+id);
@HttpCode(HttpStatus.NO_CONTENT)
public async remove(@Param('id') id: string): Promise<void> {
return this.merchantsService.remove(id);
}
}

View file

@ -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 {}

View file

@ -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();
});
});

View file

@ -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<Merchant[]> {
return await this.merchantDataService.getAllMerchants();
}
findAll() {
return `This action returns all merchants`;
public async findById(id: string): Promise<Merchant> {
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<Merchant> {
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<Merchant> {
return await this.merchantDataService.updateMerchant(updateMerchant);
}
remove(id: number) {
return `This action removes a #${id} merchant`;
public async remove(id: string): Promise<void> {
await this.merchantDataService.deleteMerchant(id);
}
}