Skip to content
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

contentReview service hazir #81

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 48 additions & 147 deletions src/routes/contentReviews/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import HTTPError from '../../exceptions/HTTPError';
import Joi from '../../joi';
import models from '../../models';
import ReviewService from '../../services/review';

const createContentReviewsSchema = {
body: Joi.object({
Expand All @@ -25,7 +27,7 @@ const updateContentReviewsSchema = {
}),
};

const create = async (req, res) => {
const create = async (req, res, next) => {
const { error } = createContentReviewsSchema.body.validate(req.body);
if (error) {
return res.status(400).send({
Expand All @@ -35,65 +37,28 @@ const create = async (req, res) => {
const {
content_id, text, score, is_spoiler,
} = req.body;
const contentReview = await models.content_reviews.create({
user_id: req.user.id,
content_id,
text,
score,
is_spoiler,
});
const count = await models.content_reviews.count(
{
where: {
content_id,
},
},
);
try {
const contentReview = await ReviewService.createReview({ content_id, text, score, is_spoiler }, req.user.id)

const content = await models.contents.findOne({
where: {
id: content_id,
},
});
content.rate = (Number(content.rate) + ((Number(score * 2) - (Number(content.rate))) / count));
await content.save();
res.send({
contentReview,
});
res.send({
contentReview,
});
} catch (err) {
next(err);
}
};

const detail = async (req, res) => {
const detail = async (req, res, next) => {
const { id } = req.params;
try {
const contentReview = await models.content_reviews.findOne({
where: {
id,
},
});

if (!contentReview) {
return res.send({
errors: [
{
message: 'Review not found or you don\'t have a permission!',
},
],
});
}

const contentReview = await ReviewService.getReview(id);
return res.send(contentReview);
} catch (err) {
return res.status(500).send({
errors: [
{
message: err.message,
},
],
});
next(err);
}
};

const update = async (req, res) => {
const update = async (req, res, next) => {
const { error } = updateContentReviewsSchema.body.validate(req.body);
if (error) {
return res.status(400).send({
Expand All @@ -102,120 +67,62 @@ const update = async (req, res) => {
}
const { id } = req.params;
try {
const contentReview = await models.content_reviews.findOne({
const contentReview = await ReviewService.getReview(id);

const { text, is_spoiler, score } = req.body;
models.content_reviews.update({ text, is_spoiler, score }, {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
models.content_reviews.update({ text, is_spoiler, score }, {
await ReviewService.updateReview({ text, is_spoiler, score }, {

Zaten bu işi yapan bir servisimiz var

where: {
id,
},
include: {
model: models.users,
as: 'user',
where: {
id: req.user.id,
},
id: contentReview.id,
},
});
if (contentReview) {
const { text, is_spoiler, score } = req.body;
models.content_reviews.update({ text, is_spoiler, score }, {
where: {
id: contentReview.id,
},
});
res.send({
message: 'Content review was updated succesfully',
});
} else {
res.status(403).send({
errors: [
{
message: 'Review not found or you don\'t have a permission!',
},
],
});
}
} catch (err) {
res.status(500).send({
errors: [
{
message: err.message,
},
],
return res.send({
message: 'Content review was updated succesfully',
});
} catch (err) {
next(err);
}
};

const deleteById = async (req, res) => {
const deleteById = async (req, res, next) => {
const { id } = req.params;
const user_id = req.user.id;
const contentReview = await models.content_reviews.findOne({
where: {
id,
},
});
if (contentReview) {
if (user_id === contentReview.user_id) {
models.content_reviews.destroy({
where: {
id,
},
});
res.send({
message: 'Data set was delected successfully!',
});
try {
const contentReview = await ReviewService.getReview(id);
if (user_id !== contentReview.user_id) {
throw new HTTPError('you don\'t have permission ', 404);
}
} else {
res.status(401).send({
errors: [
{
message: 'Review not found or you don\'t have a permission!',
},
],
await ReviewService.deleteReview(id);
return res.send({
message: 'Data set was delected successfully!',
});
} catch (err) {
next(err);
}
};

const getMyReviews = async (req, res) => {
const reviews = await models.content_reviews.findAll({
where: {
user_id: req.user.id,
},
include: {
model: models.contents,
as: 'contents',
include: {
model: models.images,
as: 'image',
},
},
});
return res.send(200, {
reviews,
});
const getMyReviews = async (req, res, next) => {
try {
const reviews = await ReviewService.getUserReviews(req.user.id);
res.send(200, {
reviews,
})
} catch (err) {
next(err);
}
};

const userReviews = async (req, res) => {
const userReviews = async (req, res, next) => {
const { userId } = req.params;
const { limit } = req.query;
try {
const reviews = await models.content_reviews.findAll({
where: {
user_id: userId,
},
limit,
});
const reviews = await ReviewService.getUserReviews(userId);
return res.send({ reviews, count: reviews.length });
} catch (err) {
return res.status(500).send({
errors: [
{
message: err.message,
},
],
});
next(err);
}
};

const contentReviews = async (req, res) => {
const contentReviews = async (req, res, next) => {
const { contentId } = req.params;
const { limit } = req.query;
try {
Expand All @@ -227,13 +134,7 @@ const contentReviews = async (req, res) => {
});
return res.send({ reviews, count: reviews.length });
} catch (err) {
return res.status(500).send({
errors: [
{
message: err.message,
},
],
});
next(err);
}
};

Expand Down
101 changes: 101 additions & 0 deletions src/services/review.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import HTTPError from '../exceptions/HTTPError';
import models from '../models';

const getReview = async (id) => {
const contentReview = await models.content_reviews.findOne({
where: {
id,
},
include: [{
model: models.users,
as: 'user',
}, {
model: models.contents,
as: 'contents',
include: {
model: models.images,
as: 'image',
},
}],
});
if (!contentReview) {
throw new HTTPError('Review not found or you don\'t have a permission!', 404);
}
return contentReview;
};

const getUserReviews = async (user_id) => {
const reviews = await models.content_reviews.findAll({
where: {
user_id,
},
include: [{
model: models.users,
as: 'user',
}, {
model: models.contents,
as: 'contents',
include: {
model: models.images,
as: 'image',
},
}],
});
return reviews;
};

const createReview = async ({ content_id, text, score, is_spoiler }, user_id) => {
const contentReview = await models.content_reviews.create({
user_id,
content_id,
text,
score,
is_spoiler,
});
const count = await models.content_reviews.count(
{
where: {
content_id,
},
},
);

const content = await models.contents.findOne({
where: {
id: content_id,
},
});
content.rate = (Number(content.rate) + ((Number(score * 2) - (Number(content.rate))) / count));
await content.save();
return contentReview;
};

await ReviewService.updateReview ({ text, is_spoiler, score }, id) => {
const review = await models.content_reviews.update({ text, is_spoiler, score }, {
where: {
id,
},
});
return contentReview;
};

const deleteReview = async (id) => {
const isDelete = await models.content_reviews.destroy({
where: {
id,
},
});
if (!isDelete) {
throw new HTTPError('review not found', 404);
}
return isDelete;
};

const ReviewService = {
createReview,
getReview,
getUserReviews,
updateReview,
deleteReview,
};
export default ReviewService;