วันพฤหัสบดีที่ 17 พฤษภาคม พ.ศ. 2561

Commit 15 Nodejs Wordbucket : add like, dislike, add new explanation to duplicate word on home page function

Nodejs : Wordbucket GitHub Link

Commit 15 Nodejs Wordbucket : add like, dislike, add new explanation to duplicate word on home page function (ต่อ)

Commits on May 3, 2018

- update README.md ตามของ project django
90  README.md
@@ -1,4 +1,86 @@
-# My WordBucket Version NodeJs + Express
-Start project : 4/25/2018
-Start push to git : 4/25/2018
-Autor Narudon S.
+# wordbucket
+
+Assignment 1 : My webapp. < WordBucket > by django framework
+เป็นส่วนหนึ่งของวิชา Software Development Practice II
+ภาษาที่ไม่มีการนำไปใช้ในชีวิตประจำวันเราเรียกว่า “ภาษาที่ตายแล้ว” ปัจจุบันแต่ละวันที่คำศัพท์เกิดใหม่มากมาย
+เช่น
+- weeb A weeb (/wi b/) is a non-Japanese male who watches and is a fan of CGDCT anime, has a waifu, a waifu pillow and is obsessed with Japan. [Credit](https://www.urbandictionary.com/define.php?term=weeb)
+
+- Jagoogala = just google it!
+ซึ่ง webapp. ที่อยากสร้างคือ word bank ให้ user มาแชร์ และ เก็บคำศัพท์ใหม่ๆที่เกิดขึ้นในปัจจุบัน
+
+## Features
+
+* Add word (with description)
+* Search (find word)
+* Browse (a-z)
+* Vote (useful or not)
+
+Domain name we thought “https://WordBucket.com/”
+
+==== Future Features ====
+* login system
+* csv
+
+
+# Django part
+
+## Model
+
+3 classes
+* word
+* explanation
+* like and dislike
+
+## View
+
+* home_page : หน้า home page ของ webapp.
+* add_word : เพิ่มคำศัพท์พร้อมคำอธิบาย (ถ้าคำศัพท์ซ้ำจะโชว์ url ของคำศัพท์นั้นๆ เพื่อให้ user เข้าไปเพิ่ม คำอธิบาย ของตัวเองใน คำศัพท์ที่มีอยู่แล้วได้)
+* view_word : ดูในแต่คำมีคำอธิบายอะไรบ้าง
+* add_explanation : ใส่คำอธิบายเพิ่มในคำๆนั้น
+* vote_like : โหวตชอบ
+* vote_dislike : โหวตไม่ชอบ
+* search : ค้นหาคำศัพท์
+
+==== Future View function ====
+(after login system)
+* remove_word (for admin) : ลบคำศัพท์สำหรับ เช่น Racism’s word
+* user_vote : โหวตของ user
+
+## URL config
+
+link urls.py ของ project กับ urls.py ของ app. name เหมือนชื่อ function ทุกอัน
+
+* home_page > '' (path)
+* add_word > 'add' (path)
+* view_word > r'^(\d+)/$' (repath)
+* add_explanation > r'^(\d+)/add_explanation$' (re_path)
+* vote_like > r'^(\d+)/vote_like$' (re_path)
+* vote_dislike > r'^(\d+)/vote_dislike$' (re_path)
+* search > r'^search/(.+)$' (re_path)
+
+## Tests
+
+functional test
+
+unit test
+
+now have 7 classes
+
+* HomePageTest
+* WordViewTest
+* NewWordTest
+* NewExplanationTest
+* VoteTest
+* AllAroundModelsTest
+* SearchAndBrowseTest
+
+
+## รายชื่อสมาชิกกลุ่ม
+
+* นาย ศุภณ์กัญจน์ สัตตพงศ์ 5701012620128 [blog link](https://b2-5720128.blogspot.com/)
+* นาย ไอยคุปต์ อาภรณ์ศิริ 5701012630204 [blog link](http://sdp-5730204.blogspot.com/)
+* นาย ณฤดล ทรงอนุสรณ์ 5701012610122 [blog link](https://softwaredevii.blogspot.com/)
+
+
+

functional_tests.py

- เพิ่มอีก functional test ของ django เข้ามา โดย test app duplicate word อธิบายเพิ่มเติม link

@@ -88,5 +88,54 @@ def test_can_start_a_list_and_retrieve_it_later_and_search(self):
# Now html render 'search' page. and 'weeb' url appear on her screen
self.check_for_row_in_list_table('weeb')
+ def test_can_view_the_word_explanation_and_add_exist_word_new_explanation(self):
+ # Ann has heard about a cool new online word app. She goes
+ # to check out its homepage
+ self.browser.get('http://localhost:3000')
+ # She is invited to enter a word item straight away
+ inputbox = self.browser.find_element_by_id('id_new_word')
+ self.assertEqual(
+ inputbox.get_attribute('placeholder'),
+ 'Add new word'
+ )
+
+ # She types "Jagoogala" into a text box and "just google it!" in a explanation text box
+ inputbox.send_keys('Jagoogala')
+ explanationbox = self.browser.find_element_by_id('id_new_eplanation')
+ explanationbox.send_keys('just google it!')
+
+ # When she hits enter, the page updates, and now the page word lists
+ # "1: Jagoogala" as an item in a word list table
+ inputbox.send_keys(Keys.ENTER)
+ self.check_for_row_in_list_table('Jagoogala')
+
+ # She test add exist word's second explanation
+ inputbox = self.browser.find_element_by_id('id_new_word')
+ inputbox.send_keys('Jagoogala')
+ explanationbox = self.browser.find_element_by_id('id_new_eplanation')
+ explanationbox.send_keys('type what you want to find out on google')
+ inputbox.send_keys(Keys.ENTER)
+ self.check_for_row_in_list_table('Jagoogala')
+
+ # She notice message "duplicate word, your explanation add to existing word."
+ time.sleep(1)
+ message_text = self.browser.find_element_by_tag_name('h4').text
+ self.assertIn('duplicate word, your explanation add to existing word.', message_text)
+
+ # She click on weeb's url.
+ url_table = self.browser.find_element_by_id('id_word_table')
+ url = url_table.find_element_by_link_text('Jagoogala')
+ url.send_keys(Keys.ENTER)
+
+ # She notices that her word has a unique URL
+ ann_list_url = self.browser.current_url
+ self.assertRegex(ann_list_url, '/.+')
+
+ # now the page "weeb" word
+ # "awesome!" as an item in a "weeb" word table
+ self.check_for_row_in_explanation_table('explanation 1 : just google it!\n 0 LIKE 0 DISLIKE')
+ self.check_for_row_in_explanation_table('explanation 2 : type what you want to find out on google\n 0 LIKE 0 DISLIKE')
+
+
if __name__ == '__main__':
unittest.main(warnings='ignore')

index.js

- ทำการ update function addingNewWord() ไม่ให้ add duplicate word เข้าไปได้ โดย query check ก่อน
- เพิ่ม function likeExplanation() กับ dislikeExplanation() เข้ามาโดย กดแล้วทำการ update ค่า vote like และ dislike ในตาราง Explanation โดยใช้ sequelize

147  index.js
@@ -22,32 +22,75 @@ var type = 1;
function addingNewWord (req, res, next) { //post route for adding new word
var word_ref = req.body.word_input
var explanation_ref = req.body.explanation_input
- if(word_ref == "" && explanation_ref == ""){
- type = 1;
- message = "Please enter the word.";
- return res.redirect("/message");
- }
- else if (word_ref != "" && explanation_ref == ""){
- message = ""
- Word.create({
- word: req.body.word_input,
- }); //add the new word from the post route into the array
- }
- else if (word_ref != "" && explanation_ref != ""){
- message = ""
- Explanation.create({
- Explanation_Word: {
- word: req.body.word_input,
- },
- explanation_text: req.body.explanation_input,
- like: 0,
- dislike: 0,
- }, {
- include: [ Explanation_Word ]
+
+
+ Word.findAll({
+ where: {
+ word: word_ref
+ },
+ attributes: ['id','word'],
+ raw : true
+ }).then(function(query) {
+ var word_k = [];
+ var id_k = []
+ var ref = [];
+ query.forEach(function(item) {
+ word_k.push(item.word);
+ id_k.push(item.id);
});
- }
- res.status(200)
- res.redirect("/"); //after adding to the array go back to the root route
+ console.log(word_k)
+ console.log(ref)
+ //no-duplicate
+ if (word_k.toString() == ref.toString()) {
+ console.log("no-duplicate")
+ if(word_ref == "" && explanation_ref == ""){
+ type = 1;
+ message = "Please enter the word.";
+ return res.redirect("/message");
+ }
+ else if (word_ref != "" && explanation_ref == ""){
+ message = ""
+ Word.create({
+ word: req.body.word_input,
+ }); //add the new word from the post route into the array
+ }
+ else if (word_ref != "" && explanation_ref != ""){
+ message = ""
+ Explanation.create({
+ Explanation_Word: {
+ word: req.body.word_input,
+ },
+ explanation_text: req.body.explanation_input,
+ like: 0,
+ dislike: 0,
+ }, {
+ include: [ Explanation_Word ]
+ });
+ }
+ res.status(200)
+ res.redirect("/"); //after adding to the array go back to the root route
+ }
+ //duplicate message "duplicate word, your explanation add to existing word."
+ else {
+ var wordID_ref = id_k.toString()
+ console.log("duplicate")
+ if (explanation_ref != "") {
+ Explanation.create({
+ ExplanationWordId: wordID_ref,
+ explanation_text: req.body.explanation_input,
+ like: 0,
+ dislike: 0,
+ });
+ type = 1;
+ message = "duplicate word, your explanation add to existing word.";
+ return res.redirect("/message");
+ }else if (explanation_ref == "") {
+ type = 1;
+ message = "duplicate word.";
+ return res.redirect("/message");
+ }
+ }
+ });
}
function addingNewExplanation (req, res, next) { //post route for adding new explanation
@@ -136,20 +179,22 @@ function view_word(req, res) { //view word detail
where: {
ExplanationWordId: wordID_ref
},
- attributes: ['ExplanationWordId','explanation_text','like','dislike'],
+ attributes: ['id','ExplanationWordId','explanation_text','like','dislike'],
raw : true
}).then(function(ref) {
var explanation = [];
var like = [];
var dislike = [];
var wordID = [];
+ var explanationID = [];
wordID.push(wordID_ref);
ref.forEach(function(item) {
explanation.push(item.explanation_text);
like.push(item.like);
dislike.push(item.dislike);
+ explanationID.push(item.id);
});
- res.render("detail", { word: word , wordID: wordID , explanation: explanation , message: message });
+ res.render("detail", { word: word , wordID: wordID , explanationID : explanationID , explanation: explanation , like : like , dislike : dislike , message: message });
});
});
}
@@ -185,11 +230,51 @@ function searchWord(req, res) { //search word
}
function likeExplanation(req, res) { //like explanation function
- pass;
+ var wordID_ref = Number(req.params.wordid)
+ var explanationID_ref = Number(req.params.explanationid)
+ Explanation.findAll({
+ where: {
+ id: explanationID_ref
+ },
+ attributes: ['id','explanation_text','like','dislike'],
+ raw : true
+ }).then(function(ref) {
+ var like = [];
+ ref.forEach(function(item) {
+ like.push(item.like);
+ });
+ Explanation.update({
+ like: Number(like.toString())+1
+ },{where: {
+ id: explanationID_ref
+ }});
+ console.log(Number(like.toString())+1)
+ return res.redirect("/word/"+wordID_ref);
+ });
}
function dislikeExplanation(req, res) { //dislike explanation function
- pass;
+ var wordID_ref = Number(req.params.wordid)
+ var explanationID_ref = Number(req.params.explanationid)
+ Explanation.findAll({
+ where: {
+ id: explanationID_ref
+ },
+ attributes: ['id','explanation_text','like','dislike'],
+ raw : true
+ }).then(function(ref) {
+ var dislike = [];
+ ref.forEach(function(item) {
+ dislike.push(item.dislike);
+ });
+ Explanation.update({
+ dislike: Number(dislike.toString())+1
+ },{where: {
+ id: explanationID_ref
+ }});
+ console.log(Number(dislike.toString())+1)
+ return res.redirect("/word/"+wordID_ref);
+ });
}
@@ -199,9 +284,9 @@ app.use('/addword', addingNewWord); //call function add word
app.use('/word/:id/addexplanation', addingNewExplanation);
-app.use('/word/:id/like', likeExplanation);
+app.use('/word/:wordid/:explanationid/like', likeExplanation);
-app.use('/word/:id/dislike', dislikeExplanation);
+app.use('/word/:wordid/:explanationid/dislike', dislikeExplanation);
app.use('/search/:word', searchWord);

ไม่มีความคิดเห็น:

แสดงความคิดเห็น