Skip to content
This repository has been archived by the owner on May 11, 2021. It is now read-only.

Commit

Permalink
feat(money): add history api declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Jan 27, 2019
1 parent 7e63148 commit e1800b0
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 1 deletion.
2 changes: 2 additions & 0 deletions back/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'

import { MoneyModule } from './money/money.module'
import { UserModule } from './user/user.module'
import { ParseDatePipe } from './utils/http/ParseDatePipe'

@Module({
imports: [UserModule, MoneyModule],
providers: [ParseDatePipe],
})
export class AppModule implements NestModule {
public configure(consumer: MiddlewareConsumer) {
Expand Down
3 changes: 2 additions & 1 deletion back/src/money/money.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'

import { HistoryController } from './presentation/http/controller/HIstoryController'
import { TransactionController } from './presentation/http/controller/TransactionController'

@Module({
controllers: [TransactionController],
controllers: [TransactionController, HistoryController],
})
export class MoneyModule implements NestModule {
public configure(consumer: MiddlewareConsumer) {
Expand Down
35 changes: 35 additions & 0 deletions back/src/money/presentation/http/controller/HistoryController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Controller, Get, Query } from '@nestjs/common'
import {
ApiBearerAuth,
ApiImplicitQuery,
ApiOkResponse,
ApiOperation,
ApiUseTags,
} from '@nestjs/swagger'

import { GroupBy } from '@shared/enum/GroupBy'

import { ParseDatePipe } from '@back/utils/http/ParseDatePipe'

import { HistoryGroupResponse } from '../response/HistoryGroupResponse'

@Controller('money/history')
@ApiUseTags('money')
@ApiBearerAuth()
export class HistoryController {
@Get('grouped')
@ApiOperation({ title: 'Fetch grouped history' })
@ApiOkResponse({
description: 'Fetching history success',
type: HistoryGroupResponse,
isArray: true,
})
@ApiImplicitQuery({ name: 'by', required: false })
public async showGrouped(
@Query('from', ParseDatePipe) from: Date,
@Query('to', ParseDatePipe) to: Date,
@Query('by') by: GroupBy = GroupBy.Month,
): Promise<HistoryGroupResponse[]> {
return []
}
}
36 changes: 36 additions & 0 deletions back/src/money/presentation/http/response/HistoryGroupResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { ApiModelProperty } from '@nestjs/swagger'

import { Currency } from '@shared/enum/Currency'
import { HistoryGroupModel } from '@shared/models/money/HistoryGroupModel'

import { IncomeResponse } from './IncomeResponse'
import { OutcomeResponse } from './OutcomeResponse'

const incomesExample: IncomeResponse[] = [
{
amount: 1200,
currency: Currency.RUB,
source: 'NASA',
date: '2019-03-22',
},
]

const outcomeExample: OutcomeResponse[] = [
{
amount: 1200,
currency: Currency.RUB,
category: 'Cafes',
date: '2019-03-22',
},
]

export class HistoryGroupResponse implements HistoryGroupModel {
@ApiModelProperty({ example: 'Jan' })
public readonly title: string

@ApiModelProperty({ example: incomesExample })
public readonly incomes: IncomeResponse[]

@ApiModelProperty({ example: outcomeExample })
public readonly outcomes: OutcomeResponse[]
}
18 changes: 18 additions & 0 deletions back/src/utils/http/ParseDatePipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Injectable, PipeTransform } from '@nestjs/common'

@Injectable()
export class ParseDatePipe implements PipeTransform<string, Date> {
public transform(value: string): Date {
const date = new Date(value)

if (!this.dateIsValid(date)) {
throw Error()
}

return date
}

private dateIsValid(date: Date): boolean {
return !isNaN(date.getTime())
}
}
5 changes: 5 additions & 0 deletions shared/enum/GroupBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum GroupBy {
Week = 'week',
Month = 'month',
Year = 'year',
}
8 changes: 8 additions & 0 deletions shared/models/money/HistoryGroupModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { IncomeModel } from './IncomeModel'
import { OutcomeModel } from './OutcomeModel'

export interface HistoryGroupModel {
title: string
incomes: IncomeModel[]
outcomes: OutcomeModel[]
}

0 comments on commit e1800b0

Please sign in to comment.