-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/vietnam-devs/coolstore-mi…
- Loading branch information
Showing
6 changed files
with
108 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -343,3 +343,4 @@ build | |
**/*/*.local.yaml | ||
**/*/key*.* | ||
src/web/shrinkwrap.yaml | ||
**/dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,39 @@ | ||
// import * as mongoose from 'mongoose'; | ||
|
||
// const schema = mongoose.Schema; | ||
import * as mongoose from 'mongoose' | ||
const Schema = mongoose.Schema | ||
|
||
export interface Product { | ||
id: string; | ||
name: string; | ||
desc: string; | ||
price: number; | ||
imageUrl: string; | ||
id: string | ||
name: string | ||
desc: string | ||
price: number | ||
imageUrl: string | ||
} | ||
|
||
export interface ProductCreateRequest { | ||
name: string; | ||
desc: string; | ||
price: number; | ||
imageUrl: string; | ||
name: string | ||
desc: string | ||
price: number | ||
imageUrl: string | ||
} | ||
|
||
// export const ProductSchema = new schema({ | ||
// id: { | ||
// type: String, | ||
// required: 'Enter a id' | ||
// }, | ||
// name: { | ||
// type: String, | ||
// required: 'Product name cannot be blank' | ||
// }, | ||
// desc: { | ||
// type: String | ||
// }, | ||
// price: { | ||
// type: Number, | ||
// required: 'Price cannot be blank' | ||
// }, | ||
// imageUrl: { | ||
// type: String, | ||
// required: 'Image cannot be blank' | ||
// } | ||
// }); | ||
export const ProductSchema = new Schema({ | ||
id: { | ||
type: String, | ||
required: 'Enter a id' | ||
}, | ||
name: { | ||
type: String, | ||
required: 'Product name cannot be blank' | ||
}, | ||
desc: { | ||
type: String | ||
}, | ||
price: { | ||
type: Number, | ||
required: 'Price cannot be blank' | ||
}, | ||
imageUrl: { | ||
type: String, | ||
required: 'Image cannot be blank' | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,37 @@ | ||
/* tslint:disable */ | ||
import { Controller, ValidateParam, FieldErrors, ValidateError, TsoaRoute } from 'tsoa'; | ||
import { ProductsController } from './controllers/productsController'; | ||
|
||
export function RegisterRoutes(app: any) { | ||
app.get('/v1/Products', | ||
function (request: any, response: any, next: any) { | ||
const controller = new ProductsController(); | ||
|
||
const promise = controller.GetAll.apply(controller); | ||
promiseHandler(controller, promise, response, next); | ||
}); | ||
app.get('/v1/Products/:productId', | ||
function (request: any, response: any, next: any) { | ||
const controller = new ProductsController(); | ||
|
||
const promise = controller.Get.apply(controller); | ||
promiseHandler(controller, promise, response, next); | ||
}); | ||
app.post('/v1/Users', | ||
function (request: any, response: any, next: any) { | ||
const controller = new ProductsController(); | ||
|
||
|
||
const promise = controller.Create.apply(controller); | ||
promiseHandler(controller, promise, response, next); | ||
}); | ||
app.delete('/v1/Users/:userId', | ||
function (request: any, response: any, next: any) { | ||
const controller = new ProductsController(); | ||
|
||
|
||
const promise = controller.Delete.apply(controller); | ||
promiseHandler(controller, promise, response, next); | ||
}); | ||
|
||
function promiseHandler(controllerObj: any, promise: any, response: any, next: any) { | ||
return Promise.resolve(promise) | ||
.then((data: any) => { | ||
let statusCode; | ||
if (controllerObj instanceof Controller) { | ||
const controller = controllerObj as Controller | ||
const headers = controller.getHeaders(); | ||
Object.keys(headers).forEach((name: string) => { | ||
response.set(name, headers[name]); | ||
}); | ||
|
||
statusCode = controller.getStatus(); | ||
} | ||
|
||
if (data) { | ||
response.status(statusCode || 200).json(data); | ||
} else { | ||
response.status(statusCode || 204).end(); | ||
} | ||
}) | ||
.catch((error: any) => next(error)); | ||
} | ||
import { | ||
Controller, | ||
ValidateParam, | ||
FieldErrors, | ||
ValidateError, | ||
TsoaRoute | ||
} from 'tsoa' | ||
import { ProductsController } from './controllers/productsController' | ||
|
||
export function RegisterRoutes(app: any, basePath: string) { | ||
app.get(`${basePath}api/products/:productId`, async (req, res) => { | ||
console.info(req.params) | ||
const controller = new ProductsController() | ||
var product = await controller.Get.apply(controller) | ||
res.send(product) | ||
}) | ||
|
||
app.get(`${basePath}api/products`, async (req, res) => { | ||
const controller = new ProductsController() | ||
var products = await controller.GetAll.apply(controller) | ||
res.send(products) | ||
}) | ||
|
||
app.post(`${basePath}api/products`, async (req, res) => { | ||
console.info(req.body) | ||
const controller = new ProductsController() | ||
var product = await controller.Create.apply(controller) | ||
res.send(product) | ||
}) | ||
|
||
app.get(`${basePath}healthz`, (req, res) => { | ||
res.send({ | ||
status: 'Healthy!' | ||
}) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,31 @@ | ||
import './controllers/productsController'; | ||
import * as bodyParser from 'body-parser'; | ||
import * as express from 'express'; | ||
import * as methodOverride from 'method-override'; | ||
import { RegisterRoutes } from './routes'; | ||
const swaggerJSON = require('./swagger.json'); | ||
import * as swaggerUI from "swagger-ui-express"; | ||
|
||
const app = express(); | ||
|
||
let basePath = process.env.BASE_PATH; | ||
import * as bodyParser from 'body-parser' | ||
import * as express from 'express' | ||
import * as methodOverride from 'method-override' | ||
import * as swaggerUI from 'swagger-ui-express' | ||
import { RegisterRoutes } from './routes' | ||
import './controllers/productsController' | ||
import * as mongoose from 'mongoose' | ||
|
||
const swaggerJSON = require('./swagger.json') | ||
const app = express() | ||
|
||
let basePath = process.env.BASE_PATH | ||
if (!basePath) { | ||
basePath = '/'; | ||
basePath = '/' | ||
} | ||
console.log(`Base path is ${basePath}`); | ||
console.log(`Base path is ${basePath}`) | ||
|
||
app.use('/swagger.json', express.static(__dirname + './swagger.json')); | ||
app.use(bodyParser.urlencoded({ extended: true })) | ||
app.use(bodyParser.json()) | ||
app.use(methodOverride()) | ||
|
||
app.use('/', swaggerUI.serve, swaggerUI.setup(swaggerJSON)); | ||
RegisterRoutes(app, basePath) | ||
|
||
app.use(bodyParser.urlencoded({ extended: true })); | ||
app.use(bodyParser.json()); | ||
app.use(methodOverride()); | ||
app.use('/swagger.json', express.static(__dirname + './swagger.json')) | ||
|
||
RegisterRoutes(app); | ||
app.use('/swagger', swaggerUI.serve, swaggerUI.setup(swaggerJSON)) | ||
|
||
/* tslint:disable-next-line */ | ||
app.listen(process.env.PORT || 5002, () => { | ||
// migrate for the first time when started | ||
// Product.findProducts().then(products => { | ||
// console.info(products) | ||
// if (products.length <= 0) { | ||
// var seeds = [ | ||
// { | ||
// id: 'ba16da71-c7dd-4eac-9ead-5c2c2244e69f', | ||
// name: 'IPhone 8', | ||
// desc: 'IPhone 8', | ||
// price: 12.5, | ||
// imageUrl: 'https://picsum.photos/400/300?image=0' | ||
// }, | ||
// { | ||
// id: '13d02035-2286-4055-ad2d-6855a60efbbb', | ||
// name: 'IPhone X', | ||
// desc: 'IPhone X', | ||
// price: 20.5, | ||
// imageUrl: 'https://picsum.photos/400/300?image=1' | ||
// }, | ||
// { | ||
// id: 'b8f0a771-339f-4602-a862-f7a51afd5b79', | ||
// name: 'MacBook Pro 2019', | ||
// desc: 'MacBook Pro 2019', | ||
// price: 15.3, | ||
// imageUrl: 'https://picsum.photos/400/300?image=2' | ||
// } | ||
// ] | ||
|
||
// seeds.map(x => { | ||
// var newProduct = new Product() | ||
// newProduct.createProduct(x) | ||
// }) | ||
// } | ||
// }) | ||
console.info(`App's running at http://localhost:${process.env.PORT || 5002}`); | ||
console.info('Press CTRL-C to stop\n'); | ||
}); | ||
console.info(`App's running at http://localhost:${process.env.PORT || 5002}`) | ||
console.info('Press CTRL-C to stop\n') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters