Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
40 changes: 25 additions & 15 deletions src/main/java/com/example/demo/feed/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.example.demo.user.domain.User;
import com.example.demo.user.domain.UserRepository;
import com.example.demo.user.domain.Username;
import com.example.demo.user.service.UserDto;
import com.example.demo.user.service.UserService;
import com.example.demo.user.service.UserSessionService;
import lombok.RequiredArgsConstructor;
Expand All @@ -34,33 +35,42 @@ public CommentDto makeCommentByFeedIdAndContents(Long feedId, String insertedCon
if (insertedContent.length()==0 || feed==null){
throw new IllegalArgumentException();
}
UserDto userInfo = userService.findUserInfo();

String[] userInfo = userService.findUserInfo();
Long friendId = feed.getWriter().getId();
Friendship friendship
= friendShipRepository.findFriendshipByFriender_FrienderIdAndFriendee_FriendeeId(
PersonId.create(friendId),
PersonId.create(Long.parseLong(userInfo[0])));


Friendship friendship = findFriendShip(feed, userInfo);


if (friendship==null || friendship.getFriendState()!= FriendShipState.ACCEPT){
throw new IllegalAccessException();
}

Content content = Content.create(insertedContent);
WriterId id = WriterId.create(Long.parseLong(userInfo[0]));
WriterName wrtName = WriterName.create(userInfo[1]);
Writer writer = Writer.create(id, wrtName);
Comment comment = Comment.create(writer, content);
Comment comment = createComment(insertedContent, userInfo);
feed.enrollComment(comment);

CommentDto commentDto = toCommentDto(comment);

CommentDto commentDto = toCommentDto(comment);
return commentDto;
}

private Friendship findFriendShip(Feed feed, UserDto userInfo) throws AccessDeniedException {
Long friendId = feed.getWriter().getId();
return friendShipRepository.findFriendshipByFriender_FrienderIdAndFriendee_FriendeeId(
PersonId.create(friendId),
PersonId.create(userInfo.getUserId()));
}


private Comment createComment(String insertedContent, UserDto userInfo) {
Content content = Content.create(insertedContent);
Writer writer = createWriter(userInfo);
return Comment.create(writer, content);
}

private Writer createWriter(UserDto userInfo) {
WriterId id = WriterId.create(userInfo.getUserId());
WriterName wrtName = WriterName.create(userInfo.getUsername());
return Writer.create(id, wrtName);
}

private CommentDto toCommentDto(Comment comment) {
return new CommentDto(
comment.getId(),
Expand Down
65 changes: 38 additions & 27 deletions src/main/java/com/example/demo/feed/service/FeedService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.demo.friend.domain.FriendShipState;
import com.example.demo.friend.domain.Friendship;
import com.example.demo.friend.domain.PersonId;
import com.example.demo.user.service.UserDto;
import com.example.demo.user.service.UserService;
import lombok.RequiredArgsConstructor;

Expand All @@ -24,7 +25,7 @@ public class FeedService {
@Transactional
public void deleteFeedByFeedId(Long id) throws AccessDeniedException, IllegalAccessException {
Feed feed = feedRepository.findFeedByIdAndDeleted(id, false);
PersonId myId = PersonId.create(Long.parseLong(userService.findUserInfo()[0]));
PersonId myId = PersonId.create(userService.findUserInfo().getUserId());

if (feed==null){
throw new IllegalArgumentException();
Expand All @@ -39,26 +40,58 @@ public void deleteFeedByFeedId(Long id) throws AccessDeniedException, IllegalAcc


public FeedDto findFeedDetailByFeedId(Long id) throws AccessDeniedException, IllegalAccessException {
String[] userInfo = userService.findUserInfo();
UserDto userInfo = userService.findUserInfo();

Feed feed = feedRepository.findFeedByIdAndDeleted(id, false);
if (feed==null){
throw new IllegalArgumentException();
}

PersonId myId = PersonId.create(Long.parseLong(userInfo[0]));
PersonId myId = PersonId.create(userInfo.getUserId());
PersonId friendId = PersonId.create(feed.getId());
Friendship friendship = friendShipRepository.findFriendshipByFriender_FrienderIdAndFriendee_FriendeeId(myId, friendId);

Friendship friendship = findFriendShip(myId, friendId);


if (friendship==null || friendship.getFriendState()!=FriendShipState.ACCEPT){
throw new IllegalAccessException();
}

FeedDto feedDto = toFeedDto(feed);
return feedDto;
}


private Friendship findFriendShip(PersonId myId, PersonId friendId) {
return friendShipRepository.findFriendshipByFriender_FrienderIdAndFriendee_FriendeeId(myId, friendId);
}


public FeedDto makeFeedByContents(String insertedTitle, String insertedContent) throws AccessDeniedException {

if (insertedTitle.length()==0){
throw new IllegalArgumentException();
}

UserDto userInfo = userService.findUserInfo();
Writer writer = createWriter(userInfo);
Feed feed = createFeed(insertedContent, insertedTitle, writer);
feedRepository.save(feed);

FeedDto feedDto = toFeedDto(feed);
return feedDto;

}

private Feed createFeed(String insertedContent, String insertedTitle ,Writer writer) {
Content content = Content.create(insertedContent);
Title title = Title.create(insertedTitle);
return Feed.create(writer, title, content);
}

private Writer createWriter(UserDto userInfo) {
WriterId id = WriterId.create(userInfo.getUserId());
WriterName wrtName = WriterName.create(userInfo.getUsername());
return Writer.create(id, wrtName);
}

private FeedDto toFeedDto(Feed feed) {
Expand Down Expand Up @@ -87,28 +120,6 @@ private CommentDto toCommentDto(Comment comment){
}


public FeedDto makeFeedByContents(String insertedTitle, String insertedContent) throws AccessDeniedException {

if (insertedTitle.length()==0){
throw new IllegalArgumentException();
}

String[] userInfo = userService.findUserInfo();

WriterId id = WriterId.create(Long.parseLong(userInfo[0]));
WriterName wrtName = WriterName.create(userInfo[1]);
Writer writer = Writer.create(id, wrtName);
Content content = Content.create(insertedContent);
Title title = Title.create(insertedTitle);
Feed feed = Feed.create(writer, title, content);
feedRepository.save(feed);

FeedDto feedDto = toFeedDto(feed);
return feedDto;

}





Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,7 @@ public void createFriendShip(@PathVariable Long friendId) throws AccessDeniedExc
@DeleteMapping("/{friendId}")
public void deleteFriendShip(@PathVariable Long friendId) throws AccessDeniedException {
FriendShipResult result = friendShipService.deleteFriendShipRequest(friendId);
switch ((FriendShipResult)result){
case NEVER_REQUESTED:
ResponseEntity.status(HttpStatus.BAD_REQUEST);
break;
case SUCCESS:
ResponseEntity.ok();
}

}

@GetMapping("/request")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.demo.friend.service;

import com.example.demo.friend.domain.*;
import com.example.demo.user.service.UserDto;
import com.example.demo.user.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
Expand All @@ -26,9 +27,9 @@ public List<FriendDto> getMyFriends(int page, int size) throws AccessDeniedExcep
throw new IllegalArgumentException();
}

String[] userInfo = userService.findUserInfo();
PersonId userId = PersonId.create(Long.parseLong(userInfo[0]));
PersonName userName = PersonName.create(userInfo[1]);
UserDto userInfo = userService.findUserInfo();
PersonId userId = PersonId.create(userInfo.getUserId());
PersonName userName = PersonName.create(userInfo.getUsername());

Friender friender = Friender.create(userId, userName);

Expand All @@ -49,14 +50,14 @@ public List<FriendDto> getMyFriends(int page, int size) throws AccessDeniedExcep

@Transactional
public void create(Long inputedFriendId) throws AccessDeniedException {
String[] user = userService.findUserInfo();
String [] friend = userService.findUserInfo(inputedFriendId);
UserDto userInfo = userService.findUserInfo();
UserDto friendInfo = userService.findUserInfo(inputedFriendId);

PersonId myId = PersonId.create(Long.parseLong(user[0]) );
PersonId friendId = PersonId.create(Long.parseLong(friend[0]));
PersonId myId = PersonId.create(userInfo.getUserId() );
PersonId friendId = PersonId.create(friendInfo.getUserId());

PersonName myName = PersonName.create(user[1]);
PersonName friendName = PersonName.create( friend[1]);
PersonName myName = PersonName.create(userInfo.getUsername());
PersonName friendName = PersonName.create( friendInfo.getUsername());

Friender frienderMe = Friender.create(myId, myName);
Friendee friendeeYou = Friendee.create(friendId, friendName);
Expand Down Expand Up @@ -98,9 +99,9 @@ public void create(Long inputedFriendId) throws AccessDeniedException {


public FriendShipResult deleteFriendShipRequest(Long inputedId) throws AccessDeniedException {
String[] userInfo = userService.findUserInfo();
PersonId myId = PersonId.create(Long.parseLong(userInfo[0]));
PersonName myName = PersonName.create( userInfo[1]);
UserDto userInfo = userService.findUserInfo();
PersonId myId = PersonId.create(userInfo.getUserId());
PersonName myName = PersonName.create(userInfo.getUsername());
Friender myInfo = Friender.create(myId, myName);

PersonId friendId = PersonId.create(inputedId);
Expand Down Expand Up @@ -131,17 +132,21 @@ public List<FriendDto> findRequestForMe(int page, int size) throws AccessDenied
throw new IllegalArgumentException();
}

PersonId myId = PersonId.create(userService.findUserInfo().getUserId());

PersonId myId = PersonId.create(Long.parseLong(userService.findUserInfo()[0]));
List<FriendDto> friendshipList = createFriendShipList(myId, page, size);

List<FriendDto> friendshipList = friendShipRepository.findAllByFriendee_FriendeeId(myId, PageRequest.of(page, size))
return friendshipList;
}


private List<FriendDto> createFriendShipList(PersonId myId, int page, int size) {
return friendShipRepository.findAllByFriendee_FriendeeId(myId, PageRequest.of(page, size))
.stream()
.filter(friendship -> friendship.getFriendState()==FriendShipState.REQUEST)
.map(Friendship::getFriender)
.map(this::toFriendDto)
.collect(Collectors.toList());

return friendshipList;
}

public Friendship createFriendship(Friender friender, Friendee friendee) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.demo.mysecurity.infrastructure;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;


@Configuration
public class PasswordEncoderConfig {

@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.example.demo.mysecurity.service;


import com.example.demo.user.domain.Authority;
import com.example.demo.user.domain.UserRepository;
import com.example.demo.user.domain.Username;
import lombok.RequiredArgsConstructor;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.SecurityUser;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class UserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {

private final UserRepository userRepository;



@Override
public UserDetails loadUserByUsername(String username){
Username name = Username.create(username);

Optional<User> user1 = userRepository
.findUserByUserBasicInfo_Username(name)
.map(user -> new SecurityUser(
user.getId(),
user.getUserBasicInfo().getUsername(),
user.getUserBasicInfo().getPassword(),
user.getAuthorities()
.stream()
.map(Authority::getAuthority)
.map(SimpleGrantedAuthority::new)
.collect(Collectors.toSet())
));
return user1.orElseThrow(
()-> new UsernameNotFoundException("Can't find by your username"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.demo.mysecurity.service;

import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.SecurityUser;

import java.nio.file.AccessDeniedException;

public class UserSessionUtils {

private UserSessionUtils(){

}

public static SecurityUser getSessionUser() throws AccessDeniedException {
Object detais = SecurityContextHolder.getContext().getAuthentication().getDetails();

if (!(detais instanceof SecurityUser)){
throw new AccessDeniedException("유저 정보가 없습니다.");
}
return (SecurityUser) detais;
}
}
Loading