Skip to content

Commit

Permalink
feat: add countries API
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyTeterin committed Mar 9, 2021
1 parent aa15302 commit 524bbc7
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/models/countries.ts
Original file line number Diff line number Diff line change
@@ -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<ICountry>('Countries', CountrySchema);
23 changes: 23 additions & 0 deletions src/models/sights.ts
Original file line number Diff line number Diff line change
@@ -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<ISight>('Sights', SightSchema);
19 changes: 19 additions & 0 deletions src/routes/countries.ts
Original file line number Diff line number Diff line change
@@ -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)
}
)
4 changes: 4 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,4 +16,6 @@ root.get('/', async (req: express.Request, res: express.Response, next: express.
export const routes: Array<IRoute> = [
{ endpoint: '/', router: root },
{ endpoint: '/auth', router: authRouter },
{ endpoint: '/countries', router: countriesRouter },
{ endpoint: '/sights', router: sightsRouter },
]
21 changes: 21 additions & 0 deletions src/routes/sights.ts
Original file line number Diff line number Diff line change
@@ -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)
}
)

0 comments on commit 524bbc7

Please sign in to comment.