Search with wine object tries to find wines matching name, year and id. This is used for finding a wine from a prelottery wine where their _id do not match.
		
			
				
	
	
		
			60 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			1.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| const path = require("path");
 | |
| const Wine = require(path.join(__dirname, "/schemas/Wine"));
 | |
| 
 | |
| const { WineNotFound } = require(path.join(__dirname, "/vinlottisErrors"));
 | |
| 
 | |
| const addWine = async wine => {
 | |
|   let existingWine = await Wine.findOne({ name: wine.name, id: wine.id, year: wine.year });
 | |
| 
 | |
|   if (existingWine == undefined) {
 | |
|     let newWine = new Wine({
 | |
|       name: wine.name,
 | |
|       vivinoLink: wine.vivinoLink,
 | |
|       rating: wine.rating,
 | |
|       occurences: 1,
 | |
|       id: wine.id,
 | |
|       year: wine.year,
 | |
|       image: wine.image,
 | |
|       price: wine.price,
 | |
|       country: wine.country
 | |
|     });
 | |
|     await newWine.save();
 | |
|     return newWine;
 | |
|   } else {
 | |
|     existingWine.occurences += 1;
 | |
|     await existingWine.save();
 | |
|     return existingWine;
 | |
|   }
 | |
| };
 | |
| 
 | |
| const allWines = () => {
 | |
|   return Wine.find();
 | |
| };
 | |
| 
 | |
| const wineById = id => {
 | |
|   return Wine.findOne({ _id: id }).then(wine => {
 | |
|     if (wine == null) {
 | |
|       throw new WineNotFound();
 | |
|     }
 | |
| 
 | |
|     return wine;
 | |
|   });
 | |
| };
 | |
| 
 | |
| const findWine = wine => {
 | |
|   return Wine.findOne({ name: wine.name, id: wine.id, year: wine.year }).then(wine => {
 | |
|     if (wine == null) {
 | |
|       throw new WineNotFound();
 | |
|     }
 | |
| 
 | |
|     return wine;
 | |
|   });
 | |
| };
 | |
| 
 | |
| module.exports = {
 | |
|   addWine,
 | |
|   allWines,
 | |
|   wineById,
 | |
|   findWine
 | |
| };
 |