loko
This commit is contained in:
		
							
								
								
									
										60
									
								
								api/request.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								api/request.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,60 @@ | ||||
| const express = require("express"); | ||||
| const path = require("path"); | ||||
| const router = express.Router(); | ||||
| const fetch = require('node-fetch'); | ||||
| const { send } = require("process"); | ||||
| const RequestedWine = require(path.join( | ||||
|   __dirname + "/../schemas/RequestedWine" | ||||
| )); | ||||
| const Wine = require(path.join( | ||||
|   __dirname + "/../schemas/Wine" | ||||
| )); | ||||
|  | ||||
| router.use((req, res, next) => { | ||||
|   next(); | ||||
| }); | ||||
|  | ||||
|  | ||||
| router.route("/request").get(async (req, res) => { | ||||
|   const rWines = await RequestedWine.find({}).populate("wine") | ||||
|   return res.send(rWines) | ||||
| }) | ||||
|  | ||||
| router.route("/request").post(async (req, res) => { | ||||
|   const {wine} = req.body | ||||
|   console.log(wine) | ||||
|  | ||||
|   let thisWineIsLOKO = await Wine.findOne({id: wine.id}) | ||||
|  | ||||
|   if(thisWineIsLOKO == undefined){ | ||||
|     thisWineIsLOKO = new Wine({ | ||||
|       name: wine.name, | ||||
|       vivinoLink: wine.vivinoLink, | ||||
|       rating: null, | ||||
|       occurences: null, | ||||
|       image: wine.image, | ||||
|       id: wine.id | ||||
|     }); | ||||
|     await thisWineIsLOKO.save() | ||||
|   } | ||||
|   console.log(thisWineIsLOKO) | ||||
|  | ||||
|   let requestedWine = await RequestedWine.findOne({ "wineId": wine.id}) | ||||
|    | ||||
|   if(requestedWine == undefined){ | ||||
|     // console.log(localWine) | ||||
|     requestedWine = new RequestedWine({ | ||||
|       count: 1, | ||||
|       wineId: wine.id, | ||||
|       wine: thisWineIsLOKO | ||||
|     }) | ||||
|   } else { | ||||
|     requestedWine.count += 1; | ||||
|   } | ||||
|   await requestedWine.save() | ||||
|  | ||||
|   res.send(requestedWine); | ||||
|  | ||||
| }); | ||||
|  | ||||
| module.exports = router; | ||||
| @@ -14,11 +14,13 @@ const convertToOurWineObject = wine => { | ||||
|   if(wine.basic.ageLimit === "18"){ | ||||
|     return { | ||||
|       name: wine.basic.productShortName, | ||||
|       image: `https://bilder.vinmonopolet.no/cache/300x300-0/${wine.basic.productId}-1.jpg`, | ||||
|       vivinoLink: "https://www.vinmonopolet.no/p/" + wine.basic.productId, | ||||
|       rating: wine.basic.alcoholContent, | ||||
|       price: wine.prices[0].salesPrice, | ||||
|       country: wine.origins.origin.country, | ||||
|       vivinoLink: "https://www.vinmonopolet.no/p/" + wine.basic.productId | ||||
|       occurences: 0, | ||||
|       id: wine.basic.productId, | ||||
|       image: `https://bilder.vinmonopolet.no/cache/300x300-0/${wine.basic.productId}-1.jpg`, | ||||
|       price: wine.prices[0].salesPrice.toString(), | ||||
|       country: wine.origins.origin.country | ||||
|     } | ||||
|   } | ||||
| } | ||||
| @@ -34,8 +36,8 @@ router.route("/wineinfo/search").get(async (req, res) => { | ||||
|     } | ||||
|   }) | ||||
|     .then(resp => resp.json()) | ||||
|     .catch(err => console.error(err)) | ||||
|  | ||||
|   const winesConverted = vinmonopoletResponse.map(convertToOurWineObject).filter(Boolean) | ||||
|  | ||||
|   if (vinmonopoletResponse.errors != null) { | ||||
|     return vinmonopoletResponse.errors.map(error => { | ||||
| @@ -48,7 +50,7 @@ router.route("/wineinfo/search").get(async (req, res) => { | ||||
|       } | ||||
|     }) | ||||
|   } | ||||
|  | ||||
|   const winesConverted = vinmonopoletResponse.map(convertToOurWineObject).filter(Boolean) | ||||
|   res.send(winesConverted); | ||||
| }); | ||||
|  | ||||
|   | ||||
| @@ -11,6 +11,7 @@ const retrieveApi = require(path.join(__dirname + "/api/retrieve")); | ||||
| const subscriptionApi = require(path.join(__dirname + "/api/subscriptions")); | ||||
| const loginApi = require(path.join(__dirname + "/api/login")); | ||||
| const wineinfoApi = require(path.join(__dirname + "/api/wineinfo")); | ||||
| const requestApi = require(path.join(__dirname + "/api/request")); | ||||
| const virtualApi = require(path.join(__dirname + "/api/virtualLottery")); | ||||
| const virtualRegistrationApi = require(path.join( | ||||
|   __dirname + "/api/virtualRegistration" | ||||
| @@ -94,6 +95,7 @@ app.use("/", loginApi); | ||||
| app.use("/api/", updateApi); | ||||
| app.use("/api/", retrieveApi); | ||||
| app.use("/api/", wineinfoApi); | ||||
| app.use("/api/", requestApi); | ||||
| app.use("/api/", chatHistory); | ||||
| app.use("/api/lottery", lottery); | ||||
| app.use("/api/virtual/", virtualApi(io)); | ||||
|   | ||||
| @@ -26,7 +26,7 @@ | ||||
|           </div> | ||||
|         </section> | ||||
|         <section class="buttons"> | ||||
|           <button class="vin-button">Send inn denne som ønske</button> | ||||
|           <button class="vin-button" @click="request(wine)">Send inn denne som ønske</button> | ||||
|           <a | ||||
|           v-if="wine.vivinoLink" | ||||
|           :href="wine.vivinoLink" | ||||
| @@ -58,10 +58,28 @@ export default { | ||||
|   methods: { | ||||
|     fetchWineFromVin(){ | ||||
|       if(this.searchString){ | ||||
|         searchForWine(this.searchString) | ||||
|         this.wines = [] | ||||
|         let localSearchString = this.searchString.replace(/ /g,"_"); | ||||
|         searchForWine(localSearchString) | ||||
|           .then(res => this.wines = res) | ||||
|       } | ||||
|     }, | ||||
|     request(wine){ | ||||
|       const options = { | ||||
|         body: JSON.stringify({ | ||||
|           wine: wine | ||||
|         }), | ||||
|          headers: { | ||||
|           'Accept': 'application/json', | ||||
|           'Content-Type': 'application/json' | ||||
|         }, | ||||
|         method: "post" | ||||
|       } | ||||
|  | ||||
|       fetch("http://localhost:30030/api/request", options) | ||||
|         .then(res => res.json()) | ||||
|         .then(console.log) | ||||
|     } | ||||
|   }, | ||||
| } | ||||
| </script> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user