소스 검색

Add recipe insert endpoint. TG-9 TG-2 #in-progress

Tati 7 년 전
부모
커밋
03052eb519
7개의 변경된 파일45개의 추가작업 그리고 6개의 파일을 삭제
  1. 1 1
      ktchn/package.json
  2. 5 0
      ktchn/src/couchdb.ts
  3. 5 0
      ktchn/src/recipes/controller.ts
  4. 16 0
      ktchn/src/recipes/data/index.ts
  5. 0 0
      ktchn/src/recipes/data/schema.ts
  6. 14 0
      ktchn/src/recipes/routes.ts
  7. 4 5
      ktchn/src/server.ts

+ 1 - 1
ktchn/package.json

@@ -40,6 +40,6 @@
     "eslint-plugin-import": "^2.14.0",
     "nodemon": "^1.18.9",
     "ts-node": "^7.0.1",
-    "typescript": "^3.1.3"
+    "typescript": "^3.3.3"
   }
 }

+ 5 - 0
ktchn/src/couchdb.ts

@@ -0,0 +1,5 @@
+import nano from 'nano';
+
+const couchdb = nano(process.env.COUCHDB_URL || 'http://localhost:5984/');
+
+export default couchdb;

+ 5 - 0
ktchn/src/recipes/controller.ts

@@ -0,0 +1,5 @@
+import { Recipe } from './model';
+import { create } from './data';
+import { DocumentInsertResponse } from 'nano';
+
+export const saveRecipe = (data: Recipe): Promise<DocumentInsertResponse> => create(data);

+ 16 - 0
ktchn/src/recipes/data/index.ts

@@ -0,0 +1,16 @@
+import CouchDb from "../../couchdb";
+import { Recipe } from "../model";
+import { MaybeIdentifiedDocument, ViewDocument, MaybeDocument, DocumentInsertResponse } from "nano";
+
+const Recipes = CouchDb.use('recipes');
+
+type CouchDBTypes = 'recipe';
+
+function identifyDocument<T>(document: T, type: CouchDBTypes):MaybeDocument {
+  return {
+    ...document,
+    couchdb_type: type,
+  } as MaybeDocument;
+}
+
+export const create = (recipe: Recipe): Promise<DocumentInsertResponse> => Recipes.insert(identifyDocument(recipe, 'recipe'));

+ 0 - 0
ktchn/src/recipes/data/schema.ts


+ 14 - 0
ktchn/src/recipes/routes.ts

@@ -1,5 +1,7 @@
 import { Request, Response, Router } from "express";
 import Scrape from "./scrape";
+import { saveRecipe } from "./controller";
+import nano = require("nano");
 
 class RecipeRoutes {
   public router: Router;
@@ -14,6 +16,18 @@ class RecipeRoutes {
         message: 'hey recipes bb'
       });
     })
+    this.router.post("/", (req, res, next) => {
+      return saveRecipe(req.body).then((x)=>{
+        res.json(x)
+      }).catch((error: any) => { // TODO: Reserch on CouchDB responses types
+        res.status(error.headers.statusCode).send(
+          {
+            name: error.error,
+            message: error.message,
+          }
+        )
+      });
+    })
     this.router.post("/scrape", (req, res, next) => {
        return Scrape(req.body.url).then((x)=>{
          res.json(x)

+ 4 - 5
ktchn/src/server.ts

@@ -10,7 +10,7 @@ import dotenv from 'dotenv';
 
 class App {
   public express: express.Application;
-  public db: nano.DatabaseScope;
+  //public db: nano.DatabaseScope;
 
   constructor() {
     this.dotENV();
@@ -18,7 +18,7 @@ class App {
     this.middleware();
     this.routes();
     this.launchConf();
-    this.db = this.couchDB();
+    //this.db = this.couchDB();
   }
 
   private dotENV(): void {
@@ -26,10 +26,9 @@ class App {
   }
 
   private couchDB(): nano.DatabaseScope {
-    let db = nano(process.env.COUCHDB_URL || 'http://localhost:5984/').db;
-    let recipes = db.use('recipes');
-    return db;
+    return nano(process.env.COUCHDB_URL || 'http://localhost:5984/').db;
   }
+  
   private routes(): void {
     this.express.use("/recipes", recipeRouter);
     this.express.use("/", rootRouter);