Skip to content
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

add entriedPrice entriedAt option to post/put API #4

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 src/actions/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export abstract class ApiResponseHandler {
try {
const response = await this.axiosInstance.post<U>(url, data);
// console.log("SUCCESS:", response.data);
console.log("post", url, response.status);
console.log("post", url, response.status, data);
// console.log("Response Headers: ", response.headers);
return response.data;
} catch (error) {
Expand Down
60 changes: 25 additions & 35 deletions src/actions/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,37 +102,6 @@ export interface AssetModel {
}

export class Asset extends ApiResponseHandler {
private formatDate(date: Date): string {
const year = date.getFullYear();
const month = date.getMonth() + 1; // getMonth() は0から始まるため、+1する
const day = date.getDate();

return `${year}/${month}/${day}`;
}

public async addAsset(
accountString: string,
assetSubclassId: AssetSubclass,
assetName: string,
assetValue: number,
assetEntryValue?: number,
assetEntryAt?: Date
) {
const postData = {
"user_asset_det[id]": "",
"user_asset_det[sub_account_id_hash]": accountString.split("@")[1],
"user_asset_det[asset_subclass_id]": assetSubclassId,
"user_asset_det[name]": assetName,
"user_asset_det[value]": assetValue,
"user_asset_det[entried_price]":
assetEntryValue !== undefined ? assetEntryValue : "",
"user_asset_det[entried_at]": assetEntryAt
? this.formatDate(assetEntryAt)
: "",
};
return this.post("/bs/portfolio/new", postData);
}

public async getAssets(accountString: string): Promise<AssetModel[]> {
const url = `/accounts/show_manual/${accountString.split("@")[0]}`;

Expand Down Expand Up @@ -189,6 +158,28 @@ export class Asset extends ApiResponseHandler {
});
}

public async addAsset(
accountString: string,
assetSubclassId: AssetSubclass,
assetName: string,
assetValue: number,
assetEntryValue?: number,
assetEntryAt?: string
) {
const postData = {
"user_asset_det[id]": "",
"user_asset_det[sub_account_id_hash]": accountString.split("@")[1],
"user_asset_det[asset_subclass_id]": assetSubclassId,
"user_asset_det[name]": assetName,
"user_asset_det[value]": assetValue,
"user_asset_det[entried_price]":
assetEntryValue !== undefined ? assetEntryValue : "",
"user_asset_det[entried_at]":
assetEntryAt !== undefined ? assetEntryAt : "",
};
return this.post("/bs/portfolio/new", postData);
}

public async deleteAsset(
accountString: string,
assetId: string
Expand All @@ -209,7 +200,7 @@ export class Asset extends ApiResponseHandler {
assetName: string,
assetValue: number,
assetEntryValue?: number,
assetEntryAt?: Date
assetEntryAt?: string
): Promise<void> {
const postData = {
_method: "put",
Expand All @@ -220,9 +211,8 @@ export class Asset extends ApiResponseHandler {
"user_asset_det[value]": assetValue,
"user_asset_det[entried_price]":
assetEntryValue !== undefined ? assetEntryValue : "",
"user_asset_det[entried_at]": assetEntryAt
? this.formatDate(assetEntryAt)
: "",
"user_asset_det[entried_at]":
assetEntryAt !== undefined ? assetEntryAt : "",
};
return this.post("/bs/portfolio/edit", postData);
}
Expand Down
14 changes: 10 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,17 @@ async function main() {
async (req: Request, res: Response) => {
try {
const { accountString } = req.params;
const { assetSubclassId, name, value } = req.body;
const { assetSubclassId, name, value, entriedPrice, entriedAt } =
req.body;
const _assetSubclassId =
AssetSubclass[assetSubclassId as keyof typeof AssetSubclass];
await assetController.addAsset(
accountString,
_assetSubclassId,
name,
value
value,
entriedPrice,
entriedAt
);
res.status(201).send();
} catch (error) {
Expand Down Expand Up @@ -131,15 +134,18 @@ async function main() {
async (req: Request, res: Response) => {
try {
const { accountString, assetId } = req.params;
const { assetSubclassId, name, value } = req.body;
const { assetSubclassId, name, value, entriedPrice, entriedAt } =
req.body;
const _assetSubclassId =
AssetSubclass[assetSubclassId as keyof typeof AssetSubclass];
await assetController.updateAsset(
accountString,
assetId,
_assetSubclassId,
name,
value
value,
entriedPrice,
entriedAt
);
res.status(204).send();
} catch (error) {
Expand Down
11 changes: 6 additions & 5 deletions src/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ paths:
schema:
type: string
requestBody:
description: assetSubclassIdは変更することができず、変更前と同じにする必要があります。
description: |
* `assetSubclassId`は変更できません。必ず同じ値を指定する必要があります。
* `entriedPrice`, `entriedAt`は指定がなければ値が初期化されます。
required: true
content:
application/json:
Expand Down Expand Up @@ -158,19 +160,18 @@ components:
description: 資産名
example: "USD/JPY"
value:
type: number
format: float
type: integer
description: 資産価値
example: 100
entriedPrice:
type: number
format: float
type: integer
description: システム登録時の価格
example: 90
nullable: true
entriedAt:
type: string
description: システム登録日
pattern: '^\d{4}/\d{2}/\d{2}$'
example: "2022/01/01"
nullable: true
AssetSubclass:
Expand Down