1- import express , { Request , Response } from " express" ;
1+ import express , { Request , Response } from ' express' ;
22import { RequiresAuthentication } from '../middleware' ;
3- import _ from " lodash" ;
4- import { createThumbnail } from " ../utils/image" ;
5- import multer from " multer" ;
3+ import _ from ' lodash' ;
4+ import { createThumbnail } from ' ../utils/image' ;
5+ import multer from ' multer' ;
66
77var router = express . Router ( ) ;
88
@@ -28,11 +28,7 @@ router.get('/', RequiresAuthentication, async (req: Request, res: Response) => {
2828 . count ( 'RowId' , { as : 'count' } ) ;
2929
3030 photos = await db
31- . column (
32- 'PH.*' ,
33- { CommunityName : 'CO.Name' } ,
34- { PlaceName : 'PL.PrimaryName' }
35- )
31+ . column ( 'PH.*' , { CommunityName : 'CO.Name' } , { PlaceName : 'PL.PrimaryName' } )
3632 . select ( )
3733 . from ( 'dbo.photo as PH' )
3834 . join ( 'dbo.Community as CO' , 'PH.CommunityId' , '=' , 'CO.Id' )
@@ -47,17 +43,10 @@ router.get('/', RequiresAuthentication, async (req: Request, res: Response) => {
4743 . limit ( limit )
4844 . offset ( offset ) ;
4945 } else {
50- counter = await db
51- . from ( 'dbo.photo' )
52- . count ( 'RowId' , { as : 'count' } )
53- . first ( ) ;
46+ counter = await db . from ( 'dbo.photo' ) . count ( 'RowId' , { as : 'count' } ) . first ( ) ;
5447
5548 photos = await db
56- . column (
57- 'PH.*' ,
58- { CommunityName : 'CO.Name' } ,
59- { PlaceName : 'PL.PrimaryName' }
60- )
49+ . column ( 'PH.*' , { CommunityName : 'CO.Name' } , { PlaceName : 'PL.PrimaryName' } )
6150 . select ( )
6251 . from ( 'dbo.photo as PH' )
6352 . join ( 'dbo.Community as CO' , 'PH.CommunityId' , '=' , 'CO.Id' )
@@ -70,39 +59,62 @@ router.get('/', RequiresAuthentication, async (req: Request, res: Response) => {
7059 res . status ( 200 ) . send ( { count : counter , body : photos } ) ;
7160} ) ;
7261
73- //LINK BOAT PHOTOS
74- router . post (
75- '/boat/link/:BoatId' ,
76- RequiresAuthentication ,
77- async ( req : Request , res : Response ) => {
62+ //LINK PLACE (aka site) PHOTOS
63+ router . post ( '/place/link/:PlaceId' , RequiresAuthentication , async ( req : Request , res : Response ) => {
64+ try {
7865 const db = req . app . get ( 'db' ) ;
7966
80- const { BoatId } = req . params ;
67+ const { PlaceId } = req . params ;
8168 const { linkPhotos } = req . body ;
8269
83- let currentPhotos = await db
84- . select ( 'Photo_RowID' )
85- . from ( 'boat.Photo' )
86- . where ( 'BoatId' , BoatId ) ;
87- let filteredLinkPhotos = _ . difference (
70+ const currentPhotosForPlace = await db
71+ . select ( 'PhotoId' )
72+ . from ( 'dbo.Photo' )
73+ . where ( 'PlaceId' , PlaceId ) ;
74+
75+ const filteredLinkPhotos = _ . difference (
8876 linkPhotos ,
89- currentPhotos . map ( ( x : any ) => {
90- return x . Photo_RowID ;
77+ currentPhotosForPlace . map ( ( x : any ) => {
78+ return x . PhotoId ;
9179 } )
9280 ) ;
9381
9482 for ( const rowId of filteredLinkPhotos ) {
95- await db
96- . insert ( { BoatId, Photo_RowID : rowId } )
97- . into ( 'boat.photo' )
98- . returning ( '*' )
99- . then ( ( rows : any ) => {
100- return rows ;
101- } ) ;
83+ console . log ( 'LINKING' , { rowId } ) ;
10284 }
10385 res . status ( 200 ) . send ( { message : 'Successfully linked the photos' } ) ;
86+ } catch ( error ) {
87+ console . error ( error ) ;
88+ res . status ( 500 ) . send ( { message : 'Failed to link photos' } ) ;
10489 }
105- ) ;
90+ } ) ;
91+
92+ //LINK BOAT PHOTOS
93+ router . post ( '/boat/link/:BoatId' , RequiresAuthentication , async ( req : Request , res : Response ) => {
94+ const db = req . app . get ( 'db' ) ;
95+
96+ const { BoatId } = req . params ;
97+ const { linkPhotos } = req . body ;
98+
99+ const currentPhotos = await db . select ( 'Photo_RowID' ) . from ( 'boat.Photo' ) . where ( 'BoatId' , BoatId ) ;
100+ const filteredLinkPhotos = _ . difference (
101+ linkPhotos ,
102+ currentPhotos . map ( ( x : any ) => {
103+ return x . Photo_RowID ;
104+ } )
105+ ) ;
106+
107+ for ( const rowId of filteredLinkPhotos ) {
108+ await db
109+ . insert ( { BoatId, Photo_RowID : rowId } )
110+ . into ( 'boat.photo' )
111+ . returning ( '*' )
112+ . then ( ( rows : any ) => {
113+ return rows ;
114+ } ) ;
115+ }
116+ res . status ( 200 ) . send ( { message : 'Successfully linked the photos' } ) ;
117+ } ) ;
106118
107119//LINK PERSON PHOTOS
108120router . post (
@@ -114,11 +126,11 @@ router.post(
114126 const { PersonID } = req . params ;
115127 const { linkPhotos } = req . body ;
116128
117- let currentPhotos = await db
129+ const currentPhotos = await db
118130 . select ( 'PhotoID' )
119131 . from ( 'Person.Photo' )
120132 . where ( 'PersonID' , PersonID ) ;
121- let filteredLinkPhotos = _ . difference (
133+ const filteredLinkPhotos = _ . difference (
122134 linkPhotos ,
123135 currentPhotos . map ( ( x : any ) => {
124136 return x . Photo_RowID ;
@@ -147,11 +159,11 @@ router.post(
147159
148160 const { AirCrashId } = req . params ;
149161 const { linkPhotos } = req . body ;
150- let currentPhotos = await db
162+ const currentPhotos = await db
151163 . select ( 'Photo_RowID' )
152164 . from ( 'AirCrash.Photo' )
153165 . where ( 'YACSINumber' , AirCrashId ) ;
154- let filteredLinkPhotos = _ . difference (
166+ const filteredLinkPhotos = _ . difference (
155167 linkPhotos ,
156168 currentPhotos . map ( ( x : any ) => {
157169 return x . Photo_RowID ;
@@ -172,72 +184,60 @@ router.post(
172184) ;
173185
174186//GET BOAT PHOTOS
175- router . get (
176- '/boat/:boatId' ,
177- RequiresAuthentication ,
178- async ( req : Request , res : Response ) => {
179- const { boatId } = req . params ;
187+ router . get ( '/boat/:boatId' , RequiresAuthentication , async ( req : Request , res : Response ) => {
188+ const { boatId } = req . params ;
180189
181- const db = req . app . get ( 'db' ) ;
182- const { page = 0 , limit = 10 } = req . query ;
183- const offset = Number ( page ) * Number ( limit ) || 0 ;
184-
185- const photos = await db
186- . select ( '*' )
187- . from ( 'boat.photo as BP' )
188- . join ( 'dbo.photo' , 'BP.Photo_RowID' , '=' , 'dbo.photo.RowID' )
189- . where ( 'BP.boatid' , boatId )
190- . limit ( limit )
191- . offset ( offset ) ;
190+ const db = req . app . get ( 'db' ) ;
191+ const { page = 0 , limit = 10 } = req . query ;
192+ const offset = Number ( page ) * Number ( limit ) || 0 ;
192193
193- res . status ( 200 ) . send ( photos ) ;
194- }
195- ) ;
194+ const photos = await db
195+ . select ( '*' )
196+ . from ( 'boat.photo as BP' )
197+ . join ( 'dbo.photo' , 'BP.Photo_RowID' , '=' , 'dbo.photo.RowID' )
198+ . where ( 'BP.boatid' , boatId )
199+ . limit ( limit )
200+ . offset ( offset ) ;
201+
202+ res . status ( 200 ) . send ( photos ) ;
203+ } ) ;
196204
197205// GET AIRCRASH PHOTOS
198- router . get (
199- '/aircrash/:aircrashId' ,
200- RequiresAuthentication ,
201- async ( req : Request , res : Response ) => {
202- const { aircrashId } = req . params ;
206+ router . get ( '/aircrash/:aircrashId' , RequiresAuthentication , async ( req : Request , res : Response ) => {
207+ const { aircrashId } = req . params ;
203208
204- const db = req . app . get ( 'db' ) ;
205- const { page = 0 , limit = 10 } = req . query ;
206- const offset = Number ( page ) * Number ( limit ) || 0 ;
207-
208- const photos = await db
209- . select ( '*' )
210- . from ( 'AirCrash.Photo as AP' )
211- . join ( 'dbo.photo' , 'AP.Photo_RowID' , '=' , 'dbo.photo.RowID' )
212- . where ( 'AP.YACSINumber' , aircrashId )
213- . limit ( limit )
214- . offset ( offset ) ;
209+ const db = req . app . get ( 'db' ) ;
210+ const { page = 0 , limit = 10 } = req . query ;
211+ const offset = Number ( page ) * Number ( limit ) || 0 ;
215212
216- res . status ( 200 ) . send ( photos ) ;
217- }
218- ) ;
213+ const photos = await db
214+ . select ( '*' )
215+ . from ( 'AirCrash.Photo as AP' )
216+ . join ( 'dbo.photo' , 'AP.Photo_RowID' , '=' , 'dbo.photo.RowID' )
217+ . where ( 'AP.YACSINumber' , aircrashId )
218+ . limit ( limit )
219+ . offset ( offset ) ;
220+
221+ res . status ( 200 ) . send ( photos ) ;
222+ } ) ;
219223
220224//GET PERSON PHOTOS
221- router . get (
222- '/people/:PersonID' ,
223- RequiresAuthentication ,
224- async ( req : Request , res : Response ) => {
225- const { PersonID } = req . params ;
225+ router . get ( '/people/:PersonID' , RequiresAuthentication , async ( req : Request , res : Response ) => {
226+ const { PersonID } = req . params ;
226227
227- const db = req . app . get ( 'db' ) ;
228- const { page = 0 , limit = 10 } = req . query ;
229- const offset = Number ( page ) * Number ( limit ) || 0 ;
230-
231- const photos = await db
232- . select ( '*' )
233- . from ( 'Person.Photo as PP' )
234- . join ( 'dbo.photo' , 'PP.PhotoID' , '=' , 'dbo.photo.RowID' )
235- . where ( 'PP.PersonID' , PersonID )
236- . limit ( limit )
237- . offset ( offset ) ;
238- res . status ( 200 ) . send ( photos ) ;
239- }
240- ) ;
228+ const db = req . app . get ( 'db' ) ;
229+ const { page = 0 , limit = 10 } = req . query ;
230+ const offset = Number ( page ) * Number ( limit ) || 0 ;
231+
232+ const photos = await db
233+ . select ( '*' )
234+ . from ( 'Person.Photo as PP' )
235+ . join ( 'dbo.photo' , 'PP.PhotoID' , '=' , 'dbo.photo.RowID' )
236+ . where ( 'PP.PersonID' , PersonID )
237+ . limit ( limit )
238+ . offset ( offset ) ;
239+ res . status ( 200 ) . send ( photos ) ;
240+ } ) ;
241241
242242// ADD NEW BOAT PHOTO
243243router . post (
0 commit comments