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

๐Ÿ› ๏ธ Fix #69 : ๊ฐ€๊ณ„๋ถ€ ์กฐํšŒ ์œ ์ € ๊ตฌ๋ถ„ ์ˆ˜์ • #70

Merged
merged 5 commits into from
Aug 10, 2023
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.umc.DongnaeFriend.domain.account.book.repository.accountBook;

import com.umc.DongnaeFriend.domain.account.book.entity.AccountBook;
import com.umc.DongnaeFriend.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -10,7 +11,13 @@

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

Optional<AccountBook> findByYearAndMonth(Integer year, Integer month);
/*@Query(value = "select ab " +
"from AccountBook ab " +
"where ab.year = :year and ab.month = :month and ab.user.id = :userId")
Optional<AccountBook> findByYearAndMonthAndUser(@Param("year")Integer year, @Param("month")Integer month, @Param("userId")Long userId);
*/

Optional<AccountBook> findByYearAndMonthAndUser(Integer year, Integer month, User user);
@Modifying
@Query(value = "update AccountBook ab "
+ "set ab.expenditure = ab.expenditure + :expenditure "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import com.umc.DongnaeFriend.domain.account.book.dto.AccountBookDto;
import com.umc.DongnaeFriend.domain.account.book.entity.AccountBook;
import com.umc.DongnaeFriend.domain.account.book.repository.accountBook.AccountBookRepository;
import com.umc.DongnaeFriend.domain.dongnae.entity.Dongnae;
import com.umc.DongnaeFriend.domain.dongnae.respository.DongnaeRepository;
import com.umc.DongnaeFriend.domain.type.Age;
import com.umc.DongnaeFriend.domain.type.Gender;
import com.umc.DongnaeFriend.domain.type.YesNo;
import com.umc.DongnaeFriend.domain.user.entity.User;
import com.umc.DongnaeFriend.domain.user.repository.UserRepository;
import com.umc.DongnaeFriend.global.exception.CustomException;
Expand All @@ -24,18 +29,21 @@ public class AccountBookService {
private final AccountBookRepository accountBookRepository;
private final UserRepository userRepository;


// ๊ฐ€๊ณ„๋ถ€ ์˜ˆ์‚ฐ ์„ค์ • (ํ•œ๋‹ฌ)
@Transactional
public void createBudget(Integer year, Integer month, Long budget){
User user = findUser();
accountBookRepository.save(AccountBookDto.BudgetRequest.toEntity(year, month, budget, user));
if (accountBookRepository.findByYearAndMonthAndUser(year, month, user).isEmpty()) {
accountBookRepository.save(AccountBookDto.BudgetRequest.toEntity(year, month, budget, user));
}else{
throw new CustomException(ErrorCode.ACCOUNTBOOK_ALREADY_EXISTS);
}
}

// ๊ฐ€๊ณ„๋ถ€ ์˜ˆ์‚ฐ ์„ค์ • ์กฐํšŒ
public AccountBookDto.BudgetResponse getBudget(Integer year, Integer month){
User user = findUser();
AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month)
AccountBook accountBook = accountBookRepository.findByYearAndMonthAndUser(year, month, user)
.orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND));
return AccountBookDto.BudgetResponse.of(accountBook.getId(),accountBook.getBudget());
}
Expand All @@ -45,7 +53,7 @@ public AccountBookDto.BudgetResponse getBudget(Integer year, Integer month){
public void updateBudget(Integer year, Integer month, Long budget){
User user = findUser();

AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month)
AccountBook accountBook = accountBookRepository.findByYearAndMonthAndUser(year, month, user)
.orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND));

if (!Objects.equals(accountBook.getUser().getId(), user.getId())) {
Expand All @@ -59,7 +67,7 @@ public void updateBudget(Integer year, Integer month, Long budget){
public AccountBookDto.AccountBookResponse getAccountBookResponse(Integer year, Integer month) {
User user = findUser();

AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month)
AccountBook accountBook = accountBookRepository.findByYearAndMonthAndUser(year, month, user)
.orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND));

if (!Objects.equals(accountBook.getUser().getId(), user.getId())) {
Expand All @@ -75,6 +83,7 @@ public AccountBookDto.AccountBookResponse getAccountBookResponse(Integer year, I

public User findUser() {
Object userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

return userRepository.findById((Long) userId)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class MemoService {
// ๋ฉ”๋ชจ ์ „์ฒด ์กฐํšŒ -> ๋ฆฌ์ŠคํŠธ
public MemoDto.MemoListResponse getMemoList(Integer year, Integer month){

AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month)
User user = findUser();
AccountBook accountBook = accountBookRepository.findByYearAndMonthAndUser(year, month, user)
.orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND));

List<Memo> memoList = memoRepository.findMemoListByAccountBookId(accountBook.getId());
Expand All @@ -42,7 +43,7 @@ public MemoDto.MemoListResponse getMemoList(Integer year, Integer month){
public void createMemo(MemoDto.MemoRequest memoRequest, Integer year, Integer month){

User user = findUser();
AccountBook accountBook = accountBookRepository.findByYearAndMonth(year, month)
AccountBook accountBook = accountBookRepository.findByYearAndMonthAndUser(year, month, user)
.orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND));

if (!Objects.equals(accountBook.getUser().getId(), user.getId())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ public void updateTransaction(TransactionDto.TransactionRequest request, Long tr
}

private AccountBook findTarget(Integer year, Integer month){
return accountBookRepository.findByYearAndMonth(year, month)
User user = findUser();
return accountBookRepository.findByYearAndMonthAndUser(year, month, user)
.orElseThrow(() -> new CustomException(ErrorCode.NO_CONTENT_FOUND));
}

public User findUser() {
Object userId = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
return userRepository.findById((Long) userId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@ public interface SharingSympathyRepository extends JpaRepository<SharingSympathy
int countAllBySharingBoardId(Long sharing_board_id);
int countAllByUserId(Long userId);
List<SharingSympathy> findByUser_Id(long user_id);

@Query(value = "select s from SharingSympathy s join fetch s.sharingBoard sb " +
"where s.user.id = :userId order by s.createdAt desc")
List<SharingSympathy> getSharingSympathyByUserId(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingBoard;
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingComment;
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingImg;
import com.umc.DongnaeFriend.domain.account.sharing.entity.SharingSympathy;
import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingBoardRepository;
import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingCommentRepository;
import com.umc.DongnaeFriend.domain.account.sharing.repository.SharingImgRepository;
Expand Down Expand Up @@ -77,9 +78,12 @@ public List<SharingDto.AccountBookProfileListResponse> getWrittenContent(Long us
List<SharingBoard> sharingBoardList;
if(category==0){
sharingBoardList= sharingBoardRepository.findAllByUserId(user.getId(), pageable);
}else{
}else if(category==1){
sharingBoardList = sharingCommentRepository.getCommentByUserIdAndBoard(user.getId())
.stream().map(SharingComment::getSharingBoard).distinct().collect(Collectors.toList());
}else{
sharingBoardList = sharingSympathyRepository.getSharingSympathyByUserId(user.getId())
.stream().map(SharingSympathy::getSharingBoard).collect(Collectors.toList());
}
return getProfileListResponse(sharingBoardList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
@AllArgsConstructor(access = PRIVATE)
@Entity
@Table(name = "users")
public class User extends BaseTimeEntity {
public class User extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = IDENTITY)
//@GeneratedValue(strategy = IDENTITY)
@Column(name = "user_id")
private Long id;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
@AllArgsConstructor
public enum ErrorCode {
/* 10* ๊ฐ€๊ณ„๋ถ€ */
MEMO_LIMIT(BAD_REQUEST, 10, "๋ฉ”๋ชจ๋Š” 8๊ฐœ๊นŒ์ง€ ์ž‘์„ฑ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."),
MEMO_LIMIT(BAD_REQUEST, 100, "๋ฉ”๋ชจ๋Š” 8๊ฐœ๊นŒ์ง€ ์ž‘์„ฑ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค."),
ACCOUNTBOOK_ALREADY_EXISTS(BAD_REQUEST, 101, "๊ฐ€๊ณ„๋ถ€๊ฐ€ ์ด๋ฏธ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค."),
/* 20* ๋™๋„ค์ •๋ณด */
POST_NOT_EXISTS(BAD_REQUEST, 200, "์—†๋Š” ๊ฒŒ์‹œ๊ธ€์ž…๋‹ˆ๋‹ค."),
COMMENT_NOT_EXISTS(BAD_REQUEST, 201, "์—†๋Š” ๋Œ“๊ธ€์ž…๋‹ˆ๋‹ค."),
Expand Down
5 changes: 2 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ logging:
com.example.carrotmarket: debug
org.hibernate.SQL: debug

---
# Settings for local
spring:
datasource:
url: jdbc:mysql://localhost:3306/dongnae?characterEncoding=UTF-8&serverTimezone=UTC&useLegacyDatetimeCode=false
username: dongnae
password: df1234
password: Tnqls9004^^

driver-class-name: com.mysql.cj.jdbc.Driver
jpa:
hibernate.ddl-auto: update
Expand All @@ -25,4 +25,3 @@ kakao:

jwt:
secret-key: 6B64DCA4EA2F53EDIKU9AAB215FE7