Init commit

This commit is contained in:
2024-12-22 16:52:30 +03:30
parent 28ab698dfc
commit aeb38982ca
28 changed files with 1532 additions and 68 deletions
@@ -0,0 +1,48 @@
const catchAsync = require('../../globalErrorHandler/catchAsync')
const belongingService = require('./belongingService')
exports.getAllBelongings = catchAsync(async (req, res, next) => {
const belongings = await belongingService.getAllBelongings()
res.status(200).json({
status: 'success',
results: belongings.length,
data: belongings,
})
})
exports.createBelonging = catchAsync(async (req, res, next) => {
const belonging = await belongingService.createBelonging(req.body)
res.status(201).json({
status: 'success',
data: belonging,
})
})
exports.getBelonging = catchAsync(async (req, res, next) => {
const belonging = await belongingService.getBelonging(req.params.belongingId)
res.status(200).json({
status: 'success',
data: belonging,
})
})
exports.updateBelonging = catchAsync(async (req, res, next) => {
const belonging = await belongingService.updateBelonging(req.params.belongingId, req.body)
res.status(200).json({
status: 'success',
data: belonging,
})
})
exports.deleteBelonging = catchAsync(async (req, res, next) => {
await belongingService.deleteBelonging(req.params.belongingId)
res.status(204).json({
status: 'success',
data: null,
})
})
+40
View File
@@ -0,0 +1,40 @@
const mongoose = require('mongoose')
const ObjectID = mongoose.Schema.Types.ObjectId
const belongingSchema = new mongoose.Schema(
{
belongingCode: {
type: String,
required: true,
unique: true,
},
belongingCategory: {
type: ObjectID,
ref: 'Category'
},
projectCode: {
type: String,
required: true,
default: false,
},
relatedProjects : [String],
location: {
type: {
type: String,
enum: ['Point'], // GeoJSON type
required: true,
},
coordinates: {
type: [Number], // Array of numbers: [longitude, latitude]
required: true,
},
},
documents: [String]
},
{ timestamps: true }
)
belongingSchema.index({ location: '2dsphere' }); // Enable geospatial queries
const Belonging = mongoose.model('Belonging', belongingSchema)
module.exports = Belonging
+20
View File
@@ -0,0 +1,20 @@
const express = require('express')
const belongingController = require('./belongingController')
const perm = require('../../middlewares/permissionMiddleware')
const loginCheck = require('../../middlewares/loginCheck')
const router = new express.Router()
router.use(loginCheck.loginProtect)
router.get('/admin/get-all', perm.requires('getallbelongings'), belongingController.getAllBelongings)
router.post('/admin/create', perm.requires('createabelonging'), belongingController.createBelonging)
router.get('/admin/get-one/:belongingId', perm.requires('getabelonging'), belongingController.getBelonging)
router.put('/admin/update/:belongingId', perm.requires('updateabelonging'), belongingController.updateBelonging)
router.delete('/admin/delete/:belongingId', perm.requires('deleteabelonging'), belongingController.deleteBelonging)
module.exports = router
+40
View File
@@ -0,0 +1,40 @@
const Belonging = require('./belongingModel')
const AppError = require('../../globalErrorHandler/AppError')
module.exports.getAllBelongings = async () => {
return Belonging.find({}, null, null)
}
module.exports.createBelonging = async (data) => {
return Belonging.create(data, null)
}
module.exports.getBelonging = async (belongingId) => {
const belonging = await Belonging.findById(belongingId, null, null)
if (!belonging) {
throw new AppError('NoBelongingFound', 404, {})
}
return belonging
}
module.exports.updateBelonging = async (belongingId, data) => {
const belonging = await Belonging.findOneAndUpdate({ _id: belongingId }, data, { new: true })
if (!belonging) {
throw new AppError('NoBelongingFound', 404, {})
}
return belonging
}
module.exports.deleteBelonging = async (belongingId) => {
const belonging = await Belonging.findByIdAndUpdate(belongingId, { isActive: false }, { new: true })
if (!belonging) {
throw new AppError('NoBelongingFound', 404, {})
}
return null
}