Skip to content

Commit

Permalink
feat: 현재 작성중인 가계부 API, 가계부 총 금액 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
jjongwon7 committed May 8, 2023
1 parent bb582ca commit b5a5a8a
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/main/java/com/bagasari/sacbagaji/config/QuerydslConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.bagasari.sacbagaji.config;

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Configuration
public class QuerydslConfig {

@PersistenceContext
private EntityManager entityManager;

@Bean
public JPAQueryFactory jpaQueryFactory() {
return new JPAQueryFactory(entityManager);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.bagasari.sacbagaji.model.dto.req.FoodRequestDTO;
import com.bagasari.sacbagaji.model.dto.req.TransportationRequestDTO;
import com.bagasari.sacbagaji.model.dto.res.AccountResponseDTO;
import com.bagasari.sacbagaji.model.dto.res.CurrentAccountResponseDTO;
import com.bagasari.sacbagaji.security.Auth;
import com.bagasari.sacbagaji.security.AuthInfo;
import com.bagasari.sacbagaji.service.AccountBookService;
Expand Down Expand Up @@ -42,4 +43,11 @@ public ResponseEntity<String> createTransportationProduct(@Auth AuthInfo authInf
accountBookService.createTransportationProduct(authInfo, transportationRequestDTO);
return ResponseEntity.ok("Transportation Product create!!");
}

@GetMapping("/current")
public ResponseEntity<CurrentAccountResponseDTO> findCurAccount(@Auth AuthInfo authInfo) {
return ResponseEntity.ok(accountBookService.findCurAccount(authInfo));
}


}
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.bagasari.sacbagaji.model.dto;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;

@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
@Getter
@Builder
public class ProductDTO {

private Long accountBookId;
Expand All @@ -19,4 +23,8 @@ public class ProductDTO {
private String country;
private String city;

public ProductDTO(String name, Integer price) {
this.name = name;
this.price = price;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.bagasari.sacbagaji.model.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.List;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class ProductListWithPurchaseDateDTO {

private LocalDate purchaseDate;
private List<ProductDTO> products;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.bagasari.sacbagaji.model.dto.res;

import com.bagasari.sacbagaji.model.dto.ProductListWithPurchaseDateDTO;
import com.bagasari.sacbagaji.model.entity.AccountBook;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.util.List;

@Getter
@NoArgsConstructor
public class CurrentAccountResponseDTO {

private String name;
private LocalDate startDate;
private LocalDate endDate;
private Integer totalPrice;
private List<ProductListWithPurchaseDateDTO> products;

public CurrentAccountResponseDTO(AccountBook accountBook, List<ProductListWithPurchaseDateDTO> products) {
this.name = accountBook.getName();
this.startDate = accountBook.getStartDate();
this.endDate = accountBook.getEndDate();
this.totalPrice = accountBook.getTotalPrice();
this.products = products;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/bagasari/sacbagaji/model/entity/AccountBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,15 @@ public class AccountBook {

@OneToMany(mappedBy = "accountBook")
private List<Destination> destinations = new ArrayList<>();

public void updateTotalPrice(AccountBook accountBook, Integer totalPrice) {
this.id = accountBook.getId();
this.user = accountBook.getUser();
this.name = accountBook.getName();
this.startDate = accountBook.getStartDate();
this.endDate = accountBook.getEndDate();
this.isPrivate = accountBook.getIsPrivate();
this.totalPrice = totalPrice;
this.createTime = accountBook.getCreateTime();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.util.List;

public interface AccountBookRepository extends JpaRepository<AccountBook, Long> {
public interface AccountBookRepository extends JpaRepository<AccountBook, Long>, AccountBookRepositoryCustom {

List<AccountBook> findAllByUserId(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.bagasari.sacbagaji.repository;

import com.bagasari.sacbagaji.model.entity.AccountBook;

public interface AccountBookRepositoryCustom {

AccountBook findFirstByOrderByUpdateTimeDesc(String email);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.bagasari.sacbagaji.repository;

import com.bagasari.sacbagaji.model.entity.AccountBook;
import com.bagasari.sacbagaji.model.entity.QAccountBook;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class AccountBookRepositoryImpl implements AccountBookRepositoryCustom {

private final JPAQueryFactory queryFactory;

@Override
public AccountBook findFirstByOrderByUpdateTimeDesc(String email) {

QAccountBook qa = QAccountBook.accountBook;

return queryFactory
.select(qa)
.from(qa)
.where(qa.user.email.eq(email))
.orderBy(qa.updateTime.desc())
.fetchFirst();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

import java.util.List;

public interface ProductRepository extends JpaRepository<Product, Long> {
public interface ProductRepository extends JpaRepository<Product, Long>, ProductRepositoryCustom {
public List<Product> findByNameContaining(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.bagasari.sacbagaji.repository;

import com.bagasari.sacbagaji.model.entity.Product;

import java.util.List;

public interface ProductRepositoryCustom {
List<Product> findOrderByPurchaseDate(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.bagasari.sacbagaji.repository;

import com.bagasari.sacbagaji.model.entity.Product;
import com.bagasari.sacbagaji.model.entity.QProduct;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;

import java.util.List;

@RequiredArgsConstructor
public class ProductRepositoryImpl implements ProductRepositoryCustom {

private final JPAQueryFactory queryFactory;

@Override
public List<Product> findOrderByPurchaseDate(Long id) {

QProduct qp = QProduct.product;

return queryFactory
.select(qp)
.from(qp)
.where(qp.accountBook.id.eq(id))
.orderBy(qp.purchaseDate.desc())
.fetch();

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.bagasari.sacbagaji.exception.CustomException;
import com.bagasari.sacbagaji.exception.ErrorCode;
import com.bagasari.sacbagaji.model.dto.ProductDTO;
import com.bagasari.sacbagaji.model.dto.ProductListWithPurchaseDateDTO;
import com.bagasari.sacbagaji.model.dto.req.AccountRequestDTO;
import com.bagasari.sacbagaji.model.dto.req.FoodRequestDTO;
import com.bagasari.sacbagaji.model.dto.req.TransportationRequestDTO;
import com.bagasari.sacbagaji.model.dto.res.AccountResponseDTO;
import com.bagasari.sacbagaji.model.dto.res.CurrentAccountResponseDTO;
import com.bagasari.sacbagaji.model.entity.*;
import com.bagasari.sacbagaji.repository.AccountBookRepository;
import com.bagasari.sacbagaji.repository.DestinationRepository;
Expand All @@ -15,9 +18,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Service
Expand Down Expand Up @@ -93,10 +95,16 @@ public void createFoodProduct(AuthInfo authInfo, FoodRequestDTO foodRequestDTO)
throw new CustomException(ErrorCode.INVALID_ACCESS_ACCOUNT);
}

Food food = new Food(foodRequestDTO.getProduct(), optionalAccountBook.get(), foodRequestDTO.getFood());
AccountBook accountBook = optionalAccountBook.get();

Food food = new Food(foodRequestDTO.getProduct(), accountBook, foodRequestDTO.getFood());

productRepository.save(food);

accountBook.updateTotalPrice(accountBook, accountBook.getTotalPrice() + foodRequestDTO.getProduct().getPrice());

accountBookRepository.save(accountBook);

}

public void createTransportationProduct(AuthInfo authInfo, TransportationRequestDTO transportationRequestDTO) {
Expand All @@ -111,9 +119,41 @@ public void createTransportationProduct(AuthInfo authInfo, TransportationRequest
throw new CustomException(ErrorCode.INVALID_ACCESS_ACCOUNT);
}

AccountBook accountBook = optionalAccountBook.get();

Transportation transportation = new Transportation(transportationRequestDTO.getProduct(), optionalAccountBook.get(), transportationRequestDTO.getTransportation());

productRepository.save(transportation);

accountBook.updateTotalPrice(accountBook, accountBook.getTotalPrice() + transportationRequestDTO.getProduct().getPrice());

accountBookRepository.save(accountBook);
}

public CurrentAccountResponseDTO findCurAccount(AuthInfo authInfo) {

User user = userRepository.findByEmail(authInfo.getEmail()).get();

AccountBook accountBook = accountBookRepository.findFirstByOrderByUpdateTimeDesc(user.getEmail());

List<Product> products = productRepository.findOrderByPurchaseDate(accountBook.getId());

List<ProductListWithPurchaseDateDTO> productListWithPurchaseDateDTOS = new ArrayList<>();

Map<LocalDate, List<ProductDTO>> productsByPurchaseDate = new HashMap<>();

// purchaseDate별로 product 분류
for (Product product : products) {
productsByPurchaseDate.computeIfAbsent(product.getPurchaseDate(), k -> new ArrayList<>())
.add(new ProductDTO(product.getName(), product.getPrice()));
}

for (Map.Entry<LocalDate, List<ProductDTO>> entry : productsByPurchaseDate.entrySet()) {
productListWithPurchaseDateDTOS.add(new ProductListWithPurchaseDateDTO(entry.getKey(), entry.getValue()));
}

return new CurrentAccountResponseDTO(accountBook, productListWithPurchaseDateDTOS);

}
}

Expand Down

0 comments on commit b5a5a8a

Please sign in to comment.