singhsanket143 / Flights-Service

34 stars 17 forks source link

Implement Update airplane API #1

Open singhsanket143 opened 1 year ago

singhsanket143 commented 1 year ago

We need to implement update city api with the following signature /api/v1/airplane - PATCH Req Body: {capacity: 250}

Do handle the error scenario as well when we dont find a record to update

amarjyotipatra commented 1 year ago

Done

Priyabrata139 commented 1 year ago

vaiya update airplane api me route ya hoga na? /api/v1/airplane/:id

amarjyotipatra commented 1 year ago

Yes with Patch method

On Sat, 13 May, 2023, 7:02 pm Priyabrata Maji, @.***> wrote:

vaiya update airplane api me route ya hoga na? /api/v1/airplane/:id

— Reply to this email directly, view it on GitHub https://github.com/singhsanket143/Flights-Service/issues/1#issuecomment-1546653887, or unsubscribe https://github.com/notifications/unsubscribe-auth/AS4ED6BUX3WQIY6ZMTSSKCDXF6EPBANCNFSM6AAAAAAX3MU37M . You are receiving this because you commented.Message ID: @.***>

shubhamkhuntia commented 1 year ago

inside airplane-service

async function updateAirplane(id, data) { try { const response = await airplaneRepository.update(id, data); return response; } catch (error) { throw new AppError("Cant update", statusCode.BAD_REQUEST); } }

inside airplane-controller

async function updateAirplane(req, res) { try { const response = await AirplaneService.updateAirplane( req.params.id, req.body.capacity ); } catch (error) { ErrorResponse.error = error; return res.status(error.status).json(ErrorResponse); } }

inside airplane-routes

router.patch("/:id", AirplaneController.updateAirplane);

animeshbarole commented 1 year ago

Crud-Repo Code
async update(data,id) /{data -> {colvalue : "val"}/
{ const response = await this.model.update(data, {

          where :{
            id :id,
          },
       });

     return response;

}

airplaneController Code

async function updateAirplane(req,res) { try{

     const airplanes = await AirplaneService.updateAirplane({
        Capacity : req.body.Capacity
     },req.params.id);
     SuccessResponse.data = airplanes;

     return res.
               status(StatusCodes.CREATED)
              .json( SuccessResponse );

}catch(error) 
{
    ErrorResponse.error = error 
    return res
              .status(error.statusCode) //Error has Self Property statusCode we simply not write again we just
                                        //Pass it with statusCode
              .json(ErrorResponse);
}

}

airplaneService Code

async function updateAirplane(data,id) { try{ const response= await airplaneRepository.update(data,id); return response; } catch(error){

    if(error.statusCode==StatusCodes.NOT_FOUND)
    {
           throw new AppError('The airplane you requested to update is not present ',error.statusCode);
    }

    throw new AppError('Not able to fectch data of all the airplanes',StatusCodes.INTERNAL_SERVER_ERROR)

  }

}

shubhamkhuntia commented 1 year ago

@animeshbarole Did you try test it using postman? Can you share screenshots?

animeshbarole commented 1 year ago
PostManReq AfterUpdate
Sk-Shubhasheesh commented 12 months ago

DONE

chetanpal12 commented 11 months ago

done

Itsayush30 commented 7 months ago

Quite late but its done! (with error handling :))

SuryaTanwar commented 6 months ago

Done, though a bit late! Feel free to reply if any improvement can be made. :)

crud-repository.js

async update(id, data){ // data -> {col:val, ....}
        const response = await this.model.update(data, {
            where: {
                id: id
            }
        });

        // sequelize update query will return either [ 0 ] or [ 1 ] 
        // which shows the no. of rows affected by this operation
        if(response[0] == 0) {
            throw new AppError("Not able to find the resource", StatusCodes.NOT_FOUND);
        }

        return response;
    }

airplane-service.js

async function updateAirplane(id, data){
    try {
        const airplane = await airplaneRepository.update(id, data);
        return airplane;
    } catch(error) {
        if(error.statusCode == StatusCodes.NOT_FOUND) {
            throw new AppError("The airplane you requested to update is not present", error.statusCode);
        }
        throw new AppError("Cannot fetch data of the required airplanes", StatusCodes.NOT_FOUND);
    }

}

airplane-controller.js

async function updateAirplane(req, res) {
    try {
        const airplane = await AirplaneService.updateAirplane(req.params.id, {
            modelNumber: req.body.modelNumber,
            capacity: req.body.capacity
        });
        SuccessResponse.data = airplane;
        return res
                .status(StatusCodes.ACCEPTED)
                .json(SuccessResponse);
    }   catch(error){
        ErrorResponse.error = error;
        return res
                .status(error.statusCode)
                .json(ErrorResponse);
    }
}

airplane-routes.js

// /api/v1/airplanes/:id PATCH
router.patch("/:id", 
        AirplaneController.updateAirplane);