-
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
13 changed files
with
315 additions
and
117 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
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
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,15 @@ | ||
import { MongooseModule, MongooseModuleOptions } from '@nestjs/mongoose'; | ||
import { ConfigService } from '@nestjs/config'; | ||
|
||
// eslint-disable max-len | ||
export const databaseModule = (options: MongooseModuleOptions = {}) => MongooseModule.forRootAsync({ | ||
inject: [ConfigService], | ||
useFactory: (configService: ConfigService) => ({ | ||
uri: `${configService.get('MONGO_DATABASE_URI')}/${configService.get( | ||
'STAGE', | ||
)}-${configService.get('APP_NAME')}`, | ||
user: configService.get('MONGO_DATABASE_USER'), | ||
pass: configService.get('MONGO_DATABASE_PASSWORD'), | ||
...options, | ||
}), | ||
}); |
30 changes: 0 additions & 30 deletions
30
services/character-sheet/src/modules/affilation/affiliation.controller.ts
This file was deleted.
Oops, something went wrong.
71 changes: 71 additions & 0 deletions
71
services/character-sheet/src/modules/affiliation/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,71 @@ | ||
import { | ||
BadRequestException, | ||
Body, | ||
Controller, | ||
Get, | ||
Param, | ||
Post, | ||
Req, | ||
VERSION_NEUTRAL, | ||
} from '@nestjs/common'; | ||
import { AffiliationService } from './affiliation.service'; | ||
import { QueryAffiliationDto } from './query-affiliation.dto'; | ||
import { Operation, UpdateAffiliationDto } from './update-affiliation.dto'; | ||
import { CharacterSheetRepository } from '../../models/character-sheet.repository'; | ||
import { AffiliationEmbeddable } from '../../models/affiliation-embeddable.schema'; | ||
|
||
@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 ?? []; | ||
} | ||
|
||
@Post() | ||
async update(@Body() body: UpdateAffiliationDto): Promise<any> { | ||
const characterSheet = await this._characterSheetRepository.findOne({ | ||
id: body.characterSheetId, | ||
}); | ||
|
||
if (!characterSheet) { | ||
throw new BadRequestException('Not found'); | ||
} | ||
|
||
characterSheet.affiliation.forEach( | ||
(affiliation: AffiliationEmbeddable, index: number) => { | ||
if (affiliation.affiliationId !== body.affiliationId) { | ||
return; | ||
} | ||
|
||
switch (body.operation) { | ||
case Operation.ADD: | ||
characterSheet.affiliation[index].amount += body.value; | ||
break; | ||
case Operation.REMOVE: | ||
characterSheet.affiliation[index].amount -= body.value; | ||
break; | ||
default: | ||
throw new BadRequestException('Invalid operation'); | ||
} | ||
}, | ||
); | ||
|
||
return this._characterSheetRepository.updateOne( | ||
{ id: body.characterSheetId }, | ||
characterSheet, | ||
); | ||
} | ||
} |
Oops, something went wrong.