From 524bbc73028c2463dc38fce50c8276cd370fbbcd Mon Sep 17 00:00:00 2001 From: Alexey Teterin Date: Tue, 9 Mar 2021 16:21:06 +0500 Subject: [PATCH] feat: add countries API --- src/models/countries.ts | 40 ++++++++++++++++++++++++++++++++++++++++ src/models/sights.ts | 23 +++++++++++++++++++++++ src/routes/countries.ts | 19 +++++++++++++++++++ src/routes/index.ts | 4 ++++ src/routes/sights.ts | 21 +++++++++++++++++++++ 5 files changed, 107 insertions(+) create mode 100644 src/models/countries.ts create mode 100644 src/models/sights.ts create mode 100644 src/routes/countries.ts create mode 100644 src/routes/sights.ts diff --git a/src/models/countries.ts b/src/models/countries.ts new file mode 100644 index 0000000..3feb756 --- /dev/null +++ b/src/models/countries.ts @@ -0,0 +1,40 @@ +import mongoose, { Schema, Document } from 'mongoose' +import { ISight } from './sights'; + +export interface ICountry extends Document { + id: string + nameEN: string + nameRU: string + nameDE: string + capitalEN: string + capitalRU: string + capitalDE: string + capitalLatLng: [Number, Number] + infoEN: string + infoRU: string + infoDE: string + currency: string + pictureURL: string + videoURL: string + sights: ISight[] +} + +export const CountrySchema: Schema = new Schema({ + id: { type: String, required: true, unique: true }, + nameEN: { type: String, required: true, unique: true }, + nameRU: { type: String, required: true, unique: true }, + nameDE: { type: String, required: true, unique: true }, + capitalEN: { type: String, required: true, unique: true }, + capitalRU: { type: String, required: true, unique: true }, + capitalDE: { type: String, required: true, unique: true }, + capitalLatLng: { type: Array, required: true, unique: true }, + infoEN: { type: String, required: true }, + infoRU: { type: String, required: true }, + infoDE: { type: String, required: true }, + currency: { type: String, required: true }, + pictureURL: { type: String, required: true, unique: true }, + videoURL: { type: String, required: true, unique: true }, + sights: { type: Array, required: true, unique: true }, +}) + +export const Countries = mongoose.model('Countries', CountrySchema); diff --git a/src/models/sights.ts b/src/models/sights.ts new file mode 100644 index 0000000..5884228 --- /dev/null +++ b/src/models/sights.ts @@ -0,0 +1,23 @@ +import mongoose, { Schema, Document } from 'mongoose' + +export interface ISight extends Document { + countryID: string + titleEN: string + titleRU: string + titleDE: string + infoEN: string + infoRU: string + infoDE: string +} + +export const SightSchema: Schema = new Schema({ + countryID: { type: String, required: true }, + titleEN: { type: String, required: true, unique: true }, + titleRU: { type: String, required: true, unique: true }, + titleDE: { type: String, required: true, unique: true }, + infoEN: { type: String, required: true }, + infoRU: { type: String, required: true }, + infoDE: { type: String, required: true }, +}) + +export const Sights = mongoose.model('Sights', SightSchema); diff --git a/src/routes/countries.ts b/src/routes/countries.ts new file mode 100644 index 0000000..046d12e --- /dev/null +++ b/src/routes/countries.ts @@ -0,0 +1,19 @@ +import express from 'express' +import { Countries } from '../models/countries' + +export const countriesRouter: express.Router = express.Router() + +countriesRouter.options( + '/', + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + res.send(200) + } +) + +countriesRouter.get( + '/', + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + const countries = await Countries.find({}) + res.json(countries) + } +) diff --git a/src/routes/index.ts b/src/routes/index.ts index 8bc00ae..c2e8096 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,5 +1,7 @@ import express from 'express' import { authRouter } from './auth' +import { countriesRouter } from './countries' +import { sightsRouter } from './sights' export interface IRoute { endpoint: string @@ -14,4 +16,6 @@ root.get('/', async (req: express.Request, res: express.Response, next: express. export const routes: Array = [ { endpoint: '/', router: root }, { endpoint: '/auth', router: authRouter }, + { endpoint: '/countries', router: countriesRouter }, + { endpoint: '/sights', router: sightsRouter }, ] diff --git a/src/routes/sights.ts b/src/routes/sights.ts new file mode 100644 index 0000000..5cadf2e --- /dev/null +++ b/src/routes/sights.ts @@ -0,0 +1,21 @@ +import express from 'express' +import { Sights } from '../models/sights' + +export const sightsRouter: express.Router = express.Router() + +sightsRouter.options( + '/', + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + res.send(200) + } +) + +sightsRouter.get( + '/', + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + const { _id } = req.query; + const filter = _id ? { _id } : {} + const sights = await Sights.find(filter) + res.json(sights) + } +)