From 254472ae56055ea35a9233bbdcb68e4958b2feaf Mon Sep 17 00:00:00 2001 From: ludvij Date: Sun, 3 Apr 2022 19:12:37 +0200 Subject: [PATCH] fixed geocoder, added name to order --- restapi/geocoder/geocoder.ts | 42 +++++++++++++++++----------- restapi/interfaces/OrderInterface.ts | 1 + restapi/routers/geocoderRouter.ts | 23 +++++++++++++++ restapi/schemas/OrderSchema.ts | 17 +++++------ restapi/server.ts | 15 ++-------- 5 files changed, 61 insertions(+), 37 deletions(-) create mode 100644 restapi/routers/geocoderRouter.ts diff --git a/restapi/geocoder/geocoder.ts b/restapi/geocoder/geocoder.ts index d57cfb5..3b01fd3 100644 --- a/restapi/geocoder/geocoder.ts +++ b/restapi/geocoder/geocoder.ts @@ -7,23 +7,22 @@ const Nominatim = require('nominatim-geocoder') const geocoder = new Nominatim() type Location = { - street: string, - city: string, + street?: string, + city?: string, region?: string country?: string, } -function tryGeocode(query: string, errorMsg: string = "") { +async function tryGeocode(query: string, errorMsg: string = "") { return geocoder.search({q: query}) .then((response : any) => { if (response[0] != {}) { let lat : number = response[0]["lat"] let lon : number = response[0]["lon"] let distance : number = distanceInKmBetweenEarthCoordinates(lat, lon, 43.354838799999996, -5.851292403149609) - console.log(response) return { "distance" : distance, - "price" : distance * parseInt(process.env.PRICE_PER_KM || ""), + "price" : distance * parseFloat(process.env.PRICE_PER_KM || ""), "error" : errorMsg, "success" : true } @@ -38,27 +37,38 @@ function tryGeocode(query: string, errorMsg: string = "") { }) } -export function getPriceFromAddress(address: Location) { - let geocodeTry = tryGeocode(address.city + ", " + address.street) - if (geocodeTry.success) return geocodeTry +export async function getPriceFromAddress(address: Location) { - if (address.region == null) + if (address.street == null || address.street == '') { + return {"error" : "needed street information"} + } + if (address.city == null || address.city == '') { + return {"error" : "needed city information"} + } + if (address.region == null || address.region == '') { return {"error" : "not enough info provided, region is needed"} + } + if (address.country == null || address.country == '') { + return {"error" : "not enough info provided, city is needed"} + } + + + let geocodeTry = await tryGeocode(address.city + ", " + address.street) + if (geocodeTry.success) return geocodeTry - geocodeTry = tryGeocode(address.region + ", " + address.street) + geocodeTry = await tryGeocode(address.region + ", " + address.street) if (geocodeTry.success) return geocodeTry - geocodeTry = tryGeocode(address.city, "failed specific info, falling back to city") + geocodeTry = await tryGeocode(address.region + ", " + address.city, "failed specific info, falling back to city") if (geocodeTry.success) return geocodeTry - geocodeTry = tryGeocode(address.region, "failed specific info, falling back to region") + geocodeTry = await tryGeocode("Province: " + address.region, "failed specific info, falling back to region") if (geocodeTry.success) return geocodeTry - - if (address.country == null) - return {"error" : "not enough info provided, city is needed"} - geocodeTry = tryGeocode(address.country, "failed specific info, falling back to city") + geocodeTry = await tryGeocode("Country: " + address.country, "failed specific info, falling back to country") if (geocodeTry.success) return geocodeTry + + return {"error" : "Address not found"} } diff --git a/restapi/interfaces/OrderInterface.ts b/restapi/interfaces/OrderInterface.ts index 8b95162..12685d8 100644 --- a/restapi/interfaces/OrderInterface.ts +++ b/restapi/interfaces/OrderInterface.ts @@ -4,6 +4,7 @@ import IProduct from "./ProductInterface"; export default interface IOrder extends Document { webId: string addres: string + name: string shipping: number totalPrice: number products: Map diff --git a/restapi/routers/geocoderRouter.ts b/restapi/routers/geocoderRouter.ts new file mode 100644 index 0000000..c4e8f40 --- /dev/null +++ b/restapi/routers/geocoderRouter.ts @@ -0,0 +1,23 @@ + +import { Router, Request, Response} from 'express'; +import * as bodyParser from 'body-parser'; +import { getPriceFromAddress } from '../geocoder/geocoder'; + +const geocoderRouter:Router = Router() + +geocoderRouter.use(bodyParser.json()); + +geocoderRouter.post("", async (req: Request, res: Response) => { + await getPriceFromAddress(req.body) + .then((response : any) => { + res.status(200).send(response); + console.log(response) + }).catch((error : any) => { + res.status(500).json({ + message: error.message, + error + }) + }) +}) + +export default geocoderRouter; \ No newline at end of file diff --git a/restapi/schemas/OrderSchema.ts b/restapi/schemas/OrderSchema.ts index 6a43880..9d990cf 100644 --- a/restapi/schemas/OrderSchema.ts +++ b/restapi/schemas/OrderSchema.ts @@ -5,14 +5,15 @@ const ObjectId = Schema.Types.ObjectId; const Map = Schema.Types.Map; const orderSchema = new Schema ( - { - webId: {type: String, required: true}, - address: String, - shipping: Number, - totalPrice: Number, - products: {type: Map, of: Number} - - } + { + webId: {type: String, required: true}, + name: String, + address: {type: String, required: true}, + shipping: {type: Number, required: true }, + totalPrice: {type: Number, required: true }, + products: {type: Map, of: Number, required: true} + + } ); diff --git a/restapi/server.ts b/restapi/server.ts index cb0e2d7..f5a0ae9 100644 --- a/restapi/server.ts +++ b/restapi/server.ts @@ -8,8 +8,8 @@ import sellerRouter from "./routers/SellerRouter"; import productRouter from "./routers/ProductRouter"; import orderRouter from "./routers/OrderRouter"; import solidRouter from "./solid/solidRouter"; +import geocoderRouter from "./routers/geocoderRouter"; -import { getPriceFromAddress } from "./geocoder/geocoder"; import 'dotenv/config' @@ -32,19 +32,8 @@ app.use("/seller", sellerRouter) app.use("/product", productRouter) app.use("/order", orderRouter) app.use("/solid", solidRouter) +app.use("/geocode", geocoderRouter) -app.get("/geocode/:address", async (req: Request, res: Response) => { - getPriceFromAddress(req.body) - .then((response : any) => { - res.status(200).send(response); - console.log(response) - }).catch((error : any) => { - res.status(500).json({ - message: error.message, - error - }) - }) -}) // Connect to the database and start the server. mongoose.connect('mongodb+srv://cluster0.2sj0r.mongodb.net/', {