Skip to content

Commit

Permalink
#167268609 Feature(trips): add feature to get trips (#10)
Browse files Browse the repository at this point in the history
* feature(trips): add feature to get trips

- get all trips `/api/v1/trips`

- get one trip `POST: /api/v1/trips/:trip_id`

* fix(seats): fix seats array in trips table

[Finishes #167268609]
  • Loading branch information
Mcdavid95 authored Jul 13, 2019
1 parent a6186e8 commit 0a03168
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 6 deletions.
47 changes: 43 additions & 4 deletions src/controllers/Trip.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import {
* @returns {object} object containing bus details
*/
const getBus = async (id) => {
const userQuery = 'SELECT * FROM Buses WHERE id = $1';
const busQuery = 'SELECT * FROM Buses WHERE id = $1';
try {
const { rows } = await db.query(userQuery, [id]);
const { rows } = await db.query(busQuery, [id]);
return rows[0];
} catch (error) {
return error;
Expand All @@ -40,10 +40,17 @@ const constructData = trip => ({
origin: trip.origin,
destination: trip.destination,
trip_date: trip.trip_date,
fare: trip.fare
fare: trip.fare,
seats: trip.seats
});

const Trip = {
/**
* @method create
* @param {object} req request object
* @param {object} res response object
* @returns {object} response object
*/
async create(req, res) {
const {
bus_id, origin, destination, trip_date, fare
Expand All @@ -57,14 +64,46 @@ const Trip = {
const values = [
bus_id, origin.trim().toLowerCase(),
destination.trim().toLowerCase(), moment(trip_date),
fare, `${createSeats(bus.id)}`,
fare, createSeats(bus.capacity),
moment(new Date()), moment(new Date())
];
const { rows } = await db.query(createQuery, values);
return handleServerResponse(res, 201, constructData(rows[0]));
} catch (error) {
handleServerError(res, error);
}
},
/**
* @method getTrips
* @param {object} req request object
* @param {object} res response object
* @returns {object} response object
*/
async getTrips(req, res) {
try {
const findAllQuery = 'SELECT * FROM Trips';
const { rows } = await db.query(findAllQuery);
const result = await rows.map(row => constructData(row));
return handleServerResponse(res, 200, result);
} catch (error) {
handleServerError(res, error);
}
},
/**
* @method getTrips
* @param {object} req request object
* @param {object} res response object
* @returns {object} response object
*/
async getOneTrip(req, res) {
try {
const { trip_id } = req.params;
const findAllQuery = 'SELECT * FROM Trips WHERE id = $1';
const { rows } = await db.query(findAllQuery, [trip_id]);
return handleServerResponse(res, 200, constructData(rows[0]));
} catch (error) {
handleServerError(res, error);
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/models/trip.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const createTripTable = async () => {
trip_date TIMESTAMP,
status status DEFAULT 'active',
fare FLOAT NOT NULL,
seats JSONB NOT NULL,
seats JSONB [] NOT NULL,
created_date TIMESTAMP,
modified_date TIMESTAMP
)`;
Expand Down
4 changes: 3 additions & 1 deletion src/routes/trip.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import Trip from '../controllers/Trip';
import ValidateInput from '../helpers/validateInput';
import { isAdmin, hasToken } from '../helpers/utils';

const { create } = Trip;
const { create, getTrips, getOneTrip } = Trip;
const { validateCreateTrip } = ValidateInput;

const router = express.Router();

router.post('/', hasToken, isAdmin, validateCreateTrip, create);
router.get('/', hasToken, getTrips);
router.get('/:trip_id', hasToken, getOneTrip);

export default router;
18 changes: 18 additions & 0 deletions tests/controllers/trip.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,22 @@ describe('Trip controller', () => {
.send(noBusId);
expect(server.statusCode).to.equal(401);
});

it('should get all trips', async () => {
const server = await api.get('/api/v1/trips')
.type('form')
.set('Content-Type', 'application/json')
.set('x-access-token', userToken);
console.log(server.body);
expect(server.statusCode).to.equal(200);
});

it('should get one trip', async () => {
const server = await api.get('/api/v1/trips/1')
.type('form')
.set('Content-Type', 'application/json')
.set('x-access-token', userToken);
console.log(server.body);
expect(server.statusCode).to.equal(200);
});
});

0 comments on commit 0a03168

Please sign in to comment.