-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ibrahimalmalki/doo-106-change-all-apis-to-add-the-id-pk-to-the-docume…
…nt-after (#65) Created new "Image" table. Added IDs, createdAt, and updatedAt to all fields
- Loading branch information
Showing
11 changed files
with
267 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Response, Request } from 'express'; | ||
import { createImage, findImageById } from '../../models/image'; | ||
import { HTTP_STATUS } from '../../constants'; | ||
|
||
/** | ||
* Firebase API controllers, logic for endpoint routes. | ||
* @author Ibrahim Almalki | ||
*/ | ||
|
||
// TODO: JSDOC | ||
|
||
// Create image | ||
export const handleCreateImage = async (req: Request, res: Response) => { | ||
try { | ||
const { imageEncoded } = req.body; | ||
const image = await createImage(imageEncoded); | ||
|
||
res.status(HTTP_STATUS.SUCCESS).json({ image }); | ||
} catch (error) { | ||
console.error('Error creating image:', error); | ||
res | ||
.status(HTTP_STATUS.INTERNAL_SERVER_ERROR) | ||
.json({ error: 'Failed to create image' }); | ||
} | ||
}; | ||
|
||
const validateId = (id: string, res: Response): id is string => { | ||
if (id === undefined) { | ||
res.status(HTTP_STATUS.ERROR).json({ error: 'NO ID PROVIDED' }); | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
const notFoundError = (res: Response) => | ||
res.status(HTTP_STATUS.ERROR).json({ error: 'image not found' }); | ||
|
||
// Get image | ||
export const handleFindImageById = async (req: Request, res: Response) => { | ||
try { | ||
const imageId = req.body.id; // The comment ID parameter is in the body. | ||
if (!validateId(imageId, res)) return; | ||
|
||
const image = await findImageById(imageId as string); | ||
|
||
if (image) { | ||
res.status(HTTP_STATUS.SUCCESS).json({ image }); | ||
} else { | ||
return notFoundError(res); | ||
} | ||
} catch (error) { | ||
console.error('Error finding image by ID:', error); | ||
res | ||
.status(HTTP_STATUS.INTERNAL_SERVER_ERROR) | ||
.json({ error: 'Failed to find image' }); | ||
} | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import express from 'express'; | ||
import { handleCreateImage, handleFindImageById } from './image.controller'; | ||
|
||
/** | ||
* Defines image routes. | ||
* @authors Ibrahim Almalki | ||
*/ | ||
|
||
// The express router | ||
const router = express.Router(); | ||
|
||
// POST create a new Image | ||
router.post('/createImage', handleCreateImage); | ||
|
||
// GET Image by ID | ||
router.get('/getImage', handleFindImageById); | ||
|
||
export default router; |
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
Oops, something went wrong.