97 lines
2.6 KiB
JavaScript
97 lines
2.6 KiB
JavaScript
const catchAsync = require('../../globalErrorHandler/catchAsync')
|
|
const propertyService = require('./propertyService')
|
|
const { eventEmitter } = require('../../events/eventEmitter')
|
|
|
|
exports.searchProperties = catchAsync(async (req, res, next) => {
|
|
const { facets, belongings, totalCount, pagesCount, searchEngine } = await propertyService.searchProperties(req.query)
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
facets,
|
|
properties: belongings,
|
|
totalCount,
|
|
pagesCount,
|
|
searchEngine,
|
|
})
|
|
})
|
|
|
|
exports.searchPropertiesMongo = catchAsync(async (req, res, next) => {
|
|
const { facets, belongings, totalCount, pagesCount, searchEngine } = await propertyService.searchPropertiesMongo(req.query)
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
facets,
|
|
properties: belongings,
|
|
totalCount,
|
|
pagesCount,
|
|
searchEngine: searchEngine || 'mongo',
|
|
})
|
|
})
|
|
|
|
|
|
exports.indexAllProperties = catchAsync(async (req, res, next) => {
|
|
const { indexedCount } = await propertyService.indexAllProperties()
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
indexedCount,
|
|
})
|
|
})
|
|
|
|
exports.indexOneProperty = catchAsync(async (req, res, next) => {
|
|
await propertyService.indexOneProperty(req.params.propertyId)
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
})
|
|
})
|
|
|
|
exports.getAllProperties = catchAsync(async (req, res, next) => {
|
|
const properties = await propertyService.getAllProperties()
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
results: properties.length,
|
|
data: properties,
|
|
})
|
|
})
|
|
|
|
exports.createProperty = catchAsync(async (req, res, next) => {
|
|
const property = await propertyService.createProperty(req.body)
|
|
eventEmitter.emit('createProperty', property)
|
|
|
|
res.status(201).json({
|
|
status: 'success',
|
|
data: property,
|
|
})
|
|
})
|
|
|
|
exports.getProperty = catchAsync(async (req, res, next) => {
|
|
const property = await propertyService.getProperty(req.params.propertyId)
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
data: property,
|
|
})
|
|
})
|
|
|
|
exports.updateProperty = catchAsync(async (req, res, next) => {
|
|
const property = await propertyService.updateProperty(req.params.propertyId, req.body)
|
|
await propertyService.indexAllProperties()
|
|
|
|
res.status(200).json({
|
|
status: 'success',
|
|
data: property,
|
|
})
|
|
})
|
|
|
|
exports.deleteProperty = catchAsync(async (req, res, next) => {
|
|
await propertyService.deleteProperty(req.params.propertyId)
|
|
// eventEmitter.emit('deleteProperty')
|
|
|
|
res.status(204).json({
|
|
status: 'success',
|
|
data: null,
|
|
})
|
|
})
|