Permalink
Browse files

Update topic now also updates the topic metadata (title, url, summary…

…, et cetera.)

Implemented mechanism to add news (to the topic list and its related content file)
Added a few files to the sandbox documenting an issue I ran into when it comes to reading and writing to the same file.
  • Loading branch information...
1 parent 188e910 commit f5cb556328bfc6b6bbcde871507735e665744bf9 @hectorcorrea committed Apr 29, 2012
View
@@ -1 +1 @@
-unit test content Mon Apr 23 2012 23:26:21 GMT-0400 (EDT)
+unit test content Sun Apr 29 2012 14:42:51 GMT-0400 (EDT)
View
@@ -0,0 +1,31 @@
+{ "nextId": 5, "blogs":[
+ {
+ "id": 1,
+ "title": "first blog post",
+ "url": "first-blog-post",
+ "summary": "Where we talk about the creation of the universe",
+ "postedOn": "2011-10-28 19:30"
+ },
+ {
+ "id": 2,
+ "title": "second blog post",
+ "url": "second-blog-post",
+ "summary": "Talking about dinosaurs",
+ "postedOn": "2012-04-08 22:10"
+ },
+ {
+ "id": 3,
+ "title": "third blog post",
+ "url": "third-blog-post",
+ "summary": "A meteor came and killed the dinosaurs",
+ "postedOn": "2012-05-09 22:10"
+ },
+ {
+ "id": 4,
+ "title": "unit test title Sat Apr 28 2012 19:13:57 GMT-0400 (EDT)",
+ "url": "unit-test-topic",
+ "summary": "This topic is for the unit tests, don't mess with it",
+ "postedOn": "2012-04-28T23:13:57.615Z",
+ "content": ""
+ }
+]}
View
@@ -1,32 +1,79 @@
-{
- "blogs": [
- {
- "id": 1,
- "title": "first blog post",
- "url": "first-blog-post",
- "summary": "Where we talk about the creation of the universe",
- "postedOn": "2011-10-28 19:30"
- },
- {
- "id": 2,
- "title": "second blog post",
- "url": "second-blog-post",
- "summary": "Talking about dinosaurs",
- "postedOn": "2012-04-08 22:10"
- },
- {
- "id": 3,
- "title": "third blog post",
- "url": "third-blog-post",
- "summary": "A meteor came and killed the dinosaurs",
- "postedOn": "2012-05-09 22:10"
- },
- {
- "id": 4,
- "title": "unit test topic",
- "url": "unit-test-topic",
- "summary": "This topic is for the unit tests, don't mess with it",
- "postedOn": "2012-04-23 22:10"
- }
- ]
-}
+{ "nextId": 11, "blogs":[
+ {
+ "id": 1,
+ "title": "first blog post",
+ "url": "first-blog-post",
+ "summary": "Where we talk about the creation of the universe",
+ "postedOn": "2011-10-28 19:30"
+ },
+ {
+ "id": 2,
+ "title": "second blog post",
+ "url": "second-blog-post",
+ "summary": "Talking about dinosaurs",
+ "postedOn": "2012-04-08 22:10"
+ },
+ {
+ "id": 3,
+ "title": "third blog post",
+ "url": "third-blog-post",
+ "summary": "A meteor came and killed the dinosaurs",
+ "postedOn": "2012-05-09 22:10"
+ },
+ {
+ "id": 4,
+ "title": "unit test title Sat Apr 28 2012 19:13:57 GMT-0400 (EDT)",
+ "url": "unit-test-topic",
+ "summary": "This topic is for the unit tests, don't mess with it",
+ "postedOn": "2012-04-28T23:13:57.615Z",
+ "content": ""
+ },
+ {
+ "title": "new unit test title Sun Apr 29 2012 14:35:44 GMT-0400 (EDT)",
+ "url": "unit-test-topic",
+ "summary": "new unit test summary Sun Apr 29 2012 14:35:44 GMT-0400 (EDT)",
+ "content": "new unit test content Sun Apr 29 2012 14:35:44 GMT-0400 (EDT)",
+ "postedOn": "2012-04-29T18:35:44.914Z",
+ "id": 5
+ },
+ {
+ "title": "new unit test title Sun Apr 29 2012 14:36:00 GMT-0400 (EDT)",
+ "url": "unit-test-topic",
+ "summary": "new unit test summary Sun Apr 29 2012 14:36:00 GMT-0400 (EDT)",
+ "content": "new unit test content Sun Apr 29 2012 14:36:00 GMT-0400 (EDT)",
+ "postedOn": "2012-04-29T18:36:00.186Z",
+ "id": 6
+ },
+ {
+ "title": "new unit test title Sun Apr 29 2012 14:40:56 GMT-0400 (EDT)",
+ "url": "unit-test-topic",
+ "summary": "new unit test summary Sun Apr 29 2012 14:40:56 GMT-0400 (EDT)",
+ "content": "new unit test content Sun Apr 29 2012 14:40:56 GMT-0400 (EDT)",
+ "postedOn": "2012-04-29T18:40:56.282Z",
+ "id": 7
+ },
+ {
+ "title": "new unit test title Sun Apr 29 2012 14:41:21 GMT-0400 (EDT)",
+ "url": "unit-test-topic",
+ "summary": "new unit test summary Sun Apr 29 2012 14:41:21 GMT-0400 (EDT)",
+ "content": "new unit test content Sun Apr 29 2012 14:41:21 GMT-0400 (EDT)",
+ "postedOn": "2012-04-29T18:41:21.167Z",
+ "id": 8
+ },
+ {
+ "title": "new unit test title Sun Apr 29 2012 14:41:28 GMT-0400 (EDT)",
+ "url": "unit-test-topic",
+ "summary": "new unit test summary Sun Apr 29 2012 14:41:28 GMT-0400 (EDT)",
+ "content": "new unit test content Sun Apr 29 2012 14:41:28 GMT-0400 (EDT)",
+ "postedOn": "2012-04-29T18:41:28.069Z",
+ "id": 9
+ },
+ {
+ "title": "new unit test title Sun Apr 29 2012 14:42:51 GMT-0400 (EDT)",
+ "url": "unit-test-topic",
+ "summary": "new unit test summary Sun Apr 29 2012 14:42:51 GMT-0400 (EDT)",
+ "content": "new unit test content Sun Apr 29 2012 14:42:51 GMT-0400 (EDT)",
+ "postedOn": "2012-04-29T18:42:51.385Z",
+ "id": 10
+ }
+]}
View
@@ -2,20 +2,23 @@ fs = require 'fs'
class BlogModel
- constructor: (@dataPath) ->
- @blogListFilePath = "#{dataPath}/blogs.json"
+ constructor: (@dataPath, blogList = "blogs.json") ->
+ @blogListFilePath = "#{dataPath}/#{blogList}"
+ @nextId = null
_getAllTopics: (callback) =>
- fs.readFile @blogListFilePath, 'utf8', (err, text) ->
+ fs.readFile @blogListFilePath, 'utf8', (err, text) =>
if err
- console.trace "_getAllTopics error"
- console.log "_getAllTopics error #{err}"
callback "Error reading topic list #{err}"
else
- data = JSON.parse text
- topics = data.blogs
- callback null, topics
+ try
+ data = JSON.parse text
+ @nextId = data.nextId
+ topics = data.blogs
+ callback null, topics
+ catch error
+ callback error
_findTopicInListByUrlSync: (topics, url) ->
@@ -52,18 +55,29 @@ class BlogModel
return topic
+ _updateTopicInListSync: (topics, topic) ->
+
+ for t in topics
+ if t.id is topic.id
+ t.title = topic.title
+ t.content= ""
+ t.postedOn = topic.postedOn
+ t.url = topic.url
+ return true
+
+ return false
+
+
# ===================================
# Public Methods
# ===================================
getAllTopics: (callback) =>
- #console.log "getAllTopics"
@_getAllTopics (err, topics) ->
callback err, topics
getTopicByUrl: (url, callback) =>
- #console.log "getTopicByUrl #{url}"
getTopicDetailsCallback = (err, topics) =>
callback err if err
@@ -84,7 +98,6 @@ class BlogModel
saveTopicByUrl: (url, content, callback) =>
- console.log "saveTopicByUrl #{url}"
getTopicDetailsCallback = (err, topics) =>
callback err if err
@@ -107,47 +120,75 @@ class BlogModel
saveTopic: (topic, callback) =>
- console.log "dataPath", @dataPath
- console.log "blogList", @blogListFilePath
- console.log "saveTopic #{topic.id}"
- callback "Topic Id is null", null if topic.id is null
- getTopicDetailsCallback = (err, topics) =>
+ updateTopic = (err, topics) =>
callback err if err
- topicMeta = @_findTopicInListByIdSync topics, topic.id
- if topicMeta is null
- callback "Topic not found, id=#{topic.id}"
- else
- updateTopic topicMeta
-
- updateTopic = (topicMeta) =>
- updateTopicMetaData topicMeta, (err, data) ->
- if err
- console.log "update topic error. Bailing out. Error: #{err}"
- callback err
+ if @_updateTopicInListSync(topics, topic) is false
+ callback "Could not find topic #{topic.id}"
+ return
+
+ try
+ jsonText = JSON.stringify topics, null, "\t"
+ jsonText = '{ "nextId": ' + @nextId + ', "blogs":' + jsonText + '}'
+ fs.writeFileSync @blogListFilePath, jsonText, 'utf8'
+ updateTopicContent()
+ catch error
+ callback error
+
+ updateTopicContent = =>
+ filePath = @dataPath + '/blog.' + topic.id + '.html'
+ fs.writeFile filePath, topic.content, 'utf8', (err) ->
+ if err
+ callback "Topic #{topic.id} content could not be saved. Error #{err}"
else
- updateTopicContent topic.id, topic.content
+ callback null, "OK"
- updateTopicMetaData = (topicMeta) =>
- console.log "TODO: update topic metadata"
- err = null
- if err
- callback err
- else
- updateTopicContent topicMeta.id, topic.content
+ if topic? is false
+ callback "No topic was received"
+ return
- updateTopicContent = (id, content) =>
- console.log "Updating topic content #{content}..."
- filePath = @dataPath + '/blog.' + id + '.html'
- fs.writeFile filePath, content, 'utf8', (err) ->
+ if topic.id? is false
+ callback "Topic Id is null"
+ return
+
+ @_getAllTopics(updateTopic)
+
+
+ saveNewTopic: (topic, callback) =>
+
+ addTopic = (err, topics) =>
+ callback err if err
+
+ try
+ # todo: validate topic data
+ topic.id = @nextId
+ topics.push topic
+ @nextId = @nextId + 1
+ jsonText = JSON.stringify topics, null, "\t"
+ jsonText = '{ "nextId": ' + @nextId + ', "blogs":' + jsonText + '}'
+ fs.writeFileSync @blogListFilePath, jsonText, 'utf8'
+ updateTopicContent()
+ catch error
+ callback error
+
+ updateTopicContent = =>
+ filePath = @dataPath + '/blog.' + topic.id + '.html'
+ fs.writeFile filePath, topic.content, 'utf8', (err) ->
if err
- callback "Topic #{url} content could not be saved. Error #{err}"
+ callback "Topic #{topic.id} content could not be saved. Error #{err}"
else
callback null, "OK"
+ if topic? is false
+ callback "No topic was received"
+ return
- @_getAllTopics(getTopicDetailsCallback)
+ # if topic.id? is false
+ # callback "Topic Id is null"
+ # return
+
+ @_getAllTopics(addTopic)
exports.BlogModel = BlogModel
View
@@ -3,7 +3,7 @@
, "version": "0.0.1"
, "private": true
, "dependencies": {
- "express": "2.5.8"
+ "express": ">= 2.5.8"
, "jade": ">= 0.0.1"
}
}
View
@@ -1,24 +1 @@
-{
- "blogs": [
- {
- "id": 1,
- "title": "first blog post",
- "url": "first-blog-post",
- "summary": "Where we talk about the creation of the universe",
- "postedOn": "2011-10-28 19:30"
- },
- {
- "id": 2,
- "title": "second blog post",
- "url": "second-blog-post",
- "summary": "Talking about dinosaurs",
- "postedOn": "2012-04-08 22:10"
- },
- {
- "id": 3,
- "title": "third blog post",
- "url": "third-blog-post",
- "summary": "A meteor came and killed the dinosaurs",
- "postedOn": "2012-05-09 22:10"
- }]
-}
+null
Oops, something went wrong.

0 comments on commit f5cb556

Please sign in to comment.