-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: hxtree <[email protected]>
- Loading branch information
Showing
8 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
services/character-sheet/src/models/affiliation-embeddable.schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Prop, Schema } from '@nestjs/mongoose'; | ||
import { | ||
IsInt, Min, Max, IsEnum, | ||
} from '@cats-cradle/validation-schemas'; | ||
import { AffiliationId, AffiliationIds } from '../data/affiliations'; | ||
|
||
@Schema({ _id: false }) | ||
export class AffiliationEmbeddable { | ||
@IsEnum(AffiliationIds) | ||
@Prop() | ||
public affiliationId!: AffiliationId; | ||
|
||
@IsInt() | ||
@Min(0) | ||
@Max(255) | ||
@Prop() | ||
public amount!: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
services/character-sheet/src/modules/affilation/affiliation.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { | ||
Controller, Get, Param, VERSION_NEUTRAL, | ||
} from '@nestjs/common'; | ||
import { AffiliationService } from './affiliation.service'; | ||
import { QueryAffiliationDto } from './query-affiliation.dto'; | ||
import { CharacterSheetRepository } from '../../models/character-sheet.repository'; | ||
|
||
@Controller({ path: '/affiliations', version: [VERSION_NEUTRAL, '1'] }) | ||
export class AffiliationController { | ||
constructor( | ||
private _affiliationService: AffiliationService, | ||
private _characterSheetRepository: CharacterSheetRepository, | ||
) {} | ||
|
||
@Get('/:id') | ||
async find(@Param() param: QueryAffiliationDto): Promise<any> { | ||
const result = await this._characterSheetRepository.findOne({ | ||
id: param.characterSheetId, | ||
}); | ||
|
||
if (!result) { | ||
return []; | ||
} | ||
|
||
return result.affiliation ?? []; | ||
} | ||
|
||
// :id/:id | ||
// POST :id/:id +2 -2 | ||
} |
62 changes: 62 additions & 0 deletions
62
services/character-sheet/src/modules/affilation/affiliation.e2e-spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import supertest from 'supertest'; | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { INestApplication } from '@nestjs/common'; | ||
import { FakerFactory } from '@cats-cradle/faker-factory'; | ||
import { | ||
closeInMongodConnection, | ||
rootMongooseTestModule, | ||
MongooseModule, | ||
} from '@cats-cradle/nestjs-modules'; | ||
import { AffiliationController } from './affiliation.controller'; | ||
import { AffiliationService } from './affiliation.service'; | ||
import { CharacterSheetRepository } from '../../models/character-sheet.repository'; | ||
import { CharacterSheet, CharacterSheetSchema } from '../../models/character-sheet.schema'; | ||
|
||
describe('/affiliations', () => { | ||
let app: INestApplication; | ||
let affiliationService: AffiliationService; | ||
let characterSheetRepository: CharacterSheetRepository; | ||
|
||
beforeEach(async () => { | ||
const moduleRef: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
rootMongooseTestModule(), | ||
MongooseModule.forFeature([ | ||
{ name: 'CharacterSheet', schema: CharacterSheetSchema }, | ||
]), | ||
], | ||
providers: [CharacterSheetRepository, AffiliationService], | ||
controllers: [AffiliationController], | ||
}).compile(); | ||
|
||
app = moduleRef.createNestApplication(); | ||
affiliationService = moduleRef.get<AffiliationService>(AffiliationService); | ||
characterSheetRepository = moduleRef.get<CharacterSheetRepository>( | ||
CharacterSheetRepository, | ||
); | ||
await app.init(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await characterSheetRepository.deleteAll(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await closeInMongodConnection(); | ||
await app.close(); | ||
}); | ||
|
||
it('/GET /affiliations/:id', async () => { | ||
const characterSheet = await FakerFactory.create<CharacterSheet>( | ||
CharacterSheet, | ||
{ archetypeId: 'MEEKU_ONI' }, | ||
); | ||
await characterSheetRepository.create(characterSheet); | ||
|
||
const response = await supertest(app.getHttpServer()) | ||
.get(`/affiliations/${characterSheet._id}`) | ||
.expect(200); | ||
|
||
expect(response.body).toEqual(characterSheet.affiliation); | ||
}); | ||
}); |
10 changes: 10 additions & 0 deletions
10
services/character-sheet/src/modules/affilation/affiliation.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AffiliationService } from './affiliation.service'; | ||
import { AffiliationController } from './affiliation.controller'; | ||
import { CharacterSheetRepository } from '../../models/character-sheet.repository'; | ||
|
||
@Module({ | ||
controllers: [AffiliationController], | ||
providers: [AffiliationService, CharacterSheetRepository], | ||
}) | ||
export class AffiliationModule {} |
33 changes: 33 additions & 0 deletions
33
services/character-sheet/src/modules/affilation/affiliation.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
export enum Reputation { | ||
HONORED = 'HONORED', | ||
FRIENDLY = 'FRIENDLY', | ||
NEUTRAL = 'NEUTRAL', | ||
HATED = 'HATED', | ||
} | ||
|
||
@Injectable() | ||
export class AffiliationService { | ||
toString(amount: number | undefined): Reputation { | ||
if (amount === undefined) { | ||
return Reputation.NEUTRAL; | ||
} | ||
if (amount > 90) { | ||
return Reputation.HONORED; | ||
} | ||
if (amount > 50) { | ||
return Reputation.FRIENDLY; | ||
} | ||
|
||
if (amount < 0) { | ||
return Reputation.HATED; | ||
} | ||
|
||
return Reputation.NEUTRAL; | ||
} | ||
|
||
async find(instanceId: string, characterId: string): Promise<any> { | ||
// get from mongo | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
services/character-sheet/src/modules/affilation/query-affiliation.dto.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { v4 } from 'uuid'; | ||
|
||
export class QueryAffiliationDto { | ||
@ApiProperty({ | ||
default: v4(), | ||
}) | ||
characterSheetId: string; | ||
} |