Git-HUB : https://github.com/braverokmc79/spring_boot_demo2
class QuestionController
package net.slipp.web;
import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import net.slipp.domain.Question;
import net.slipp.domain.QuestionRepository;
import net.slipp.domain.User;
@Controller
@RequestMapping("/questions")
public class QuestionController {
private static final Logger log = LoggerFactory.getLogger(QuestionController.class);
@Autowired
private QuestionRepository questionRepository;
@GetMapping("/form")
public String form(HttpSession session){
if(!HttpSessionUtils.isLoginUser(session)){
return "/users/loginForm";
}
return "/qna/form";
}
@PostMapping("")
public String create(HttpSession session, String title, String contents){
if(!HttpSessionUtils.isLoginUser(session)){
return "/users/loginForm";
}
log.info("create(String title, String contents) {} , {} ", title, contents);
User sessionUser =HttpSessionUtils.getUserFromSession(session);
Question newQuestion =new Question(sessionUser, title, contents);
questionRepository.save(newQuestion);
return "redirect:/";
}
@GetMapping("/{id}")
public String show(@PathVariable Long id, Model model){
model.addAttribute("question", questionRepository.findOne(id));
return "/qna/show";
}
@GetMapping("/{id}/form")
public String updateForm(@PathVariable Long id, Model model, HttpSession session){
if(!HttpSessionUtils.isLoginUser(session)){
return "/users/loginForm";
}
User sessionedUser =HttpSessionUtils.getUserFromSession(session);
Question question =questionRepository.findOne(id);
if(question.isSameWriter(sessionedUser)){
return "/users/loginForm";
}
model.addAttribute("question", questionRepository.findOne(id));
return "/qna/updateForm";
}
@PutMapping("/{id}")
public String update(@PathVariable Long id, String title, String contents, HttpSession session){
if(!HttpSessionUtils.isLoginUser(session)){
return "/users/loginForm";
}
User sessionedUser =HttpSessionUtils.getUserFromSession(session);
Question question =questionRepository.findOne(id);
if(question.isSameWriter(sessionedUser)){
return "/users/loginForm";
}
question.update(title, contents);
questionRepository.save(question);
return String.format("redirect:/questions/%d", id);
}
@DeleteMapping("/{id}")
public String delete(@PathVariable Long id, HttpSession session){
if(!HttpSessionUtils.isLoginUser(session)){
return "/users/loginForm";
}
User sessionedUser =HttpSessionUtils.getUserFromSession(session);
Question question =questionRepository.findOne(id);
if(question.isSameWriter(sessionedUser)){
return "/users/loginForm";
}
questionRepository.delete(id);
return "redirect:/";
}
}
class User
package net.slipp.domain;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
//널 방지
@Column(nullable=false, length=20)
private String userId;
private String name;
private String email;
private String password;
public User() {
}
public User(String userId, String name, String email, String password) {
super();
this.userId = userId;
this.name = name;
this.email = email;
this.password = password;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public boolean matchPassword(String newPassword){
if(newPassword==null){
return false;
}
return this.password.equals(newPassword);
}
public boolean matchId(Long newId){
if(newId==null){
return false;
}
return this.id.equals(newId);
}
@Override
public String toString() {
return "User [id=" + id + ", userId=" + userId + ", name=" + name + ", email=" + email + ", password="
+ password + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
}
class Question
package net.slipp.domain;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
public class Question {
@Id
@GeneratedValue
private Long id;
@ManyToOne
@JoinColumn(foreignKey=@ForeignKey(name="fk_question_writer"))
private User writer;
private String title;
private String contents;
@Temporal(TemporalType.TIMESTAMP)
private Date datetime;
public Question() {
}
public Question(User writer, String title, String contents) {
this.datetime = new Date();
this.writer = writer;
this.title = title;
this.contents = contents;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public User getWriter() {
return writer;
}
public void setWriter(User writer) {
this.writer = writer;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public Date getDatetime() {
return datetime;
}
public void setDatetime(Date datetime) {
this.datetime = datetime;
}
@Override
public String toString() {
return "Question [id=" + id + ", writer=" + writer + ", title=" + title + ", contents=" + contents
+ ", datetime=" + datetime + "]";
}
public void update(String title2, String contents2) {
this.title=title2;
this.contents=contents2;
}
public boolean isSameWriter(User sessionedUser) {
return this.writer.equals(sessionedUser);
}
}
class LocalDateTimeConverter
package net.slipp;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter(autoApply =true)
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Timestamp>{
@Override
public Timestamp convertToDatabaseColumn(LocalDateTime localDateTime) {
return localDateTime !=null ? Timestamp.valueOf(localDateTime) :null;
}
@Override
public LocalDateTime convertToEntityAttribute(Timestamp timestamp) {
return timestamp !=null ? timestamp.toLocalDateTime() : null;
}
}
import.sql
INSERT INTO USER (ID, USER_ID, PASSWORD , NAME, EMAIL) VALUES(1, 'admin', '1111' , '김민수' , 'kiminsu@gmail.com');
INSERT INTO USER (ID, USER_ID, PASSWORD , NAME, EMAIL) VALUES(2, 'test', '1111' , '홍길동' , 'test@gmail.com');
INSERT INTO QUESTION (id, writer_id, title, contents, create_date)
VALUES(1, 1, '국내에서 RuBy on Rails와 paly 가', 'Ruby on Rails(이하 RoR) 는', CURRENT_TIMESTAMP());
INSERT INTO QUESTION (id, writer_id, title, contents, create_date)
VALUES(1, 2, '순수', '나는 사실 울었어~ 후회 하면서', CURRENT_TIMESTAMP());
댓글 ( 4)
댓글 남기기