-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.js
130 lines (107 loc) · 3.22 KB
/
jobs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"use strict";
/** Routes for jobs. */
const jsonschema = require("jsonschema");
const express = require("express");
const { BadRequestError } = require("../expressError");
const { ensureAdmin } = require("../middleware/auth");
const Job = require("../models/job");
const jobNewSchema = require("../schemas/jobNew.json");
const jobUpdateSchema = require("../schemas/jobUpdate.json");
const jobSearchSchema = require("../schemas/jobSearch.json");
const router = express.Router({ mergeParams: true });
/** POST / { job } => { job }
*
* job should be { title, salary, equity, companyHandle }
*
* Returns { id, title, salary, equity, companyHandle }
*
* Authorization required: admin
*/
router.post("/", ensureAdmin, async function (req, res, next) {
try {
const validator = jsonschema.validate(req.body, jobNewSchema);
if (!validator.valid) {
const errs = validator.errors.map(e => e.stack);
throw new BadRequestError(errs);
}
const job = await Job.create(req.body);
return res.status(201).json({ job });
} catch (err) {
return next(err);
}
});
/** GET / =>
* { jobs: [ { id, title, salary, equity, companyHandle, companyName }, ...] }
*
* Can provide search filter in query:
* - minSalary
* - hasEquity (true returns only jobs with equity > 0, other values ignored)
* - title (will find case-insensitive, partial matches)
* Authorization required: none
*/
router.get("/", async function (req, res, next) {
const q = req.query;
// arrive as strings from querystring, but we want as int/bool
if (q.minSalary !== undefined) q.minSalary = +q.minSalary;
q.hasEquity = q.hasEquity === "true";
try {
const validator = jsonschema.validate(q, jobSearchSchema);
if (!validator.valid) {
const errs = validator.errors.map(e => e.stack);
throw new BadRequestError(errs);
}
const jobs = await Job.findAll(q);
return res.json({ jobs });
} catch (err) {
return next(err);
}
});
/** GET /[jobId] => { job }
*
* Returns { id, title, salary, equity, company }
* where company is { handle, name, description, numEmployees, logoUrl }
*
* Authorization required: none
*/
router.get("/:id", async function (req, res, next) {
try {
const job = await Job.get(req.params.id);
return res.json({ job });
} catch (err) {
return next(err);
}
});
/** PATCH /[jobId] { fld1, fld2, ... } => { job }
*
* Data can include: { title, salary, equity }
*
* Returns { id, title, salary, equity, companyHandle }
*
* Authorization required: admin
*/
router.patch("/:id", ensureAdmin, async function (req, res, next) {
try {
const validator = jsonschema.validate(req.body, jobUpdateSchema);
if (!validator.valid) {
const errs = validator.errors.map(e => e.stack);
throw new BadRequestError(errs);
}
const job = await Job.update(req.params.id, req.body);
return res.json({ job });
} catch (err) {
return next(err);
}
});
/** DELETE /[handle] => { deleted: id }
*
* Authorization required: admin
*/
router.delete("/:id", ensureAdmin, async function (req, res, next) {
try {
await Job.remove(req.params.id);
return res.json({ deleted: +req.params.id });
} catch (err) {
return next(err);
}
});
module.exports = router;