-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#159] Property sectors APIs to post and patch & more refactoring
- Loading branch information
Showing
16 changed files
with
205 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ export class PropertySectorDTO { | |
property: PropertyDTO; | ||
sector: SectorDTO; | ||
sectorKind: string; | ||
status: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,37 @@ | ||
import { Request, Response, NextFunction } from 'express'; | ||
import { UnprocessableEntity } from '../errors/UnprocessableEntity'; | ||
|
||
const validateBody = (parameters: any) => (req: Request, res: Response, next: NextFunction) => { | ||
var missingParameters = []; | ||
for(const parameter of parameters) { | ||
if(!req.body[parameter]) { | ||
export const validateBody = (parameters: any) => ( | ||
req: Request, res: Response, next: NextFunction) => { | ||
const missingParameters = []; | ||
for (const parameter of parameters) { | ||
if (!req.body[parameter]) { | ||
missingParameters.push(parameter); | ||
} | ||
} | ||
} | ||
if (missingParameters.length > 0) { | ||
res.status(422).json(new UnprocessableEntity( | ||
`Request body missing [${missingParameters.toString()}].`)) | ||
`Request body missing [${missingParameters.toString()}].`)); | ||
} else { | ||
next(); | ||
} | ||
} | ||
}; | ||
|
||
export default validateBody; | ||
export const validateArrayBody = (parameters: any) => ( | ||
req: Request, res: Response, next: NextFunction) => { | ||
const missingParameters = new Set(); | ||
for (const object of req.body) { | ||
for (const parameter of parameters) { | ||
if (!object[parameter]) { | ||
missingParameters.add(parameter); | ||
} | ||
} | ||
} | ||
if (missingParameters.size > 0) { | ||
res.status(422).json(new UnprocessableEntity( | ||
'At least one request object is missing parameter ' + | ||
`[${Array.from(missingParameters).toString()}].`)); | ||
} else { | ||
next(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,30 @@ | ||
import { PropertySector } from '../entities/PropertySector'; | ||
import { BaseRepository } from './BaseRepository'; | ||
import { Property } from '../entities/Property'; | ||
import { PROPERTY_SECTOR_FIELDS } from '../constants/FindOptionsFields'; | ||
import { FindOptions } from 'typeorm'; | ||
import { Sector } from '../entities/Sector'; | ||
|
||
class PropertySectorRepository extends BaseRepository<PropertySector> { | ||
|
||
async save(propertySectors: PropertySector[]) { | ||
return await this.getRepositoryConnection(PropertySector).save(propertySectors); | ||
} | ||
|
||
async update(id: number, propertySector: PropertySector) { | ||
return await this.getRepositoryConnection(PropertySector).update( | ||
{ id }, propertySector); | ||
} | ||
|
||
async getSectorsByProperty(property: Property) { | ||
const findOptions : FindOptions<PropertySector> = PROPERTY_SECTOR_FIELDS; | ||
findOptions.where = { property }; | ||
return await this.getRepositoryConnection(PropertySector).find(findOptions); | ||
} | ||
|
||
async getPropertySectorById(id: number) { | ||
return await this.getRepositoryConnection(PropertySector).findOne({ id }); | ||
} | ||
} | ||
|
||
export { PropertySectorRepository }; |
Oops, something went wrong.