Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions microservices/asset-service/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AssetController } from './asset/asset.controller';
import { AssetService } from './asset/asset.service';
import { Asset } from './asset/asset.entity';

@Module({
imports: [
ConfigModule.forRoot({ isGlobal: true }),
TypeOrmModule.forRoot({
type: 'postgres',
url: process.env.DATABASE_URL,
entities: [Asset],
synchronize: false,
}),
TypeOrmModule.forFeature([Asset]),
],
controllers: [AssetController],
providers: [AssetService],
})
export class AppModule {}
27 changes: 27 additions & 0 deletions microservices/asset-service/src/asset/asset.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Controller, Get, Post, Delete, Body, Param } from '@nestjs/common';
import { AssetService } from './asset.service';

@Controller('assets')
export class AssetController {
constructor(private readonly service: AssetService) {}

@Get('owner/:ownerId')
findByOwner(@Param('ownerId') ownerId: string) {
return this.service.findByOwner(ownerId);
}

@Get(':id')
findById(@Param('id') id: string) {
return this.service.findById(id);
}

@Post()
register(@Body() body: { ownerId: string; filename: string; mimeType: string; sizeBytes: number }) {
return this.service.register(body.ownerId, body.filename, body.mimeType, body.sizeBytes);
}

@Delete(':id')
delete(@Param('id') id: string) {
return this.service.delete(id);
}
}
28 changes: 28 additions & 0 deletions microservices/asset-service/src/asset/asset.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Entity, PrimaryGeneratedColumn, Column, CreateDateColumn } from 'typeorm';

@Entity('assets')
export class Asset {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column()
ownerId: string;

@Column()
filename: string;

@Column()
mimeType: string;

@Column({ type: 'bigint', default: 0 })
sizeBytes: number;

@Column({ nullable: true })
storageKey: string | null;

@Column({ default: 'active' })
status: string;

@CreateDateColumn()
createdAt: Date;
}
29 changes: 29 additions & 0 deletions microservices/asset-service/src/asset/asset.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Asset } from './asset.entity';

@Injectable()
export class AssetService {
constructor(
@InjectRepository(Asset)
private readonly repo: Repository<Asset>,
) {}

findByOwner(ownerId: string): Promise<Asset[]> {
return this.repo.find({ where: { ownerId, status: 'active' } });
}

findById(id: string): Promise<Asset | null> {
return this.repo.findOne({ where: { id } });
}

register(ownerId: string, filename: string, mimeType: string, sizeBytes: number): Promise<Asset> {
const asset = this.repo.create({ ownerId, filename, mimeType, sizeBytes });
return this.repo.save(asset);
}

async delete(id: string): Promise<void> {
await this.repo.update(id, { status: 'deleted' });
}
}
8 changes: 8 additions & 0 deletions microservices/asset-service/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT ?? 3017);
}
bootstrap();
Loading