Skip to content

Commit

Permalink
Merge pull request #14 from likelion-backendschool/3_week_mission_dev
Browse files Browse the repository at this point in the history
3_week_mission
  • Loading branch information
shzero211 authored Nov 4, 2022
2 parents fbb9190 + c557cea commit ae0fc60
Show file tree
Hide file tree
Showing 48 changed files with 1,965 additions and 30 deletions.
1 change: 1 addition & 0 deletions 2_Week_Mission/multbooks/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ repositories {
}

dependencies {
implementation 'org.springframework.batch:spring-batch-core:4.3.3'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.ebook.multbooks;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling//스케줄링 기능 활성화
@EnableBatchProcessing//배치기능 활성화
@EnableJpaAuditing// Auditing 기능 사용하겠다는 설정
@SpringBootApplication
public class MultbooksApplication {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.ebook.multbooks.app.base;

import com.ebook.multbooks.app.cart.entity.CartItem;
import com.ebook.multbooks.app.cart.service.CartService;
import com.ebook.multbooks.app.cash.event.EventType;
import com.ebook.multbooks.app.member.entity.Member;
import com.ebook.multbooks.app.member.service.MemberService;
import com.ebook.multbooks.app.order.entity.Order;
import com.ebook.multbooks.app.order.service.OrderService;
import com.ebook.multbooks.app.order.service.PayService;
import com.ebook.multbooks.app.post.dto.PostWriteForm;
import com.ebook.multbooks.app.post.entity.Post;
import com.ebook.multbooks.app.post.service.PostService;
Expand All @@ -17,21 +22,27 @@
@Configuration
@Profile("dev")
public class DevInitData {
private boolean initDataDone=false;
@Bean
CommandLineRunner init(MemberService memberService, PasswordEncoder passwordEncoder, PostService postService, ProductService productService){
CommandLineRunner initData(MemberService memberService, PasswordEncoder passwordEncoder, PostService postService, ProductService productService, CartService cartService, OrderService orderService, PayService payService){
return args -> {
//initData가 1번만 사용될수 있도록 설정
if(initDataDone)return;
initDataDone=true;

String password=passwordEncoder.encode("1234");
//3명회원가입=>2명작가,1명그냥회원
Member member1=memberService.join("user1",password,"[email protected]","author1");
Member member2=memberService.join("user2",password,"[email protected]","author2");
Member member3=memberService.join("user3",password,"[email protected]","");
Member member4=memberService.join("admin",password,"[email protected]","admin");

//회원 3명 예치금 10만원 충전
memberService.addCash(member1,100000, EventType.CHARGE_FOR_PAYMENT);
memberService.addCash(member2,100000, EventType.CHARGE_FOR_PAYMENT);
memberService.addCash(member3,100000, EventType.CHARGE_FOR_PAYMENT);

//작가 회원들이 1개씩 글쓰기
//2명의작가 회원들이 1개씩 글쓰기
Post post1=postService.writePost(member1.getUsername(),new PostWriteForm("글제목1","글내용1","글내용1","#마법 #기사"));
Post post2=postService.writePost(member2.getUsername(),new PostWriteForm("글제목2","글내용2","글내용2","#로맨스 #판타지"));

Expand All @@ -41,6 +52,21 @@ CommandLineRunner init(MemberService memberService, PasswordEncoder passwordEnco
Product product3=productService.createProduct(member2,"도서3",3000,3L);//자신의 글의 키워드 중 로맨스 키워드를 가지는 도서 생성
Product product4=productService.createProduct(member2,"도서4",4000,4L);//자신의 글의 키워드 중 판타지 키워드를 가지는 도서 생성

//장바구니에 다른사람 상품 담기
CartItem cartItem1=cartService.addItem(member1,product3);
CartItem cartItem2=cartService.addItem(member1,product4);
CartItem cartItem3=cartService.addItem(member2,product1);
CartItem cartItem4=cartService.addItem(member2,product2);

//장바구니로 주문
Order order1=orderService.createOrderFromCart(member1);
Order order2=orderService.createOrderFromCart(member2);

// 주문1,2 결제
payService.payByRestCash(order1);
payService.payByRestCash(order2);
//주문1 환불
payService.refund(order2);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
@Profile("test")
public class TestInitData {
@Bean
CommandLineRunner init(MemberService memberService, PasswordEncoder passwordEncoder, PostService postService, ProductService productService, CartService cartService, OrderService orderService){
CommandLineRunner initData(MemberService memberService, PasswordEncoder passwordEncoder, PostService postService, ProductService productService, CartService cartService, OrderService orderService){
return args -> {
String password=passwordEncoder.encode("1234");
//회원 가입
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.ebook.multbooks.app.member.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/adm")
public class AdmHomeController {

@GetMapping("")
@PreAuthorize("hasAuthority('ADMIN')")
public String showIndex(){
return "redirect:/adm/home/main";
}

@GetMapping("/home/main")
public String showMain(){
return "adm/home/main";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
@SuperBuilder
@NoArgsConstructor
public class Member extends BaseEntity {

@Column(nullable = false)
@Column(nullable = false,unique = true)
private String username;

@Column(nullable = false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,25 @@ public Member join(String username, String password, String email, String nickna
Member member;
//작가 명이 있는경우
if(!nickname.equals("")){
member=Member.builder()
.email(email)
.username(username)
.password(password)
.nickname(nickname)
.authLevel(AuthLevel.AUTHOR)
.build();
//username 이 admin 인경우 admin 권한부여
if(nickname=="admin"){
member=Member.builder()
.email(email)
.username(username)
.password(password)
.nickname(nickname)
.authLevel(AuthLevel.ADMIN)
.build();
}else{
member=Member.builder()
.email(email)
.username(username)
.password(password)
.nickname(nickname)
.authLevel(AuthLevel.AUTHOR)
.build();
}

}else{
//작가 명이 없는 경우
member=Member.builder()
Expand Down Expand Up @@ -92,4 +104,19 @@ public long addCash(Member member, long price, EventType eventType){

return newRestCash;
}

public CashLog addCashAndReturnCashLog(Member member, int calculateRebatePrice, EventType eventType) {
//예치금 로그 남기기
CashLog cashLog=cashService.addCash(member,calculateRebatePrice,eventType);
//현재 + 충전 예치금 계산
long newRestCash=member.getRestCash()+cashLog.getPrice();

//예치금 업데이트
member.updateRestCash(newRestCash);


memberRepository.save(member);

return cashLog;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
/**
*
Expand Down Expand Up @@ -113,4 +115,6 @@ public boolean actorCanAccess(Member actor, Order order) {
public List<Order> getOrdersByMember(Member member) {
return orderRepository.findByMember(member);
}


}
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
package com.ebook.multbooks.app.orderItem.repository;

import com.ebook.multbooks.app.orderItem.entity.OrderItem;
import com.ebook.multbooks.app.rebate.entity.RebateOrderItem;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.LocalDateTime;
import java.util.List;

public interface OrderItemRepository extends JpaRepository<OrderItem,Long> {
List<OrderItem> findAllByPayDateBetween(LocalDateTime fromDate, LocalDateTime toDate);
Page<OrderItem> findAllByPayDateBetween(LocalDateTime fromDate, LocalDateTime toDate, Pageable pageable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityNotFoundException;
import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
Expand All @@ -34,4 +38,13 @@ public OrderItem createOrderItem(Product product,Order order) {
orderItemRepository.save(orderItem);
return orderItem;
}


public List<OrderItem> findAllByPayDateBetween(LocalDateTime fromDate, LocalDateTime toDate) {
return orderItemRepository.findAllByPayDateBetween(fromDate,toDate);
}

public OrderItem findByOrderItemId(long orderItemId) {
return orderItemRepository.findById(orderItemId).orElseThrow(()->new EntityNotFoundException("해당 주문 품목을 찾을수 없습니다."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ public Product createProduct(Member author, String subject, int wholesalePrice,
.author(author)
.subject(subject)
.postKeyword(postKeyword)
.price(wholesalePrice*4)
.price(wholesalePrice*3)
.wholesalePrice(wholesalePrice)
.salePrice(wholesalePrice*3)
.salePrice(wholesalePrice*2)
.build();
productRepository.save(product);
return product;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.ebook.multbooks.app.rebate.controller;

import com.ebook.multbooks.app.rebate.entity.RebateOrderItem;
import com.ebook.multbooks.app.rebate.service.RebateService;
import com.ebook.multbooks.global.util.Util;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.persistence.EntityNotFoundException;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Controller
@RequiredArgsConstructor
@RequestMapping("/adm/rebate")
public class AdmRebateController {
private final RebateService rebateService;
@GetMapping("/makeData")
@PreAuthorize("hasAuthority('ADMIN')")
public String showMakeData(String msg,Model model) {

model.addAttribute("msg",msg);
return "adm/rebate/makeData";
}

@PostMapping("/makeData")
@PreAuthorize("hasAuthority('ADMIN')")
public String makeData(String yearMonth){

rebateService.makeData(yearMonth);

return "redirect:/adm/rebate/rebateOrderItemList?yearMonth="+yearMonth+"&msg="+Util.url.encode("정산데이터가 성공적으로 생성되었습니다.");
}
@GetMapping("/rebateOrderItemList")
@PreAuthorize("hasAuthority('ADMIN')")
public String showRebateOrderItemList(String yearMonth, String msg,Model model){
List<RebateOrderItem> items=rebateService.findRebateOrderItemsByPayDateInOrderByIdAsc(yearMonth);
model.addAttribute("items",items);
model.addAttribute("msg",msg);
return "adm/rebate/rebateOrderItemList";
}

@PostMapping("/rebateOne/{orderItemId}")
@PreAuthorize("hasAuthority('ADMIN')")
public String rebateOne(@PathVariable long orderItemId, HttpServletRequest req) {
String resultMsg=null;
try{
resultMsg=rebateService.rebate(orderItemId);
}catch (Exception exception){
return "redirect:/adm/rebate/makeData?msg="+Util.url.encode(exception.getMessage());
}
String refer=req.getHeader("Referer");
String yearMonth=Util.url.getQueryParamValue(refer,"yearMonth","");
String redirect="redirect:/adm/rebate/rebateOrderItemList?yearMonth="+yearMonth;

return redirect+"&msg="+Util.url.encode(resultMsg);
}
@PostMapping("/rebate")
@PreAuthorize("hasAuthority('ADMIN')")
public String rebate(String ids, HttpServletRequest req) {

String[] idsArr = ids.split(",");

Arrays.stream(idsArr)
.mapToLong(Long::parseLong)
.forEach(id -> {
rebateService.rebate(id);
});

String referer = req.getHeader("Referer");
String yearMonth = Util.url.getQueryParamValue(referer, "yearMonth", "");

String redirect = "redirect:/adm/rebate/rebateOrderItemList?yearMonth=" + yearMonth;
redirect += "&msg=" + Util.url.encode("%d건의 정산품목을 정산처리하였습니다.".formatted(idsArr.length));

return redirect;
}
}
Loading

0 comments on commit ae0fc60

Please sign in to comment.