Skip to content

Commit

Permalink
refact, please ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
i5ting committed Apr 25, 2023
1 parent 6f4d50d commit 3c3092a
Show file tree
Hide file tree
Showing 16 changed files with 385 additions and 55 deletions.
4 changes: 2 additions & 2 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import simpleRestProvider from "ra-data-simple-rest";
// "X-Total-Count"
// );

// const dataProvider = jsonServerProvider("http://127.0.0.1:7001");
const dataProvider = jsonServerProvider("http://127.0.0.1:7003");

const dataProvider = jsonServerProvider("https://jsonplaceholder.typicode.com");
// const dataProvider = jsonServerProvider("https://jsonplaceholder.typicode.com");

const App = () => (
<Admin
Expand Down
2 changes: 1 addition & 1 deletion client/src/users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const UserList = () => {
<EmailField source="email" />
<TextField source="phone" />
<MyUrlField source="website" />
<TextField source="company.name" />
<TextField source="company" />
</Datagrid>
)}
</List>
Expand Down
33 changes: 33 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,36 @@ $ npm run dev
$ npm run test
```


## User


```
curl http://localhost:7002/users/
HTTP/1.1 200 OK
Vary: Origin
Content-Range: users 0-10/7
Access-Control-Expose-Headers: X-Total-Count
X-Total-Count: 7
Content-Type: application/json; charset=utf-8
set-cookie: locale=en-us; path=/; max-age=31557600; expires=Fri, 12 Apr 2024 07:51:37 GMT
Content-Length: 2346
Date: Thu, 13 Apr 2023 01:51:37 GMT
Connection: keep-alive
Keep-Alive: timeout=5
```


create

```
curl -X POST http://localhost:7002/users/ -H 'Content-Type: application/json; charset=UTF-8' -d '{"name":"alfred","password":"123456","avatar":"avatar","description":"description"}'
{"name":"alfred","description":"description","password":"123456","language":"zh","avatar":"avatar","created_at":"2023-04-13 02:19:17","updated_at":"2023-04-13 02:19:10","accessToken":null,"refreshToken":null,"scope":null,"token_type":null,"id_token":null,"expire_date":null,"id":8,"time_zone":"Asia/Shanghai"}%
```



curl -X POST http://localhost:7003/posts/ -H 'Content-Type: application/json; charset=UTF-8' -d '{"title":"test1","body":"127.0.0.1","userId":1}'
4 changes: 3 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
"description": "",
"private": true,
"dependencies": {
"@koa/cors": "^4.0.0",
"@midwayjs/bootstrap": "^3.0.0",
"@midwayjs/core": "^3.0.0",
"@midwayjs/decorator": "^3.0.0",
"@midwayjs/info": "^3.0.0",
"@midwayjs/koa": "^3.0.0",
"@midwayjs/logger": "^2.14.0",
"@midwayjs/static-file": "^3.4.12",
"@midwayjs/cross-domain": "^3.11.3",
"@midwayjs/typeorm": "^3.0.0",
"@midwayjs/validate": "^3.0.0",
"@midwayjs/view-nunjucks": "^3.4.12",
Expand All @@ -36,7 +38,7 @@
},
"scripts": {
"start": "NODE_ENV=production node ./bootstrap.js",
"dev": "cross-env NODE_ENV=local midway-bin dev --ts --port 7002",
"dev": "cross-env NODE_ENV=local midway-bin dev --ts --port 7003",
"test": "midway-bin test --ts",
"cov": "midway-bin cov --ts",
"lint": "mwts check",
Expand Down
22 changes: 21 additions & 1 deletion server/src/config/config.default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ export default {
view: {
defaultViewEngine: 'nunjucks',
},
cors: {
// 允许跨域的方法,【默认值】为 GET,HEAD,PUT,POST,DELETE,PATCH
allowMethods: '*',
// 设置 Access-Control-Allow-Origin 的值,【默认值】会获取请求头上的 origin
// 也可以配置为一个回调方法,传入的参数为 request,需要返回 origin 值
// 例如:http://test.midwayjs.org
// 如果设置了 credentials,则 origin 不能设置为 *
origin: '*',
// 设置 Access-Control-Allow-Headers 的值,【默认值】会获取请求头上的 Access-Control-Request-Headers
allowHeaders: '*',
// 设置 Access-Control-Expose-Headers 的值
exposeHeaders: '*',
// 设置 Access-Control-Allow-Credentials,【默认值】false
// 也可以配置为一个回调方法,传入的参数为 request,返回值为 true 或 false
credentials: false,
// 是否在执行报错的时候,把跨域的 header 信息写入到 error 对的 headers 属性中,【默认值】false
keepHeadersOnError: true,
// 设置 Access-Control-Max-Age
maxAge: 36000,
},
typeorm: {
dataSource: {
default: {
Expand All @@ -17,7 +37,7 @@ export default {
port: 3306,
username: 'root',
password: '',
database: '1stblue',
database: 'demo',
synchronize: true, // 如果第一次使用,不存在表,有同步的需求可以写 true,注意会丢数据
logging: true,

Expand Down
2 changes: 2 additions & 0 deletions server/src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import { ReportMiddleware } from './middleware/report.middleware';
import * as view from '@midwayjs/view-nunjucks';
import { WeatherErrorFilter } from './filter/weather.filter';
import * as orm from '@midwayjs/typeorm';
import * as crossDomain from "@midwayjs/cross-domain";

@Configuration({
imports: [
orm,
koa,
crossDomain,
validate,
{
component: info,
Expand Down
4 changes: 2 additions & 2 deletions server/src/controller/api.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class APIController {

@Get('/get_user')
async getUser(@Query('uid') uid) {
const user = await this.userService.getUser({ uid });
return { success: true, message: 'OK', data: user };
// const user = await this.userService.getUser({ uid });
return { success: true, message: 'OK', data: {} };
}
}
100 changes: 100 additions & 0 deletions server/src/controller/post.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import {
Controller,
Inject,
Get,
Del,
Put,
Post,
Body,
Param,
Query,
} from "@midwayjs/core";
import { PostService } from "../service/post.service";
import { Context } from "@midwayjs/koa";
import { Post as PostEntity } from "../entity/post.entity";

@Controller("/posts")
export class PostController {
@Inject()
ctx: Context;

@Inject()
service: PostService;

@Del("/:id")
async findOne2(@Param("id") uid: string) {
console.dir(uid);

const a = await this.service.delete(uid);
console.dir(a[0]);

return a;
}

@Put("/:id")
async update(@Param("id") uid: string) {
console.log("put uid = ") + uid;

const body = this.ctx.request.body;

console.dir(body);

let user = await this.service.update(uid, body);

return user;
}

@Get("/:id")
async findOne(@Param("id") uid: string) {
console.log(" uid = ") + uid;
const a = await this.service.findById(uid);
// console.dir(a[0]);

return a;
}

@Post("/")
async create(@Body() user: PostEntity) {
console.log(" create");
console.dir(user);
const a = await this.service.save(user);

return a;
}

@Get("/")
async home(
@Query("_filter") filter: string,
@Query("_order") order: "DESC" | "ASC",
@Query("_page") page: number,
@Query("_perPage") perPage: number,
@Query("_sort") sort: string
) {
const query = this.ctx.query;

console.dir(query);
console.log(" all");

console.dir(page);
console.dir(perPage);
console.dir(filter);
console.dir(order);
console.dir(sort);

const a = await this.service.find(
page,
perPage,
filter,
order,
sort
);

this.ctx.set("Content-Range", `users 0-10/${a.length}`);
this.ctx.set("Access-Control-Expose-Headers", `Content-Range`);

this.ctx.set("X-Total-Count", `${a.length}`);
this.ctx.set("Access-Control-Expose-Headers", `X-Total-Count`);

return a;
}
}
101 changes: 93 additions & 8 deletions server/src/controller/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,100 @@
import { Controller, Inject, Get } from '@midwayjs/core';
import { PhotoService } from '../service/photo.service';
import {
Controller,
Inject,
Get,
Del,
Put,
Post,
Body,
Param,
Query,
} from "@midwayjs/core";
import { UserService } from "../service/user.service";
import { Context } from "@midwayjs/koa";
import { User } from "../entity/user.entity";

@Controller('/users')
@Controller("/users")
export class UserController {
@Inject()
userService: PhotoService;
ctx: Context;

@Inject()
service: UserService;

@Del("/:id")
async findOne2(@Param("id") uid: string) {
console.dir(uid);

const a = await this.service.delete(uid);
console.dir(a[0]);

return a;
}

@Put("/:id")
async update(@Param("id") uid: string) {
console.log("put uid = ") + uid;

const body = this.ctx.request.body;

console.dir(body);

let user = await this.service.update(uid, body);

return user;
}

@Get("/:id")
async findOne(@Param("id") uid: string) {
console.log(" uid = ") + uid;
const a = await this.service.findById(uid);
// console.dir(a[0]);

return a;
}

@Post("/")
async create(@Body() user: User) {
console.log(" create");
console.dir(user);
const a = await this.service.create(user);

return a;
}

@Get("/")
async home(
@Query("_filter") filter: string,
@Query("_order") order: "DESC" | "ASC",
@Query("_page") page: number,
@Query("_perPage") perPage: number,
@Query("_sort") sort: string
) {
const query = this.ctx.query;

console.dir(query);
console.log(" all");

console.dir(page);
console.dir(perPage);
console.dir(filter);
console.dir(order);
console.dir(sort);

const a = await this.service.find(
page,
perPage,
filter,
order,
sort
);

this.ctx.set("Content-Range", `users 0-10/${a.length}`);
this.ctx.set("Access-Control-Expose-Headers", `Content-Range`);

this.ctx.set("X-Total-Count", `${a.length}`);
this.ctx.set("Access-Control-Expose-Headers", `X-Total-Count`);

@Get('/')
async home() {
console.log(' all');
const a = await this.userService.findPhotos();
return a;
}
}
Loading

0 comments on commit 3c3092a

Please sign in to comment.