-
Notifications
You must be signed in to change notification settings - Fork 40
Дубровин Алексей #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Дубровин Алексей #15
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,22 @@ | |
| module.exports = class Queries { | ||
| constructor(mongoose, { souvenirsCollection, cartsCollection }) { | ||
| const souvenirSchema = mongoose.Schema({ // eslint-disable-line new-cap | ||
| // Ваша схема сувенира тут | ||
| _id: mongoose.Schema.Types.ObjectId, | ||
| tags: [String], | ||
| reviews: [mongoose.Schema.Types.Mixed], | ||
| name: String, | ||
| image: String, | ||
| price: { type: Number, index: true }, | ||
| amount: Number, | ||
| country: { type: String, index: true }, | ||
| rating: { type: Number, index: true }, | ||
| isRecent: Boolean | ||
| }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Было бы круто явным образом указать default и required для полей |
||
|
|
||
| const cartSchema = mongoose.Schema({ // eslint-disable-line new-cap | ||
| // Ваша схема корзины тут | ||
| _id: mongoose.Schema.Types.ObjectId, | ||
| login: String, | ||
| items: [{ souvenirId: String, amount: Number }] | ||
| }); | ||
|
|
||
| // Модели в таком формате нужны для корректного запуска тестов | ||
|
|
@@ -18,42 +29,38 @@ module.exports = class Queries { | |
| // Далее идут методы, которые вам необходимо реализовать: | ||
|
|
||
| getAllSouvenirs() { | ||
| // Данный метод должен возвращать все сувениры | ||
| return this._Souvenir.find(); | ||
| } | ||
|
|
||
| getCheapSouvenirs(price) { | ||
| // Данный метод должен возвращать все сувениры, цена которых меньше или равна price | ||
| return this._Souvenir.find({ price: { $lte: price } }); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно немного симпатичнее |
||
| } | ||
|
|
||
| getTopRatingSouvenirs(n) { | ||
| // Данный метод должен возвращать топ n сувениров с самым большим рейтингом | ||
| return this._Souvenir.find() | ||
| .sort({ rating: -1 }) | ||
| .limit(n); | ||
| } | ||
|
|
||
| getSouvenirsByTag(tag) { | ||
| // Данный метод должен возвращать все сувениры, в тегах которых есть tag | ||
| // Кроме того, в ответе должны быть только поля name, image и price | ||
| return this._Souvenir.find({ tags: tag }, { name: 1, image: 1, price: 1, _id: 0 }); | ||
| } | ||
|
|
||
| getSouvenrisCount({ country, rating, price }) { | ||
| // Данный метод должен возвращать количество сувениров, | ||
| // из страны country, с рейтингом больше или равной rating, | ||
| // и ценой меньше или равной price | ||
|
|
||
| // ! Важно, чтобы метод работал очень быстро, | ||
| // поэтому учтите это при определении схем | ||
| return this._Souvenir.find({ country, rating: { $gte: rating }, price: { $lte: price } }) | ||
| .count(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Давай немного приберёмся здесь с форматированием, выглядит странно. Кстати, можно обойтись без find и сразу написать эти же параметры в count. |
||
| } | ||
|
|
||
| searchSouvenirs(substring) { | ||
| // Данный метод должен возвращать все сувениры, в название которых входит | ||
| // подстрока substring. Поиск должен быть регистронезависимым | ||
| return this._Souvenir.find({ name: { $regex: substring, $options: 'i' } }); | ||
| } | ||
|
|
||
| getDisscusedSouvenirs(date) { | ||
| // Данный метод должен возвращать все сувениры, | ||
| // первый отзыв на которые был оставлен не раньше даты date | ||
| return this._Souvenir.find({ 'reviews.0.date': { $gte: date } }); | ||
| } | ||
|
|
||
| deleteOutOfStockSouvenirs() { | ||
| return this._Souvenir.find({ amount: { $eq: 0 } }).remove(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| // Данный метод должен удалять все сувениры, которых нет в наличии | ||
| // (то есть amount = 0) | ||
|
|
||
|
|
@@ -62,16 +69,39 @@ module.exports = class Queries { | |
| } | ||
|
|
||
| async addReview(souvenirId, { login, rating, text }) { | ||
| // Данный метод должен добавлять отзыв к сувениру souvenirId, отзыв добавляется | ||
| // в конец массива (чтобы сохранить упорядоченность по дате), | ||
| // содержит login, rating, text - из аргументов, | ||
| // date - текущая дата и isApproved - false | ||
| // Обратите внимание, что при добавлении отзыва рейтинг сувенира должен быть пересчитан | ||
| let reviewsCount = 1; | ||
| let ratingSum = rating; | ||
| await this._Souvenir.find({ _id: souvenirId }) | ||
| .then(souvenir => souvenir[0].reviews | ||
| .forEach(review => { | ||
| reviewsCount++; | ||
| ratingSum += review.rating; | ||
| }) | ||
| ); | ||
|
|
||
| let review = { | ||
| login, | ||
| rating, | ||
| text, | ||
| date: new Date(), | ||
| isApproved: false | ||
| }; | ||
| await this._Souvenir.findOneAndUpdate({ _id: souvenirId }, { | ||
| $push: { reviews: review }, | ||
| $set: { rating: ratingSum / reviewsCount } | ||
| }); | ||
| // await | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? |
||
| } | ||
|
|
||
| async getCartSum(login) { | ||
| // Данный метод должен считать общую стоимость корзины пользователя login | ||
| // У пользователя может быть только одна корзина, поэтому это тоже можно отразить | ||
| // в схеме | ||
| const cart = ((await this._Cart.find({ login }, { items: 1, _id: 0 }))[0]).items; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. findOne |
||
|
|
||
| let sum = 0; | ||
| for (let orderedSouvenir of cart) { | ||
| const souvenir = (await this._Souvenir.find({ _id: orderedSouvenir.souvenirId }))[0]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Await в цикле это верный сигнал к тому, что стоит переписать кусок кода. |
||
| sum += souvenir.price * orderedSouvenir.amount; | ||
| } | ||
|
|
||
| return sum; | ||
| } | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Всё таки для большей надёжности лучше определить схему Review, а в этой строке сделать
reviews: [reviewSchema]