Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/codesquad/CannotDeleteException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package codesquad;

public class CannotDeleteException extends RuntimeException {
private static final long serialVersionUID = 1L;

public CannotDeleteException(String message) {
super(message);
}
}
5 changes: 5 additions & 0 deletions src/main/java/codesquad/domain/ContentType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package codesquad.domain;

public enum ContentType {
Issue;
}
39 changes: 39 additions & 0 deletions src/main/java/codesquad/domain/DeleteHIstory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package codesquad.domain;

import javax.persistence.*;
import java.time.LocalDateTime;

@Entity
public class DeleteHIstory {
@Id
@GeneratedValue
private Long id;

@Enumerated(EnumType.STRING)
private ContentType contentType;

@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "fk_deletehistory_to_user"))
private User deleteBy;

private LocalDateTime createTime = LocalDateTime.now();

public DeleteHIstory() {
}

public DeleteHIstory(ContentType contentType,Long id, User deleteBy) {
this.contentType = contentType;
this.id = id;
this.deleteBy = deleteBy;
}

@Override
public String toString() {
return "DeleteHIstory{" +
"id=" + id +
", contentType=" + contentType +
", deleteBy=" + deleteBy +
", createTime=" + createTime +
'}';
}
}
7 changes: 7 additions & 0 deletions src/main/java/codesquad/domain/DeleteHistoryRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package codesquad.domain;

import org.springframework.data.repository.CrudRepository;

public interface DeleteHistoryRepository extends CrudRepository<DeleteHIstory, Long> {

}
75 changes: 68 additions & 7 deletions src/main/java/codesquad/domain/Issue.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package codesquad.domain;

import codesquad.CannotDeleteException;
import codesquad.UnAuthorizedException;
import codesquad.dto.IssueDto;
import support.domain.AbstractEntity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.*;
import javax.validation.constraints.Size;

@Entity
Expand All @@ -17,18 +19,29 @@ public class Issue extends AbstractEntity {
@Column(nullable = false, length = 500)
private String comment;

@ManyToOne
@JoinColumn(foreignKey = @ForeignKey(name = "fk_Issue_writter"))
private User writer;

private boolean deleted = false;

public Issue() {
}

public Issue(@Size(min = 3, max = 50) String subject, @Size(min = 3, max = 500) String content) {
public Issue(String subject, String content) {
this.subject = subject;
this.comment = content;
}

public Issue(long id, @Size(min = 3, max = 50) String subject, @Size(min = 3, max = 500) String content) {
public Issue(String subject, String comment, User writer) {
this(0L, subject, comment, writer);
}

public Issue(long id, String subject, String content, User writer) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

생성자가 너무 많은데 꼭 필요한 생성자만 추가할 수 없을까?

super(id);
this.subject = subject;
this.comment = content;
this.writer = writer;
}

public String getSubject() {
Expand All @@ -47,8 +60,56 @@ public void setComment(String comment) {
this.comment = comment;
}

public User getWriter() {
return writer;
}

public void setWriter(User writer) {
this.writer = writer;
}

public boolean isDeleted() {
return deleted;
}

public void setDeleted(boolean deleted) {
this.deleted = deleted;
}

public void update(User loginUser, Issue toIssue) {
if (!matchWriter(loginUser)) {
throw new UnAuthorizedException();
}

if (!matchPassord(loginUser.getPassword())) {
return;
}
this.subject = toIssue.subject;
this.comment = toIssue.comment;
}

public boolean isOwner(User loginUser) {
return writer.equals(loginUser);
}

private boolean matchPassord(String password) {
return this.writer.getPassword().equals(password);
}

private boolean matchWriter(User loginUser) {
return this.writer.getUserId().equals(loginUser.getUserId());
}

// ApiIssueController에서 사용할 것이지만 일단은 만들어 보았다.
// public IssueDto _toIssueDto() {
// return new IssueDto(this.subject, this.comment);
// }
public IssueDto _toIssueDto() {
return new IssueDto(this.subject, this.comment, this.writer);
}

public DeleteHIstory delete(User loginUser) {
if (!this.writer.equals(loginUser)) {
throw new CannotDeleteException("본인외의 정보는 삭제할수 없습니다.");
}
this.deleted = true;
return new DeleteHIstory(ContentType.Issue, getId(), loginUser);
}
}
6 changes: 2 additions & 4 deletions src/main/java/codesquad/domain/IssueRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface IssueRepository extends JpaRepository<Issue, Long> {
@Override
Optional<Issue> findById(Long Long);

Iterable<Issue> findByDelete(boolean b);
}
32 changes: 24 additions & 8 deletions src/main/java/codesquad/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.validation.constraints.Size;
import java.util.Objects;

@Entity
public class User extends AbstractEntity {
Expand All @@ -29,13 +30,8 @@ public class User extends AbstractEntity {
public User() {
}

/* public User(String userId, String password, String name) {
this(0L, userId, password, name);
}*/
public User(String userId, String password, String name) {
this.userId = userId;
this.password = password;
this.name = name;
this(0L, userId, password, name);
}

public User(long id, String userId, String password, String name) {
Expand Down Expand Up @@ -82,7 +78,7 @@ public void update(User loginUser, User target) {
}

if (!matchPassword(target.getPassword())) {
return;
throw new UnAuthorizedException();
}

this.name = target.name;
Expand Down Expand Up @@ -110,6 +106,26 @@ public boolean isGuestUser() {

@Override
public String toString() {
return "User [userId=" + userId + ", password=" + password + ", name=" + name + "]";
return "User{" +
"userId='" + userId + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
User user = (User) o;
return Objects.equals(userId, user.userId) &&
Objects.equals(password, user.password) &&
Objects.equals(name, user.name);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), userId, password, name);
}
}
30 changes: 28 additions & 2 deletions src/main/java/codesquad/dto/IssueDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package codesquad.dto;

import codesquad.domain.Issue;
import codesquad.domain.User;

import javax.validation.constraints.Size;

Expand All @@ -11,18 +12,35 @@ public class IssueDto {
@Size(min = 3, max = 500)
private String comment;

private User writer;

private boolean deleted;

public IssueDto() {
}

public IssueDto(@Size(min = 3, max = 50) String subject, @Size(min = 3, max = 500) String content) {
public IssueDto(String subject, String content, User writer) {
this.subject = subject;
this.comment = content;
this.writer = writer;
}

public void writeBy(User user) {
this.writer = user;
}

public String getSubject() {
return subject;
}

public User getWriter() {
return writer;
}

public void setWriter(User writer) {
this.writer = writer;
}

public void setSubject(String subject) {
this.subject = subject;
}
Expand All @@ -35,8 +53,16 @@ public void setComment(String comment) {
this.comment = comment;
}

public boolean isDeleted() {
return deleted;
}

public void setDeleted(boolean deleted) {
this.deleted = deleted;
}

public Issue _toIssue() {
return new Issue(subject, comment);
return new Issue(this.subject, this.comment, this.writer);
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/codesquad/dto/UserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ public class UserDto {
private String name;

public UserDto() {

}

public UserDto(String userId, String password, String name) {
// super();
super();
this.userId = userId;
this.password = password;
this.name = name;
Expand Down
1 change: 0 additions & 1 deletion src/main/java/codesquad/security/HttpSessionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public static User getUserFromSession(HttpSession session) {
if (!isLoginUser(session)) {
return null;
}

return (User) session.getAttribute(USER_SESSION_KEY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.web.bind.annotation.ResponseStatus;

import javax.persistence.EntityNotFoundException;
import javax.security.auth.login.LoginException;

@ControllerAdvice
public class SecurityControllerAdvice {
Expand All @@ -32,4 +33,8 @@ public void unAuthorized() {
public void unAuthentication() {
log.debug("UnAuthenticationException is happened!");
}

@ExceptionHandler(LoginException.class)
@ResponseStatus(value = HttpStatus.FORBIDDEN)
public void loginFail() {log.debug("login fail");}
}
21 changes: 21 additions & 0 deletions src/main/java/codesquad/service/DeleteHistoryService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package codesquad.service;

import codesquad.domain.DeleteHistoryRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DeleteHistoryService {

@Resource(name = "deleteHistoryRepository")
private DeleteHistoryRepository deleteHistoryRepository;

// @Transactional(propagation = Propagation.REQUIRES_NEW)
// public void saveAll() {
// deleteHistoryRepository.save();
// }
}
Loading