Skip to content

Commit

Permalink
fix: 오류 수정
Browse files Browse the repository at this point in the history
글쓴이가 ADMIN일경우만 글 작성 가능,
post CRUD 확인, 유저 조회할때 dto로 내려주게 수정.
  • Loading branch information
s13121312 committed Aug 22, 2024
1 parent 207c374 commit 31289d0
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 51 deletions.
11 changes: 6 additions & 5 deletions src/main/java/certis/CertisHomepage/config/WebConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class WebConfig implements WebMvcConfigurer {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/photo/**")
.addResourceLocations("file:photo/");
registry.addResourceHandler("/document/**")
.addResourceLocations("file:document/");
}

//어떤것들을 검증하지않겠다라는걸 추가해줘야하는데.. 일일히 추가해야하므로 많아질수잇음
Expand All @@ -49,14 +51,13 @@ public void addInterceptors(InterceptorRegistry registry) {

/*registry.addInterceptor(authorizationInterceptor)
.excludePathPatterns(DEFAULT_EXCLUDE)
.excludePathPatterns(SWAGGER);
.excludePathPatterns(SWAGGER);*/

//공지사항 게시판 - 관리자만 작성 가능하게
registry.addInterceptor(notiInterceptor)
.addPathPatterns("/noti/*")
.excludePathPatterns("/noti/{id}")
.excludePathPatterns("/noti/all");
*/
.addPathPatterns("/NOTI/write", "/noti/write", "/project/write", "/PROJECT/write")
.addPathPatterns("/users/**"); //전체 회원보기나 유저 조회도 일단 관리자만 볼 수있게


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import java.util.Optional;

@RequiredArgsConstructor
@Component
Expand All @@ -34,7 +33,6 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
return false;
}else{
//로그인 되어있을때 관리자인지 일반유저인지 확인해서 관리자는 글작성할수있게
//TODO 일단 true리턴
//accesstoken을 받아서 validationToken메소드로 검증하고 id받아옴
var loginid = tokenBusiness.validationAccessToken(accessToken);
UserEntity user = userRepository.findByIdAndStatus(loginid, UserStatus.REGISTERED);
Expand All @@ -44,9 +42,7 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
return true;
}else {
//일반 유저면
request.setAttribute("message", "관리자만 접근할 수있습니다.");
request.setAttribute("path", "/noti"); // 그냥 보드 페이지로 돌아가게
request.getRequestDispatcher("/noti").forward(request, response);//공지사항 페이지(/noti)로 포워딩하여
response.sendError(HttpServletResponse.SC_FORBIDDEN, "관리자만 접근할 수 있습니다.");
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public PostDto update(Long id, PostDto postDto, List<MultipartFile> files) throw
return new IllegalArgumentException("해당 게시물 id 가 없습니다");
});

post.setTitle(postDto.getContent());
post.setTitle(postDto.getTitle());
post.setContent(postDto.getContent());
post.setModifiedAt(LocalDateTime.now());

Expand Down
15 changes: 11 additions & 4 deletions src/main/java/certis/CertisHomepage/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import certis.CertisHomepage.domain.token.controller.model.TokenResponse;
import certis.CertisHomepage.exception.ApiException;
import certis.CertisHomepage.repository.UserRepository;
import certis.CertisHomepage.web.dto.user.UserDto;
import certis.CertisHomepage.web.dto.user.UserLoginRequest;
import certis.CertisHomepage.web.dto.user.UserRegisterRequest;
import certis.CertisHomepage.web.dto.user.UserResponse;
Expand All @@ -22,6 +23,7 @@
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -102,15 +104,20 @@ public UserEntity getUserWithThrow(
);
}

public List<UserEntity> findAll(){
return userRepository.findAll();
public List<UserDto> findAll(){
return userRepository.findAll().stream()
.map(UserDto::convertToDto)
.collect(Collectors.toList());

}


public UserEntity findUser(Long id){
return userRepository.findById(id).orElseThrow(() -> {
public UserDto findUser(Long id){
UserEntity user = userRepository.findById(id).orElseThrow(() -> {
return new IllegalArgumentException("User Id를 찾을 수없습니다.");
});

return UserDto.convertToDto(user);
}

public void checkAccountDuplicate(String account){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Response getPost(
public PageApi<List<PostDto>> searchPosts(
@PathVariable BoardType boardType,
@RequestParam(value = "word", required = false)String word,
@RequestParam(value = "crieria", required = false, defaultValue = "TITLE")SearchCr criteria,
@RequestParam(value = "criteria", required = false, defaultValue = "TITLE")SearchCr criteria,
@PageableDefault(page = 0, size = 10)Pageable pageable
){
return postService.searchPosts(pageable,boardType,criteria,word);
Expand Down

This file was deleted.

This file was deleted.

50 changes: 50 additions & 0 deletions src/main/java/certis/CertisHomepage/web/dto/user/UserDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package certis.CertisHomepage.web.dto.user;

import certis.CertisHomepage.domain.UserEntity;
import certis.CertisHomepage.service.UserService;
import certis.CertisHomepage.web.dto.post.PostDto;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Getter
@Builder
public class UserDto {

private Long id;
private String account;
private String username;
private String nickname;
private String email;
private LocalDateTime registeredAt;
private LocalDateTime modifiedAt;
private Long exp;
private Long level;
private String roleType;
private String status;
private List<PostDto> posts;


public static UserDto convertToDto(UserEntity user) {
return UserDto.builder()
.id(user.getId())
.account(user.getAccount())
.username(user.getUsername())
.nickname(user.getNickname())
.email(user.getEmail())
.registeredAt(user.getRegisteredAt())
.modifiedAt(user.getModifiedAt())
.exp(user.getExp())
.level(user.getLevel())
.roleType(user.getRoleType().toString()) // Enum to String
.status(user.getStatus().toString()) // Enum to String
.posts(user.getPosts().stream()
.map(PostDto::toDto)
.collect(Collectors.toList()))
.build();

}
}

0 comments on commit 31289d0

Please sign in to comment.