-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticles.models.js
149 lines (132 loc) · 3.66 KB
/
articles.models.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
const db = require("../db/connection");
const { convertTimestampToDate } = require("../db/seeds/utils");
exports.setArticles = (
topic,
sort_by = "created_at",
order = "desc",
limit = 10,
p = 1
) => {
let formatedArticlesQuery = `
SELECT
articles.article_id,
articles.title,
articles.author,
articles.topic,
articles.created_at,
articles.votes,
articles.article_img_url,
COUNT(comments.article_id)::int AS comment_count,
COUNT(*) OVER()::INT AS total_count
FROM articles
LEFT JOIN comments ON comments.article_id = articles.article_id
`;
const topicQuery = [];
const sortQueries = [
"votes",
"comment_count",
"article_id",
"author",
"title",
"topic",
"created_at",
];
const orderQueries = ["desc", "asc"];
if (topic) {
formatedArticlesQuery += "WHERE articles.topic = $1 ";
topicQuery.push(topic);
}
formatedArticlesQuery += " GROUP BY articles.article_id ";
if (sortQueries.includes(sort_by)) {
formatedArticlesQuery += `ORDER BY ${sort_by}`;
} else {
return Promise.reject({ status: 400, msg: "Invalid sort_by" });
}
if (orderQueries.includes(order)) {
formatedArticlesQuery += ` ${order}`;
} else {
return Promise.reject({ status: 400, msg: "Invalid order query" });
}
if (limit || p) {
let offset = 0;
if (!p || p < 0) p = 0;
if (isNaN(Number(limit)) || isNaN(Number(p))) {
return Promise.reject({
status: 400,
msg: "Invalid Input",
});
}
if (limit < 10) limit = 10;
if (p) offset = (p - 1) * limit;
formatedArticlesQuery += ` OFFSET ${offset} ROWS FETCH NEXT ${limit} ROWS ONLY;`;
}
return db.query(formatedArticlesQuery, topicQuery).then(({ rows }) => {
return rows;
});
};
exports.selectArticleById = (article_id) => {
let formatedArticleQuery = `
SELECT
articles.article_id,
articles.title,
articles.author,
articles.topic,
articles.created_at,
articles.votes,
articles.article_img_url,
articles.body,
COUNT(comments.article_id)::int AS comment_count
FROM articles
LEFT JOIN comments ON comments.article_id = articles.article_id
WHERE articles.article_id = $1
GROUP BY articles.article_id
`;
return db.query(formatedArticleQuery, [article_id]).then(({ rows }) => {
if (!rows.length) {
return Promise.reject({ status: 404, msg: "article was not found" });
}
return rows[0];
});
};
exports.patchArticleById = (article_id, newVotes) => {
return db
.query(
"UPDATE articles SET votes = votes + $1 WHERE article_id = $2 RETURNING *",
[newVotes, article_id]
)
.then(({ rows }) => rows[0]);
};
exports.insertArticle = (article) => {
const { created_at } = convertTimestampToDate({
created_at: new Date().getTime(),
});
if (!article.article_img_url) {
article.article_img_url =
"https://unsplash.com/photos/bundle-of-newspaper-on-table-Mwuod2cm8g4";
}
// const requiredKeys = ["title", "topic", "author", "body", "article_img_url"];
// for (let key of requiredKeys) {
// if (!article[key]) {
// throw { status: 400, msg: "Required key missing" };
// }
// }
return db
.query(
`INSERT INTO articles (title, topic, author, body, created_at, votes, article_img_url)
VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *`,
[
article.title,
article.topic,
article.author,
article.body,
created_at,
0,
article.article_img_url,
]
)
.then((article) => {
article.rows[0].comment_count = 0;
return article.rows;
});
};
exports.createAnArticle = (reqBody) => {};