Permalink
Browse files

added a separate add comment action to the overview controller and a …

…new CommentService
  • Loading branch information...
1 parent 99ecc80 commit 637828ede856547e6a1efd4374b19a5f63c5adeb @jettro committed Nov 26, 2009
@@ -1,5 +1,7 @@
class OverviewController {
+ def commentService
+
def show = {
redirect(uri:"/server/show/${params.id}")
}
@@ -14,6 +16,21 @@ class OverviewController {
render(template:"/shared/comment",model:[comments:allComments, server:currentServer])
}
+ def addComment = {
+ commentService.createComment(params.serverId.toLong(),params.name,params.content)
+ redirect(action:'index')
+ }
+
+ def newComment = {
+ def currentServer = Server.findById(params.serverid)
+ if (!currentServer) {
+ flash.error = "You need to provide a server to be able to create new comment"
+
+ render(action:'index')
+ }
+ [server:currentServer]
+ }
+
def index = {
def environments = Environment.list()
[environments:environments]
@@ -0,0 +1,24 @@
+class CommentException extends RuntimeException {
+ String message
+ Comment comment
+}
+
+
+class CommentService {
+
+ boolean transactional = true
+
+ Comment createComment(long serverId, String name, String content) {
+ def server = Server.get(serverId)
+ if (!server) {
+ throw new CommentException(message:'provided id of server does not correspond to a real server instance')
+ }
+ def comment = new Comment(server:server, name:name, content:content)
+ if (comment.save()) {
+ return comment
+ } else {
+ throw new CommentException (message: 'invalid comment being created', comment:comment)
+ }
+
+ }
+}
@@ -1,11 +1,3 @@
-<%--
- Created by IntelliJ IDEA.
- User: jettropro
- Date: Nov 5, 2009
- Time: 8:49:07 AM
- To change this template use File | Settings | File Templates.
---%>
-
<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
@@ -0,0 +1,20 @@
+<%@ page contentType="text/html;charset=UTF-8" %>
+<html>
+<head>
+ <title>Add comment</title>
+ <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
+ <meta name="layout" content="main"/>
+</head>
+<body>
+<div class="nav">
+ <span class="menuButton"><a class="home" href="${resource(dir: '')}">Home</a></span>
+</div>
+ <span>New comment for ${server.toString()}</span>
+ <g:form action="addComment" method="POST">
+ <g:hiddenField name="serverId" value="${server.id}"/>
+ Name : <g:textField name="name" value=""/><br/>
+ Content : <g:textArea name="content" value=""/><br/>
+ <g:actionSubmit value="Add Comment"/>
+ </g:form>
+</body>
+</html>
@@ -7,7 +7,7 @@
--%>
<div class="comment">
- <span class="title">${server.name}</span><span class="action"><g:link controller="comment" action="create" params="['server.id':server.id]">create comment</g:link></span>
+ <span class="title">${server.name}</span><span class="action"><g:link controller="comment" action="create" params="['server.id':server.id]">create comment</g:link><g:link controller="overview" action="newComment" params="['serverid':server.id]">create form</g:link></span>
<g:each in="${comments}" status="i" var="comment">
<div>
<span class="author">${comment.name}</span>

0 comments on commit 637828e

Please sign in to comment.