diff --git a/src/main/java/playground/PlaygroundApplication.java b/src/main/java/playground/PlaygroundApplication.java index bea3f7f..f3b38df 100644 --- a/src/main/java/playground/PlaygroundApplication.java +++ b/src/main/java/playground/PlaygroundApplication.java @@ -2,8 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; @SpringBootApplication +@EnableJpaAuditing public class PlaygroundApplication { public static void main(String[] args) { SpringApplication.run(PlaygroundApplication.class, args); diff --git a/src/main/java/playground/controller/DocumentController.java b/src/main/java/playground/controller/DocumentController.java index 4c158eb..0b6c36e 100644 --- a/src/main/java/playground/controller/DocumentController.java +++ b/src/main/java/playground/controller/DocumentController.java @@ -3,6 +3,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import playground.dto.CategoryResponse; import playground.dto.DocumentOutboxResponse; import playground.dto.DocumentRequest; import playground.dto.DocumentResponse; @@ -11,26 +12,30 @@ import java.util.List; @RestController -@RequestMapping("/api") +@RequestMapping("/api/documents") public class DocumentController { @Autowired DocumentService documentService; - @GetMapping("/document/{documentId}") + @GetMapping("/{documentId}") public ResponseEntity getDocumentById(@PathVariable Long documentId) { DocumentResponse DocumentResponse = documentService.findDocumentBy(documentId); return ResponseEntity.ok().body(DocumentResponse); } - @GetMapping("/documents/outbox") + @GetMapping("/outbox") public ResponseEntity> getDocumentsOutbox(@RequestParam Long userId) { List DocumentOutboxResponse = documentService.findDocumentsOutbox(userId); return ResponseEntity.ok().body(DocumentOutboxResponse); } - @PostMapping("/document") + @PostMapping public ResponseEntity insertDocument(@RequestBody DocumentRequest documentRequest) { Long id = documentService.insertDocument(documentRequest); return ResponseEntity.ok().body(id); } + @GetMapping("/categories") + public List getCategories(){ + return documentService.getCategories(); + } } diff --git a/src/main/java/playground/controller/TeamController.java b/src/main/java/playground/controller/TeamController.java new file mode 100644 index 0000000..3c2649d --- /dev/null +++ b/src/main/java/playground/controller/TeamController.java @@ -0,0 +1,23 @@ +package playground.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; +import playground.dto.TeamResponse; +import playground.service.TeamService; + +import java.util.List; + +@RestController +@RequestMapping("/api/teams") +public class TeamController { + + @Autowired + TeamService teamService; + + @GetMapping() + public List getTeams() { + return teamService.findAll(); + } +} diff --git a/src/main/java/playground/controller/UserController.java b/src/main/java/playground/controller/UserController.java new file mode 100644 index 0000000..8b4c14a --- /dev/null +++ b/src/main/java/playground/controller/UserController.java @@ -0,0 +1,26 @@ +package playground.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import playground.dto.UserResponse; +import playground.service.UserService; + +import java.util.List; + +@RestController +@RequestMapping("/api/users") +public class UserController { + + @Autowired + UserService userService; + + @GetMapping + public List getUserBy( + @RequestParam Long teamId + ) { + return userService.findBy(teamId); + } +} diff --git a/src/main/java/playground/domain/ApprovalState.java b/src/main/java/playground/domain/ApprovalState.java index 8f66b2c..e557521 100644 --- a/src/main/java/playground/domain/ApprovalState.java +++ b/src/main/java/playground/domain/ApprovalState.java @@ -11,6 +11,6 @@ public enum ApprovalState { APPROVED("승인"), CANCELED("거절"); - private final String status; + private final String name; } \ No newline at end of file diff --git a/src/main/java/playground/domain/BaseEntity.java b/src/main/java/playground/domain/BaseEntity.java new file mode 100644 index 0000000..2d6e330 --- /dev/null +++ b/src/main/java/playground/domain/BaseEntity.java @@ -0,0 +1,21 @@ +package playground.domain; + +import lombok.Getter; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import javax.persistence.EntityListeners; +import javax.persistence.MappedSuperclass; +import java.time.LocalDateTime; + +@Getter +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +public class BaseEntity { + @CreatedDate + private LocalDateTime createdDate; + + @LastModifiedDate + private LocalDateTime modifiedDate; +} diff --git a/src/main/java/playground/domain/Category.java b/src/main/java/playground/domain/Category.java index 393d284..e73cc4b 100644 --- a/src/main/java/playground/domain/Category.java +++ b/src/main/java/playground/domain/Category.java @@ -11,7 +11,7 @@ public enum Category { EDUCATION("교육"), PRODUCT_PURCHASING("물품구매"); - private final String category; + private final String name; } \ No newline at end of file diff --git a/src/main/java/playground/domain/Document.java b/src/main/java/playground/domain/Document.java index b18e06c..a4f5ef4 100644 --- a/src/main/java/playground/domain/Document.java +++ b/src/main/java/playground/domain/Document.java @@ -5,7 +5,6 @@ import lombok.Getter; import lombok.NoArgsConstructor; import org.springframework.data.annotation.CreatedDate; -import playground.dto.DocumentRequest; import javax.persistence.*; import java.sql.Date; @@ -17,9 +16,9 @@ @Entity @NoArgsConstructor @AllArgsConstructor -public class Document { +public class Document extends BaseEntity { @Id - @GeneratedValue + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String title; @@ -29,37 +28,46 @@ public class Document { private String contents; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "drafter_id") private User drafter; @Enumerated(EnumType.STRING) private ApprovalState approvalState = ApprovalState.DRAFTING; - @Transient + @OneToMany(mappedBy = "document", cascade = CascadeType.ALL, orphanRemoval = true) private List documentApprovals = new ArrayList<>(); @Transient - private int approvalIndex; - - @CreatedDate - private Date createdAt; + private int approvalIndex = 0; @Builder - public Document(Long id, String title, Category category, String contents, User drafter, Date createdAt) { + public Document(Long id, String title, Category category, String contents, User drafter, List documentApprovals) { this.id = id; this.title = title; this.category = category; this.contents = contents; this.drafter = drafter; - this.createdAt = createdAt; + this.documentApprovals = documentApprovals; + } + + public Document(String title, Category category, String contents, User drafter) { + this.title = title; + this.category = category; + this.contents = contents; + this.drafter = drafter; + } + + public void addDocumentApproval(final DocumentApproval documentApproval){ + documentApprovals.add(documentApproval); + documentApproval.setDocument(this); } public void addApprovers(List approvals) { AtomicInteger index = new AtomicInteger(); index.getAndIncrement(); approvalIndex = 0; - approvals.forEach(approver-> addDocumetApprovals(approver, index.getAndIncrement())); + approvals.forEach(approver -> addDocumetApprovals(approver, index.getAndIncrement())); } public List getDocumentApprovals() { diff --git a/src/main/java/playground/domain/DocumentApproval.java b/src/main/java/playground/domain/DocumentApproval.java index 2d29c41..bcbfdec 100644 --- a/src/main/java/playground/domain/DocumentApproval.java +++ b/src/main/java/playground/domain/DocumentApproval.java @@ -2,17 +2,21 @@ import lombok.Builder; import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; import javax.persistence.*; @Getter +@Setter @Entity -public class DocumentApproval { +@NoArgsConstructor +public class DocumentApproval extends BaseEntity { @Id - @GeneratedValue + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @ManyToOne + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") private User approver; @@ -23,6 +27,10 @@ public class DocumentApproval { private String approvalComment; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "document_id") + private Document document; + @Builder public DocumentApproval(User approver, ApprovalState approvalState, int approvalOrder) { this.approver = approver; @@ -30,6 +38,14 @@ public DocumentApproval(User approver, ApprovalState approvalState, int approval this.approvalOrder = approvalOrder; } + @Builder + public DocumentApproval(User approver, int approvalOrder, Document document) { + this.approver = approver; + this.approvalState = ApprovalState.DRAFTING; + this.approvalOrder = approvalOrder; + this.document = document; + } + public DocumentApproval approveBy(User approver, String approvalComment) { this.approvalComment = approvalComment; this.approver = approver; diff --git a/src/main/java/playground/domain/Rank.java b/src/main/java/playground/domain/Rank.java new file mode 100644 index 0000000..5ada5ef --- /dev/null +++ b/src/main/java/playground/domain/Rank.java @@ -0,0 +1,16 @@ +package playground.domain; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +@Getter +@RequiredArgsConstructor +public enum Rank { + + TEAM_LEADER("팀장"), + PART_MANAGER("파트장"), + TEAM_MEMBER("팀원"); + + private final String name; + +} \ No newline at end of file diff --git a/src/main/java/playground/domain/Team.java b/src/main/java/playground/domain/Team.java new file mode 100644 index 0000000..0fd84fe --- /dev/null +++ b/src/main/java/playground/domain/Team.java @@ -0,0 +1,26 @@ +package playground.domain; + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +@Getter +@NoArgsConstructor +public class Team extends BaseEntity { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String name; + + @Builder + public Team(String name) { + this.name = name; + } +} diff --git a/src/main/java/playground/domain/User.java b/src/main/java/playground/domain/User.java index 0731939..26a62cb 100644 --- a/src/main/java/playground/domain/User.java +++ b/src/main/java/playground/domain/User.java @@ -5,19 +5,36 @@ import lombok.Getter; import lombok.NoArgsConstructor; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; +import javax.persistence.*; @NoArgsConstructor @Getter @AllArgsConstructor @Builder @Entity -public class User { +public class User extends BaseEntity { @Id - @GeneratedValue + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + private String name; + private String email; + + private String password; + + @Enumerated(EnumType.STRING) + private Rank rank; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "team_id") + private Team team; + + public User(String name, String email, String password, Rank rank, Team team) { + this.name = name; + this.email = email; + this.password = password; + this.rank = rank; + this.team = team; + } } diff --git a/src/main/java/playground/dto/ApprovalResponse.java b/src/main/java/playground/dto/ApprovalResponse.java new file mode 100644 index 0000000..6411f21 --- /dev/null +++ b/src/main/java/playground/dto/ApprovalResponse.java @@ -0,0 +1,35 @@ +package playground.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import playground.domain.DocumentApproval; + +@Data +@AllArgsConstructor +@NoArgsConstructor +public class ApprovalResponse { + + private String approvarTeamName; + + private String approvarName; + + private int approvalOrder; + + private String approvalComment; + + private String approvalStateText; + + public ApprovalResponse(DocumentApproval approvar) { + + this.approvarTeamName = approvar.getApprover().getTeam().getName(); + + this.approvarName = approvar.getApprover().getName(); + + this.approvalOrder = approvar.getApprovalOrder(); + + this.approvalComment = approvar.getApprovalComment(); + + this.approvalStateText = approvar.getApprovalState().getName(); + } +} diff --git a/src/main/java/playground/dto/CategoryResponse.java b/src/main/java/playground/dto/CategoryResponse.java new file mode 100644 index 0000000..fa7160c --- /dev/null +++ b/src/main/java/playground/dto/CategoryResponse.java @@ -0,0 +1,14 @@ +package playground.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class CategoryResponse { + private String value; + + private String text; +} diff --git a/src/main/java/playground/dto/DocumentOutboxResponse.java b/src/main/java/playground/dto/DocumentOutboxResponse.java index 5e94535..a6ea262 100644 --- a/src/main/java/playground/dto/DocumentOutboxResponse.java +++ b/src/main/java/playground/dto/DocumentOutboxResponse.java @@ -19,7 +19,7 @@ public DocumentOutboxResponse(Document document) { this.title = document.getTitle(); this.category = document.getCategory().name(); this.approvalState = document.getApprovalState().name(); - this.categoryText = document.getCategory().getCategory(); - this.approvalStateText = document.getApprovalState().getStatus(); + this.categoryText = document.getCategory().getName(); + this.approvalStateText = document.getApprovalState().getName(); } } diff --git a/src/main/java/playground/dto/DocumentResponse.java b/src/main/java/playground/dto/DocumentResponse.java index f6880f5..b570a86 100644 --- a/src/main/java/playground/dto/DocumentResponse.java +++ b/src/main/java/playground/dto/DocumentResponse.java @@ -1,8 +1,12 @@ package playground.dto; -import playground.domain.Document; import lombok.Getter; import lombok.Setter; +import playground.domain.Document; +import playground.domain.DocumentApproval; + +import java.util.ArrayList; +import java.util.List; @Getter @Setter @@ -11,35 +15,27 @@ public class DocumentResponse { private String title; private String category; private String contents; - private long userId; - private String approvalState; - private String userName; + private UserResponse drafter; + private List approvers; private String categoryText; private String approvalStateText; - public DocumentResponse(Document document, String userName) { + public DocumentResponse(Document document) { this.id = document.getId(); this.title = document.getTitle(); this.category = document.getCategory().name(); this.contents = document.getContents(); - this.userId = document.getDrafter().getId(); - this.approvalState = document.getApprovalState().name(); - this.userName = document.getDrafter().getName(); - this.categoryText = document.getCategory().getCategory(); - this.approvalStateText = document.getApprovalState().getStatus(); - this.userName = userName; + this.drafter = new UserResponse(document.getDrafter()); + this.approvers = getApprovars(document.getDocumentApprovals()); + this.categoryText = document.getCategory().getName(); + this.approvalStateText = document.getApprovalState().getName(); } - public DocumentResponse(Document document) { - this.id = document.getId(); - this.title = document.getTitle(); - this.category = document.getCategory().name(); - this.contents = document.getContents(); - this.userId = document.getDrafter().getId(); - this.approvalState = document.getApprovalState().name(); - this.userName = document.getDrafter().getName(); - this.categoryText = document.getCategory().getCategory(); - this.approvalStateText = document.getApprovalState().getStatus(); - this.userName = document.getDrafter().getName(); + private List getApprovars(List approvars) { + List approvarsResponse = new ArrayList<>(); + for (DocumentApproval documentApproval : approvars) { + approvarsResponse.add(new ApprovalResponse(documentApproval)); + } + return approvarsResponse; } } diff --git a/src/main/java/playground/dto/TeamResponse.java b/src/main/java/playground/dto/TeamResponse.java new file mode 100644 index 0000000..8fbda5b --- /dev/null +++ b/src/main/java/playground/dto/TeamResponse.java @@ -0,0 +1,19 @@ +package playground.dto; + +import lombok.Data; +import lombok.NoArgsConstructor; +import playground.domain.Team; + +@NoArgsConstructor +@Data +public class TeamResponse { + + private Long id; + + private String name; + + public TeamResponse(Team team){ + this.id = team.getId(); + this.name = team.getName(); + } +} diff --git a/src/main/java/playground/dto/UserResponse.java b/src/main/java/playground/dto/UserResponse.java new file mode 100644 index 0000000..9a2b8ca --- /dev/null +++ b/src/main/java/playground/dto/UserResponse.java @@ -0,0 +1,28 @@ +package playground.dto; + +import lombok.Data; +import lombok.NoArgsConstructor; +import playground.domain.User; + +@Data +@NoArgsConstructor +public class UserResponse { + + private Long id; + + private String jobPosition; + + private String teamName; + + private String name; + + private String jobPositionText; + + public UserResponse(User user) { + this.id = user.getId(); + this.jobPosition = user.getRank().toString(); + this.teamName = user.getTeam().getName(); + this.name = user.getName(); + this.jobPositionText = user.getRank().getName(); + } +} diff --git a/src/main/java/playground/repository/DocumentApprovalRepository.java b/src/main/java/playground/repository/DocumentApprovalRepository.java new file mode 100644 index 0000000..e5ab3f7 --- /dev/null +++ b/src/main/java/playground/repository/DocumentApprovalRepository.java @@ -0,0 +1,12 @@ +package playground.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import playground.domain.Document; +import playground.domain.DocumentApproval; + +import java.util.List; + +public interface DocumentApprovalRepository extends JpaRepository { + + List findByDocument(Document document); +} diff --git a/src/main/java/playground/repository/DocumentRepository.java b/src/main/java/playground/repository/DocumentRepository.java index 9652fd0..acecf4c 100644 --- a/src/main/java/playground/repository/DocumentRepository.java +++ b/src/main/java/playground/repository/DocumentRepository.java @@ -1,10 +1,12 @@ package playground.repository; import org.springframework.data.jpa.repository.JpaRepository; +import playground.domain.ApprovalState; import playground.domain.Document; import java.util.List; public interface DocumentRepository extends JpaRepository { - List findByDrafterId(Long userId); + + List findByDrafterIdOrderByCreatedDateDesc(Long id); } diff --git a/src/main/java/playground/repository/TeamRepositiory.java b/src/main/java/playground/repository/TeamRepositiory.java new file mode 100644 index 0000000..dbf672e --- /dev/null +++ b/src/main/java/playground/repository/TeamRepositiory.java @@ -0,0 +1,7 @@ +package playground.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import playground.domain.Team; + +public interface TeamRepositiory extends JpaRepository { +} diff --git a/src/main/java/playground/repository/UserRepository.java b/src/main/java/playground/repository/UserRepository.java index 083b629..3772dd5 100644 --- a/src/main/java/playground/repository/UserRepository.java +++ b/src/main/java/playground/repository/UserRepository.java @@ -1,7 +1,11 @@ package playground.repository; import org.springframework.data.jpa.repository.JpaRepository; +import playground.domain.Team; import playground.domain.User; +import java.util.List; + public interface UserRepository extends JpaRepository { + List findByTeam(Team team); } diff --git a/src/main/java/playground/service/DocumentService.java b/src/main/java/playground/service/DocumentService.java index 8f83201..bb81b01 100644 --- a/src/main/java/playground/service/DocumentService.java +++ b/src/main/java/playground/service/DocumentService.java @@ -4,13 +4,14 @@ import org.springframework.stereotype.Component; import playground.domain.Category; import playground.domain.Document; +import playground.domain.DocumentApproval; import playground.domain.User; -import playground.dto.DocumentOutboxResponse; -import playground.dto.DocumentRequest; -import playground.dto.DocumentResponse; +import playground.dto.*; +import playground.repository.DocumentApprovalRepository; import playground.repository.DocumentRepository; import playground.repository.UserRepository; +import javax.transaction.Transactional; import java.util.ArrayList; import java.util.List; @@ -22,13 +23,18 @@ public class DocumentService { @Autowired UserRepository userRepository; + @Autowired + DocumentApprovalRepository documentApprovalRepository; + + @Transactional public DocumentResponse findDocumentBy(Long documentId) { Document document = documentRepository.findById(documentId).get(); return new DocumentResponse(document); } + @Transactional public List findDocumentsOutbox(Long userId) { - List documents = documentRepository.findByDrafterId(userId); + List documents = documentRepository.findByDrafterIdOrderByCreatedDateDesc(userId); List documentOutboxDtos = new ArrayList<>(); for (Document document : documents) { documentOutboxDtos.add(new DocumentOutboxResponse(document)); @@ -36,12 +42,17 @@ public List findDocumentsOutbox(Long userId) { return documentOutboxDtos; } + @Transactional public long insertDocument(DocumentRequest documentRequest) { Document document = getDocumentBy(documentRequest); documentRepository.save(document); + List documentApprovals = getDocumentApprovals(documentRequest.getApproverIds(), document); + documentApprovals.stream().forEach((documentApproval) -> documentApprovalRepository.save(documentApproval)); return document.getId(); } - private Document getDocumentBy(DocumentRequest documentRequest){ + + @Transactional + private Document getDocumentBy(DocumentRequest documentRequest) { User drafter = userRepository.findById(documentRequest.getDrafterId()).get(); return Document.builder() .title(documentRequest.getTitle()) @@ -50,4 +61,22 @@ private Document getDocumentBy(DocumentRequest documentRequest){ .drafter(drafter) .build(); } + + @Transactional + public List getCategories() { + List categoryResponses = new ArrayList<>(); + for (Category category : Category.values()) { + categoryResponses.add(new CategoryResponse(category.toString(), category.getName())); + } + return categoryResponses; + } + + private List getDocumentApprovals(List approvars, Document document) { + List documentApprovals = new ArrayList<>(); + for (int i = 0; i < approvars.size(); i++) { + User approvar = userRepository.findById(approvars.get(i)).get(); + documentApprovals.add(new DocumentApproval(approvar, i, document)); + } + return documentApprovals; + } } diff --git a/src/main/java/playground/service/TeamService.java b/src/main/java/playground/service/TeamService.java new file mode 100644 index 0000000..77f7e43 --- /dev/null +++ b/src/main/java/playground/service/TeamService.java @@ -0,0 +1,37 @@ +package playground.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import playground.domain.Category; +import playground.domain.Document; +import playground.domain.Team; +import playground.domain.User; +import playground.dto.DocumentOutboxResponse; +import playground.dto.DocumentRequest; +import playground.dto.DocumentResponse; +import playground.dto.TeamResponse; +import playground.repository.TeamRepositiory; +import playground.repository.UserRepository; + +import javax.transaction.Transactional; +import java.util.ArrayList; +import java.util.List; + +@Component +public class TeamService { + @Autowired + TeamRepositiory teamRepositiory; + + @Autowired + UserRepository userRepository; + + @Transactional + public List findAll() { + List teams = teamRepositiory.findAll(); + List teamResponses = new ArrayList<>(); + for (Team team : teams) { + teamResponses.add(new TeamResponse(team)); + } + return teamResponses; + } +} diff --git a/src/main/java/playground/service/UserService.java b/src/main/java/playground/service/UserService.java new file mode 100644 index 0000000..0e493c5 --- /dev/null +++ b/src/main/java/playground/service/UserService.java @@ -0,0 +1,34 @@ +package playground.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import playground.domain.Team; +import playground.domain.User; +import playground.dto.UserResponse; +import playground.repository.TeamRepositiory; +import playground.repository.UserRepository; + +import javax.transaction.Transactional; +import java.util.ArrayList; +import java.util.List; + +@Component +public class UserService { + + @Autowired + UserRepository userRepository; + + @Autowired + TeamRepositiory teamRepository; + + @Transactional + public List findBy(Long teamId) { + Team team = teamRepository.findById(teamId).get(); + List users = userRepository.findByTeam(team); + List usersResponse = new ArrayList<>(); + for (User user : users) { + usersResponse.add(new UserResponse(user)); + } + return usersResponse; + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 6097658..4cfee43 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -6,7 +6,7 @@ spring: active: local datasource: driver-class-name: org.h2.Driver - url: jdbc:h2:mem:userdb;DB_CLOSE_DELAY=-1 + url: jdbc:h2:mem:playground;DB_CLOSE_DELAY=-1 username: sa password: sql-script-encoding: UTF-8 diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index 9d9e205..c9d19d0 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -1,5 +1,11 @@ -insert into user(id, name) values (1, '박우빈'); -insert into user(id, name) values (2, '이다은'); +insert into team(id, name) values (1, '정산시스템팀'); +insert into team(id, name) values (2, '서비스개발팀'); + +insert into user(id, name, rank, team_id, email, password) values (1, '박우빈', 'TEAM_LEADER', 1, 'aa@naver.com', '1212'); +insert into user(id, name, rank, team_id, email, password) values (2, '이다은', 'PART_MANAGER', 1, 'aa@naver.com', '1212'); +insert into user(id, name, rank, team_id, email, password) values (3, '박우빈2', 'TEAM_LEADER', 2, 'aa@naver.com', '1212'); +insert into user(id, name, rank, team_id, email, password) values (4, '이다은2', 'PART_MANAGER', 2, 'aa@naver.com', '1212'); --insert into document(id, title, category, contents, drafter_id, approval_state) --- values (1, '첫번째 문서제목', 'EDUCATION', '문서내용', 1, 'DRAFTING'); \ No newline at end of file +-- values (1, '첫번째 문서제목', 'EDUCATION', '문서내용', 1, 'DRAFTING'); + diff --git a/src/test/java/playground/repository/DocumentRepositoryTest.java b/src/test/java/playground/repository/DocumentRepositoryTest.java new file mode 100644 index 0000000..0d4475e --- /dev/null +++ b/src/test/java/playground/repository/DocumentRepositoryTest.java @@ -0,0 +1,54 @@ +package playground.repository; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import playground.domain.Document; +import playground.domain.Team; +import playground.domain.User; + +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static playground.domain.Category.EDUCATION; +import static playground.domain.Category.PRODUCT_PURCHASING; +import static playground.domain.Rank.TEAM_MEMBER; + +@DataJpaTest +public class DocumentRepositoryTest { + + @Autowired + private TeamRepositiory teamRepositiory; + + @Autowired + private UserRepository userRepository; + + @Autowired + private DocumentRepository documentRepository; + + @DisplayName("기안자 id의 모든 문서를 작성일 역순 조회한다.") + @Test + void findByDrafterIdAndOrderByCreatedDateDesc() { + // given + Team team = teamRepositiory.save(new Team("서비스개발팀")); + User user1 = userRepository.save(new User("김우빈", "wbluke@abc.com", "p@ssw0rd", TEAM_MEMBER, team)); + User user2 = userRepository.save(new User("김우빈2", "wbluke2@abc.com", "p@ssw0rd", TEAM_MEMBER, team)); + + Document document1 = new Document("title1", EDUCATION, "content", user1); + Document document2 = new Document("title2", PRODUCT_PURCHASING, "content", user1); + Document document3 = new Document("title3", EDUCATION, "content", user1); + Document document4 = new Document("title4", PRODUCT_PURCHASING, "content", user2); + documentRepository.saveAll(Arrays.asList(document1, document2, document3, document4)); + + // when + List documents = documentRepository.findByDrafterIdOrderByCreatedDateDesc(user1.getId()); + + // then + assertThat(documents).hasSize(3) + .extracting("title") + .containsExactly("title3", "title2", "title1"); + } + +} diff --git a/src/test/java/playground/repository/UserRepositoryTest.java b/src/test/java/playground/repository/UserRepositoryTest.java new file mode 100644 index 0000000..4a1aa97 --- /dev/null +++ b/src/test/java/playground/repository/UserRepositoryTest.java @@ -0,0 +1,46 @@ +package playground.repository; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import playground.domain.Document; +import playground.domain.Team; +import playground.domain.User; + +import java.util.Arrays; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static playground.domain.Rank.TEAM_MEMBER; + +@DataJpaTest +public class UserRepositoryTest { + + @Autowired + private TeamRepositiory teamRepositiory; + + @Autowired + private UserRepository userRepository; + + @DisplayName("특정 팀의 모든 사용자를 조회한다.") + @Test + void findByTeam() { + // given + Team team1 = teamRepositiory.save(new Team("서비스개발팀")); + Team team2 = teamRepositiory.save(new Team("서비스개발팀2")); + User user1 = userRepository.save(new User("김우빈", "wbluke@abc.com", "p@ssw0rd", TEAM_MEMBER, team1)); + User user2 = userRepository.save(new User("김우빈2", "wbluke2@abc.com", "p@ssw0rd", TEAM_MEMBER, team1)); + User user3 = userRepository.save(new User("김우빈3", "wbluke@abc.com", "p@ssw0rd", TEAM_MEMBER, team2)); + User user4 = userRepository.save(new User("김우빈4", "wbluke2@abc.com", "p@ssw0rd", TEAM_MEMBER, team2)); + + // when + List users = userRepository.findByTeam(team1); + + // then + assertThat(users).hasSize(2) + .extracting("name") + .contains("김우빈", "김우빈2"); + } + +} diff --git a/src/test/java/playground/service/DocumentServiceTest.java b/src/test/java/playground/service/DocumentServiceTest.java new file mode 100644 index 0000000..9bd8fcd --- /dev/null +++ b/src/test/java/playground/service/DocumentServiceTest.java @@ -0,0 +1,58 @@ +package playground.service; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import playground.domain.Document; +import playground.domain.Team; +import playground.domain.User; +import playground.dto.DocumentResponse; +import playground.repository.DocumentRepository; +import playground.repository.TeamRepositiory; +import playground.repository.UserRepository; + +import static org.assertj.core.api.Assertions.assertThat; +import static playground.domain.Category.EDUCATION; +import static playground.domain.Rank.TEAM_MEMBER; + +@SpringBootTest +public class DocumentServiceTest { + @Autowired + private DocumentService documentService; + + @Autowired + private DocumentRepository documentRepository; + + @Autowired + private TeamRepositiory teamRepositiory; + + @Autowired + private UserRepository userRepository; + + @AfterEach + public void cleanup() { + documentRepository.deleteAllInBatch(); + userRepository.deleteAllInBatch(); + teamRepositiory.deleteAllInBatch(); + } + + @DisplayName("id로 Document 단건을 조회한다") + @Test + void fetchDocumentById() { + // given + Team team = teamRepositiory.save(new Team("서비스개발팀")); + User user = userRepository.save(new User("김우빈", "wbluke@abc.com", "p@ssw0rd", TEAM_MEMBER, team)); + Document document = new Document("제목", EDUCATION, "내용", user); // private method + Document savedDocument = documentRepository.save(document); + + // when + DocumentResponse result = documentService.findDocumentBy(savedDocument.getId()); + + // then + assertThat(result) + .extracting("title", "contents", "category", "drafter.name") + .containsExactly("제목", "내용", "EDUCATION", "김우빈"); + } +}