Skip to content

Commit 0019ec6

Browse files
committed
feat(chrono): add cat-file use-case
1 parent 12543e9 commit 0019ec6

File tree

5 files changed

+77
-3
lines changed

5 files changed

+77
-3
lines changed

packages/chrono/.eslintrc.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
{
22
"root": true,
3-
"extends": "@index-san/eslint-config/ts"
3+
"extends": "@index-san/eslint-config/ts",
4+
"overrides": [
5+
{
6+
"files": ["playground/*.ts"],
7+
"rules": {
8+
"no-console": "off"
9+
}
10+
}
11+
]
412
}

packages/chrono/playground/PlayApp.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const hash = new PlayHash()
77

88
const app = new ChronoApp(drive, hash)
99

10-
// app.init()
10+
async function run() {
11+
// app.init()
1112

12-
app.hashFile('message.md')
13+
// app.hashFile('message.md')
14+
15+
console.log(await app.catFile('1cd0ff195e53a0a31601aae77b23e20e63f6d244'))
16+
}
17+
18+
run()

packages/chrono/src/ChronoApp.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import BlobRepositoryImpl from './repositories/BlobRepositoryImpl'
44
import IBlobRepository from './repositories/IBlobRepository'
55
import IObjectRepository from './repositories/IObjectRepository'
66
import ObjectRepositoryImpl from './repositories/ObjectRepositoryImpl'
7+
import CatFileUseCase from './use-cases/CatFileUseCase'
78
import HashFileUseCase from './use-cases/HashFileUseCase'
89
import InitUseCase from './use-cases/InitUseCase'
910

@@ -30,4 +31,10 @@ export default class ChronoApp {
3031

3132
await useCase.execute({ path })
3233
}
34+
35+
public async catFile(objectHash: string) {
36+
const useCase = new CatFileUseCase(this.objectRepository)
37+
38+
return await useCase.execute({ objectHash })
39+
}
3340
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { test, expect, describe } from 'vitest'
2+
import InMemoryDrive from '../__tests__/InMemoryDrive'
3+
import HelperService from '../services/HelperService'
4+
import CatFileUseCase from './CatFileUseCase'
5+
import InMemoryHash from '../__tests__/InMemoryHash'
6+
import ObjectRepositoryImpl from '../repositories/ObjectRepositoryImpl'
7+
import ChronoObject from '../entities/ChronoObject'
8+
9+
const drive = new InMemoryDrive()
10+
const hash = new InMemoryHash()
11+
const objectRepository = new ObjectRepositoryImpl(drive, hash)
12+
13+
const useCase = new CatFileUseCase(objectRepository)
14+
15+
describe('CatFileUseCase', () => {
16+
test('should return chrono object', async () => {
17+
await drive.write('message.md', HelperService.encode('Hello World!'))
18+
19+
const { objectHash } = await objectRepository.save(
20+
new ChronoObject({
21+
type: 'blob',
22+
blobHash: '123',
23+
})
24+
)
25+
26+
const result = await useCase.execute({ objectHash })
27+
28+
expect(result).toEqual({
29+
type: 'blob',
30+
blobHash: '123',
31+
})
32+
})
33+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import BaseException from '../exceptions/BaseException'
2+
import IObjectRepository from '../repositories/IObjectRepository'
3+
4+
interface CatUseCaseParams {
5+
objectHash: string
6+
}
7+
8+
export default class CatFileUseCase {
9+
constructor(private readonly objectRepository: IObjectRepository) {}
10+
11+
async execute({ objectHash }: CatUseCaseParams) {
12+
const object = await this.objectRepository.find(objectHash)
13+
14+
if (!object) {
15+
throw new BaseException(`Object ${objectHash} not found`)
16+
}
17+
18+
return object
19+
}
20+
}

0 commit comments

Comments
 (0)