Commit 3188a486 authored by wudizhanche1000's avatar wudizhanche1000

AppService 添加功能

parent ef683b5b
...@@ -19,7 +19,7 @@ export class AppDetailComponent implements OnInit { ...@@ -19,7 +19,7 @@ export class AppDetailComponent implements OnInit {
async ngOnInit() { async ngOnInit() {
this.route.parent.params this.route.parent.params
.switchMap((params: Params) => this.appService.getApp(params['id'])) .switchMap((params: Params) => this.appService.find(params['id']))
.subscribe(app => { .subscribe(app => {
this.app = app; this.app = app;
}); });
......
...@@ -21,7 +21,7 @@ export class AppLocalesComponent implements OnInit { ...@@ -21,7 +21,7 @@ export class AppLocalesComponent implements OnInit {
async ngOnInit() { async ngOnInit() {
this.route.parent.params this.route.parent.params
.switchMap((params: Params) => this.appService.getApp(params['id'])) .switchMap((params: Params) => this.appService.find(params['id']))
.subscribe(app => { .subscribe(app => {
this.app = app; this.app = app;
this.locales = Object.keys(app.name); this.locales = Object.keys(app.name);
......
...@@ -19,7 +19,7 @@ export class AppPackagesComponent implements OnInit { ...@@ -19,7 +19,7 @@ export class AppPackagesComponent implements OnInit {
async ngOnInit() { async ngOnInit() {
this.route.parent.params this.route.parent.params
.switchMap((params: Params) => this.appService.getApp(params['id'])) .switchMap((params: Params) => this.appService.find(params['id']))
.subscribe(app => { .subscribe(app => {
this.app = app; this.app = app;
}); });
......
...@@ -15,7 +15,7 @@ export class AppComponent implements OnInit { ...@@ -15,7 +15,7 @@ export class AppComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.route.params this.route.params
.switchMap((params: Params) => this.appService.getApp(params['id'])) .switchMap((params: Params) => this.appService.find(params['id']))
.subscribe(app => { .subscribe(app => {
this.app = app; this.app = app;
}); });
......
...@@ -6,18 +6,29 @@ import {Observable} from 'rxjs/Rx'; ...@@ -6,18 +6,29 @@ import {Observable} from 'rxjs/Rx';
/** /**
* Created by weijian on 2016/12/30. * Created by weijian on 2016/12/30.
*/ */
function addJsonOptions(options?: RequestOptions): RequestOptions {
if (options) {
options.headers.append('Content-Type', 'application/json');
} else {
let headers = new Headers({'Content-Type': 'application/json'})
options = new RequestOptions({headers: headers});
}
return options;
}
@Injectable() @Injectable()
export class AppService { export class AppService {
constructor(private http: Http) { constructor(private http: Http) {
} }
getApps(): Promise<App[]> { all(): Promise<App[]> {
return this.http.get('http://localhost:8000/apps').map((response) => response.json().map((app: any) => new App(app))).toPromise(); return this.http.get('http://localhost:8000/apps')
} .map((response) => {
return response.json().map((app: any) => new App(app));
getApp(id: string): Promise<App> { }).catch(this.handleError)
return this.http.get(`http://localhost:8000/apps/${id}`).map((response) => new App(response.json())).toPromise(); .toPromise();
} }
handleError(error: Response | any) { handleError(error: Response | any) {
...@@ -32,8 +43,7 @@ export class AppService { ...@@ -32,8 +43,7 @@ export class AppService {
} }
save(app: App): Promise<any> { save(app: App): Promise<any> {
let headers = new Headers({'Content-Type': 'application/json'}); let options = addJsonOptions();
let options = new RequestOptions({headers: headers});
return this.http.post(`http://localhost:8000/apps/${app.id}`, app, options) return this.http.post(`http://localhost:8000/apps/${app.id}`, app, options)
.map((response) => response.json()) .map((response) => response.json())
.catch(this.handleError) .catch(this.handleError)
...@@ -41,7 +51,26 @@ export class AppService { ...@@ -41,7 +51,26 @@ export class AppService {
} }
update(app: App) { update(app: App) {
let options = addJsonOptions();
return this.http.patch(`http://localhost:8000/apps/${app.id}`, app, options)
.map((response) => response.json())
.catch(this.handleError)
.toPromise();
}
remove(app: App) {
return this.http.delete(`http://localhost:8000/apps/${app.id}`)
.map((response) => response.json())
.catch(this.handleError)
.toPromise();
}
find(id: string): Promise<App> {
return this.http.get(`http://localhost:8000/apps/${id}`)
.map((response) => response.json)
.catch(this.handleError)
.toPromise();
} }
} }
...@@ -22,7 +22,7 @@ export class AppsComponent implements OnInit { ...@@ -22,7 +22,7 @@ export class AppsComponent implements OnInit {
} }
async getApps() { async getApps() {
this.apps = await this.appService.getApps(); this.apps = await this.appService.all();
} }
async create_app() { async create_app() {
......
...@@ -32,6 +32,7 @@ export class Model { ...@@ -32,6 +32,7 @@ export class Model {
static async findOne(query: Object): Promise<any|null> { static async findOne(query: Object): Promise<any|null> {
let collection = await this.getCollection(); let collection = await this.getCollection();
let result = await collection.find(query).limit(1).next(); let result = await collection.find(query).limit(1).next();
console.log(result, query,this.dbName);
if (result) { if (result) {
return new this(result); return new this(result);
} else { } else {
......
...@@ -2,10 +2,9 @@ ...@@ -2,10 +2,9 @@
* Created by weijian on 2016/12/28. * Created by weijian on 2016/12/28.
*/ */
import Router = require('koa-router'); import Router = require('koa-router');
import {NotFound, BadRequest, InternalError} from '../koa/errors'; import {NotFound, InternalError} from '../koa/errors';
import {Model} from '../db/mongo';
import {App} from '../models/app'; import {App} from '../models/app';
import {ModelExistsError, ModelError, ModelInvalidError} from '../models/errors'; import {ModelError, ModelInvalidError} from '../models/errors';
const router = new Router(); const router = new Router();
router.get('/apps', async(ctx, next) => { router.get('/apps', async(ctx, next) => {
...@@ -38,20 +37,23 @@ router.post('/apps/:id', async(ctx, next) => { ...@@ -38,20 +37,23 @@ router.post('/apps/:id', async(ctx, next) => {
}); });
router.patch('/apps/:id', async(ctx, next) => { router.patch('/apps/:id', async(ctx, next) => {
let app: App|null = await Model.findOne({id: ctx.params.id}); let app: App|null = await App.findOne({id: ctx.params.id});
if (!app) { if (!app) {
throw new NotFound(`App ${ctx.params.id} Not Found`); throw new NotFound(`App ${ctx.params.id} Not Found`);
} }
if (!ctx.request.body.id || ctx.request.body.id !== app.id) {
throw new ModelInvalidError('Can not change AppID');
}
Object.assign(app, ctx.request.body); Object.assign(app, ctx.request.body);
await app.save(); ctx.body = await app.save();
}); });
router.delete('/apps/:id', async(ctx, next) => { router.delete('/apps/:id', async(ctx, next) => {
let result = await App.remove({id: ctx.params.id}); let result = await App.remove({id: ctx.params.id});
if (!result.deletedCount) { if (!result.result.n) {
throw new NotFound(`App ${ctx.params.id} Not Found`); throw new NotFound(`App ${ctx.params.id} Not Found`);
} }
ctx.body = result; ctx.body = result.result;
}); });
export default router; export default router;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment