PageRenderTime 17ms CodeModel.GetById 10ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/ShowMeTheBestReply/grails-app/controllers/NewsController.groovy

http://showmethebestreply.googlecode.com/
Groovy | 74 lines | 55 code | 14 blank | 5 comment | 13 complexity | fe0c5aba58f3004f3710c75727495a6b MD5 | raw file
 1class NewsController {
 2	
 3	def index = { redirect(action:rank,params:params) }
 4	
 5    // ??? ? ?? ??? ??? NATE ???? ??? DB? ??? ???? (5?)
 6	static int updateTime = 1000 * 60 * 5
 7	
 8	def rank = { 
 9    	String section
10		String date
11		
12		if(params.sc) {
13			section = params.sc
14		} else {
15			section = 'all'
16		}
17    	
18    	if(params.date) {
19    		date = params.date
20    	} else {
21    		date = new Date().format('yyyyMMdd').toString()
22    	}
23    	
24    	Date dateParam = Date.parse('yyyyMMdd', date)
25    	def rankInfo = RankInfo.findBySectionAndDateCreatedBetween(section, new Date(dateParam.time), new Date((dateParam+1).time))
26		if(!rankInfo) {
27			rankInfo = RankInfo.parseRankInfo(section, date)
28			rankInfo.save()
29		} else if(rankInfo && new Date().time - rankInfo.dateViewed.time > updateTime) {
30			RankInfo.withTransaction {
31				rankInfo.delete()
32				rankInfo = RankInfo.parseRankInfo(section, date)
33				rankInfo.save()
34			}
35		}
36    	
37		def sortedListOfArticleRank = rankInfo.listOfArticleRank.sort{ it.rank }
38    	
39    	[ section:section, date:date, listOfArticleRank:sortedListOfArticleRank, ]
40    }
41    
42    def article = {
43    	def article = Article.findByArticleId(params.id)
44		def listOfBeple
45		
46		if(article && new Date().time - article.dateViewed.time > updateTime) {
47			// ?? articleId? ??? ??? updateTime? ????
48			// ?? ??? ???? Nate ???? ??? ??? ?? ??? DB? ??
49			Article.withTransaction {
50				article.delete()
51			
52				String articleLink = "http://news.nate.com/view/${params.id}"
53		    	article = Article.parseArticle(articleLink)
54				listOfBeple = Reply.parseBestReplies(articleLink)
55				article.listOfBeple = listOfBeple
56				
57				article.save()
58			}
59		} else if(article) {
60			// updateTime? ??? ?? ?? articleId? ??? ??? ?? ????
61			listOfBeple = article.listOfBeple.sort{ it.countOfGood }.reverse()
62		} else {
63			// DB? ?? ??? ??? Nate ???? ??? ??? ??? DB? ??
64			String articleLink = "http://news.nate.com/view/${params.id}"
65	    	article = Article.parseArticle(articleLink)
66			listOfBeple = Reply.parseBestReplies(articleLink)
67			article.listOfBeple = listOfBeple
68			
69			article.save()
70		}
71    	
72		[ article:article, listOfBeple:listOfBeple, ]
73    }
74}