Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test #32

Closed
wants to merge 5 commits into from
Closed

test #32

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
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package nbdream.accountBook.controller;

import lombok.RequiredArgsConstructor;
import nbdream.accountBook.service.AccountBookHistoryService;
import nbdream.accountBook.service.AccountBookService;
import nbdream.accountBook.service.dto.GetAccountBookListReqDto;
import nbdream.accountBook.service.dto.GetAccountBookListResDto;
import nbdream.accountBook.service.dto.PostAccountBookReqDto;
import nbdream.auth.config.AuthenticatedMemberId;
import nbdream.common.advice.response.ApiResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -16,6 +19,7 @@
public class AccountBookController {

private final AccountBookService accountBookService;
private final AccountBookHistoryService accountBookHistoryService;

//내 장부 조회
@GetMapping("/auth/account")
Expand All @@ -24,4 +28,11 @@ public ApiResponse<GetAccountBookListResDto> getMyAccountBookList( @RequestBody
GetAccountBookListResDto resDto = accountBookService.getMyAccountBookList(reqDto, memberId);
return ApiResponse.ok(resDto);
}
}

//장부 작성
@PostMapping("/auth/account/register")
public ApiResponse<Void> writeAccountBookHistory(@RequestBody PostAccountBookReqDto reqDto,
@AuthenticatedMemberId Long memberId) {
return accountBookHistoryService.writeAccountBookHistory(reqDto, memberId);
}
}
3 changes: 3 additions & 0 deletions src/main/java/nbdream/accountBook/domain/AccountBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public class AccountBook extends BaseEntity {
@JoinColumn(name = "member_id", referencedColumnName = "id")
private Member member;

@OneToMany(mappedBy = "accountBook", cascade = CascadeType.ALL)
private List<AccountBookHistory> accountBookHistoryList;

@Builder
public AccountBook(Member member) {
this.member = member;
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/nbdream/accountBook/domain/AccountBookHistory.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package nbdream.accountBook.domain;

import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.*;
import nbdream.common.entity.BaseEntity;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.TextStyle;
import java.util.Locale;

Expand All @@ -32,23 +29,24 @@ public class AccountBookHistory extends BaseEntity {
@Enumerated(EnumType.STRING)
private TransactionType transactionType;

private int amount;
private Long amount;

private String content;

private LocalDate date;
private LocalDateTime dateTime;

public AccountBookHistory(final AccountBook accountBook, final AccountBookCategory accountBookCategory, final TransactionType transactionType, final int amount, final String content, final LocalDate date) {
@Builder
public AccountBookHistory(final AccountBook accountBook, final AccountBookCategory accountBookCategory, final TransactionType transactionType, final Long amount, final String content, final LocalDateTime dateTime) {
this.accountBook = accountBook;
this.accountBookCategory = accountBookCategory;
this.transactionType = transactionType;
this.amount = amount;
this.content = content;
this.date = date;
this.dateTime = dateTime;
}

public String getKoreanDayOfWeek() {
DayOfWeek dayOfWeek = this.date.getDayOfWeek();
DayOfWeek dayOfWeek = this.dateTime.getDayOfWeek();
return dayOfWeek.getDisplayName(TextStyle.FULL, Locale.KOREAN);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package nbdream.accountBook.exception;

import nbdream.common.exception.NotFoundException;

public class AccountBookNotFoundException extends NotFoundException {
public AccountBookNotFoundException() {
super("장부를 찾을 수 없습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package nbdream.accountBook.exception;

import nbdream.common.exception.NotFoundException;

public class DateNotFoundException extends NotFoundException {
public DateNotFoundException() {
super("날짜 정보를 읽어올 수 없습니다.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package nbdream.accountBook.exception;

public class InvalidDateFormatException extends IllegalArgumentException {
public InvalidDateFormatException() {
super("날짜 형식이 올바르지 않습니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import jakarta.persistence.criteria.Predicate;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -29,11 +31,13 @@ public static Specification<AccountBookHistory> withFilters(GetAccountBookListRe
// 날짜
if (reqDto.getStart() != null) {
LocalDate startDate = LocalDate.parse(reqDto.getStart());
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("date"), startDate));
LocalDateTime startDateTime = startDate.atStartOfDay();
predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("dateTime"), startDateTime));
}
if (reqDto.getEnd() != null) {
LocalDate endDate = LocalDate.parse(reqDto.getEnd());
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("date"), endDate));
LocalDateTime endDateTime = endDate.atTime(LocalTime.MAX);
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("dateTime"), endDateTime));
}

// 수입/지출
Expand All @@ -46,9 +50,9 @@ public static Specification<AccountBookHistory> withFilters(GetAccountBookListRe
if (reqDto.getSort() != null) {
Sort sortEnum = Sort.fromValue(reqDto.getSort());
if (sortEnum == Sort.EARLIEST) {
query.orderBy(criteriaBuilder.desc(root.get("date")));
query.orderBy(criteriaBuilder.desc(root.get("dateTime")));
} else if (sortEnum == Sort.OLDEST) {
query.orderBy(criteriaBuilder.asc(root.get("date")));
query.orderBy(criteriaBuilder.asc(root.get("dateTime")));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,76 +1,41 @@
package nbdream.accountBook.service;

import lombok.RequiredArgsConstructor;
import nbdream.accountBook.domain.AccountBook;
import nbdream.accountBook.domain.AccountBookCategory;
import nbdream.accountBook.domain.AccountBookHistory;
import nbdream.accountBook.domain.TransactionType;
import nbdream.accountBook.exception.AccountBookNotFoundException;
import nbdream.accountBook.repository.AccountBookHistoryRepository;
import nbdream.accountBook.repository.specifications.AccountBookHistorySpecifications;
import nbdream.accountBook.service.dto.GetAccountBookListReqDto;
import nbdream.accountBook.service.dto.GetAccountBookResDto;
import nbdream.image.domain.Image;
import nbdream.accountBook.repository.AccountBookRepository;
import nbdream.accountBook.service.dto.PostAccountBookReqDto;
import nbdream.common.advice.response.ApiResponse;
import nbdream.image.repository.ImageRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class AccountBookHistoryService {

private final AccountBookRepository accountBookRepository;
private final AccountBookHistoryRepository accountBookHistoryRepository;
private final ImageRepository imageRepository;
private static final int PAGE_SIZE = 3; // 페이지 크기

// 장부 내역의 카테고리 항목을 리스트로 반환
public List<String> getCategoryList() {
return List.of(AccountBookCategory.values())
.stream()
.map(AccountBookCategory::getValue)
.collect(Collectors.toList());
}

// 내 장부 내역을 리스트로 반환
public List<AccountBookHistory> getMyAccountBookHistoryList(GetAccountBookListReqDto reqDto, Long memberId) {
Pageable pageable = PageRequest.of(reqDto.getPage(), PAGE_SIZE);
Specification<AccountBookHistory> spec = AccountBookHistorySpecifications.withFilters(reqDto, memberId);
Page<AccountBookHistory> historyPage = accountBookHistoryRepository.findAll(spec, pageable);
return historyPage.getContent();
}

// 장부 내역을 dtoList로 변환
public List<GetAccountBookResDto> convertToDtoList(List<AccountBookHistory> historyList) {
return historyList.stream()
.map(this::convertToDto)
.collect(Collectors.toList());
}

// 장부 내역을 dto로 변환
private GetAccountBookResDto convertToDto(AccountBookHistory history) {
List<Image> imgList = imageRepository.findAllByTargetId(history.getId());

String imgUrl = imgList.stream()
.findFirst()
.map(Image::getStoredPath)
.orElse(null);
//장부 작성
public ApiResponse<Void> writeAccountBookHistory(PostAccountBookReqDto reqDto, Long memberId) {
AccountBook accountBook = accountBookRepository.findByMemberId(memberId)
.orElseThrow(AccountBookNotFoundException::new);

return GetAccountBookResDto.builder()
.id(history.getId().toString()) // 장부 내역의 id
.title(history.getContent())
.category(history.getAccountBookCategory().getValue())
.year(history.getDate().getYear())
.month(history.getDate().getMonthValue())
.day(history.getDate().getDayOfMonth())
.dayName(history.getKoreanDayOfWeek())
.expense(history.getTransactionType() == TransactionType.EXPENSE ? (long) history.getAmount() : null)
.revenue(history.getTransactionType() == TransactionType.REVENUE ? (long) history.getAmount() : null)
.thumbnail(imgUrl)
.imageSize(imgList.size())
AccountBookHistory accountBookHistory = AccountBookHistory.builder()
.accountBook(accountBook)
.accountBookCategory(AccountBookCategory.fromValue(reqDto.getCategory()))
.transactionType(reqDto.getExpense() == null ? TransactionType.REVENUE : TransactionType.EXPENSE)
.amount(reqDto.getExpense() == null ? reqDto.getRevenue() : reqDto.getExpense())
.content(reqDto.getTitle())
.dateTime(reqDto.getParsedRegisterDateTime())
.build();
accountBookHistoryRepository.save(accountBookHistory);
return ApiResponse.ok();
}
}
83 changes: 69 additions & 14 deletions src/main/java/nbdream/accountBook/service/AccountBookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,63 +2,117 @@

import lombok.RequiredArgsConstructor;
import nbdream.accountBook.domain.AccountBook;
import nbdream.accountBook.domain.AccountBookCategory;
import nbdream.accountBook.domain.AccountBookHistory;
import nbdream.accountBook.domain.TransactionType;
import nbdream.accountBook.exception.CategoryNotFoundException;
import nbdream.accountBook.repository.AccountBookHistoryRepository;
import nbdream.accountBook.repository.AccountBookRepository;
import nbdream.accountBook.repository.specifications.AccountBookHistorySpecifications;
import nbdream.accountBook.service.dto.GetAccountBookListReqDto;
import nbdream.accountBook.service.dto.GetAccountBookListResDto;
import nbdream.accountBook.service.dto.GetAccountBookResDto;
import nbdream.image.domain.Image;
import nbdream.image.repository.ImageRepository;
import nbdream.member.domain.Member;
import nbdream.member.exception.MemberNotFoundException;
import nbdream.member.repository.MemberRepository;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class AccountBookService {

private final AccountBookHistoryService accountBookHistoryService;
private final AccountBookRepository accountBookRepository;
private final MemberRepository memberRepository;
private final AccountBookHistoryRepository accountBookHistoryRepository;
private final ImageRepository imageRepository;

private static final int PAGE_SIZE = 3;

// 장부 조회에 필요한 리스트 가져오는 메서드
public GetAccountBookListResDto getMyAccountBookList(GetAccountBookListReqDto reqDto, Long memberId) {
List<String> categories = getCategoryList();
AccountBook accountBook = accountBookRepository.findByMemberId(memberId)
.orElseGet(() -> createNewAccountBook(memberId));
List<AccountBookHistory> accountBookHistoryList = accountBookHistoryService.getMyAccountBookHistoryList(reqDto, accountBook.getMember().getId());
List<String> categories = getCategoryList();

Pageable pageable = PageRequest.of(reqDto.getPage(), PAGE_SIZE);
Specification<AccountBookHistory> spec = AccountBookHistorySpecifications.withFilters(reqDto, memberId);
Page<AccountBookHistory> accountBookHistoryPage = accountBookHistoryRepository.findAll(spec, pageable);

List<AccountBookHistory> accountBookHistoryList = accountBookHistoryPage.getContent();

Long totalRevenue = accountBook.getTotalRevenue(accountBookHistoryList);
Long totalExpense = accountBook.getTotalExpense(accountBookHistoryList);
Long totalCost = totalRevenue + totalExpense;

List<GetAccountBookResDto> items = accountBookHistoryService.convertToDtoList(accountBookHistoryList);
return buildAccountBookListResDto(categories, items, totalRevenue, totalExpense, totalCost);
List<GetAccountBookResDto> items = convertToDtoList(accountBookHistoryList);
return createAccountBookListResDto(categories, items, totalRevenue, totalExpense, totalCost);
}

// 장부 생성
private AccountBook createNewAccountBook(Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(MemberNotFoundException::new);
AccountBook accountBook = AccountBook.builder()
.member(member)
.build();
return accountBookRepository.save(accountBook);
}

// 카테고리 목록 조회 메서드
private List<String> getCategoryList() {
List<String> categories = accountBookHistoryService.getCategoryList();
List<String> categories = List.of(AccountBookCategory.values())
.stream()
.map(AccountBookCategory::getValue)
.collect(Collectors.toList());

if (categories == null || categories.isEmpty()) {
throw new CategoryNotFoundException();
}
return categories;
}

// 장부 생성
private AccountBook createNewAccountBook(Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberNotFoundException());
AccountBook accountBook = AccountBook.builder()
.member(member)
// 내역 리스트를 DTO로 변환
private List<GetAccountBookResDto> convertToDtoList(List<AccountBookHistory> historyList) {
return historyList.stream()
.map(this::convertToDto)
.collect(Collectors.toList());
}

// 내역을 DTO로 변환
private GetAccountBookResDto convertToDto(AccountBookHistory history) {
// List<Image> imgList = imageRepository.findAllByTargetId(history.getId());
//
// String imgUrl = imgList.stream()
// .findFirst()
// .map(Image::getStoredPath)
// .orElse(null);

return GetAccountBookResDto.builder()
.id(history.getId().toString())
.title(history.getContent())
.category(history.getAccountBookCategory().getValue())
.year(history.getDateTime().getYear())
.month(history.getDateTime().getMonthValue())
.day(history.getDateTime().getDayOfMonth())
.dayName(history.getKoreanDayOfWeek())
.expense(history.getTransactionType() == TransactionType.EXPENSE ? (long) history.getAmount() : null)
.revenue(history.getTransactionType() == TransactionType.REVENUE ? (long) history.getAmount() : null)
// .thumbnail(imgUrl)
// .imageSize(imgList.size())
.build();
return accountBookRepository.save(accountBook);
}

// resDto 생성
private GetAccountBookListResDto buildAccountBookListResDto(List<String> categories, List<GetAccountBookResDto> items,
private GetAccountBookListResDto createAccountBookListResDto(List<String> categories, List<GetAccountBookResDto> items,
Long totalRevenue, Long totalExpense, Long totalCost) {
return GetAccountBookListResDto.builder()
.categories(categories)
Expand All @@ -68,4 +122,5 @@ private GetAccountBookListResDto buildAccountBookListResDto(List<String> categor
.totalCost(totalCost)
.build();
}

}
Loading
Loading