const deleteTour: RequestHandler = (req, res, next) => { const tourId = req.params.id; const tourIndex = DataStore.Tours.findIndex((item: any) => item.tourId == tourId); let response = { "status": "failed", "message": "Tour not found" }; if ( tourIndex > -1 ) { DataStore.Tours.splice(tourIndex, 1); response = { "status": "success", "message": "Tour removed!" }; res.json(response); return; } res.json(response); }
const getTourDetail = (id: any) => { const selectedTour = DataStore.Tours.find(t => t.tourId == id); if ( selectedTour ) { const selectedReviews = DataStore.Reviews.filter((item: any) => item.tourId == id); const res = new TourDetail(selectedTour, selectedReviews, selectedTour.img); return res; } const res = {"status": "failed", "message": "Element not found"}; return res; }
const addTour: RequestHandler = (req, res, next) => { const newTour = { tourId: uuid(), tour_name: req.body.tour_name || "", location: req.body.location || "", price: req.body.price || "", img: [] }; DataStore.Tours.push(newTour); const response = { status: "success", message: "New Tour Added!", data: newTour } res.json(response); }
const apiGetTours: RequestHandler = (req, res, next) => { res.json(DataStore.Tours.map((item: any) => new TourSummary(item))); }
const getTourDetail = (req: Request, id: any) => { const tourId = req.params.id; const query = "SELECT * FROM tours WHERE tourId = ${tourId}"; db.one(query, { tourId : tourId }).then((selectedTour: dbModel.tours) => { if (selectedTour) { const imgNames = selectedTour.img || []; const imageUrls = imgNames.map(fileMapper(req.app.get("env"))); const subQueryReviews = "SELECT * FROM reviews WHERE tourId = ${tourId}"; db.any(subQueryReviews, { tourId: tourId } ).then((selectedReviews: dbModel.reviews[]) => { const tourDetailed = new TourDetail(selectedTour, selectedReviews, imageUrls); return tourDetailed; }); } else { return ApiError.errNotFound(); } }) const selectedTour = DataStore.Tours.find(t => t.tourId == id); const res = { "status": "failed", "message": "Element not found" }; return res; }
DataStore.Tours.findIndex((item:any) => item.tourId == tourId)
const patchTour: RequestHandler = (req, res, next) => { const tourId = req.params.id; const tourIndex = DataStore.Tours.findIndex((item:any) => item.tourId == tourId); let response = { "status": "failed", "message": "Tour not found!", "data": {} }; if ( tourIndex > -1 ) { const originalTour = DataStore.Tours[tourIndex]; const newTour = { tourId: tourId, tour_name: req.body.tour_name || originalTour.tour_name, location: req.body.location || originalTour.location, price: req.body.price || originalTour.price, img: originalTour.img }; DataStore.Tours[tourIndex] = newTour; response = { "status": "success", "message": "Tour updated!", "data": newTour }; res.json(response); } }
const updateTour: RequestHandler = (req, res, next) => { const tourId = req.params.id; const tourIndex = DataStore.Tours.findIndex((item:any) => item.tourId == tourId); let response = { "status": "failed", "message": "Tour not found!", "data": {} }; if ( tourIndex > -1 ) { const newTour = { tourId: tourId, tour_name: req.body.tour_name || "", location: req.body.location || "", price: req.body.price || "", img: [] }; DataStore.Tours[tourIndex] = newTour; response = { "status": "success", "message": "Tour updated!", "data": newTour }; res.json(response); } }
DataStore.Tours.findIndex((item: any) => item.tourId == tourId)
DataStore.Tours.findIndex((item:any) => item.tourId == tourId)