더북(TheBook)

10.4.2 코멘트 컨트롤러 작성하기

코멘트 모델을 만들었으니 이제 코멘트를 생성할 컨트롤러를 만들어 보자. app/controller에 comment.go 파일을 생성하고 다음과 같이 코드를 작성한다.

▼ app/controllers/comment.go

package controllers
 
import (
    “goblog/app/routes”
    “time”
 
“github.com/revel/modules/db/app” “github.com/revel/revel” ) type Comment struct { *revel.Controller db.Transactional } func (c Comment) Create(postId int, body, commenter string) revel.Result { // 댓글 저장 _, err := c.Txn.Exec(“insert into comments(body, commenter, post_id, created_at, updated_at) values(?,?,?,?,?)”, body, commenter, postId, time.Now(), time.Now()) if err != nil { panic(err) }
// 뷰에 Flash 메시지 전달 c.Flash.Success(“댓글 작성 완료”)
// 포스트 상세 보기 화면으로 이동 return c.Redirect(routes.Post.Show(postId)) }

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.