Skip to content

Commit 16e8989

Browse files
AlexanderDmitrievkseniyameschnestakiev
authored
Dist (#15)
* добавляет реэкспорт отзыва в котроллерах * some part from statistics * fix schema swagger * small fix swagger and routes Co-authored-by: Kseniia <[email protected]> Co-authored-by: nestakiev <[email protected]>
1 parent 4ec02a6 commit 16e8989

File tree

14 files changed

+230
-24
lines changed

14 files changed

+230
-24
lines changed

FinalProjectReact-Node-Back

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 4ec02a6a5a484ea7b575647113c9e5ebfea403d3

app.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const authRouter = require('./routes/api/auth');
1212
/* const usersRouter = require("./routes/api/users"); */
1313

1414
const booksRouter = require("./routes/api/books");
15+
const statsRouter = require("./routes/api/stats");
1516

1617
// const statsRouter = require("./routes/api/stats");
1718

@@ -23,13 +24,15 @@ const formatsLogger = app.get("env") === "development" ? "dev" : "short";
2324
app.use(logger(formatsLogger));
2425
app.use(cors());
2526
app.use(express.json());
26-
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
27+
app.use('/', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
2728
app.use(express.static("public"))
2829

2930
app.use('/api/auth', authRouter);
3031
/* app.use("/api/users", usersRouter); */
3132

3233
app.use("/api/books", booksRouter);
34+
app.use("/api/training", statsRouter);
35+
3336

3437
// app.use("/api/stats", statsRouter);
3538

controllers/books/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const add = require("./add");
22
const getAll = require("./getAll");
3+
const updateResume = require("./updateResume");
34

45
module.exports = {
56
add,
67
getAll,
8+
updateResume
79
}

controllers/stat/addDailyStats.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const { Book } = require("../../models/book");
2+
const { Stat } = require("../../models/stat");
3+
4+
const { RequestError } = require("../../helpers");
5+
6+
const addStatistics = async (req, res) => {
7+
// const {book, start, end} = req.body;
8+
9+
// if (book.length < 1) {
10+
// throw RequestError(400, "Bad request");
11+
// }
12+
13+
// try {
14+
// book.forEach(async (item) => {
15+
// await Book.findByIdAndUpdate(item.id, { status: "in progress" });
16+
// });
17+
// } catch (err) {
18+
// throw RequestError(500, err);
19+
// }
20+
21+
// const newTraining = {
22+
// active: book,
23+
// start,
24+
// end
25+
// };
26+
27+
// const result = Stat.create(newTraining);
28+
29+
// res.status(201).json(result);
30+
};
31+
32+
module.exports = addStatistics;
33+

controllers/stat/addTraining.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const { Book } = require("../../models/book");
2+
const { Stat } = require("../../models/stat");
3+
4+
const { RequestError } = require("../../helpers");
5+
6+
const addTraining = async (req, res) => {
7+
const {book, start, end} = req.body;
8+
9+
if (book.length < 1) {
10+
throw RequestError(400, "Bad request");
11+
}
12+
13+
try {
14+
book.forEach(async (item) => {
15+
await Book.findByIdAndUpdate(item.id, { status: "in progress" });
16+
});
17+
} catch (err) {
18+
throw RequestError(500, err);
19+
}
20+
21+
const newTraining = {
22+
active: book,
23+
start,
24+
end
25+
};
26+
27+
const result = Stat.create(newTraining);
28+
29+
res.status(201).json(result);
30+
};
31+
32+
module.exports = addTraining;

controllers/stat/getStatistics.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const { Stat } = require("../../models/stat");
2+
const { RequestError } = require("../../helpers");
3+
4+
const getStatistics = async (req, res) => {
5+
const { _id } = req.user;
6+
7+
const result = Stat.findOne({owner: _id})
8+
9+
if (!result) {
10+
throw RequestError(500, "Something went wrong");
11+
}
12+
13+
res.status(201).json(result);
14+
};
15+
16+
module.exports = getStatistics;

controllers/stat/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const addTraining = require("./addTraining");
2+
const addDailyStats = require("./addDailyStats");
3+
const getStatistics = require("./getStatistics");
4+
5+
module.exports = {
6+
addTraining,
7+
addDailyStats,
8+
getStatistics
9+
};

middlewares/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
const validationBody = require('./validationBody');
22
const authenticate = require('./authenticate');
3-
3+
const isValidId = require("./isValidId");
44

55
module.exports = {
66
validationBody,
77
authenticate,
8-
8+
isValidId
99
}

models/book.js

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Joi = require("joi");
33
const { handleSchemaValidationErrors } = require("../helpers");
44

55
const statusKind = ["plan", "in progress", "finished"];
6+
const ratingKind = ["1", "2", "3", "4", "5", null];
67

78
const bookSchema = new Schema ({
89
title: {
@@ -26,21 +27,21 @@ const bookSchema = new Schema ({
2627
enum: statusKind,
2728
default: "plan",
2829
},
29-
// resume: {
30-
// comment: {
31-
// type: String,
32-
// default: null,
33-
// },
34-
// rating: {
35-
// type: String,
36-
// enum: ["1", "2", "3", "4", "5"],
37-
// default: null,
38-
// },
39-
// },
40-
// owner: {
41-
// type: Schema.Types.ObjectId,
42-
// ref: "User",
43-
// },
30+
resume: {
31+
comment: {
32+
type: String,
33+
default: null,
34+
},
35+
rating: {
36+
type: String,
37+
enum: ["1", "2", "3", "4", "5"],
38+
default: null,
39+
},
40+
},
41+
owner: {
42+
type: Schema.Types.ObjectId,
43+
ref: "User",
44+
},
4445
}, { versionKey: false, timestamps: true });
4546

4647
bookSchema.post("save", handleSchemaValidationErrors);
@@ -55,9 +56,14 @@ const addSchema = Joi.object({
5556
status: Joi.string().valueOf(...statusKind),
5657
})
5758

59+
const updateResumeSchema = Joi.object({
60+
comment: Joi.string().required(),
61+
rating: Joi.string().valueOf(...ratingKind)
62+
})
63+
5864
const schemas = {
5965
addSchema,
60-
66+
updateResumeSchema
6167
}
6268

6369
const Book = model("book", bookSchema);

models/stat.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
const { Schema, model } = require("mongoose");
2+
const Joi = require("joi");
3+
const { handleSchemaValidationErrors } = require("../helpers");
4+
5+
const statSchema = new Schema(
6+
{
7+
training: {
8+
active: [
9+
{
10+
book: {
11+
type: Schema.Types.ObjectId,
12+
ref: "Book",
13+
},
14+
},
15+
],
16+
start: {
17+
type: Date,
18+
default: null,
19+
},
20+
end: {
21+
type: Date,
22+
default: null,
23+
},
24+
},
25+
statistic: [
26+
{
27+
date: {
28+
type: Date,
29+
default: null,
30+
},
31+
pages: {
32+
type: String,
33+
default: null,
34+
},
35+
},
36+
],
37+
owner: {
38+
type: Schema.Types.ObjectId,
39+
ref: "User",
40+
},
41+
},
42+
{ versionKey: false, timestamps: true }
43+
);
44+
45+
statSchema.post("save", handleSchemaValidationErrors);
46+
47+
const addTrainSchema = Joi.object({
48+
book: Joi.array().items(Joi.string().length(24)).min(1).required(),
49+
start: Joi.date().timestamp().required(),
50+
end: Joi.date().timestamp().required(),
51+
});
52+
53+
const addDailyStatsSchema = Joi.object({
54+
date: Joi.date().timestamp().required(),
55+
pages: Joi.string().pattern(/\d/).required(),
56+
});
57+
58+
const schemas = {
59+
addTrainSchema,
60+
addDailyStatsSchema,
61+
};
62+
63+
const Stat = model("stat", statSchema);
64+
65+
module.exports = {
66+
Stat,
67+
schemas,
68+
};

0 commit comments

Comments
 (0)