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

[FEAT] 한달 예산 수정 API 구현 #32

Merged
merged 10 commits into from
Aug 1, 2024
2 changes: 2 additions & 0 deletions src/main/java/umc/haruchi/HaruchiApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication(exclude = SecurityAutoConfiguration.class)
@EnableJpaRepositories
@EnableJpaAuditing
@EnableScheduling
public class HaruchiApplication {

public static void main(String[] args) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public enum ErrorStatus implements BaseErrorCode {
WITHDRAWAL_MEMBER(HttpStatus.NOT_FOUND, "MEMBER4007", "탈퇴한 회원입니다."),

// MonthBudget 관련 에러
NOT_MONTH_BUDGET(HttpStatus.NOT_FOUND, "MONTHBUDGET4001", "한 달 예산이 존재하지 않습니다."),
EXCEED_USAGE(HttpStatus.BAD_REQUEST, "MONTHBUDGET4002", "사용가능한 한달 금액을 초과한 지출입니다."),
MONTH_BUDGET_NOT_FOUND(HttpStatus.NOT_FOUND, "MONTHBUDGET4001", "한 달 예산이 존재하지 않습니다."),
NOT_MONTH_BUDGET(HttpStatus.BAD_REQUEST, "MONTHBUDGET4002", "입력된 한 달 예산이 없거나 유효하지 않습니다."),
EXCEED_USAGE(HttpStatus.BAD_REQUEST, "MONTHBUDGET4003", "사용가능한 한달 금액을 초과한 지출입니다."),

// DayBudget 관련 에러
NOT_DAY_BUDGET(HttpStatus.NOT_FOUND, "DAYBUDGET4001", "하루 예산이 존재하지 않습니다."),
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/umc/haruchi/converter/DayBudgetConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@
import umc.haruchi.domain.DayBudget;
import umc.haruchi.domain.Expenditure;
import umc.haruchi.domain.Income;
import umc.haruchi.domain.MonthBudget;
import umc.haruchi.domain.enums.DayBudgetStatus;
import umc.haruchi.web.dto.DayBudgetRequestDTO;
import umc.haruchi.web.dto.DayBudgetResponseDTO;
import umc.haruchi.web.dto.MonthBudgetResponseDTO;

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

public class DayBudgetConverter {

public static DayBudget toDayBudget(Integer dayBudget, int day, DayBudgetStatus status, MonthBudget monthBudget) {
return DayBudget.builder()
.dayBudget(dayBudget)
.day(Long.valueOf(day))
.dayBudgetStatus(status)
.monthBudget(monthBudget)
.build();
}

public static DayBudgetResponseDTO.getDayBudget toGetDayBudget(Integer todayBudget) {
return DayBudgetResponseDTO.getDayBudget.builder()
.dayBudget(todayBudget)
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/umc/haruchi/converter/MonthBudgetConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package umc.haruchi.converter;

import umc.haruchi.domain.MonthBudget;
import umc.haruchi.web.dto.MonthBudgetRequestDTO;
import umc.haruchi.web.dto.MonthBudgetResponseDTO;

import java.time.LocalDate;

public class MonthBudgetConverter {
public static MonthBudgetResponseDTO.CreateMonthResultDTO toCreateMonthResultDTO(MonthBudget monthBudget) {
return MonthBudgetResponseDTO.CreateMonthResultDTO.builder()
.id(monthBudget.getId())
.build();
}

public static MonthBudgetResponseDTO.UpdateMonthResultDTO toUpdateMonthResultDTO(MonthBudget monthBudget) {
return MonthBudgetResponseDTO.UpdateMonthResultDTO.builder()
.id(monthBudget.getId())
.updatedAt(monthBudget.getUpdatedAt())
.build();
}

public static MonthBudget toMonthBudget(Long monthBudget) {
return MonthBudget.builder()
.monthBudget(monthBudget)
.build();
}
}
9 changes: 8 additions & 1 deletion src/main/java/umc/haruchi/domain/DayBudget.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class DayBudget extends BaseEntity {
//@Builder.Default
private List<Expenditure> expenditureList = new ArrayList<>();

@PrePersist
//@PrePersist
public void prePersist() {
LocalDate now = LocalDate.now();
this.day = (long) now.getDayOfMonth();
Expand All @@ -78,6 +78,13 @@ public void setIncome(long amount, int how) {
dayBudget += (int)amount;
}

public void setStatus(DayBudgetStatus status) {
dayBudgetStatus = status;
}

public void setDayBudget(int distributedAmount) {
dayBudget = distributedAmount;

public void setExpenditure(long amount, int how) {
if(how == 0)
dayBudget += (int)amount;
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/umc/haruchi/domain/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ public class Member extends BaseEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

// @Column(nullable = false, columnDefinition = "bigint default 0")
// private Long monthBudget;

@Column(nullable = false, length = 5)
private String name;

Expand Down Expand Up @@ -53,7 +50,11 @@ public void encodePassword(String password) {
}

public void addSafeBox(long safeBoxAmount) {
safeBox += safeBoxAmount;
if(safeBox == null) {
safeBox = safeBoxAmount;
}
else
safeBox += safeBoxAmount;
}

public void subSafeBox(long safeBoxAmount) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/umc/haruchi/domain/MonthBudget.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ public void prePersist() {
this.month = now.getMonthValue();
}

public void setMember(Member member) {
if(this.member != null)
member.getMonthBudgetList().remove(this);
this.member = member;
member.getMonthBudgetList().add(this);
}

public void updateMonthBudget(Long monthBudget) {
LocalDate now = LocalDate.now();
this.year = now.getYear();
this.month = now.getMonthValue();
this.monthBudget = monthBudget;

public void setMonthUse(long amount, int how) {
if(how == 0)
usedAmount += (int)amount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import umc.haruchi.domain.DayBudget;
import umc.haruchi.domain.MonthBudget;

import java.util.Optional;

public interface DayBudgetRepository extends JpaRepository<DayBudget, Long> {
DayBudget findByMonthBudgetAndDay(MonthBudget monthBudget, int day);
Optional<DayBudget> findByMonthBudgetAndDay(MonthBudget monthBudget, int day);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import org.springframework.data.jpa.repository.JpaRepository;
import umc.haruchi.domain.MonthBudget;

import java.util.Optional;

public interface MonthBudgetRepository extends JpaRepository<MonthBudget, Long> {

MonthBudget findByMemberIdAndYearAndMonth(Long memberId, int year, int month);
Optional<MonthBudget> findByMemberIdAndYearAndMonth(Long memberId, int year, int month);
Optional<MonthBudget> findByMemberId(Long memberId);

}
21 changes: 14 additions & 7 deletions src/main/java/umc/haruchi/service/BudgetRedistributionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import umc.haruchi.apiPayload.exception.handler.BudgetRedistributionHandler;
import umc.haruchi.apiPayload.exception.handler.DayBudgetHandler;
import umc.haruchi.apiPayload.exception.handler.MemberHandler;
import umc.haruchi.apiPayload.exception.handler.MonthBudgetHandler;
import umc.haruchi.converter.BudgetRedistributionConverter;
import umc.haruchi.domain.*;
import umc.haruchi.domain.enums.DayBudgetStatus;
Expand Down Expand Up @@ -36,14 +37,16 @@ public class BudgetRedistributionService {
public PushPlusClosing push(BudgetRedistributionRequestDTO.createPushDTO request, Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberHandler(NO_MEMBER_EXIST)); //영속화
MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(member.getId(), year, month);
DayBudget sourceBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, request.getSourceDay());

MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(member.getId(), year, month)
.orElseThrow(() -> new MonthBudgetHandler(MONTH_BUDGET_NOT_FOUND));
DayBudget sourceBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, request.getSourceDay())
.orElseThrow(() -> new DayBudgetHandler(NOT_DAY_BUDGET));
long totalAmount = request.getAmount();
//target에 해당하는 daybudget 찾기
DayBudget targetBudget = null;
if (request.getTargetDay() != null) {
targetBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget,request.getTargetDay());
targetBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget,request.getTargetDay())
.orElseThrow(() -> new DayBudgetHandler(NOT_DAY_BUDGET));
}

if (request.getAmount() > sourceBudget.getDayBudget() && request.getAmount() < 0) {
Expand Down Expand Up @@ -169,8 +172,11 @@ public PullMinusClosing pull(BudgetRedistributionRequestDTO.createPullDTO reques
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new MemberHandler(NO_MEMBER_EXIST)); //영속화

MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(member.getId(), year, month);
DayBudget targetBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, request.getTargetDay());
MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(member.getId(), year, month)
.orElseThrow(() -> new MonthBudgetHandler(MONTH_BUDGET_NOT_FOUND));

DayBudget targetBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, request.getTargetDay())
.orElseThrow(() -> new DayBudgetHandler(NOT_DAY_BUDGET));
long totalAmount = request.getAmount();
int tossAmount = (int) (roundDownToNearestHundred(totalAmount)); //233원-> 200원

Expand All @@ -182,7 +188,8 @@ public PullMinusClosing pull(BudgetRedistributionRequestDTO.createPullDTO reques
//source에 해당하는 daybudget 찾기
DayBudget sourceBudget = null;
if (request.getSourceDay() != null) {
sourceBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, request.getSourceDay());
sourceBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, request.getSourceDay())
.orElseThrow(() -> new DayBudgetHandler(NOT_DAY_BUDGET));
}

if (request.getAmount() < 0) {
Expand Down
32 changes: 14 additions & 18 deletions src/main/java/umc/haruchi/service/DayBudgetService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import java.util.ArrayList;
import java.util.List;

import static umc.haruchi.apiPayload.code.status.ErrorStatus.NOT_DAY_BUDGET;
import static umc.haruchi.apiPayload.code.status.ErrorStatus.NOT_SOME_DAY_BUDGET;

@Service
@RequiredArgsConstructor
public class DayBudgetService {
Expand Down Expand Up @@ -51,20 +54,19 @@ public MonthBudget check(Long memberId){
throw new MemberHandler(ErrorStatus.NO_MEMBER_EXIST);
}

MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, year, month);
MonthBudget monthBudget = monthBudgetRepository.findByMemberIdAndYearAndMonth(memberId, year, month)
.orElseThrow(() -> new MonthBudgetHandler(ErrorStatus.MONTH_BUDGET_NOT_FOUND));
if(monthBudget == null){
throw new MonthBudgetHandler(ErrorStatus.NOT_MONTH_BUDGET);
throw new MonthBudgetHandler(ErrorStatus.MONTH_BUDGET_NOT_FOUND);
}
return monthBudget;
}

public Integer findDayBudget(Long memberId) {
MonthBudget monthBudget = check(memberId);

DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, day);
if(dayBudget == null){
throw new DayBudgetHandler(ErrorStatus.NOT_DAY_BUDGET);
}
DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, day)
.orElseThrow(() -> new DayBudgetHandler(ErrorStatus.NOT_DAY_BUDGET));

return dayBudget.getDayBudget();
}
Expand All @@ -74,10 +76,8 @@ public List<Integer> findAllBudget(Long memberId) {

List<Integer> allBudget = new ArrayList<>();
for(int i=day; i<=lastDay; i++){
DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, i);
if(dayBudget == null){
throw new DayBudgetHandler(ErrorStatus.NOT_SOME_DAY_BUDGET);
}
DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, i)
.orElseThrow(() -> new DayBudgetHandler(NOT_SOME_DAY_BUDGET));
allBudget.add(dayBudget.getDayBudget());
}

Expand All @@ -89,10 +89,8 @@ public List<Integer> findAllBudget(Long memberId) {
public void deleteIncome(Long memberId, Long incomeId) {
MonthBudget monthBudget = check(memberId);

DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, day);
if(dayBudget == null){
throw new DayBudgetHandler(ErrorStatus.NOT_DAY_BUDGET);
}
DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, day)
.orElseThrow(() -> new DayBudgetHandler(ErrorStatus.NOT_DAY_BUDGET));
if(dayBudget.getDayBudgetStatus() == DayBudgetStatus.INACTIVE){
throw new DayBudgetHandler(ErrorStatus.TODAY_CLOSED);
}
Expand All @@ -114,10 +112,8 @@ public Income joinIncome(DayBudgetRequestDTO.createIncomeDTO request, Long membe

MonthBudget monthBudget = check(memberId);

DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, day);
if(dayBudget == null){
throw new DayBudgetHandler(ErrorStatus.NOT_DAY_BUDGET);
}
DayBudget dayBudget = dayBudgetRepository.findByMonthBudgetAndDay(monthBudget, day)
.orElseThrow(() -> new DayBudgetHandler(NOT_DAY_BUDGET));

Income newIncome = DayBudgetConverter.toIncome(request, dayBudget);

Expand Down
29 changes: 27 additions & 2 deletions src/main/java/umc/haruchi/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@
import umc.haruchi.apiPayload.code.status.ErrorStatus;
import umc.haruchi.apiPayload.exception.handler.JwtExceptionHandler;
import umc.haruchi.apiPayload.exception.handler.MemberHandler;
import umc.haruchi.apiPayload.exception.handler.MonthBudgetHandler;
import umc.haruchi.config.login.jwt.JwtUtil;
import umc.haruchi.converter.MemberConverter;
import umc.haruchi.converter.MonthBudgetConverter;
import umc.haruchi.domain.Member;

import umc.haruchi.domain.MemberToken;
import umc.haruchi.domain.MonthBudget;
import umc.haruchi.domain.Withdrawer;
import umc.haruchi.repository.MemberRepository;
import umc.haruchi.repository.WithdrawerRepository;
import umc.haruchi.domain.DayBudget;
import umc.haruchi.repository.*;

import umc.haruchi.web.dto.MemberRequestDTO;
import umc.haruchi.web.dto.MemberResponseDTO;
import umc.haruchi.web.dto.MonthBudgetRequestDTO;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

Expand All @@ -31,6 +39,7 @@
public class MemberService {

private final MemberRepository memberRepository;
private final MonthBudgetRepository monthBudgetRepository;
private final BCryptPasswordEncoder passwordEncoder;
private final JavaMailSender mailSender;
private final RedisTemplate redisTemplate;
Expand All @@ -39,15 +48,30 @@ public class MemberService {

public static int code;

private final MonthBudgetService monthBudgetService;
private final DayBudgetRepository dayBudgetRepository;

// 회원가입
@Transactional
public Member joinMember(MemberRequestDTO.MemberJoinDTO request) throws Exception {

Member newMember = MemberConverter.toMember(request);
newMember.encodePassword(passwordEncoder.encode(request.getPassword()));

//회원가입 시 monthBudget 생성
MonthBudget monthBudget = MonthBudgetConverter.toMonthBudget(request.getMonthBudget());
monthBudget.setMember(newMember);

return memberRepository.save(newMember);
}

//dayBudget 생성
@Transactional
public void connectToDayBudget(Long memberId) {
List<DayBudget> dayBudgets = monthBudgetService.distributeDayBudgets(memberId);
dayBudgetRepository.saveAll(dayBudgets);
}

// 비밀번호 확인
@Transactional
public void checkPassword(String password, String verifyPassword) {
Expand Down Expand Up @@ -213,6 +237,7 @@ public void withdrawer(String reason) {
withdrawerRepository.save(withdrawer);
}


// 회원 더보기 정보(가입일, 가입 이메일, 닉네임) 조회
public MemberResponseDTO.MemberDetailResultDTO getMemberDetail(String email) {
Member member = memberRepository.findByEmail(email)
Expand Down
Loading
Loading