Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit 6200b86

Browse files
committed
Merge branch 'dev'
2 parents fba34da + 2fd2658 commit 6200b86

File tree

17 files changed

+2861
-2180
lines changed

17 files changed

+2861
-2180
lines changed

course-02/exercises/udacity-c2-basic-server/src/server.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,60 @@ import { Car, cars as cars_list } from './cars';
7070

7171
// @TODO Add an endpoint to GET a list of cars
7272
// it should be filterable by make with a query paramater
73-
73+
// GET /cars?make=make
74+
app.get( "/cars",
75+
( req: Request, res: Response ) => {
76+
let { make } = req.query;
77+
let ret = make ? cars.filter(car => car.make == make) : cars;
78+
return res.status(200)
79+
.send(ret);
80+
} );
7481
// @TODO Add an endpoint to get a specific car
7582
// it should require id
7683
// it should fail gracefully if no matching car is found
84+
// GET /cars/:id
85+
// 404
86+
app.get( "/cars/:id",
87+
( req: Request, res: Response ) => {
88+
let { id } = req.params;
89+
90+
if ( !id ) {
91+
return res.status(400)
92+
.send(`id is required`);
93+
}
94+
95+
let ret = cars.filter(car => car.id == Number(id));
96+
97+
console.log(ret);
98+
99+
if ( ret.length == 0 ) {
100+
return res.status(404)
101+
.send(`car not found`);
102+
}
103+
104+
return res.status(200)
105+
.send(ret);
106+
} );
77107

78108
/// @TODO Add an endpoint to post a new car to our list
79109
// it should require id, type, model, and cost
110+
// POST /cars Car
111+
app.post( "/cars",
112+
async ( req: Request, res: Response ) => {
113+
const car = req.body as Car;
114+
console.log(car);
115+
116+
117+
if ( !car.id || !car.type || !car.model || !car.cost ) {
118+
return res.status(400)
119+
.send(`car is malformed`);
120+
}
121+
122+
cars.push(car);
123+
124+
return res.status(201)
125+
.send(cars);
126+
} );
80127

81128
// Start the Server
82129
app.listen( port, () => {

course-02/exercises/udacity-c2-basic-server/src/unit-test-examples/units.tests.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { add, divide } from './units';
1+
import { add, divide, concat } from './units';
22

33
import { expect } from 'chai';
44
import 'mocha';
@@ -35,6 +35,19 @@ describe('divide', () => {
3535

3636
});
3737

38+
describe('concat', () => {
39+
40+
it('should concat a and b in order', () => {
41+
const result = concat("a","b");
42+
expect(result).to.equal("ab");
43+
});
44+
45+
it('should throw an error if one is empty', () => {
46+
expect(()=>{ concat("a", "") }).to.throw('empty string')
47+
});
48+
49+
});
50+
3851
// @TODO try creating a new describe block for the "concat" method
3952
// it should contain an it block for each it statement in the units.ts @TODO.
4053
// don't forget to import the method ;)

course-02/exercises/udacity-c2-basic-server/src/unit-test-examples/units.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ export const divide = (a: number, b: number) => {
1010
return a / b;
1111
}
1212

13+
export const concat = (a: string, b: string) => {
14+
if (a.length == 0 || b.length == 0) {
15+
throw new Error('empty string')
16+
}
17+
return a.concat(b)
18+
}
1319
// @TODO try creating a method "concat" to concatenate two strings
1420
// it should take two string paramaters.
1521
// it should return one string combining the two strings.

0 commit comments

Comments
 (0)