1+ import moment from 'moment' ;
12import Response from '../utils/helpers/response' ;
3+ import db from '../models/index' ;
24import Id from '../utils/helpers/id' ;
35import Data from '../db/property' ;
46import PropertyModel from '../models/property.model' ;
57
68class PropertyController {
79 static async create ( req , res ) {
810 try {
11+ const createQuery = `INSERT INTO
12+ property (owner, status, price, state, city, address, type, created_on, image_url, owner_email, owner_phone_number)
13+ VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING *` ;
914 const property = req . body ;
10- const newId = Id ( Data ) ;
11- property . id = newId ;
12- property . owner = res . locals . user ;
13- property . created_on = new Date ( ) ;
14- const newProperty = new PropertyModel ( { ...property } ) ;
15- newProperty . create ( ) ;
16- return Response . handleSuccess ( 201 , 'Successfully Created' , newProperty . result , res ) ;
15+ let { type } = property ;
16+ const {
17+ price, state, city, address, image_url,
18+ } = property ;
19+ property . owner = res . locals . user . id ;
20+ property . status = 'available' ;
21+ property . created_on = moment ( new Date ( ) ) ;
22+ property . owner_email = res . locals . user . email ;
23+ property . owner_phone_number = res . locals . user . phone_number ;
24+ type = type . toLowerCase ( ) . trim ( ) ;
25+ const values = [
26+ property . owner , property . status , price , state , city , address , type ,
27+ property . created_on , image_url , property . owner_email , property . owner_phone_number
28+ ] ;
29+ const { rows } = await db . query ( createQuery , values ) ;
30+ const obj = rows [ 0 ] ;
31+ property . id = obj . id ;
32+ return Response . handleSuccess ( 201 , 'Successfully Created' , { ...property } , res ) ;
1733 } catch ( error ) {
1834 return Response . handleError ( 500 , error . toString ( ) , res ) ;
1935 }
2036 }
2137
2238 static async findAll ( req , res ) {
2339 try {
24- const listOfProperties = await PropertyModel . findAll ( ) ;
25- if ( listOfProperties ) {
26- return Response . handleSuccess ( 200 , 'Got all property adverts successfully' , listOfProperties , res ) ;
40+ const selectQuery = 'SELECT * FROM property WHERE owner = $1' ;
41+ const owner = res . locals . user . id ;
42+ const { rows, rowCount } = await db . query ( selectQuery , [ owner ] ) ;
43+ if ( rowCount !== 0 ) {
44+ return Response . handleSuccess ( 200 , 'Got all property adverts successfully' , rows , res ) ;
2745 }
2846 return Response . handleError ( 404 , 'No property found' , res ) ;
2947 } catch ( error ) {
@@ -33,9 +51,14 @@ class PropertyController {
3351
3452 static async findOne ( req , res ) {
3553 try {
36- const propertyId = parseInt ( req . params . id , 10 ) ;
37- const property = new PropertyModel ( propertyId ) ;
38- if ( await property . findOne ( ) ) return Response . handleSuccess ( 200 , 'Got the specific property advert successfully' , property . result , res ) ;
54+ const selectQuery = 'SELECT * FROM property WHERE id = $1 AND owner = $2' ;
55+ const owner = res . locals . user . id ;
56+ const id = parseInt ( req . params . property_id , 10 ) ;
57+ const values = [ id , owner ] ;
58+ const { rows, rowCount } = await db . query ( selectQuery , values ) ;
59+ if ( rowCount !== 0 ) {
60+ return Response . handleSuccess ( 200 , 'Got the specific property advert successfully' , rows [ 0 ] , res ) ;
61+ }
3962 return Response . handleError ( 404 , 'Property not found' , res ) ;
4063 } catch ( error ) {
4164 return Response . handleError ( 500 , error . toString ( ) , res ) ;
@@ -44,9 +67,15 @@ class PropertyController {
4467
4568 static async findByType ( req , res ) {
4669 try {
47- const { type } = req . query ;
48- const property = new PropertyModel ( type ) ;
49- if ( await property . findByType ( ) ) return Response . handleSuccess ( 200 , 'Got the property type successfully' , property . result , res ) ;
70+ const selectQuery = 'SELECT * FROM property WHERE type = $1 AND owner = $2' ;
71+ const owner = res . locals . user . id ;
72+ let { type } = req . query ;
73+ type = type . toLowerCase ( ) . trim ( ) ;
74+ const values = [ type , owner ] ;
75+ const { rows, rowCount } = await db . query ( selectQuery , values ) ;
76+ if ( rowCount !== 0 ) {
77+ return Response . handleSuccess ( 200 , 'Got the property type successfully' , rows , res ) ;
78+ }
5079 return Response . handleError ( 404 , 'Property type not found, check the property type query value' , res ) ;
5180 } catch ( error ) {
5281 return Response . handleError ( 500 , error . toString ( ) , res ) ;
@@ -55,34 +84,68 @@ class PropertyController {
5584
5685 static async update ( req , res ) {
5786 try {
58- const propertyId = parseInt ( req . params . id , 10 ) ;
59- const newProperty = req . body ;
60- newProperty . id = propertyId ;
61- const property = new PropertyModel ( { ...newProperty } ) ;
62- await property . updateProperty ( ) ;
63- return Response . handleSuccess ( 200 , 'Updated Successfully' , property . result , res ) ;
87+ const findOneQuery = 'SELECT * FROM property WHERE id = $1 AND owner = $2' ;
88+ const updateQuery = `UPDATE property
89+ SET price = $1, state = $2, city = $3, address = $4, type = $5, image_url = $6
90+ WHERE id = $7 AND owner = $8 RETURNING *` ;
91+ const owner = res . locals . user . id ;
92+ const id = parseInt ( req . params . property_id , 10 ) ;
93+ let values = [ id , owner ] ;
94+ const { rows, rowCount } = await db . query ( findOneQuery , values ) ;
95+ if ( rowCount === 0 ) {
96+ return Response . handleError ( 404 , 'Property not found' , res ) ;
97+ }
98+ const property = req . body ;
99+ values = [
100+ property . price || rows [ 0 ] . price ,
101+ property . state || rows [ 0 ] . state ,
102+ property . city || rows [ 0 ] . city ,
103+ property . address || rows [ 0 ] . address ,
104+ property . type || rows [ 0 ] . type ,
105+ property . image_url || rows [ 0 ] . image_url ,
106+ rows [ 0 ] . id ,
107+ rows [ 0 ] . owner
108+ ] ;
109+ const response = await db . query ( updateQuery , values ) ;
110+ return Response . handleSuccess ( 200 , 'Updated Successfully' , response . rows [ 0 ] , res ) ;
64111 } catch ( error ) {
65112 return Response . handleError ( 500 , error . toString ( ) , res ) ;
66113 }
67114 }
68115
69116 static async markSold ( req , res ) {
70117 try {
71- const id = parseInt ( req . params . id , 10 ) ;
72- const soldProperty = { status : 'sold' } ;
73- const property = new PropertyModel ( { id, ...soldProperty } ) ;
74- await property . updateProperty ( ) ;
75- return Response . handleSuccess ( 200 , 'Mark as sold successfully' , property . result , res ) ;
118+ const updateOneQuery = `UPDATE property
119+ SET status = $1
120+ WHERE id = $2 AND owner = $3 RETURNING *` ;
121+ const id = parseInt ( req . params . property_id , 10 ) ;
122+ const owner = res . locals . user . id ;
123+ const status = 'sold' ;
124+ const values = [ status , id , owner ] ;
125+ const { rows, rowCount } = await db . query ( updateOneQuery , values ) ;
126+ if ( rowCount === 0 ) {
127+ return Response . handleError ( 404 , 'Property not found' , res ) ;
128+ }
129+ return Response . handleSuccess ( 200 , 'Mark as sold successfully' , rows , res ) ;
76130 } catch ( error ) {
77131 return Response . handleError ( 500 , error . toString ( ) , res ) ;
78132 }
79133 }
80134
81135 static async delete ( req , res ) {
82136 try {
83- const propertyId = parseInt ( req . params . id , 10 ) ;
84- const property = new PropertyModel ( propertyId ) ;
85- if ( await property . deleteProperty ( ) ) return Response . handleDelete ( 200 , property . result , res ) ;
137+ const deleteQuery = `DELETE FROM property WHERE id = $1 AND owner = $2
138+ RETURNING *` ;
139+ const is_admin = res . locals . user . is_admin ;
140+ const owner = res . locals . user . id ;
141+ if ( ! is_admin ) return Response . handleError ( 403 , '!!!You do not have access to this endpoint' , res ) ;
142+ const id = parseInt ( req . params . property_id , 10 ) ;
143+ const values = [ id , owner ] ;
144+ const { rowCount } = await db . query ( deleteQuery , values ) ;
145+ const result = { message : 'Deleted successfully' } ;
146+ if ( rowCount !== 0 ) {
147+ return Response . handleDelete ( 200 , result , res ) ;
148+ }
86149 return Response . handleError ( 404 , 'Property id not found' , res ) ;
87150 } catch ( error ) {
88151 return Response . handleError ( 500 , error . toString ( ) , res ) ;
0 commit comments