Skip to content

Commit

Permalink
[WHAT-169]Feature/what 169 (#69)
Browse files Browse the repository at this point in the history
* feat:CRUD 완료, test 필요

* feat:삭제 제외 테스트 완료

* feat : 삭제 제외 완료, 총량 amount 계산 필요

* fix: 삭제 주석처리

* fix: 삭제 주석처리

* fix: 삭제 주석처리

* fix : link Id return

* fix: 일정 삭제 완료

* fix : 공결 수정,일정삭제 수정 완료

---------

Co-authored-by: leehaesong3966 <[email protected]>
  • Loading branch information
pinetree2 and leehaesong3966 committed Sep 23, 2023
1 parent 7e3f72e commit 0d5c42d
Show file tree
Hide file tree
Showing 16 changed files with 151 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import gdg.whatssue.controller.inter.AbsentController;
import gdg.whatssue.service.AbsentService;
import gdg.whatssue.service.dto.AbsentRequestDto;
import gdg.whatssue.util.JwtTokenUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jdk.jshell.spi.ExecutionControl.NotImplementedException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -23,7 +25,7 @@ public class AbsentControllerImpl implements AbsentController {
@Operation(
summary = "공결 신청",
description = "api 경로로 일정ID를 전달받아 해당 일정에 대한 공결 요청")
public ResponseEntity requestAbsent(@PathVariable Long scheduleId,@RequestBody AbsentRequestDto absentRequestDto) throws NotImplementedException {
public ResponseEntity requestAbsent(@PathVariable Long scheduleId, @RequestBody AbsentRequestDto absentRequestDto) throws NotImplementedException {
//"TODO: This api should be implemented"
return absentService.requestAbsent(scheduleId,absentRequestDto);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,16 @@ public ResponseEntity updateBook(Long bookId, @RequestBody AccountBookCreateDto
return accountService.updateBook(bookId, accountBookCreateDto);
}

@Override
public ResponseEntity deleteBook(Long bookId) throws Exception {
return null;
}

/*

@Override
@Operation(
summary = "입출금 내역 삭제 api",
description = "입출금 내역 삭제")
public ResponseEntity deleteBook(Long bookId) throws Exception {
return accountService.deleteBook(bookId);
}
*/



// @PostMapping("/claim/{memberId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import gdg.whatssue.service.dto.AbsentRequestDto;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jdk.jshell.spi.ExecutionControl.NotImplementedException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand Down
13 changes: 8 additions & 5 deletions src/main/java/gdg/whatssue/entity/MoneyBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
import jakarta.persistence.Id;
import jakarta.persistence.OneToOne;
import lombok.*;
import org.hibernate.annotations.Formula;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Getter
@Setter
@Entity
@Builder
@Setter
@RequiredArgsConstructor
@AllArgsConstructor

@Table(name = "money_book")
public class MoneyBook {

@Id
Expand All @@ -32,9 +34,6 @@ public class MoneyBook {
private BigDecimal totalPaidAmount;


//totalPaidAmount 는 club_id 에 해당하는 bookAmount 들의 총 값을 값이 변경/추가/삭제 될때 마다 트리거해서 저장한다.


// 생성일시에 대한 기본값 설정
@Column(name = "created_at", updatable = false)
private LocalDateTime createdAt;
Expand All @@ -44,10 +43,14 @@ protected void onCreate() {
createdAt = LocalDateTime.now();
}

public void saveClub(Club club) {
public void saveMoneyBook(Club club,BigDecimal totalPaidAmount) {
this.club = club;
this.totalPaidAmount = totalPaidAmount;
}

public void saveClub(Club club){
this.club = club;
}
public void updateMoneyBook(MoneyBook updateMoneyBook) {

this.bookTitle = updateMoneyBook.bookTitle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import gdg.whatssue.entity.Member;
import gdg.whatssue.entity.Schedule;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -14,9 +13,5 @@ public interface AttendanceByUserByScheduleRepository extends JpaRepository<Atte
AttendanceByUserBySchedule findBySchedule_ScheduleIdAndMember_MemberId(Long scheduleId,Long MemberId);
List<AttendanceByUserBySchedule> findBySchedule_ScheduleId(Long scheduleId);
AttendanceByUserBySchedule findByMemberAndSchedule(Member member, Schedule schedule);





List<AttendanceByUserBySchedule> findBySchedule(Schedule schedule);
}
2 changes: 2 additions & 0 deletions src/main/java/gdg/whatssue/repository/LinkRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface LinkRepository extends JpaRepository<Link, Long> {
List<Link> findAllByClub(Club club);

Link findByLinkId(Long linkId);

Optional<Link> findByLinkName(String linkName);
}
3 changes: 3 additions & 0 deletions src/main/java/gdg/whatssue/repository/MemberRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gdg.whatssue.repository;

import gdg.whatssue.entity.Member;

import java.util.Map;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
Expand All @@ -9,4 +11,5 @@
public interface MemberRepository extends JpaRepository<Member, Long> {

Optional<Member> findByMemberNickName(String loginId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;

Expand All @@ -16,12 +17,10 @@ public interface MoneyBookRepository extends JpaRepository<MoneyBook,Long> {
Optional<MoneyBook> findByMoneyBookId(Long bookId);

@Query("select m from MoneyBook m where m.club.ClubId = :clubId and m.moneyBookId = :moneyBookId")
Optional<MoneyBook> findByClub_ClubIdAndMoneyBookId(@Param("clubId") Long clubId, @Param("moneyBookId") Long moneyBookId);
Optional<MoneyBook> findByClubIdAndMoneyBookId(@Param("clubId") Long clubId, @Param("moneyBookId") Long moneyBookId);

List<MoneyBook> findAllByClub(Club club);

/*
@Query("delete from MoneyBook m where m.moneyBookId = :bookId")
void deleteByMoneyBookId(@Param("moneyBookId")Long bookId);
*/
@Query("select m.totalPaidAmount from MoneyBook m where m.club = :club")
BigDecimal findTotalAmountByClub(Club club);
}
26 changes: 18 additions & 8 deletions src/main/java/gdg/whatssue/service/AbsentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -118,20 +119,29 @@ public ResponseEntity requestAbsent(Long scheduleId, AbsentRequestDto absentRequ
if(schedule == null){
return ResponseEntity.badRequest().build();
}
//

//Dto -> Entity 위해 AbsentRequestMapper 사용
ApplyOfficialAbsent applyOfficialAbsent = AbsentRequestMapper.INSTANCE.toEntity(absentRequestDto);
//ApplyOfficialAbsent applyOfficialAbsent = AbsentRequestMapper.INSTANCE.toEntity(absentRequestDto);

try{

//Dto -> Entity builder로 변환
ApplyOfficialAbsent applyOfficialAbsent1 = ApplyOfficialAbsent.builder()
.absentReason(absentRequestDto.getAbsentReason())
.absentDate(LocalDate.parse(absentRequestDto.getAbsentDate()))
.build();
//ApplyOfficalAbsent 에 Schedule 객체 전달
applyOfficialAbsent.saveSchedule(schedule);
applyOfficialAbsent.saveMember(member);
applyOfficialAbsent1.saveSchedule(schedule);
applyOfficialAbsent1.saveMember(member);
String absentIsAccepted = "WAIT";
applyOfficialAbsent.saveIsAccepted(absentIsAccepted);
applyOfficialAbsentRepository.save(applyOfficialAbsent);
return ResponseEntity.ok().build();
applyOfficialAbsent1.saveIsAccepted(absentIsAccepted);
applyOfficialAbsentRepository.save(applyOfficialAbsent1);

//ReponseEntity key:value 로 공결 id 리턴
return ResponseEntity.ok().body("absentId :"+applyOfficialAbsent1.getApplyOfficialAbsentId());

}catch (Exception e){
e.printStackTrace();
e.printStackTrace();;
return ResponseEntity.badRequest().build();
}

Expand Down
76 changes: 55 additions & 21 deletions src/main/java/gdg/whatssue/service/AccountService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
import gdg.whatssue.entity.Club;
import gdg.whatssue.entity.MoneyBook;
import gdg.whatssue.mapper.MoneyBookCreateMapper;
import gdg.whatssue.mapper.MoneyBookListMapper;
import gdg.whatssue.repository.ClaimRepository;
import gdg.whatssue.repository.ClubRepository;
import gdg.whatssue.repository.MoneyBookRepository;
import gdg.whatssue.service.dto.AccountBookCreateDto;
import gdg.whatssue.service.dto.AccountBookListDto;
import gdg.whatssue.service.dto.AccountClaimDto;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.text.translate.NumericEntityUnescaper;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -48,20 +47,57 @@ public ResponseEntity<?> createClaim(AccountClaimDto dto) throws Exception {
}


//변경 (삭제, 입력) 이 일어날때마다 clubId에 해당하는 모든 TotalPaidAmount 가 같은 값을 가져야하는데 그게 안됨 (어려움)
public ResponseEntity createBook(AccountBookCreateDto accountBookCreateDto) {
Long clubId = 1L;
Club club = clubRepository.findById(clubId).orElseThrow(() -> (
new ResponseStatusException(HttpStatus.NOT_FOUND, "클럽을 찾을 수 없습니다.")
));

Optional<Club> optionalClub = clubRepository.findById(clubId);
//club 이 존재 할 경우
if (club != null) {
if (optionalClub.isPresent()) {
Club club = optionalClub.get();
//MoneyBook moneyBook = MoneyBookCreateMapper.INSTANCE.toEntity(accountBookCreateDto);

//builder 이용
MoneyBook moneyBook = MoneyBook.builder()
.club(club)
//bookAmount 형변환
.bookAmount(new BigDecimal(accountBookCreateDto.getBookAmount()))
.bookTitle(accountBookCreateDto.getBookTitle())
.build();

MoneyBook moneyBook = MoneyBookCreateMapper.INSTANCE.toEntity(accountBookCreateDto);
try {
//기존에 clubId 와 일치하는 리스트 가져오기
List<MoneyBook> moneyBookList = moneyBookRepository.findAllByClub(club);

if (moneyBookList.size() != 0) {
//moneyBookList 에서 totalPaidAmount를 가져온다.
BigDecimal totalAmount = moneyBookRepository.findTotalAmountByClub(club);

//totalAmount 에 입력받은 bookamount 를 더해준다.
totalAmount = totalAmount.add(moneyBook.getBookAmount());
System.out.println("totalAmount2 = " + totalAmount );
if(totalAmount.compareTo(BigDecimal.ZERO) < 0){
totalAmount = BigDecimal.ZERO;
}

//입출금 내역 생성
moneyBook.saveClub(club);
//clubId 에 해당하는 모든 totalAmount 의 값을 forEach 로 변경해준다.
for (MoneyBook book : moneyBookList) {
book.saveMoneyBook(club, totalAmount);
}

moneyBookRepository.save(moneyBook);
}
else{
//totalAmount 에 입력받은 bookamount 를 더해준다.
BigDecimal totalAmount = moneyBook.getBookAmount();
System.out.println("totalAmount2 = " + totalAmount );

//입출금 내역 생성
moneyBook.saveMoneyBook(club, totalAmount);
moneyBookRepository.save(moneyBook);
}

moneyBook.saveClub(club);
moneyBookRepository.save(moneyBook);
return ResponseEntity.ok("입출금 내역 생성 완료");

} catch (Exception e) {
Expand Down Expand Up @@ -106,7 +142,7 @@ public ResponseEntity getBookList() {
public ResponseEntity getBookDetail(Long bookId) {
Long clubId = 1L;
//club 와 bookID가 moneybook 테이블에 존재하는지 확인
MoneyBook moneyBook = moneyBookRepository.findByClub_ClubIdAndMoneyBookId(clubId, bookId).orElseThrow(() -> (
MoneyBook moneyBook = moneyBookRepository.findByClubIdAndMoneyBookId(clubId, bookId).orElseThrow(() -> (
new ResponseStatusException(HttpStatus.NOT_FOUND, "입출금 내역을 찾을 수 없습니다.")
));

Expand Down Expand Up @@ -136,7 +172,7 @@ public ResponseEntity getBookDetail(Long bookId) {
public ResponseEntity updateBook(Long bookId, AccountBookCreateDto accountBookCreateDto) {
Long clubId = 1L;
//club 와 bookID가 moneybook 테이블에 존재하는지 확인
MoneyBook moneyBook = moneyBookRepository.findByClub_ClubIdAndMoneyBookId(clubId, bookId).orElseThrow(() -> (
MoneyBook moneyBook = moneyBookRepository.findByClubIdAndMoneyBookId(clubId, bookId).orElseThrow(() -> (
new ResponseStatusException(HttpStatus.NOT_FOUND, "입출금 내역을 찾을 수 없습니다.")
));

Expand All @@ -156,24 +192,22 @@ public ResponseEntity updateBook(Long bookId, AccountBookCreateDto accountBookCr
}
}

/*

@Transactional
public ResponseEntity deleteBook(Long bookId) {
Long clubId = 1L;
//club 와 bookID가 moneybook 테이블에 존재하는지 확인
MoneyBook moneyBook = moneyBookRepository.findByClub_ClubIdAndMoneyBookId(clubId, bookId).orElseThrow(() -> (
new ResponseStatusException(HttpStatus.NOT_FOUND, "입출금 내역을 찾을 수 없습니다.")
));
//bookID가 moneybook 테이블에 존재하는지 확인
MoneyBook moneyBook = moneyBookRepository.findByClubIdAndMoneyBookId(clubId,bookId).orElseThrow(() -> (
new ResponseStatusException(HttpStatus.NOT_FOUND, "입출금 내역을 찾을 수 없습니다."
)));

try {
moneyBook.setClub(null);
moneyBookRepository.deleteByMoneyBookId(bookId);
moneyBookRepository.delete(moneyBookRepository.findByMoneyBookId(bookId).orElseThrow(() -> new EntityNotFoundException("존재하지 않는 데이터 입니다.")));
return ResponseEntity.ok("입출금 내역 삭제 완료");
} catch (EmptyResultDataAccessException e) {
return ResponseEntity.badRequest().body("존재하지 않는 데이터 입니다.");
}
}
*/

}
Loading

0 comments on commit 0d5c42d

Please sign in to comment.