Skip to content

Commit 869d269

Browse files
authored
Merge pull request #72 from boostcamp-2020/feature/MainPage_DB
DB์—์„œ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋กœ ๋ฉ”์ธํŽ˜์ด์ง€ ๋ Œ๋”๋ง
2 parents 450013e + 531463c commit 869d269

File tree

18 files changed

+176
-83
lines changed

18 files changed

+176
-83
lines changed

โ€Ž.gitignoreโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dist
1313
.DS_Store
1414
.env.local
1515
.env.development.local
16+
.env.development
1617
.env.test.local
1718
.env.production.local
1819

โ€Žbe/src/controllers/transaction/index.tsโ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import Koa from 'koa';
22
import { getTransaction, createTransaction } from 'services/transaction';
33

44
const get = async (ctx: Koa.Context) => {
5-
const res = await getTransaction();
5+
const { year, month } = ctx.query;
6+
const res = await getTransaction({ year, month });
67
ctx.status = 200;
78
ctx.body = res;
89
};

โ€Žbe/src/libs/date.tsโ€Ž

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const getOneMonthRange = (year: string, month: string) => {
2+
if (month === '12') {
3+
return {
4+
start: `${year}-${month}`,
5+
end: `${Number(year) + 1}-${1}`,
6+
};
7+
}
8+
return {
9+
start: `${year}-${month}`,
10+
end: `${year}-${Number(month) + 1}`,
11+
};
12+
};
13+
14+
export default getOneMonthRange;

โ€Žbe/src/models/transaction.tsโ€Ž

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ const TransactionSchema = new Schema({
3535
export interface Transaction extends Document {
3636
client: String;
3737
method: String;
38-
classification: String;
3938
category: String;
40-
date: String;
39+
date: Date;
4140
price: Number;
4241
memo?: String;
4342
excludeFromBudget?: Boolean;

โ€Žbe/src/seeds/transaction.tsโ€Ž

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ export const up = ({ methods, categories }: TransactionType) => {
3131
const categoryPosition =
3232
(getRandomNumber({ start: 0, end: 100 }) % METHOD_LENGTH) + base;
3333
const category = categories[categoryPosition]._id;
34+
const price = getRandomNumber({
35+
start: 1000,
36+
end: 300000,
37+
});
3438
transactionList.push({
3539
client,
3640
date,
3741
memo,
3842
method,
3943
category,
44+
price,
4045
});
4146
return transactionList;
4247
}, []);

โ€Žbe/src/services/transaction/index.tsโ€Ž

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
import { TransactionModel, Transaction } from 'models/transaction';
2+
import getOneMonthRange from 'libs/date';
23

3-
export const getTransaction = async () => {
4-
return TransactionModel.find().populate('category').populate('method');
4+
const oneMonthTransactionsReducer = (acc: any, transaction: Transaction) => {
5+
const year = transaction.date.getFullYear();
6+
const month = transaction.date.getMonth() + 1;
7+
const date = transaction.date.getDate();
8+
const key = `${year}-${month}-${date}`;
9+
return acc[key]
10+
? { ...acc, [key]: [...acc[key], transaction] }
11+
: { ...acc, [key]: [transaction] };
12+
};
13+
export const getTransaction = async ({
14+
year,
15+
month,
16+
}: {
17+
year: string;
18+
month: string;
19+
}) => {
20+
const oneMonthTransactions: Transaction[] = await TransactionModel.find()
21+
.populate('category')
22+
.populate('method')
23+
.where('date')
24+
.gte(new Date(getOneMonthRange(year, month).start))
25+
.lt(new Date(getOneMonthRange(year, month).end))
26+
.sort('date');
27+
28+
const result = oneMonthTransactions.reduce(oneMonthTransactionsReducer, {});
29+
return result;
530
};
631

732
export const createTransaction = async ({
833
client,
9-
classification,
1034
date,
1135
memo,
1236
method,
@@ -16,7 +40,6 @@ export const createTransaction = async ({
1640
}: Transaction) => {
1741
const newTransaction = new TransactionModel({
1842
client,
19-
classification,
2043
date,
2144
memo,
2245
method,

โ€Žbe/yarn.lockโ€Ž

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,13 @@ eslint-plugin-import@^2.22.1:
13651365
resolve "^1.17.0"
13661366
tsconfig-paths "^3.9.0"
13671367

1368+
eslint-plugin-prettier@^3.1.4:
1369+
version "3.1.4"
1370+
resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-3.1.4.tgz#168ab43154e2ea57db992a2cd097c828171f75c2"
1371+
integrity sha512-jZDa8z76klRqo+TdGDTFJSavwbnWK2ZpqGKNZ+VvweMW516pDUMmQ2koXvxEE4JhzNvTv+radye/bWGBmA6jmg==
1372+
dependencies:
1373+
prettier-linter-helpers "^1.0.0"
1374+
13681375
eslint-scope@^5.0.0, eslint-scope@^5.1.1:
13691376
version "5.1.1"
13701377
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-5.1.1.tgz#e786e59a66cb92b3f6c1fb0d508aab174848f48c"
@@ -1501,6 +1508,11 @@ fast-deep-equal@^3.1.1:
15011508
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
15021509
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==
15031510

1511+
fast-diff@^1.1.2:
1512+
version "1.2.0"
1513+
resolved "https://registry.yarnpkg.com/fast-diff/-/fast-diff-1.2.0.tgz#73ee11982d86caaf7959828d519cfe927fac5f03"
1514+
integrity sha512-xJuoT5+L99XlZ8twedaRf6Ax2TgQVxvgZOYoPKqZufmJib0tL2tegPBOZb1pVNgIhlqDlA0eO0c3wBvQcmzx4w==
1515+
15041516
fast-glob@^3.1.1:
15051517
version "3.2.4"
15061518
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-3.2.4.tgz#d20aefbf99579383e7f3cc66529158c9b98554d3"
@@ -2137,11 +2149,6 @@ koa-convert@^1.2.0:
21372149
co "^4.6.0"
21382150
koa-compose "^3.0.0"
21392151

2140-
koa-cors@^0.0.16:
2141-
version "0.0.16"
2142-
resolved "https://registry.yarnpkg.com/koa-cors/-/koa-cors-0.0.16.tgz#98107993a7909e34c042986c5ec6156d77f3432e"
2143-
integrity sha1-mBB5k6eQnjTAQphsXsYVbXfzQy4=
2144-
21452152
koa-router@^10.0.0:
21462153
version "10.0.0"
21472154
resolved "https://registry.yarnpkg.com/koa-router/-/koa-router-10.0.0.tgz#7bc76a031085731e61fc92c1683687b2f44de6a4"
@@ -2799,6 +2806,13 @@ prepend-http@^2.0.0:
27992806
resolved "https://registry.yarnpkg.com/prepend-http/-/prepend-http-2.0.0.tgz#e92434bfa5ea8c19f41cdfd401d741a3c819d897"
28002807
integrity sha1-6SQ0v6XqjBn0HN/UAddBo8gZ2Jc=
28012808

2809+
prettier-linter-helpers@^1.0.0:
2810+
version "1.0.0"
2811+
resolved "https://registry.yarnpkg.com/prettier-linter-helpers/-/prettier-linter-helpers-1.0.0.tgz#d23d41fe1375646de2d0104d3454a3008802cf7b"
2812+
integrity sha512-GbK2cP9nraSSUF9N2XwUwqfzlAFlMNYYl+ShE/V+H8a9uNl/oUqB1w2EL54Jh0OlyRSd8RfWYJ3coVS4TROP2w==
2813+
dependencies:
2814+
fast-diff "^1.1.2"
2815+
28022816
prettier@^2.1.2:
28032817
version "2.1.2"
28042818
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.1.2.tgz#3050700dae2e4c8b67c4c3f666cdb8af405e1ce5"
@@ -3320,6 +3334,11 @@ strip-json-comments@~2.0.1:
33203334
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-2.0.1.tgz#3c531942e908c2697c0ec344858c286c7ca0a60a"
33213335
integrity sha1-PFMZQukIwml8DsNEhYwobHygpgo=
33223336

3337+
styled-reset@^4.3.1:
3338+
version "4.3.1"
3339+
resolved "https://registry.yarnpkg.com/styled-reset/-/styled-reset-4.3.1.tgz#7834a8fa843bb8f57e13396e3ef6c191248aa1ac"
3340+
integrity sha512-H8f2YsjUtJ+oc2y4fB3Jzm/BzByqC15o8DaBvdSMPktT6Q+Dmw74ZbOqhMR7Zzt01p6sN3MN14qqI17kfRCRiQ==
3341+
33233342
supports-color@^5.3.0, supports-color@^5.5.0:
33243343
version "5.5.0"
33253344
resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-5.5.0.tgz#e2e69a44ac8772f78a1ec0b35b689df6530efc8f"

โ€Žfe/package.jsonโ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
"@types/react-dom": "^16.9.8",
1414
"@types/styled-components": "^5.1.4",
1515
"axios": "^0.21.0",
16+
"dayjs": "^1.9.6",
1617
"mobx": "^6.0.4",
1718
"mobx-react": "^7.0.5",
18-
"dayjs": "^1.9.6",
1919
"polished": "^4.0.4",
2020
"react": "^17.0.1",
2121
"react-dom": "^17.0.1",
2222
"react-scripts": "4.0.0",
2323
"styled-components": "^5.2.1",
24+
"styled-reset": "^4.3.1",
2425
"typescript": "~4.0.5",
2526
"web-vitals": "^0.2.4"
2627
},

โ€Žfe/src/apis/axios.tsโ€Ž

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import axios from 'axios';
2+
3+
const instance = axios.create({
4+
baseURL: `${process.env.REACT_APP_API_URL}:${process.env.REACT_APP_API_PORT}`,
5+
timeout: 1000,
6+
});
7+
8+
instance.interceptors.response.use(
9+
(response) => response.data,
10+
(error) => Promise.reject(error),
11+
);
12+
export default instance;

โ€Žfe/src/apis/urls/index.tsโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export default {
2-
transaction: (accountObjId: string) => `api/transactions/${accountObjId}`,
2+
transaction: (accountObjId: string) => `/api/transactions/${accountObjId}`,
33
};

0 commit comments

Comments
ย (0)