Skip to content

Commit

Permalink
Merge pull request #67 from dailylifev1/dm
Browse files Browse the repository at this point in the history
#66 DAILYLIFE2-20 dm
  • Loading branch information
shihhhdm310 authored Jul 28, 2022
2 parents eb8bdbb + f1ae9da commit 230ba81
Show file tree
Hide file tree
Showing 22 changed files with 310 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@ public ResponseEntity<Boolean> deleteBoard(@PathVariable("boardNum")Long boardNu
}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.dailylife.domain.follow.controller;

import com.dailylife.domain.follow.dto.FollowingRequest;
import com.dailylife.domain.follow.entity.Follow;
import com.dailylife.domain.follow.service.FollowingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("api/users")
@RequiredArgsConstructor
@Api(tags = "Following API")
public class FollowingController {

private final FollowingService followingService;

@ApiOperation(value = "팔로우를 추가합니다.", notes = "팔로우를 추가합니다.")
@PostMapping("/following")
public ResponseEntity<Follow> following(@Valid @RequestBody FollowingRequest followingRequest) {
return ResponseEntity.ok(followingService.following(followingRequest));
}

@ApiOperation(value = "팔로우를 해제합니다.", notes = "팔로우를 해제합니다.")
@PostMapping("/unfollowing")
public ResponseEntity<String> unfollowing(@Valid @RequestBody FollowingRequest followingRequest) {
followingService.unfollowing(followingRequest);
return ResponseEntity.ok("확인");
}

@ApiOperation(value = "팔로워를 확인합니다.", notes = "팔로워를 확인합니다.")
@PostMapping("/getFollower")
public ResponseEntity<List<Follow>> getFollower() {
return ResponseEntity.ok(followingService.getFollower());
}

@ApiOperation(value = "팔로우를 확인합니다", notes = "팔로우를 확인합니다.")
@PostMapping("/getFollow")
public String getFollow() {
followingService.getFollow();
return null;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dailylife.domain.user_follow.dto;
package com.dailylife.domain.follow.dto;


import io.swagger.annotations.ApiModel;
Expand All @@ -8,15 +8,14 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "팔로우 요청을 위한 객체")
public class UserFollowingRequest {
public class FollowingRequest {

@NotNull(message = "아이디를 입력해주세요")
@ApiModelProperty(notes = "아이디를 입력해주세요")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.dailylife.domain.user_follow.entity;
package com.dailylife.domain.follow.entity;

import com.dailylife.domain.user.entity.User;
import com.dailylife.domain.user_follow.dto.UserFollowingRequest;
import com.dailylife.domain.follow.dto.FollowingRequest;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;

Expand All @@ -13,7 +13,7 @@
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserFollow {
public class Follow {

//1 - > 3 팔로우
//3 을 팔로우한 1번
Expand All @@ -31,15 +31,15 @@ public class UserFollow {

public void setUser(User user) {
this.user = user;
user.getUserFollows().add(this);
user.getFollows().add(this);
}

public static UserFollow toEntity(UserFollowingRequest userFollowRequest , User user) {
UserFollow userFollow = UserFollow.builder()
public static Follow toEntity(FollowingRequest userFollowRequest , User user) {
Follow follow = Follow.builder()
.followNum(userFollowRequest.getFollowNum())
.build();
userFollow.setUser(user);
return userFollow;
follow.setUser(user);
return follow;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.dailylife.domain.follow.repository;

import com.dailylife.domain.follow.entity.Follow;
import com.dailylife.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface FollowingRepository extends JpaRepository<Follow, Long> {

Optional<List<Follow>> findByFollowNum(Long followNum);

void deleteByFollowNum(Long followNum);

Optional<List<Follow>> findByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.dailylife.domain.follow.service;

import com.dailylife.domain.follow.dto.FollowingRequest;
import com.dailylife.domain.follow.entity.Follow;

import java.util.List;

public interface FollowingService {
Follow following(FollowingRequest userFollowRequest);

List<Follow> getFollower();

void unfollowing(FollowingRequest followingRequest);

List<Follow> getFollow();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.dailylife.domain.follow.service;

import com.dailylife.domain.user.entity.User;
import com.dailylife.domain.user.repository.UserRepository;
import com.dailylife.domain.follow.dto.FollowingRequest;
import com.dailylife.domain.follow.entity.Follow;
import com.dailylife.domain.follow.repository.FollowingRepository;
import com.dailylife.global.jwt.service.JwtService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@RequiredArgsConstructor
@Service
@Slf4j
public class FollowingServiceImpl implements FollowingService {

private final FollowingRepository userFollowRepository;
private final JwtService jwtService;
private final UserRepository userRepository;

@Override
@Transactional
public Follow following(FollowingRequest userFollowRequest) {

try{
userRepository.findByUserNum(userFollowRequest.getFollowNum()).get();
}catch (Exception e){
e.printStackTrace();
throw new RuntimeException("유저를 찾을수 없습니다.");
}

User user = userRepository.findByUserId(jwtService.getLoginId());

return userFollowRepository.save(Follow.toEntity(userFollowRequest,user));

}


@Override
@Transactional
public void unfollowing(FollowingRequest followingRequest) {

User user = userRepository.findByUserId(jwtService.getLoginId());
userFollowRepository.deleteByFollowNum(followingRequest.getFollowNum());

}

@Override
@Transactional
public List<Follow> getFollow() {
User user = userRepository.findByUserId(jwtService.getLoginId());
return userFollowRepository.findByUser(user).orElseThrow(RuntimeException::new);
}

@Override
@Transactional
public List<Follow> getFollower() {
User user = userRepository.findByUserId(jwtService.getLoginId());
return userFollowRepository.findByFollowNum(user.getUserNum()).orElseThrow(RuntimeException::new);
}


}
2 changes: 0 additions & 2 deletions src/main/java/com/dailylife/domain/reply/entity/Reply.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.dailylife.domain.reply.dto.ReplyInsertRequest;
import com.dailylife.domain.replyReply.entity.ReplyReply;
import com.dailylife.domain.user.entity.User;
import com.dailylife.domain.user_follow.entity.UserFollow;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;

Expand Down Expand Up @@ -41,7 +40,6 @@ public class Reply {
private Board board;*/



@OneToMany(mappedBy = "reply" /*,cascade = CascadeType.ALL*/) // reply이 삭제되면 자동으로 heart또한 삭제
@JsonIgnore
private List<Heart> hearts = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
public class ReplyServiceImpl implements ReplyService{
private final ReplyRepository replyRepository;



@Override
@Transactional
public Reply insert(ReplyInsertRequest replyInsertRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import com.dailylife.domain.user.dto.UserModifyRequest;
import com.dailylife.domain.user.entity.User;
import com.dailylife.domain.user.service.UserService;
import com.dailylife.global.dto.ApplicationResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
Expand All @@ -29,18 +32,28 @@ public class UserController {

@ApiOperation(value = "회원가입", notes = "회원가입을 합니다.")
@PostMapping("/join")
public ResponseEntity<User> saveUser(@Valid @RequestBody UserJoinRequest userJoinRequest) {
log.info("userJoinId By React => {}" , userJoinRequest.getUserId());
return ResponseEntity.ok(userService.join(userJoinRequest));
public ApplicationResponse<User> saveUser(@Valid @RequestBody UserJoinRequest userJoinRequest) {
return ApplicationResponse.create("회원가입이 완료되었습니다." , HttpStatus.CREATED , userService.join(userJoinRequest));
}

@ApiOperation(value = "로그인", notes = "로그인을 합니다.")
@PostMapping("/login")
public ResponseEntity<User> loginUser(@Valid @RequestBody UserLoginRequest userLoginRequestRequest) {
log.info("userLoginId By React => {}" , userLoginRequestRequest.getUserId());
return ResponseEntity.ok(userService.login(userLoginRequestRequest));
public ApplicationResponse<User> loginUser(@Valid @RequestBody UserLoginRequest userLoginRequestRequest) {
return ApplicationResponse.create("로그인이 완료되었습니다." , HttpStatus.OK , userService.login(userLoginRequestRequest));
}

@ApiOperation(value = "회원탈퇴" , notes = "회원을 탈퇴합니다.")
@DeleteMapping("/quit/{userNum}")
public ApplicationResponse<User> quitUser(@Valid @PathVariable Long userNum) {
userService.quit(userNum);
return ApplicationResponse.ok();
}

@ApiOperation(value = "내정보 보기" , notes = "내정보를 자세하게 봅니다")
@PostMapping("/details/{userNum}")
public ApplicationResponse<User> detailsUser(@Valid @PathVariable Long userNum) {
return ApplicationResponse.create("유저의 대한 정보입니다" , HttpStatus.OK , userService.getDetails(userNum));
}

@PostMapping("/post")
public String PostTest(@RequestBody String msg) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.dailylife.domain.user.dto;

import com.dailylife.domain.board.entity.Board;
import com.dailylife.domain.user.entity.User;
import io.swagger.annotations.ApiModel;
import lombok.*;

import java.time.LocalDateTime;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "유저의 대한 상세정보를 전달하기위한 객체")
@Builder
public class UserDetailResponse {

private String userId;
private String userName;
private String userEmail;
private String userProfileImg;

private int followSize;
private int followerSize;

private Long boardNum;

public static UserDetailResponse from(User user , int followSize , int followerSize , Board board) {
return UserDetailResponse.builder()
.userId(user.getUserId())
.userName(user.getUserName())
.userEmail(user.getUserEmail())
.userProfileImg(user.getUserProfileImg())
.followerSize(followerSize)
.followSize(followSize)
.boardNum(board.getBoardNum()).build();
}

}
15 changes: 10 additions & 5 deletions src/main/java/com/dailylife/domain/user/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.dailylife.domain.user.entity;

import com.dailylife.domain.board.entity.Board;
import com.dailylife.domain.heart.entity.Heart;
import com.dailylife.domain.reply.entity.Reply;
import com.dailylife.domain.user.dto.UserJoinRequest;
import com.dailylife.domain.user_follow.entity.UserFollow;
import com.dailylife.domain.follow.entity.Follow;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;

import javax.persistence.*;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -49,7 +46,7 @@ public class User {

@OneToMany(mappedBy = "user" , orphanRemoval = true, cascade = CascadeType.PERSIST)
@JsonIgnore
private List<UserFollow> userFollows = new ArrayList<>();
private List<Follow> follows = new ArrayList<>();

@OneToMany(mappedBy = "user")
@JsonIgnore
Expand All @@ -69,5 +66,13 @@ public static User toEntity(UserJoinRequest userJoinRequest) {
.build();
}

/*
public static User toEntity(UserJoinReq userJoinReq) {
return User.builder()
.name(userJoinReq.getName())
.build();
}
*/


}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ public interface UserService {

String modifyProfileImg(MultipartFile img) throws IOException;

void quit(Long userNum);

User getDetails(Long userNum);
}
Loading

0 comments on commit 230ba81

Please sign in to comment.