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] 리팩터링 일부 진행 #74

Merged
merged 5 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion favor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id 'java'
id 'org.springframework.boot' version '2.7.5'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
id 'com.google.cloud.tools.jib' version '3.3.2'
id 'com.google.cloud.tools.jib' version '3.4.0'
}

group = 'com.favor'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.List;

import static com.favor.favor.common.DefaultResponseDto.resWithData;
import static com.favor.favor.common.DefaultResponseDto.resWithoutData;

@Api(tags = "Anniversary")
@RestController
@RequestMapping("/anniversaries")
Expand All @@ -32,7 +33,7 @@ public class AnniversaryController {
@ApiResponse(code = 400,
message = "FIELD_REQUIRED / *_CHARACTER_INVALID / *_LENGTH_INVALID"),
@ApiResponse(code = 404,
message = "USER_NOT_FOUND / FREIND_NOT_FOUND"),
message = "USER_NOT_FOUND / FRIEND_NOT_FOUND"),
@ApiResponse(code = 500,
message = "SERVER_ERROR")
})
Expand All @@ -50,11 +51,7 @@ public ResponseEntity<DefaultResponseDto<Object>> createAnniversary(
AnniversaryResponseDto dto = anniversaryService.returnDto(anniversary);

return ResponseEntity.status(201)
.body(DefaultResponseDto.builder()
.responseCode("ANNIVERSARY_CREATED")
.responseMessage("기념일 생성 완료")
.data(dto)
.build());
.body(resWithData("ANIVERSARY_CREATED", "기념일 생성 완료", dto));
}

@ApiOperation("기념일 조회")
Expand All @@ -79,11 +76,7 @@ public ResponseEntity<DefaultResponseDto<Object>> readAnniversary(
AnniversaryResponseDto dto = anniversaryService.returnDto(anniversary);

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("ANNIVERSARY_FOUND")
.responseMessage("기념일 조회 완료")
.data(dto)
.build());
.body(resWithData("ANNIVERSARY_FOUND", "기념일 조회 완료", dto));
}

@ApiOperation("기념일 수정")
Expand Down Expand Up @@ -111,11 +104,7 @@ public ResponseEntity<DefaultResponseDto<Object>> updateAnniversary(
AnniversaryResponseDto dto = anniversaryService.returnDto(anniversary);

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseMessage("ANNIVERSARY_UPDATED")
.responseCode("기념일 수정 완료")
.data(dto)
.build());
.body(resWithData("ANNIVERSARY_UPDATED", "기념일 수정 완료", dto));
}

@ApiOperation("기념일 핀 여부 수정")
Expand All @@ -142,11 +131,7 @@ public ResponseEntity<DefaultResponseDto<Object>> updateIsPinned(
AnniversaryResponseDto dto = anniversaryService.returnDto(anniversary);

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseMessage("ANNIVERSARY_PIN_UPDATED")
.responseCode("기념일 핀 여부 수정 완료")
.data(dto)
.build());
.body(resWithData("ANNIVERSARY_PIN_UPDATED", "기념일 핀 수정 완료", dto));
}

@ApiOperation("기념일 삭제")
Expand All @@ -169,16 +154,11 @@ public ResponseEntity<DefaultResponseDto<Object>> deleteAnniversary(
anniversaryService.isExistingAnniversaryNo(anniversaryNo);

Anniversary anniversary = anniversaryService.findAnniversaryByAnniversaryNo(anniversaryNo);
AnniversaryResponseDto dto = anniversaryService.returnDto(anniversary);

anniversaryService.deleteAnniversary(anniversaryNo);

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("ANNIVERSARY_DELETED")
.responseMessage("기념일 삭제 완료")
.data(dto)
.build());
.body(resWithoutData("ANNIVERSARY_DELETED", "기념일_삭제_완료"));
}

@ApiOperation("전체 기념일 조회")
Expand All @@ -198,10 +178,6 @@ public ResponseEntity<DefaultResponseDto<Object>> readAll(){
List<AnniversaryResponseDto> dto = anniversaryService.readAll();

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("ANNIVERSARIES_FOUND")
.responseMessage("전체 기념일 조회 완료")
.data(dto)
.build());
.body(resWithData("ANIVERSARIES_FOUND", "전체 기념일 조회 완료", dto));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class AnniversaryRequestDto {
@ApiModelProperty(position = 3, required = true, value = "종류", example = "축하_생일")
private AnniversaryCategory anniversaryCategory;

@Transactional
public Anniversary toEntity(String anniversaryTitle, User user){
return Anniversary.builder()
.anniversaryTitle(anniversaryTitle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
import com.favor.favor.user.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.transaction.Transactional;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

import static com.favor.favor.exception.ExceptionCode.*;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class AnniversaryService {

Expand All @@ -30,6 +31,7 @@ public Anniversary createAnniversary(AnniversaryRequestDto anniversaryRequestDto
return anniversaryRepository.save(anniversary);
}

@Transactional
public void updateAnniversary(AnniversaryUpdateRequestDto dto, Anniversary anniversary){
anniversary.setAnniversaryTitle(dto.getAnniversaryTitle());
anniversary.setAnniversaryDate(LocalDate.parse(dto.getAnniversaryDate()));
Expand All @@ -38,6 +40,7 @@ public void updateAnniversary(AnniversaryUpdateRequestDto dto, Anniversary anniv
anniversaryRepository.save(anniversary);
}

@Transactional
public void updateIsPinned(Anniversary anniversary){
anniversary.setIsPinned(anniversary.getIsPinned() == true ? false : true);
anniversaryRepository.save(anniversary);
Expand Down
20 changes: 14 additions & 6 deletions favor/src/main/java/com/favor/favor/common/DefaultResponseDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import lombok.*;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "기본 응답")
public class DefaultResponseDto <T> {

Expand All @@ -18,11 +15,22 @@ public class DefaultResponseDto <T> {
@ApiModelProperty(position = 3, value = "응답 데이터", example = "응답 데이터")
private T data;

public static <T> DefaultResponseDto<T> response(final String responseCode, final String responseMessage){
return response(responseCode, responseMessage, null);

@Builder
private DefaultResponseDto(String responseCode, String responseMessage, T data){
this.responseCode = responseCode;
this.responseMessage = responseMessage;
this.data = data;
}

public static <T> DefaultResponseDto<T> resWithoutData(final String responseCode, final String responseMessage){
return DefaultResponseDto.<T>builder()
.responseCode(responseCode)
.responseMessage(responseMessage)
.build();
}

public static <T> DefaultResponseDto<T> response(final String responseCode, final String responseMessage, final T data){
public static <T> DefaultResponseDto<T> resWithData(final String responseCode, final String responseMessage, final T data){
return DefaultResponseDto.<T>builder()
.responseCode(responseCode)
.responseMessage(responseMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Getter
public class TimeStamped {
public abstract class TimeStamped {
@CreatedDate
private LocalDateTime createdAt;

Expand Down
1 change: 0 additions & 1 deletion favor/src/main/java/com/favor/favor/friend/Friend.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
@Transactional
public class Friend extends TimeStamped {

@Id
Expand Down
53 changes: 12 additions & 41 deletions favor/src/main/java/com/favor/favor/friend/FriendController.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.List;

import static com.favor.favor.common.DefaultResponseDto.resWithData;
import static com.favor.favor.common.DefaultResponseDto.resWithoutData;


@Api(tags = "Friend")
@RestController
@RequestMapping("/friends")
Expand Down Expand Up @@ -52,11 +55,7 @@ public ResponseEntity<DefaultResponseDto<Object>> addFriend(
FriendResponseDto dto = friendService.returnDto(friend);

return ResponseEntity.status(201)
.body(DefaultResponseDto.builder()
.responseCode("FRIEND_ADDED")
.responseMessage("친구 추가 완료")
.data(dto)
.build());
.body(resWithData("FRIEND_ADDED", "친구 추가 완료", dto));
}

@ApiOperation("친구 조회")
Expand All @@ -82,11 +81,7 @@ public ResponseEntity<DefaultResponseDto<Object>> readFriend(
FriendResponseDto dto = friendService.returnDto(friend);

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("FRIEND_FOUND")
.responseMessage("친구 조회 완료")
.data(dto)
.build());
.body(resWithData("FRIEND_FOUND", "친구 조회 완료", dto));
}

@ApiOperation("친구 메모 수정")
Expand Down Expand Up @@ -116,11 +111,7 @@ public ResponseEntity<DefaultResponseDto<Object>> updateFriend(
FriendResponseDto dto = friendService.returnDto(friend);

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("FRIEND_MEMO_UPDATED")
.responseMessage("친구 메모 수정 완료")
.data(dto)
.build());
.body(resWithData("FRIEND_MEMO_UPDATED", "친구 메모 수정 완료", dto));
}

@ApiOperation("친구 삭제")
Expand All @@ -147,11 +138,7 @@ public ResponseEntity<DefaultResponseDto<Object>> deleteFriend(
friendService.deleteFriend(friendNo);

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("FRIEND_DELETED")
.responseMessage("친구 삭제 완료")
.data(dto)
.build());
.body(resWithoutData("FRIEND_DELETED", "친구 삭제 완료"));
}

@ApiOperation("전체 친구 조회")
Expand All @@ -171,11 +158,7 @@ public ResponseEntity<DefaultResponseDto<Object>> readAll(){
List<FriendResponseDto> dto = friendService.readAll();

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("FRIENDS_FOUND")
.responseMessage("전체 친구 조회 완료")
.data(dto)
.build());
.body(resWithData("FRIENDS_FOUND", "전체 친구 조회 완료", dto));
}

@ApiOperation("친구의 선물 전체 조회")
Expand All @@ -196,11 +179,7 @@ public ResponseEntity<DefaultResponseDto<Object>> readTotalGiftList(
List<GiftSimpleDto> dto = friendService.findGiftListByFriendNo(friendNo);

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("GIFTS_FOUND")
.responseMessage("친구의 선물 전체 조회 완료")
.data(dto)
.build());
.body(resWithData("GIFTS_FOUND", "친구의 선물 전체 조회 완료", dto));
}

@ApiOperation("친구가 준 선물 전체 조회")
Expand All @@ -221,11 +200,7 @@ public ResponseEntity<DefaultResponseDto<Object>> readGivenGiftList(
List<GiftSimpleDto> dto = friendService.findReceivedGiftList(friendNo);

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("GIFTS_FOUND")
.responseMessage("친구가 준 선물 전체 조회 완료")
.data(dto)
.build());
.body(resWithData("GIFTS_FOUND", "친구가 준 선물 전체 조회 완료", dto));
}

@ApiOperation("친구가 받은 선물 전체 조회")
Expand All @@ -246,10 +221,6 @@ public ResponseEntity<DefaultResponseDto<Object>> readReceivedGiftList(
List<GiftSimpleDto> dto = friendService.findGivenGiftList(friendNo);

return ResponseEntity.status(200)
.body(DefaultResponseDto.builder()
.responseCode("GIFTS_FOUND")
.responseMessage("친구가 받은 선물 전체 조회 완료")
.data(dto)
.build());
.body(resWithData("GIFTS_FOUND", "친구가 받은 선물 전체 조회 완료", dto));
}
}
9 changes: 3 additions & 6 deletions favor/src/main/java/com/favor/favor/friend/FriendService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,22 @@
import com.favor.favor.exception.CustomException;
import com.favor.favor.gift.Gift;
import com.favor.favor.gift.GiftRepository;
import com.favor.favor.gift.GiftResponseDto;
import com.favor.favor.gift.GiftSimpleDto;
import com.favor.favor.reminder.Reminder;
import com.favor.favor.reminder.ReminderResponseDto;
import org.springframework.transaction.annotation.Transactional;
import com.favor.favor.reminder.ReminderSimpleDto;
import com.favor.favor.user.User;
import com.favor.favor.user.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.favor.favor.exception.ExceptionCode.*;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class FriendService {
private final FriendRepository friendRepository;
Expand Down Expand Up @@ -54,6 +52,7 @@ public Boolean isDuplicateFriendUser(User user, User friendUser){
return isDuplicate;
}

@Transactional
public void updateMemo(Friend friend, MemoUpdateRequestDto memoUpdateRequestDto){
friend.setFriendMemo(memoUpdateRequestDto.getMemo());
friendRepository.save(friend);
Expand All @@ -76,7 +75,6 @@ public void deleteFriend(Long friendNo){
friendRepository.deleteById(friendNo);
}

@Transactional
public List<FriendResponseDto> readAll(){
List<FriendResponseDto> f_List = new ArrayList<>();
List<Friend> friendList = friendRepository.findAll();
Expand Down Expand Up @@ -165,7 +163,6 @@ public List<GiftSimpleDto> findReceivedGiftList(Long friendNo){


//RETURN
@Transactional
public FriendResponseDto returnDto(Friend friend){
User friendUser = findUserByUserNo(friend.getFriendUserNo());

Expand Down
1 change: 0 additions & 1 deletion favor/src/main/java/com/favor/favor/gift/Gift.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@Builder
@ToString
public class Gift extends TimeStamped {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand Down
Loading