Skip to content

Commit

Permalink
feat: add affiliation endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: hxtree <[email protected]>
  • Loading branch information
hxtree committed Oct 13, 2023
1 parent a392869 commit 6d2f01c
Show file tree
Hide file tree
Showing 13 changed files with 315 additions and 117 deletions.
209 changes: 142 additions & 67 deletions services/character-sheet/openapi-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -381,69 +381,6 @@
"responses": { "200": { "description": "" } }
}
},
"/character-sheets/{id}": {
"get": {
"operationId": "CharacterSheetController_findOne",
"parameters": [
{
"name": "id",
"required": true,
"in": "path",
"schema": { "type": "string" }
}
],
"responses": { "200": { "description": "" } }
},
"delete": {
"operationId": "CharacterSheetController_delete",
"parameters": [
{
"name": "id",
"required": true,
"in": "path",
"schema": { "type": "string" }
}
],
"responses": { "200": { "description": "" } }
}
},
"/character-sheets": {
"get": {
"operationId": "CharacterSheetController_findAll",
"parameters": [],
"responses": { "200": { "description": "" } }
},
"post": {
"operationId": "CharacterSheetController_create",
"parameters": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CreateCharacterSheetDto"
}
}
}
},
"responses": { "201": { "description": "" } }
}
},
"/npcs": {
"post": {
"operationId": "NpcController_create",
"parameters": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/CreateSpawnDto" }
}
}
},
"responses": { "201": { "description": "" } }
}
},
"/items": {
"get": {
"operationId": "ItemController_list",
Expand Down Expand Up @@ -537,6 +474,101 @@
],
"responses": { "200": { "description": "" } }
}
},
"/character-sheets/{id}": {
"get": {
"operationId": "CharacterSheetController_findOne",
"parameters": [
{
"name": "id",
"required": true,
"in": "path",
"schema": { "type": "string" }
}
],
"responses": { "200": { "description": "" } }
},
"delete": {
"operationId": "CharacterSheetController_delete",
"parameters": [
{
"name": "id",
"required": true,
"in": "path",
"schema": { "type": "string" }
}
],
"responses": { "200": { "description": "" } }
}
},
"/character-sheets": {
"get": {
"operationId": "CharacterSheetController_findAll",
"parameters": [],
"responses": { "200": { "description": "" } }
},
"post": {
"operationId": "CharacterSheetController_create",
"parameters": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CreateCharacterSheetDto"
}
}
}
},
"responses": { "201": { "description": "" } }
}
},
"/npcs": {
"post": {
"operationId": "NpcController_create",
"parameters": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/CreateSpawnDto" }
}
}
},
"responses": { "201": { "description": "" } }
}
},
"/affiliations/{id}": {
"get": {
"operationId": "AffiliationController_find",
"parameters": [
{
"name": "characterSheetId",
"required": true,
"in": "path",
"schema": {
"default": "c63bd3b9-02a2-4f32-b836-9aac1665cc96",
"type": "string"
}
}
],
"responses": { "200": { "description": "" } }
}
},
"/affiliations": {
"post": {
"operationId": "AffiliationController_update",
"parameters": [],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": { "$ref": "#/components/schemas/UpdateAffiliationDto" }
}
}
},
"responses": { "201": { "description": "" } }
}
}
},
"info": {
Expand All @@ -554,11 +586,11 @@
"properties": {
"_id": {
"type": "string",
"default": "5e799b22-74c0-4f51-963c-9d402a9825e1"
"default": "d8fba7e1-3bb2-4715-986e-1846209627a0"
},
"instanceId": {
"type": "string",
"default": "43d7e427-4ba7-4f0e-9afc-d0dccd54a69a"
"default": "05f888f0-07ad-484f-9f73-33b828abc09e"
},
"archetypeId": {
"type": "string",
Expand Down Expand Up @@ -615,11 +647,11 @@
"properties": {
"id": {
"type": "string",
"default": "62309dfb-d1e0-49e3-84c7-d9a86d43cf88"
"default": "712756ed-6b55-42c1-ba6d-2568729b2403"
},
"instanceId": {
"type": "string",
"default": "903ce92d-a8fb-406f-a03f-5cee1effc3c4"
"default": "8445ffd3-4e7f-4d58-b2a9-620b0d3c4847"
},
"place": {
"type": "string",
Expand All @@ -636,6 +668,49 @@
}
},
"required": ["id", "instanceId", "place"]
},
"UpdateAffiliationDto": {
"type": "object",
"properties": {
"characterSheetId": {
"type": "string",
"default": "ebfe4388-5a84-494a-9e55-b806b9719689"
},
"affiliationId": {
"type": "string",
"enum": [
"Reputation",
"THE_DESTROYERS",
"THE_CATS",
"ARCHANGELS",
"DEVILS",
"GUARDIANS",
"VALLONS_SEVEN",
"BROTHERHOOD",
"SOLIDER_FORCES",
"X_SOLIDER_FORCES",
"MAGI_ORDER",
"MAGI_ORDER_ELDERS",
"CATCHERS",
"REBEL",
"ORIGINALS",
"OCEANIA",
"EURASIA",
"BARBARIANS",
"NINJA",
"LEGION_SEE",
"FLIPPING_PROFITEERS"
],
"default": "THE_CATS"
},
"value": { "type": "number" },
"operation": {
"type": "string",
"enum": ["ADD", "REMOVE"],
"default": "ADD"
}
},
"required": ["characterSheetId", "affiliationId", "value", "operation"]
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion services/character-sheet/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { HealthModule } from './modules/health/health.module';
import { SkillModule } from './modules/skill/skill.module';
import { NpcModule } from './modules/npc/npc.module';
import { ItemModule } from './modules/item/item.module';
import { AffiliationModule } from './modules/affiliation/affiliation.module';

@Module({
imports: [
Expand All @@ -15,9 +16,10 @@ import { ItemModule } from './modules/item/item.module';
ArchetypeModule,
GearModule,
SkillModule,
ItemModule,
CharacterSheetModule,
NpcModule,
ItemModule,
AffiliationModule,
],
providers: [],
exports: [],
Expand Down
15 changes: 15 additions & 0 deletions services/character-sheet/src/database.module.ts
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,
}),
});

This file was deleted.

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,
);
}
}
Loading

0 comments on commit 6d2f01c

Please sign in to comment.