Skip to content

Commit

Permalink
[add] Check Event model & controller
Browse files Browse the repository at this point in the history
[add] models & generics of Input, Filter, List & User
[optimize] update Read Me document
  • Loading branch information
TechQuery committed Sep 16, 2023
1 parent 0de27c0 commit 4601dd9
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 7 deletions.
18 changes: 18 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,24 @@ pnpm dock
pnpm docker
```

## Releasing

### Deploy Application

```shell
git checkout master
git tag v0.6.0 # this version tag comes from ./package.json
git push origin master --tags
```

### Publish Type Package

```shell
git checkout master
git tag type-v0.6.0 # this version tag comes from ./type/package.json
git push origin master --tags
```

[1]: https://kaiyuanshe.cn
[2]: https://github.com/kaiyuanshe/KYS-service/actions/workflows/deploy-production.yml
[3]: https://github.com/settings/tokens
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kys-service",
"version": "0.5.0",
"version": "0.6.0",
"license": "AGPL-3.0",
"author": "[email protected]",
"description": "RESTful API service of KaiYuanShe",
Expand Down
46 changes: 46 additions & 0 deletions src/controller/CheckEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
Authorized,
Body,
CurrentUser,
Get,
JsonController,
Post,
QueryParams
} from 'routing-controllers';
import { ResponseSchema } from 'routing-controllers-openapi';

import {
BaseFilter,
CheckEvent,
CheckEventChunk,
CheckEventInput,
User,
dataSource
} from '../model';

@JsonController('/event/check')
export class CheckEventController {
store = dataSource.getRepository(CheckEvent);

@Post()
@Authorized()
@ResponseSchema(CheckEvent)
createOne(@CurrentUser() createdBy: User, @Body() data: CheckEventInput) {
return this.store.save({ ...data, createdBy });
}

@Get('/session')
@Authorized()
@ResponseSchema(CheckEventChunk)
async getSessionList(
@CurrentUser() createdBy: User,
@QueryParams() { pageSize, pageIndex }: BaseFilter
) {
const [list, count] = await this.store.findAndCount({
where: { createdBy },
skip: pageSize * (pageIndex - 1),
take: pageSize
});
return { list, count };
}
}
35 changes: 34 additions & 1 deletion src/model/Base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { IsDateString, IsInt, IsOptional } from 'class-validator';
import { Type } from 'class-transformer';
import {
IsDateString,
IsInt,
IsOptional,
IsString,
Min
} from 'class-validator';
import { NewData } from 'mobx-restful';
import { CreateDateColumn, PrimaryGeneratedColumn } from 'typeorm';

export abstract class Base {
Expand All @@ -15,3 +23,28 @@ export abstract class Base {
@CreateDateColumn()
updatedAt?: string;
}

export type InputData<T> = NewData<Omit<T, keyof Base>, Base>;

export class BaseFilter {
@Type(() => Number)
@IsInt()
@Min(1)
@IsOptional()
pageSize?: number = 10;

@Type(() => Number)
@IsInt()
@Min(1)
@IsOptional()
pageIndex?: number = 1;

@IsString()
@IsOptional()
keywords?: string;
}

export interface ListChunk<T extends Base> {
count: number;
list: T[];
}
65 changes: 65 additions & 0 deletions src/model/CheckEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Type } from 'class-transformer';
import {
IsInt,
IsLatLong,
IsOptional,
IsString,
Min,
ValidateNested
} from 'class-validator';
import { Column, Entity } from 'typeorm';

import { ListChunk } from './Base';
import { UserBase, UserInputData } from './User';

@Entity()
export class CheckEvent extends UserBase {
@IsLatLong()
@IsOptional()
@Column({ nullable: true })
coordinate?: string;

@IsString()
@Column()
activityId: string;

@IsString()
@Column()
activityName: string;

@IsString()
@Column()
agendaId: string;

@IsString()
@Column()
agendaTitle: string;
}

export class CheckEventInput implements UserInputData<CheckEvent> {
@IsLatLong()
@IsOptional()
coordinate?: string;

@IsString()
activityId: string;

@IsString()
activityName: string;

@IsString()
agendaId: string;

@IsString()
agendaTitle: string;
}

export class CheckEventChunk implements ListChunk<CheckEvent> {
@IsInt()
@Min(0)
count: number;

@Type(() => CheckEvent)
@ValidateNested({ each: true })
list: CheckEvent[];
}
24 changes: 21 additions & 3 deletions src/model/User.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { Type } from 'class-transformer';
import {
IsEnum,
IsMobilePhone,
IsOptional,
IsString,
IsUrl
IsUrl,
ValidateNested
} from 'class-validator';
import { JsonWebTokenError, JwtPayload } from 'jsonwebtoken';
import { Column, Entity } from 'typeorm';

import { ParameterizedContext } from 'koa';
import { NewData } from 'mobx-restful';
import { Column, Entity, ManyToOne } from 'typeorm';

import { Base } from './Base';

export enum Gender {
Expand Down Expand Up @@ -39,6 +42,21 @@ export class User extends Base {
avatar?: string;
}

export abstract class UserBase extends Base {
@Type(() => User)
@ValidateNested()
@ManyToOne(() => User)
createdBy: User;

@Type(() => User)
@ValidateNested()
@IsOptional()
@ManyToOne(() => User)
updatedBy?: User;
}

export type UserInputData<T> = NewData<Omit<T, keyof UserBase>, UserBase>;

export type AuthingAddress = Partial<
Record<'country' | 'postal_code' | 'region' | 'formatted', string>
>;
Expand Down
4 changes: 3 additions & 1 deletion src/model/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { DataSource } from 'typeorm';
import { SqliteConnectionOptions } from 'typeorm/driver/sqlite/SqliteConnectionOptions';

import { DATABASE_URL, isProduct } from '../utility';
import { CheckEvent } from './CheckEvent';
import { User } from './User';

export * from './Base';
export * from './CheckEvent';
export * from './Crawler';
export * from './User';

Expand All @@ -18,7 +20,7 @@ const commonOptions: Pick<
'synchronize' | 'entities' | 'migrations'
> = {
synchronize: true,
entities: [User],
entities: [User, CheckEvent],
migrations: [`${isProduct ? '.tmp' : 'migration'}/*.ts`]
};

Expand Down
3 changes: 2 additions & 1 deletion type/package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "@kaiyuanshe/kys-service",
"version": "0.4.0",
"version": "0.6.0",
"types": "index.d.ts",
"dependencies": {
"@types/jsonwebtoken": "^9.0.3",
"@types/koa": "^2.13.9",
"mobx-restful": "^0.6.11",
"web-fetch": "^1.3.2"
}
}

0 comments on commit 4601dd9

Please sign in to comment.