Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API validate update property #9

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions apps/zidence-api/src/app/middlewares/properties/controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Request, Response } from 'express'
import slugify from 'slugify'
import { validationResult } from "express-validator"

import prisma from '../prismaClient'

export interface PropertyTypes {
id?: string
name?: string
slug?: string
price?: number
Expand All @@ -22,6 +24,11 @@ export interface PropertyTypes {
}

export const addProperty = async (req: Request, res: Response) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}

try {
const {
name,
Expand Down Expand Up @@ -100,3 +107,70 @@ export const getProperties = async (req: Request, res: Response) => {
})
}
}

export const updateProperty = async (req: Request, res: Response) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
}

try {
const { propertyId } = req.params
const {
name,
images,
price,
category,
subcategory,
owner,
developer,
yearBuilt,
lotSize,
unitSize,
numberOfBedrooms,
numberOfBathrooms,
parkingLot,
listOfNearestObjects,
}: PropertyTypes = req.body

const updatedProperty = await prisma.property.update({
where: {
id: propertyId,
},
data: {
name,
images,
price,
category,
subcategory,
owner,
developer,
yearBuilt,
lotSize,
unitSize,
numberOfBedrooms,
numberOfBathrooms,
parkingLot,
listOfNearestObjects,
}
})

if (!updatedProperty) {
return res.status(404).json({
success: false,
message: 'Update property failed',
})
}

res.status(200).json({
success: true,
data: updatedProperty
})
} catch (error) {
console.error(error.message)
res.status(400).json({
message: 'Update properties failed',
error: error.message,
})
}
}
32 changes: 30 additions & 2 deletions apps/zidence-api/src/app/middlewares/properties/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
import * as express from 'express'
const router = express.Router()

import { getProperties, addProperty } from './controller'
import { check } from "express-validator"

import { getProperties, addProperty, updateProperty } from './controller'

router.get('/', getProperties)
router.post('/', addProperty)

router.post('/', [
check('name', 'Name Field min.3 characters')
.isLength({ min: 3 }),
check('category', 'Category Field min.3 characters')
.isLength({ min: 3 }),
check('subcategory', 'Subcategory Field min.3 characters')
.isLength({ min: 3 }),
check('developer', 'Developer Field min.3 characters')
.isLength({ min: 3 }),
check('owner', 'Owner Field min.3 characters')
.isLength({ min: 3 })
], addProperty)

router.put("/:propertyId", [
check('name', 'Name Field min.3 characters')
.isLength({ min: 3 }),
check('category', 'Category Field min.3 characters')
.isLength({ min: 3 }),
check('subcategory', 'Subcategory Field min.3 characters')
.isLength({ min: 3 }),
check('developer', 'Developer Field min.3 characters')
.isLength({ min: 3 }),
check('owner', 'Owner Field min.3 characters')
.isLength({ min: 3 })
], updateProperty)


export default router
8 changes: 6 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
module.exports = {
projects: ['<rootDir>/apps/zidence-api'],
};
projects: [
'<rootDir>/apps/zidence-api',
'<rootDir>/apps/server',
'<rootDir>/apps/server/server',
],
}
9 changes: 9 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@
}
},
"projects": {
"--server": {
"tags": []
},
".-server-server": {
"tags": []
},
"server-server": {
"tags": []
},
"zidence-api": {
"tags": []
}
Expand Down
65 changes: 53 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@prisma/client": "^2.26.0",
"cors": "2.8.5",
"express": "4.17.1",
"express-validator": "^6.12.0",
"morgan": "1.10.0",
"slugify": "^1.5.3",
"tslib": "^2.0.0"
Expand Down
Loading