Skip to content

Commit 09677cc

Browse files
committed
added update airplane api
1 parent fbc664b commit 09677cc

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

src/controllers/airplaneController.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,25 @@ async function deleteAirplane(req, res) {
5151
}
5252
}
5353

54+
async function updateAirplane(req, res) {
55+
try {
56+
const airplane = await AirplaneService.updateAirplane(req.params.id, {
57+
modelNumber: req.body.modelNumber,
58+
capacity: req.body.capacity,
59+
})
60+
SuccessResponse.data = airplane
61+
SuccessResponse.message = `Successfully updated airplane with id ${req.params.id}`
62+
return res.status(200).json(SuccessResponse)
63+
} catch (error) {
64+
ErrorResponse.error = error
65+
return res.status(error.statusCode).json(ErrorResponse)
66+
}
67+
}
68+
5469
module.exports = {
5570
createAirplane,
5671
getAirplanes,
5772
getAirplane,
5873
deleteAirplane,
74+
updateAirplane,
5975
}

src/database/models/airplane.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ module.exports = (sequelize, DataTypes) => {
1616
modelNumber: {
1717
type: DataTypes.STRING,
1818
allowNull: false,
19-
validate: {
20-
isAlphanumeric: true,
21-
},
2219
},
2320
capacity: {
2421
type: DataTypes.INTEGER,

src/repositories/crudRepositories.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ class CrudRepository {
2929
const response = await this.model.findAll()
3030
return response
3131
}
32-
async update(id, data) {
32+
async update(id, data, name) {
3333
const response = await this.model.update(data, { where: { id: id } })
34+
console.log(response)
35+
if (response[0] === 0) {
36+
throw new AppError(`Error updating the ${name}, id ${id} are not found`, 404)
37+
}
3438
return response
3539
}
3640
}

src/routes/v1/airplaneRouter.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ router
99
.post(AirplaneMiddlewares.validateCreateRequest, AirplaneController.createAirplane)
1010
.get(AirplaneController.getAirplanes)
1111

12-
router.route("/:id").get(AirplaneController.getAirplane).delete(AirplaneController.deleteAirplane)
12+
router
13+
.route("/:id")
14+
.get(AirplaneController.getAirplane)
15+
.delete(AirplaneController.deleteAirplane)
16+
.patch(AirplaneController.updateAirplane)
1317

1418
module.exports = router

src/services/airplaneService.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,22 @@ async function deleteAirplane(id) {
5151
}
5252
}
5353

54+
async function updateAirplane(id, data) {
55+
try {
56+
const airplane = await airplaneRepository.update(id, data, "Airplane")
57+
return airplane
58+
} catch (error) {
59+
if (error.statusCode === 404) {
60+
throw new AppError(error, error.statusCode)
61+
}
62+
throw new AppError("Error while deleting the airplane", 500)
63+
}
64+
}
65+
5466
module.exports = {
5567
createAirplane,
5668
getAirplanes,
5769
getAirplane,
5870
deleteAirplane,
71+
updateAirplane,
5972
}

0 commit comments

Comments
 (0)