| 1 | import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common'; |
| 2 | import { ApiOperation, ApiTags } from '@nestjs/swagger'; |
| 3 | import { AccountService } from './account.service'; |
| 4 | import { Account } from '../../db/schema/account.schema'; |
| 5 | import { ApiResult } from '../../common/decorators/api-result.decorator'; |
| 6 | import { |
| 7 | AccountIdDto, |
| 8 | AccountListByIdsDto, |
| 9 | AccountStatisticsDto, |
| 10 | CreateAccountDto, |
| 11 | UpdateAccountStatusDto, |
| 12 | UpdateAccountStatisticsDto, |
| 13 | GoogleLoginDto, |
| 14 | DeleteAccountsDto, |
| 15 | UpdateAccountDto, |
| 16 | } from './dto/account.dto'; |
| 17 | import { TokenInfo } from 'src/auth/interfaces/auth.interfaces'; |
| 18 | import { GetToken } from 'src/auth/auth.guard'; |
| 19 | import { ParamsValidationPipe } from 'src/validation.pipe'; |
| 20 | import { Public } from '../../auth/auth.guard'; |
| 21 | import { AccountGroup } from '../../db/schema/accountGroup.schema'; |
| 22 | import { DeleteAccountGroupDto } from './accountGroup/dto/accountGroup.dto'; |
| 23 | |
| 24 | @ApiTags('账户') |
| 25 | @Controller('account') |
| 26 | export class AccountController { |
| 27 | constructor(private readonly accountService: AccountService) {} |
| 28 | |
| 29 | @ApiOperation({ summary: '创建账号' }) |
| 30 | @Post('login') |
| 31 | @ApiResult({ type: Account }) |
| 32 | async createOrUpdateAccount( |
| 33 | @GetToken() token: TokenInfo, |
| 34 | @Body(new ParamsValidationPipe()) body: CreateAccountDto, |
| 35 | ) { |
| 36 | return this.accountService.addOrUpdateAccount({ |
| 37 | userId: token.id, |
| 38 | ...body, |
| 39 | }); |
| 40 | } |
| 41 | |
| 42 | @ApiOperation({ summary: '创建或更新账号' }) |
| 43 | @Post('update') |
| 44 | @ApiResult({ type: Account }) |
| 45 | async updateOrUpdateAccount( |
| 46 | @GetToken() token: TokenInfo, |
| 47 | @Body(new ParamsValidationPipe()) body: UpdateAccountDto, |
| 48 | ) { |
| 49 | return this.accountService.addOrUpdateAccount({ |
| 50 | userId: token.id, |
| 51 | ...body, |
| 52 | }); |
| 53 | } |
| 54 | |
| 55 | @ApiOperation({ summary: '更新账号状态' }) |
| 56 | @Post('status') |
| 57 | @ApiResult({ type: Account }) |
| 58 | async updateAccountStatus( |
| 59 | @Body(new ParamsValidationPipe()) body: UpdateAccountStatusDto, |
| 60 | ) { |
| 61 | return this.accountService.updateAccountStatus(body.id, body.status); |
| 62 | } |
| 63 | |
| 64 | @ApiOperation({ summary: '获取账号信息' }) |
| 65 | @Get(':id') |
| 66 | @ApiResult({ type: Account }) |
| 67 | async getAccountInfo(@Param(new ParamsValidationPipe()) param: AccountIdDto) { |
| 68 | return this.accountService.getAccountById(param.id); |
| 69 | } |
| 70 | |
| 71 | @ApiOperation({ summary: '获取用户所有账户' }) |
| 72 | @Get('list/all') |
| 73 | @ApiResult({ type: [Account] }) |
| 74 | async getUserAccounts(@GetToken() token: TokenInfo) { |
| 75 | return this.accountService.getAccounts(token.id); |
| 76 | } |
| 77 | |
| 78 | @ApiOperation({ summary: '删除多个账户' }) |
| 79 | @Post('deletes') |
| 80 | @ApiResult({ type: AccountGroup }) |
| 81 | async deletes( |
| 82 | @GetToken() token: TokenInfo, |
| 83 | @Body(new ParamsValidationPipe()) body: DeleteAccountsDto, |
| 84 | ) { |
| 85 | return this.accountService.deleteAccounts(body.ids, token.id); |
| 86 | } |
| 87 | |
| 88 | @ApiOperation({ summary: '获取账户列表' }) |
| 89 | @Post('list/ids') |
| 90 | @ApiResult({ type: Account }) |
| 91 | async getAccountListByIds( |
| 92 | @GetToken() token: TokenInfo, |
| 93 | @Query(new ParamsValidationPipe()) query: AccountListByIdsDto, |
| 94 | ) { |
| 95 | return this.accountService.getAccountListByIds(token.id, query.ids); |
| 96 | } |
| 97 | |
| 98 | @ApiOperation({ summary: '获取账户总数' }) |
| 99 | @Get('count') |
| 100 | @ApiResult({ type: Number }) |
| 101 | async getAccountCount(@GetToken() token: TokenInfo) { |
| 102 | return this.accountService.getAccountCount(token.id); |
| 103 | } |
| 104 | |
| 105 | @ApiOperation({ summary: '获取账户统计' }) |
| 106 | @Get('statistics') |
| 107 | @ApiResult({ type: Account }) |
| 108 | async getAccountStatistics( |
| 109 | @GetToken() token: TokenInfo, |
| 110 | @Query(new ParamsValidationPipe()) query: AccountStatisticsDto, |
| 111 | ) { |
| 112 | return this.accountService.getAccountStatistics(token.id, query.type); |
| 113 | } |
| 114 | |
| 115 | @ApiOperation({ summary: '删除账户' }) |
| 116 | @Post('delete/:id') |
| 117 | @ApiResult({ type: Boolean }) |
| 118 | async deleteAccount( |
| 119 | @GetToken() token: TokenInfo, |
| 120 | @Param(new ParamsValidationPipe()) param: AccountIdDto, |
| 121 | ) { |
| 122 | return this.accountService.deleteAccount(param.id, token.id); |
| 123 | } |
| 124 | |
| 125 | @ApiOperation({ summary: '更新账户统计信息' }) |
| 126 | @Post('statistics/update') |
| 127 | @ApiResult({ type: Boolean }) |
| 128 | async updateAccountStatistics( |
| 129 | @GetToken() token: TokenInfo, |
| 130 | @Body(new ParamsValidationPipe()) body: UpdateAccountStatisticsDto, |
| 131 | ) { |
| 132 | const { |
| 133 | id, |
| 134 | fansCount, |
| 135 | readCount, |
| 136 | likeCount, |
| 137 | collectCount, |
| 138 | commentCount, |
| 139 | income, |
| 140 | workCount, |
| 141 | } = body; |
| 142 | return this.accountService.updateAccountStatistics( |
| 143 | id, |
| 144 | fansCount, |
| 145 | readCount, |
| 146 | likeCount, |
| 147 | collectCount, |
| 148 | commentCount, |
| 149 | income, |
| 150 | workCount, |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | // @ApiOperation({ summary: 'Google登录' }) |
| 155 | // @Post('login/google') |
| 156 | // @Public() |
| 157 | // @ApiResult({ type: Account }) |
| 158 | // async googleLogin( |
| 159 | // @Body(new ParamsValidationPipe()) body: GoogleLoginDto, |
| 160 | // ) { |
| 161 | // // console.log(body.clientId, body.credential) |
| 162 | // return this.accountService.googleLogin(body.clientId, body.credential); |
| 163 | // } |
| 164 | } |
| 165 |