Skip to content

Commit 7e0d9ea

Browse files
authored
Merge pull request #93 from marc101101/feature/my_courses
Feature/my courses
2 parents 2103cf6 + 5319d84 commit 7e0d9ea

6 files changed

Lines changed: 145 additions & 45 deletions

File tree

server/api/swagger.yaml

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ info:
1616
host: "localhost:8080"
1717
basePath: "/v1"
1818
paths:
19-
/user:
19+
/user/me:
2020
get:
2121
tags:
2222
- "User"
2323
summary: "get me"
2424
description: "Get authorized User (me)"
25-
operationId: "userGET"
25+
operationId: "userMeGET"
2626
parameters: []
2727
responses:
2828
200:
@@ -42,17 +42,11 @@ paths:
4242
security:
4343
- JWT: []
4444
x-swagger-router-controller: "User"
45-
post:
45+
put:
4646
tags:
4747
- "User"
48-
summary: "register a new user"
49-
description: "register new user"
50-
operationId: "userPOST"
51-
consumes:
52-
- "application/json"
53-
produces:
54-
- "application/json"
55-
- "application/xml"
48+
summary: "edit user details"
49+
operationId: "userPUT"
5650
parameters:
5751
- in: "body"
5852
name: "data"
@@ -61,8 +55,8 @@ paths:
6155
schema:
6256
type: "object"
6357
responses:
64-
201:
65-
description: "user created"
58+
200:
59+
description: "user has been edited"
6660
schema:
6761
type: "object"
6862
properties: {}
@@ -79,11 +73,46 @@ paths:
7973
security:
8074
- JWT: []
8175
x-swagger-router-controller: "User"
82-
put:
76+
/user/me/courses:
77+
get:
8378
tags:
8479
- "User"
85-
summary: "edit user details"
86-
operationId: "userPUT"
80+
summary: "get my courses"
81+
description: "Get Users (me) courses"
82+
operationId: "userMeCoursesGET"
83+
parameters: []
84+
responses:
85+
200:
86+
description: "request successful"
87+
schema:
88+
type: "array"
89+
items:
90+
$ref: "#/definitions/Category"
91+
401:
92+
description: Invalid authentication credentials.
93+
403:
94+
description: Permission 'xxx' denied on file 'yyy'.
95+
404:
96+
description: Permission 'xxx' denied on file 'yyy'.
97+
409:
98+
description: Resource 'xxx' already exists.
99+
500:
100+
description: Internal Server Error.
101+
security:
102+
- JWT: []
103+
x-swagger-router-controller: "User"
104+
/user:
105+
post:
106+
tags:
107+
- "User"
108+
summary: "register a new user"
109+
description: "register new user"
110+
operationId: "userPOST"
111+
consumes:
112+
- "application/json"
113+
produces:
114+
- "application/json"
115+
- "application/xml"
87116
parameters:
88117
- in: "body"
89118
name: "data"
@@ -92,8 +121,8 @@ paths:
92121
schema:
93122
type: "object"
94123
responses:
95-
200:
96-
description: "user has been edited"
124+
201:
125+
description: "user created"
97126
schema:
98127
type: "object"
99128
properties: {}

server/controllers/User.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,21 @@ var verifyToken = require('../utils/VerifyToken');
77
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
88
var config = require('../config'); // get config file
99

10-
module.exports.userGET = function userGET (req, res, next) {
10+
module.exports.userMeGET = function userMeGET (req, res, next) {
1111
verifyToken(req, res, (decoded) => {
12-
User.userGET(decoded.id)
12+
User.userMeGET(decoded.id)
13+
.then(function (response) {
14+
utils.writeJson(res, response);
15+
})
16+
.catch(function (response) {
17+
utils.writeJson(res, response, response.code);
18+
});
19+
});
20+
};
21+
22+
module.exports.userMeCoursesGET = function userMeCoursesGET (req, res, next) {
23+
verifyToken(req, res, (decoded) => {
24+
User.userMeCoursesGET(decoded.id)
1325
.then(function (response) {
1426
utils.writeJson(res, response);
1527
})

server/service/CoursesService.js

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +74,28 @@ function applicationFound(applications, user_id, course_id) {
7474
}
7575

7676
function generateApplicationFor(user_id, course_id) {
77-
return {
78-
ANM_DATUM: moment().format('YYYY-MM-DD'),
79-
ANM_TEIL_ID: user_id,
80-
ANM_KURS_ID: course_id,
81-
ANM_BEARBEITER: 0,
82-
ANM_STAT_ID: 0,
83-
ANM_ABR_DATUM: 0,
84-
ANM_ABR_RUECKLAST_DATUM: 0,
85-
ANM_TNB_GEM_DRUCKEN: 0,
86-
ANM_TEIL_ID_ZAHLER: 0,
87-
ANM_WARTEL_INFORMIEREN: 0,
88-
ANM_ABR_ABRECHNEN: 0,
89-
ANM_ABR_ABGERECHNET: 0,
90-
EINGEGEBEN_VON_USER: user_id,
91-
EINGEGEBEN_AM_DATUM: 0,
92-
EINGEGEBEN_AM_ZEIT: 0,
93-
DATENHISTORY: ""
94-
};
77+
return {
78+
ANM_DATUM: moment().format('YYYY-MM-DD'),
79+
ANM_TEIL_ID: user_id,
80+
ANM_KURS_ID: course_id,
81+
ANM_BEARBEITER: 0,
82+
ANM_STAT_ID: 0,
83+
ANM_ABR_DATUM: 0,
84+
ANM_ABR_RUECKLAST_DATUM: 0,
85+
ANM_TNB_GEM_DRUCKEN: 0,
86+
ANM_TEIL_ID_ZAHLER: 0,
87+
ANM_WARTEL_INFORMIEREN: 0,
88+
ANM_ABR_ABRECHNEN: 0,
89+
ANM_ABR_ABGERECHNET: 0,
90+
EINGEGEBEN_VON_USER: user_id,
91+
EINGEGEBEN_AM_DATUM: 0,
92+
EINGEGEBEN_AM_ZEIT: 0,
93+
DATENHISTORY: ""
94+
};
9595
}
9696

97+
exports.generateApplicationFor = generateApplicationFor;
98+
9799
/**
98100
* give feedback to a course
99101
* give feedback to a course
@@ -272,7 +274,7 @@ if (process.env.NODE_ENV === 'test') {
272274
Promise.all(courses.invokeMap('save'))
273275
.then((data) => {
274276
console.log("Finished Setting up Content in Table vhslq_kurse")
275-
resolve("done");
277+
resolve(data);
276278
})
277279
.catch((error) => {
278280
reject(error);

server/service/UserService.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var User = require('../utils/database').User;
4+
var Applications = require('../utils/database').Application
45
var knex = require('../utils/database').knex;
56

67
var jwt = require('jsonwebtoken'); // used to create, sign, and verify tokens
@@ -14,7 +15,7 @@ var Errors = require('../utils/errors');
1415
*
1516
* returns User
1617
**/
17-
exports.userGET = function(id) {
18+
exports.userMeGET = function(id) {
1819
return new Promise((resolve, reject) => {
1920
User.where({teil_id: id})
2021
.fetch()
@@ -30,6 +31,20 @@ exports.userGET = function(id) {
3031
});
3132
}
3233

34+
exports.userMeCoursesGET = function(user_id) {
35+
return new Promise(function (resolve, reject) {
36+
Applications
37+
.where({ANM_TEIL_ID: user_id})
38+
.fetchAll({withRelated: ["course"]})
39+
.then((applications) => {
40+
resolve(applications.map(item => item.related('course').toJSON()))
41+
})
42+
.catch((error) => {
43+
reject(error);
44+
});
45+
});
46+
}
47+
3348

3449
/**
3550
* register a new user
@@ -92,4 +107,25 @@ if (process.env.NODE_ENV === 'test') {
92107
})
93108
})
94109
}
110+
111+
exports.addCoursesToSampleUser = (user_id) => {
112+
return new Promise((resolve, reject) => {
113+
console.log("Adding Sample Courses To User ID " + user_id);
114+
let setupCourses = require('../service/CoursesService').setupDataBase;
115+
setupCourses().then((courses) => {
116+
let _Applications = require('../utils/database').Applications
117+
let generateApplicationFor = require('../service/CoursesService').generateApplicationFor;
118+
let applications = courses
119+
.map(item => item.attributes.id)
120+
.map(course_id => generateApplicationFor(user_id, course_id))
121+
Promise.all(_Applications.forge(applications).invokeMap('save')).then(() => {
122+
console.log("Finished Adding Courses to User ID " + user_id);
123+
resolve("done");
124+
}).catch((error) => {
125+
console.log(error)
126+
reject(error);
127+
})
128+
})
129+
});
130+
}
95131
}

server/test/user.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ chai.use(chaiHttp);
1111

1212
describe('User', () => {
1313
let authToken = "";
14+
let userID = "";
1415
before(() => {
1516
return userService
1617
.clearDataBase()
1718
});
1819
it("it should fail with 401 because auth token is missing", (done) => {
1920
chai.request(server)
20-
.get('/v1/user')
21+
.get('/v1/user/me')
2122
.end((err, res) => {
2223
let compareError = Errors.missingAuth();
2324
res.should.have.status(compareError.code);
@@ -27,7 +28,7 @@ describe('User', () => {
2728
})
2829
it("it should fail with 401 because auth token is incorrect", (done) => {
2930
chai.request(server)
30-
.get('/v1/user')
31+
.get('/v1/user/me')
3132
.set('authorization', 'Bearer ' + "obviously.incorrect.token")
3233
.end((err, res) => {
3334
let compareError = Errors.invalidAuth();
@@ -57,13 +58,14 @@ describe('User', () => {
5758
res.body.user.teil_vorname.should.equal(user.teil_vorname);
5859
res.body.user.teil_nachname.should.equal(user.teil_nachname);
5960
res.body.user.teil_email.should.equal(user.teil_email);
61+
userID = res.body.user.id;
6062
authToken = res.body.token;
6163
done();
6264
})
6365
});
6466
it("it should get user John Doe", (done) => {
6567
chai.request(server)
66-
.get('/v1/user')
68+
.get('/v1/user/me')
6769
.set('authorization', 'Bearer ' + authToken)
6870
.end((err, res) => {
6971
res.should.have.status(200);
@@ -75,15 +77,15 @@ describe('User', () => {
7577
});
7678
it("it should update John Doe's Name to John Although", (done) => {
7779
chai.request(server)
78-
.put('/v1/user')
80+
.put('/v1/user/me')
7981
.set('authorization', 'Bearer ' + authToken)
8082
.send({teil_nachname: "Although"})
8183
.end((err, res) => {
8284
res.should.have.status(200);
8385
res.body.should.be.a('object');
8486
res.body.teil_nachname.should.equal("Although");
8587
chai.request(server)
86-
.get('/v1/user')
88+
.get('/v1/user/me')
8789
.set('authorization', 'Bearer ' + authToken)
8890
.end((err, res) => {
8991
res.should.have.status(200);
@@ -94,4 +96,17 @@ describe('User', () => {
9496
});
9597
});
9698
});
99+
it("it should get John Doe's Courses", (done) => {
100+
userService.addCoursesToSampleUser(userID).then(() => {
101+
chai.request(server)
102+
.get('/v1/user/me/courses')
103+
.set('authorization', 'Bearer ' + authToken)
104+
.end((err, res) => {
105+
res.should.have.status(200);
106+
res.body.should.be.a('array');
107+
res.body.length.should.equal(2);
108+
done();
109+
})
110+
})
111+
})
97112
});

server/utils/database.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ exports.User = bookshelf.Model.extend({
5353
// -- APPLICATIONS
5454
let application = bookshelf.Model.extend({
5555
tableName: 'vhslq_anmeldungen',
56+
course: function() {
57+
return this.belongsTo(course, "KURS_ID", "ANM_KURS_ID")
58+
},
59+
user: function() {
60+
return this.belongsTo(user, "TEIL_ID", "ANM_TEIL_ID")
61+
}
5662
})
5763

5864
exports.Application = application;

0 commit comments

Comments
 (0)