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/3] 친구 추가, 친구 삭제, 친구 목록 조회 #8

Merged
merged 1 commit into from
Jul 4, 2024
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 src/main/java/com/umc/dream/apiPayload/ApiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import lombok.AllArgsConstructor;
import lombok.Getter;
import umc.spring.apiPayload.code.status.SuccessStatus;
import com.umc.dream.apiPayload.code.status.SuccessStatus;

@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;
import umc.spring.apiPayload.code.BaseCode;
import umc.spring.apiPayload.code.ReasonDTO;
import com.umc.dream.apiPayload.code.BaseCode;
import com.umc.dream.apiPayload.code.ReasonDTO;

@Getter
@AllArgsConstructor
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/umc/dream/controller/FollowController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.umc.dream.controller;

import com.umc.dream.apiPayload.ApiResponse;
import com.umc.dream.converter.FollowConverter;
import com.umc.dream.domain.Follow;
import com.umc.dream.dto.FollowRequestDto;
import com.umc.dream.dto.FollowResponseDto;
import com.umc.dream.dto.ViewFollowResponseDto;
import com.umc.dream.service.FollowService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@AllArgsConstructor
public class FollowController {

private final FollowService followService;

@PostMapping("/user/follow")
public ApiResponse<FollowResponseDto> follow(@RequestBody FollowRequestDto followRequestDto) {
Follow f = followService.follow(followRequestDto);
FollowResponseDto followResponseDto = FollowConverter.toFollowResponse(f);
return ApiResponse.onSuccess(followResponseDto);
}

@DeleteMapping("/user/followCancel")
public ApiResponse<?> followCancel(@RequestBody FollowRequestDto followRequestDto) {
followService.followCancel(followRequestDto);
return ApiResponse.onSuccess("팔로우 취소됨");
}

@GetMapping("/user/{user_id}/following")
public ApiResponse<List<ViewFollowResponseDto>> viewFollowings(@PathVariable Long user_id) {
List<ViewFollowResponseDto> viewFollowResponseDtos = followService.viewFollowings(user_id);
return ApiResponse.onSuccess(viewFollowResponseDtos);
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/umc/dream/converter/FollowConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.umc.dream.converter;

import com.umc.dream.domain.Follow;
import com.umc.dream.dto.FollowResponseDto;

public class FollowConverter {

public static FollowResponseDto toFollowResponse(Follow follow) {
return FollowResponseDto.builder()
.updatedAt(follow.getFollower().getLastModifiedDate())
.build();
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/umc/dream/dto/FollowRequestDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.umc.dream.dto;

import com.umc.dream.domain.Follow;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class FollowRequestDto {
private Long followerId;
private Long acceptorId;

@Builder
public FollowRequestDto(Follow follow) {
this.followerId = follow.getFollower().getId();
this.acceptorId = follow.getAcceptor().getId();
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/umc/dream/dto/FollowResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.umc.dream.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class FollowResponseDto {
private LocalDateTime updatedAt;
}
15 changes: 15 additions & 0 deletions src/main/java/com/umc/dream/dto/ViewFollowResponseDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.umc.dream.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class ViewFollowResponseDto {
private Long userId;
private String name;
}
13 changes: 13 additions & 0 deletions src/main/java/com/umc/dream/repository/FollowRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.umc.dream.repository;

import com.umc.dream.domain.Follow;
import com.umc.dream.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface FollowRepository extends JpaRepository<Follow, Long> {
List<Follow> findAllByFollower_Id(Long memberId);

Follow findByFollowerAndAcceptor(User follower, User acceptor);
}
7 changes: 7 additions & 0 deletions src/main/java/com/umc/dream/repository/UserRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.umc.dream.repository;

import com.umc.dream.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
}
62 changes: 62 additions & 0 deletions src/main/java/com/umc/dream/service/FollowService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.umc.dream.service;

import com.umc.dream.apiPayload.code.status.ErrorStatus;
import com.umc.dream.apiPayload.exception.GeneralException;
import com.umc.dream.domain.Follow;
import com.umc.dream.domain.User;
import com.umc.dream.dto.FollowRequestDto;
import com.umc.dream.dto.ViewFollowResponseDto;
import com.umc.dream.repository.FollowRepository;
import com.umc.dream.repository.UserRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class FollowService {

private final FollowRepository followRepository;
private final UserRepository userRepository;
@Transactional
public Follow follow(FollowRequestDto followRequestDto) {
User follower = userRepository.findById(followRequestDto.getFollowerId())
.orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST));
User acceptor = userRepository.findById(followRequestDto.getAcceptorId())
.orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST));
Follow f = Follow.builder()
.follower(follower)
.acceptor(acceptor)
.build();
followRepository.save(f);
return f;
}

@Transactional
public void followCancel(FollowRequestDto followRequestDto) {
User follower = userRepository.findById(followRequestDto.getFollowerId())
.orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST));
User acceptor = userRepository.findById(followRequestDto.getAcceptorId())
.orElseThrow(() -> new GeneralException(ErrorStatus._BAD_REQUEST));
Follow f = followRepository.findByFollowerAndAcceptor(follower,acceptor);
followRepository.delete(f);
}

@Transactional
public List<ViewFollowResponseDto> viewFollowings(Long memberId) {
List<Follow> follows = followRepository.findAllByFollower_Id(memberId);
List<ViewFollowResponseDto> viewFollowResponseDtos = new ArrayList<>();
for(Follow follow : follows) {
User user = follow.getAcceptor();
ViewFollowResponseDto viewFollowResponseDto = ViewFollowResponseDto.builder()
.userId(user.getId())
.name(user.getName())
.build();
viewFollowResponseDtos.add(viewFollowResponseDto);
}
return viewFollowResponseDtos;
}
}
Loading