Skip to content
This repository has been archived by the owner on Oct 22, 2020. It is now read-only.

Commit

Permalink
[#164] feat: WishBookHistory 와 Auditing 을 추가한다.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevLuffy committed May 23, 2020
1 parent 38f0569 commit cd0e543
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.woowacourse.tecobrary.config.audit;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.domain.AuditorAware;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Optional;

@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorProvider")
public class JpaAuditingConfiguration {

@Bean
public AuditorAware<String> auditorProvider() {
return () -> Optional.ofNullable(SecurityContextHolder.getContext()
.getAuthentication()
.getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.woowacourse.tecobrary.domain.common.entity;

import lombok.Getter;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.MappedSuperclass;

@Getter
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public class ModifiableBaseEntity extends ModifiableEntity {

@CreatedBy
@Column(length = 50)
private String createdBy;

@LastModifiedBy
@Column(length = 50)
private String modifiedBy;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.woowacourse.tecobrary.domain.wishbook.entity;

import com.woowacourse.tecobrary.domain.admin.entity.Admin;
import com.woowacourse.tecobrary.domain.common.entity.DeletableEntity;
import com.woowacourse.tecobrary.domain.user.entity.User;
import lombok.*;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
Expand Down Expand Up @@ -47,6 +50,9 @@ public class WishBook extends DeletableEntity {
@JoinColumn(name = "wish_book_request_user_id")
private User user;

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "wishBook")
private List<WishBookHistory> wishBookHistories = new ArrayList<>();

@Builder
public WishBook(String image,
String title,
Expand All @@ -65,14 +71,16 @@ public WishBook(String image,
this.status = WishBookStatus.REQUESTED;
this.reason = reason;
this.user = user;

logHistory("신규 등록", this.status);
}

public boolean isHandled() {
return status.isHandled();
}

public void enrollWishBook() {
handleWishBook("희망도서 등록", WishBookStatus.ENROLLED);
public void enrollWishBook(String reason) {
handleWishBook(reason, WishBookStatus.ENROLLED);
}

public void cancelWishBook(String reason) {
Expand All @@ -92,5 +100,15 @@ private void handleWishBook(String reason, WishBookStatus status) {
this.status = status;
this.reason = reason;
this.softDelete();

logHistory(reason, status);
}

private void logHistory(String reason, WishBookStatus status) {
wishBookHistories.add(WishBookHistory.builder()
.wishBook(this)
.reason(reason)
.status(status)
.build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.woowacourse.tecobrary.domain.wishbook.entity;

import com.woowacourse.tecobrary.domain.common.entity.ModifiableBaseEntity;
import lombok.*;

import javax.persistence.*;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
@Entity
@EqualsAndHashCode(of = "id", callSuper = false)
public class WishBookHistory extends ModifiableBaseEntity {

@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
private Long id;

@Column(nullable = false)
private String reason;

@Column(nullable = false, length = 30)
@Enumerated(EnumType.STRING)
private WishBookStatus status;

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "wish_book_id")
private WishBook wishBook;

@Builder
public WishBookHistory(String reason, WishBookStatus status, WishBook wishBook) {
this.reason = reason;
this.status = status;
this.wishBook = wishBook;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.woowacourse.tecobrary.domain.wishbook.repository;

import com.woowacourse.tecobrary.domain.wishbook.entity.WishBookHistory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface WishBookHistoryRepository extends JpaRepository<WishBookHistory, Long> {
}

0 comments on commit cd0e543

Please sign in to comment.