Skip to content

Commit 40402da

Browse files
committed
rename shorten-url with create-shorten-url
1 parent 6dbc62f commit 40402da

File tree

7 files changed

+26
-23
lines changed

7 files changed

+26
-23
lines changed

api/src/shorten-url/create-shorten-url/shorten-url.controller.spec.ts renamed to api/src/shorten-url/create-shorten-url/create-shorten-url.controller.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { Test, TestingModule } from '@nestjs/testing';
2-
import { ShortenUrlController } from './shorten-url.controller';
2+
import { CreateShortenUrlController } from './create-shorten-url.controller';
33
import { ConfigModule } from '@nestjs/config';
44
import { ShortenUrlModule } from '../shorten-url.module';
55

66
describe('ShortenUrl controller', () => {
7-
let underTest: ShortenUrlController;
7+
let underTest: CreateShortenUrlController;
88

99
beforeEach(async () => {
1010
const app: TestingModule = await Test.createTestingModule({
1111
imports: [await ConfigModule.forRoot(), ShortenUrlModule],
1212
}).compile();
1313

14-
underTest = app.get<ShortenUrlController>(ShortenUrlController);
14+
underTest = app.get<CreateShortenUrlController>(CreateShortenUrlController);
1515
});
1616

1717
describe('shortenUrl', () => {

api/src/shorten-url/create-shorten-url/shorten-url.controller.ts renamed to api/src/shorten-url/create-shorten-url/create-shorten-url.controller.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import { Body, Controller, Logger, Post } from '@nestjs/common';
2-
import { ShortenUrlUsecase } from './shorten-url.usecase';
2+
import { CreateShortenUrlUsecase } from './create-shorten-url.usecase';
33
import { CreateShortenUrlDto } from './create-shorten-url.dto';
44

55
@Controller('/shorten-url')
6-
export class ShortenUrlController {
7-
constructor(private readonly shortenUrlUsecase: ShortenUrlUsecase) {}
6+
export class CreateShortenUrlController {
7+
constructor(private readonly shortenUrlUsecase: CreateShortenUrlUsecase) {}
88

99
@Post()
1010
async shortenUrl(
1111
@Body() request: CreateShortenUrlDto,
1212
): Promise<{ shortenedUrl: string }> {
13-
Logger.log(`Received url ${request.url} to shorten`);
1413
// TODO perhaps it is worth converting the URL from string to URL object
1514
const shortenedUrl = await this.shortenUrlUsecase.shortenUrl(request.url);
1615
return { shortenedUrl };

api/src/shorten-url/create-shorten-url/shorten-url.usecase.ts renamed to api/src/shorten-url/create-shorten-url/create-shorten-url.usecase.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { ShortenUrlRepository } from '../shorten-url.repository';
44
import { ConfigService } from '@nestjs/config';
55

66
@Injectable()
7-
export class ShortenUrlUsecase {
7+
export class CreateShortenUrlUsecase {
88
private static BASE62_CHARACTERS =
99
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
1010

11-
private static BASE62 = ShortenUrlUsecase.BASE62_CHARACTERS.length;
11+
private static BASE62 = CreateShortenUrlUsecase.BASE62_CHARACTERS.length;
1212

1313
private readonly shortenedBaseUrl: string;
1414

@@ -35,13 +35,13 @@ export class ShortenUrlUsecase {
3535
}
3636

3737
private encodeBase62(id: number): string {
38-
if (id === 0) return ShortenUrlUsecase.BASE62_CHARACTERS[0];
38+
if (id === 0) return CreateShortenUrlUsecase.BASE62_CHARACTERS[0];
3939

4040
let encoded = '';
4141
while (id > 0) {
42-
const remainder = id % ShortenUrlUsecase.BASE62;
43-
encoded = ShortenUrlUsecase.BASE62_CHARACTERS[remainder] + encoded;
44-
id = Math.floor(id / ShortenUrlUsecase.BASE62);
42+
const remainder = id % CreateShortenUrlUsecase.BASE62;
43+
encoded = CreateShortenUrlUsecase.BASE62_CHARACTERS[remainder] + encoded;
44+
id = Math.floor(id / CreateShortenUrlUsecase.BASE62);
4545
}
4646
return encoded;
4747
}

api/src/shorten-url/redirect-to-original-url/redirect-to-original-url-controller.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
import { ShortenUrlController } from '../create-shorten-url/shorten-url.controller';
1+
import { CreateShortenUrlController } from '../create-shorten-url/create-shorten-url.controller';
22
import { RedirectToOriginalUrlController } from './redirect-to-original-url.controller';
33
import { Test, TestingModule } from '@nestjs/testing';
44
import { ConfigModule } from '@nestjs/config';
55
import { ShortenUrlModule } from '../shorten-url.module';
66

77
describe('Redirect to original url controller', () => {
8-
let shortenUrlController: ShortenUrlController;
8+
let shortenUrlController: CreateShortenUrlController;
99
let underTest: RedirectToOriginalUrlController;
1010

1111
beforeEach(async () => {
1212
const app: TestingModule = await Test.createTestingModule({
1313
imports: [await ConfigModule.forRoot(), ShortenUrlModule],
1414
}).compile();
1515

16-
shortenUrlController = app.get<ShortenUrlController>(ShortenUrlController);
16+
shortenUrlController = app.get<CreateShortenUrlController>(
17+
CreateShortenUrlController,
18+
);
1719
underTest = app.get<RedirectToOriginalUrlController>(
1820
RedirectToOriginalUrlController,
1921
);

api/src/shorten-url/redirect-to-original-url/redirect-to-original-url.controller.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ export class RedirectToOriginalUrlController {
1616
@Get(':slug')
1717
@Redirect(undefined, 302)
1818
async redirectToOriginalUrl(@Param('slug') slug: string) {
19-
const originalUrl = await this.getOriginalUrlUsecase.getOriginalUrl(slug);
19+
const originalUrl = await this.getOriginalUrlUsecase.redirectToOriginalUrl(
20+
slug,
21+
);
2022
if (!originalUrl) {
2123
throw new NotFoundException(`Shortened URL "${slug}" not found`);
2224
}

api/src/shorten-url/redirect-to-original-url/redirect-to-original-url.usecase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ export class RedirectToOriginalUrlUsecase {
88
private readonly shortenUrlRepository: ShortenUrlRepository,
99
) {}
1010

11-
async getOriginalUrl(shortenedUrl: string): Promise<string | null> {
11+
async redirectToOriginalUrl(slug: string): Promise<string | null> {
1212
// TODO: reconstruct the URL ? or store only the shorted path ??
13-
const url = 'http://localhost:3000/' + shortenedUrl;
13+
const url = 'http://localhost:3000/' + slug;
1414
return await this.shortenUrlRepository.findOriginalURL(url);
1515
}
1616
}

api/src/shorten-url/shorten-url.module.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Module } from '@nestjs/common';
22
import { ConfigModule } from '@nestjs/config';
3-
import { ShortenUrlController } from './create-shorten-url/shorten-url.controller';
4-
import { ShortenUrlUsecase } from './create-shorten-url/shorten-url.usecase';
3+
import { CreateShortenUrlController } from './create-shorten-url/create-shorten-url.controller';
4+
import { CreateShortenUrlUsecase } from './create-shorten-url/create-shorten-url.usecase';
55
import { ShortenUrlIdGeneratorService } from './create-shorten-url/shorten-url.id-generator.service';
66
import { InfrastructureModule } from '../infrastructure/infrastructure.module';
77
import { ShortenUrlRepository } from './shorten-url.repository';
@@ -13,14 +13,14 @@ import { RedirectToOriginalUrlController } from './redirect-to-original-url/redi
1313
ConfigModule, // ✅ Ensure ConfigModule is available
1414
InfrastructureModule.register(), // ✅ Register InfrastructureModule dynamically
1515
],
16-
controllers: [ShortenUrlController, RedirectToOriginalUrlController],
16+
controllers: [CreateShortenUrlController, RedirectToOriginalUrlController],
1717
providers: [
1818
{
1919
provide: 'ShortenUrlRepository',
2020
useExisting: ShortenUrlRepository, // ✅ Use injected provider
2121
},
2222
ShortenUrlIdGeneratorService,
23-
ShortenUrlUsecase,
23+
CreateShortenUrlUsecase,
2424
RedirectToOriginalUrlUsecase,
2525
],
2626
})

0 commit comments

Comments
 (0)