Skip to content

Add Link Resolver Module with CRUD functionality and DTOs #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: staging
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@prisma/client": "^4.1.1",
"bcrypt": "^5.0.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.13.2",
"class-validator": "^0.14.1",
"cookie-parser": "^1.4.6",
"csurf": "^1.11.0",
"passport": "^0.6.0",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { UserModule } from './user/user.module';
import { TodoModule } from './todo/todo.module';
import { PrismaModule } from './prisma/prisma.module';
import { ConfigModule } from '@nestjs/config';
import { LinkResolverModule } from './link-resolver/link-resolver.module';

@Module({
imports: [
Expand All @@ -14,6 +15,7 @@ import { ConfigModule } from '@nestjs/config';
UserModule,
TodoModule,
PrismaModule,
LinkResolverModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
8 changes: 8 additions & 0 deletions src/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Common Barrel File
*
* This file exports all common elements from the common directory,
* making them easier to import elsewhere in the application.
*/

export * from './interfaces';
8 changes: 8 additions & 0 deletions src/common/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Interfaces Barrel File
*
* This file exports all interfaces from the interfaces directory,
* making them easier to import elsewhere in the application.
*/

export * from './repository.interface';
55 changes: 55 additions & 0 deletions src/common/interfaces/repository.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Repository Provider Interface
*
* This file defines a TypeScript interface for a repository provider,
* which is a common pattern in software architecture for abstracting data access operations.
*/

/**
* Defines the structure for data being saved
* Requires an id property as a string
* Allows any additional properties
*/
export type SaveParams = {
id: string;
[k: string]: any;
};

/**
* Repository Provider Interface
*
* Defines four standard CRUD operations:
* - save: Stores data with the given parameters
* - one: Retrieves a single item by ID
* - all: Retrieves all items of a specific category
* - delete: Removes an item by ID
*/
export interface IRepositoryProvider {
/**
* Stores data with the given parameters
* @param data The data to be saved
* @returns A promise resolving to void
*/
save(data: SaveParams): Promise<void>;

/**
* Retrieves a single item by ID
* @param id The unique identifier of the item
* @returns A promise resolving to the requested item or null if not found
*/
one<T>(id: string): Promise<T | null>;

/**
* Retrieves all items of a specific category
* @param filter Optional filtering criteria
* @returns A promise resolving to an array of items
*/
all<T>(filter?: object): Promise<T[]>;

/**
* Removes an item by ID
* @param id The unique identifier of the item to delete
* @returns A promise resolving to void
*/
delete(id: string): Promise<void>;
}
83 changes: 83 additions & 0 deletions src/link-resolver/dto/create-link-resolver.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {
IsString,
IsBoolean,
IsOptional,
IsArray,
IsObject,
ValidateNested,
} from 'class-validator';
import { Type } from 'class-transformer';
import { Linkset, Response } from '../entities/link-resolver.entity';

/**
* Data Transfer Object for creating a Link Resolver entry
*/
export class CreateLinkResolverDto {
@IsString()
namespace: string;

@IsString()
identificationKeyType: string;

@IsString()
identificationKey: string;

@IsString()
@IsOptional()
itemDescription?: string;

@IsString()
@IsOptional()
qualifierPath?: string;

@IsBoolean()
active: boolean;

@IsObject()
@ValidateNested()
@Type(() => Object) // We can't directly validate Linkset due to dynamic keys
linkset: Linkset;

@IsArray()
@ValidateNested({ each: true })
@Type(() => ResponseDto)
responses: Response[];
}

export class ResponseDto implements Response {
@IsBoolean()
defaultLinkType: boolean;

@IsBoolean()
defaultMimeType: boolean;

@IsBoolean()
fwqs: boolean;

@IsBoolean()
active: boolean;

@IsString()
linkType: string;

@IsString()
title: string;

@IsString()
targetUrl: string;

@IsString()
mimeType: string;

@IsString()
ianaLanguage: string;

@IsString()
context: string;

@IsBoolean()
defaultContext: boolean;

@IsBoolean()
defaultIanaLanguage: boolean;
}
53 changes: 53 additions & 0 deletions src/link-resolver/dto/update-link-resolver.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
IsString,
IsBoolean,
IsOptional,
IsArray,
IsObject,
ValidateNested,
} from 'class-validator';
import { Type } from 'class-transformer';
import { Linkset, Response } from '../entities/link-resolver.entity';
import { ResponseDto } from './create-link-resolver.dto';

/**
* Data Transfer Object for updating a Link Resolver entry
* All fields are optional for partial updates
*/
export class UpdateLinkResolverDto {
@IsString()
@IsOptional()
namespace?: string;

@IsString()
@IsOptional()
identificationKeyType?: string;

@IsString()
@IsOptional()
identificationKey?: string;

@IsString()
@IsOptional()
itemDescription?: string;

@IsString()
@IsOptional()
qualifierPath?: string;

@IsBoolean()
@IsOptional()
active?: boolean;

@IsObject()
@IsOptional()
@ValidateNested()
@Type(() => Object)
linkset?: Linkset;

@IsArray()
@IsOptional()
@ValidateNested({ each: true })
@Type(() => ResponseDto)
responses?: Response[];
}
58 changes: 58 additions & 0 deletions src/link-resolver/entities/link-resolver.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Link Resolver Entity
*
* This entity represents the Link Resolver data model used to:
* 1. Store the link resolver configuration for a specific identifier
* 2. Store metadata for a specific identifier
* 3. Store responses (redirects) for a specific identifier
* 4. Store the link set for a specific identifier
*/

export interface LinksetEntry {
href: string;
title?: string;
type?: string;
hreflang?: string[];
'title*'?: { value: string; language: string }[];
}

export interface Linkset {
anchor: string;
[key: string]: LinksetEntry[] | string;
}

export interface Response {
defaultLinkType: boolean;
defaultMimeType: boolean;
fwqs: boolean;
active: boolean;
linkType: string;
title: string;
targetUrl: string;
mimeType: string;
ianaLanguage: string;
context: string;
defaultContext: boolean;
defaultIanaLanguage: boolean;
}

export class LinkResolver {
id: string;
createdAt: Date;
linkset: Linkset;
linkHeaderText: string;
namespace: string;
identificationKeyType: string;
identificationKey: string;
itemDescription?: string;
qualifierPath?: string;
active: boolean;
responses: Response[];

// Add index signature to allow dynamic property access
[key: string]: any;

constructor(partial: Partial<LinkResolver>) {
Object.assign(this, partial);
}
}
14 changes: 14 additions & 0 deletions src/link-resolver/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Link Resolver Module Barrel File
*
* This file exports all elements from the link-resolver module,
* making them easier to import elsewhere in the application.
*/

export * from './entities/link-resolver.entity';
export * from './dto/create-link-resolver.dto';
export * from './dto/update-link-resolver.dto';
export * from './repositories/link-resolver.repository';
export * from './link-resolver.service';
export * from './link-resolver.controller';
export * from './link-resolver.module';
Loading