Commit af2efa57 authored by nano's avatar nano

fix

parent 1cf95efe
# Created by .ignore support plugin (hsz.mobi)
/node_modules/*
/node_modules/@types/*
node_modules/
/.idea/
*.js
*.js.map
node-debug.log
!/node_modules/@types/
!/node_modules/@types/mongorito/
!/node_modules/@types/is-zip/
!systemjs.config.js
/mongodb_config.json
.DS_Store
......
This diff is collapsed.
......@@ -15,6 +15,7 @@ interface Platform<T> {
}
interface Package{
id: string;
name: string;
platforms: Platform<string[]>;
locales: I18n<string[]>;
files: File[];
......
......@@ -15,9 +15,9 @@ export interface Action {
}
export interface File {
path: string,
size: number,
hash: string,
path: string;
size: number;
hash: string;
}
......@@ -26,6 +26,8 @@ export class Package extends Model {
@field
id: string;
@field
name: string;
@field
appId: string;
@field
version: string;
......
/**
* Created by weijian on 2017/1/6.
*/
export function isZip()
interface isZip {
(f: Buffer|Uint8Array): boolean
}
declare module "is-zip" {
export = is_zip;
}
declare var is_zip: isZip;
/**
* Created by weijian on 2016/12/29.
*/
/// <reference types="mongodb" />
import * as mongodb from "mongodb";
declare module Mongorito {
function connect(url: string): Promise<mongodb.Db>
function setDriver(driver: any);
function getDriver(): any;
interface Options {
[skip: string]: string|string[];
}
class Model {
constructor(o: any, options?: Object);
get(key: string): any;
set(key: string, value: Object);
unset(key: string);
toJSON();
before(action: string, method: Function);
before(action: string, method: Function[]);
before(action: string, method: string);
before(action: string, method: string[]);
after(action: string, method: Function);
after(action: string, method: Function[]);
after(action: string, method: string);
after(action: string, method: string[]);
around(action: string, method: Function);
around(action: string, method: Function[]);
around(action: string, method: string);
around(action: string, method: string[]);
/**
* Configure model (usually, set hooks here)
* Supposed to be overriden
*
* @api public
*/
configure();
/**
* Save a model
*
* @param {Object} options - options for save operation
* @api public
*/
save(options?: Options): Promise<any>;
/**
* Create a model
*
* @api private
*/
create(options?: Options): Promise<any>;
update(options?: Options): Promise<any>;
remove(options?: Options): Promise<any>;
/**
* Atomically increment a model property
*
* @param {Object} props - set of properties and values
* @param {Object} options - options for update operation
* @api public
*/
inc(props: Object, options: Options);
/**
* Find documents
*
* @param {Object} query - find conditions, same as this.where()
* @api public
*/
static find(query: Object): Promise<any[]>;
/**
* Count documents
*
* @param {Object} query - find conditions, same as this.where()
* @api public
*/
static count(query: Object): Promise<any>;
/**
* Get distinct
*
* @param {String} field for distinct
* @param {Object} query - query to filter the results
* @see http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#distinct
* @api public
*/
static distinct(filed: string, query?: Object): Promise<any>;
/**
* Aggregation query
*
* @param {String} pipeline aggregation pipeline
* @param {Object} options - Options to be passed to aggregation pipeline
* @see http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#distinct
* @api public
*/
static aggregate(pipeline: Object[]): Promise<any>;
/**
* Find all documents in a collection
*
* @api public
*/
static all(): Promise<any[]>
/**
* Find one document
*
* @param {Object} query - find conditions, same as this.where()
* @api public
*/
static findOne(query: Object): Promise<any>;
/**
* Find a document by ID
*
* @param {ObjectID} id - document id
* @api public
*/
static findById(id: string): Promise<any>;
/**
* Remove documents
*
* @param {Object} query - remove conditions, same as this.where()
* @api public
*/
static remove(query: Object): Promise<any>;
/**
* Drop collection
*
* @api public
*/
static drop(): Promise<any>;
}
}
export=Mongorito
......@@ -4,11 +4,25 @@
import Router = require('koa-router');
import {NotFound, InternalError} from '../koa/errors';
import {App} from '../models/app';
import {Package} from '../models/Package'
import {ModelError, ModelInvalidError} from '../models/errors';
const router = new Router();
router.get('/apps', async(ctx, next) => {
ctx.body = await App.all();
let apps: App[]|null = await App.all();
apps = await Promise.all(apps.map(async app => {
if(app.packages && app.packages.length > 0) {
app.packages = await Promise.all(app.packages.map(async id => {
return await Package.findOne({id})
}))
}
return app
}))
ctx.body = apps
});
router.get('/apps/:id', async(ctx, next) => {
......@@ -45,8 +59,28 @@ router.patch('/apps/:id', async(ctx, next) => {
throw new ModelInvalidError('Can not change AppID');
}
Object.assign(app, ctx.request.body);
ctx.body = await app.save();
try {
if(ctx.request.body.packages.length > 0) {
ctx.request.body.packages = await Promise.all(ctx.request.body.packages.map(async _p=> {
const p: Package|null = await Package.findOne({ id: _p.id })
if(p) {
Object.assign(p, _p)
await p.save()
return p.id
}
const newP = new Package(_p)
await newP.save()
return newP.id
}))
}
Object.assign(app, ctx.request.body);
ctx.body = await app.save();
} catch (error) {
ctx.throw(403, error)
}
});
router.delete('/apps/:id', async(ctx, next) => {
......
......@@ -5,6 +5,7 @@
import Router = require('koa-router');
import {NotFound} from '../koa/errors';
import {Package} from '../models/package';
import {ModelInvalidError} from '../models/errors';
import * as tmp from 'tmp';
import {ChildProcess} from 'child_process';
import fs = require('fs');
......@@ -76,7 +77,21 @@ router.get('/packages/:id', async(ctx, next) => {
}
ctx.body = p;
});
router.post('/packages/:id', async(ctx, next) => {
// router.post('/packages/:id', async(ctx, next) => {
// let p: Package|null = await Package.findOne({id: ctx.params.id});
// if (!p) {
// throw new NotFound(`Package ${ctx.params.id} Not Found`);
// }
// if (!ctx.request.body.id || ctx.request.body.id !== p.id) {
// throw new ModelInvalidError('Can not change AppID');
// }
// Object.assign(p, ctx.request.body);
// ctx.body = await p.save();
// })
router.patch('/packages/:id', async(ctx, next) => {
new Promise<string|Buffer>((resolve, reject) => {
let downloadUrl = ctx.request.body.downloadUrl;
tmp.tmpName((e, file) => {
......
......@@ -5,8 +5,8 @@ import * as _fs from 'fs'
import { promisifyAll} from 'bluebird'
const fs:any = promisifyAll(_fs)
const busboy = require('async-busboy')
const mime = require('mime')
import * as busboy from 'async-busboy'
import * as mime from 'mime'
const router = new Router();
......
......@@ -5,7 +5,8 @@ import users from './routes/users';
import apps from './routes/apps';
import packages from './routes/packages';
import bodyParser = require('koa-bodyparser');
import Mongorito = require('mongorito');
import * as mongoose from 'mongoose'
// import Mongorito = require('mongorito');
import log4js = require('log4js');
const logger = log4js.getLogger();
......@@ -62,11 +63,18 @@ app.use(users.routes());
app.use(apps.routes());
app.use(upload.routes());
app.use(packages.routes());
Mongorito.connect(url).then(() => {
mongoose.connect(url).then(() => {
app.listen(8001, () => {
console.log("app listening port 8001")
});
});
})
// Mongorito.connect(url).then(() => {
// app.listen(8001, () => {
// console.log("app listening port 8001")
// });
// });
// function getKoaLogger (logger4js, options) {
// if (typeof options === 'object') {
......
......@@ -83,12 +83,19 @@
version "0.0.29"
resolved "https://registry.yarnpkg.com/@types/mime/-/mime-0.0.29.tgz#fbcfd330573b912ef59eeee14602bface630754b"
"@types/mongodb@^2.1.36":
"@types/mongodb@*", "@types/mongodb@^2.1.36":
version "2.1.41"
resolved "https://registry.yarnpkg.com/@types/mongodb/-/mongodb-2.1.41.tgz#92ea0f832b9e0269c7826fb7f899cf86fe5c4df5"
dependencies:
"@types/node" "*"
"@types/mongoose@^4.7.8":
version "4.7.8"
resolved "https://registry.yarnpkg.com/@types/mongoose/-/mongoose-4.7.8.tgz#9dd829635c75198b3ba60b1626fd65ad5698ac8c"
dependencies:
"@types/mongodb" "*"
"@types/node" "*"
"@types/node@*", "@types/node@^6.0.55":
version "6.0.64"
resolved "https://registry.yarnpkg.com/@types/node/-/node-6.0.64.tgz#cdc901411f6301e1437dfed23bc68f54442e4dc8"
......
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