Skip to content

Commit

Permalink
#89 DAILYLIFE2-41 by dm
Browse files Browse the repository at this point in the history
  • Loading branch information
shihhhdm310 committed Aug 14, 2022
1 parent 814375a commit f5f3a9a
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ResponseEntity<List<Follow>> getFollower() {
@PostMapping("/getFollow")
public ApplicationResponse<List<Follow>> getFollow() {
List<Follow> follow = followingService.getFollow();
return ApplicationResponse.create("팔로워를 불러왔습니다" , HttpStatus.CREATED , follow);
return ApplicationResponse.create("팔로워를 불러왔습니다" , 200 , follow);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.dailylife.domain.user.dto.UserJoinRequest;
import com.dailylife.domain.user.dto.UserLoginRequest;
import com.dailylife.domain.user.dto.UserModifyRequest;
import com.dailylife.domain.user.dto.UserPagination;
import com.dailylife.domain.user.entity.User;
import com.dailylife.domain.user.service.UserService;
import com.dailylife.global.dto.ApplicationResponse;
Expand All @@ -19,6 +20,7 @@

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

@RestController
@Slf4j
Expand All @@ -32,13 +34,13 @@ public class UserController {
@ApiOperation(value = "회원가입", notes = "회원가입을 합니다.")
@PostMapping("/join")
public ApplicationResponse<User> saveUser(@Valid @RequestBody UserJoinRequest userJoinRequest) {
return ApplicationResponse.create("회원가입이 완료되었습니다." , HttpStatus.CREATED , userService.join(userJoinRequest));
return ApplicationResponse.create("회원가입이 완료되었습니다." , 200 , userService.join(userJoinRequest));
}

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

@ApiOperation(value = "회원탈퇴" , notes = "회원을 탈퇴합니다.")
Expand All @@ -51,9 +53,18 @@ public ApplicationResponse<User> quitUser(@Valid @PathVariable Long userNum) {
@ApiOperation(value = "내정보 보기" , notes = "내정보를 자세하게 봅니다")
@PostMapping("/details/{userNum}")
public ApplicationResponse<User> detailsUser(@Valid @PathVariable Long userNum) {
return ApplicationResponse.create("유저의 대한 정보입니다" , HttpStatus.OK , userService.getDetails(userNum));
return ApplicationResponse.create("유저의 대한 정보입니다" , 200 , userService.getDetails(userNum));
}

@ApiOperation(value = "유저찾기" , notes = "특정 유저를 찾고 그것과 유사한 name을 가진 유저를 전부 찾습니다.")
@PostMapping("/find")
public ApplicationResponse<List<User>> findUser(@Valid UserPagination userPagination) {
return ApplicationResponse.create("유저에 대한 정보입니다." , 250, userService.findUser(userPagination));
}




@PostMapping("/post")
public String PostTest(@RequestBody String msg) {
return "post success!!!"+msg;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/dailylife/domain/user/dto/UserPagination.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.dailylife.domain.user.dto;

import lombok.Data;

@Data
public class UserPagination {

int pg = 1; // 현재 페이지
int sz = 15; // 페이지 당 레코드 수
String userNickName;
int recordCount; // 전체 레코드 수

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

import com.dailylife.domain.board.dto.BoardPagination;
import com.dailylife.domain.board.entity.Board;
import com.dailylife.domain.user.dto.UserPagination;
import com.dailylife.domain.user.entity.User;
import com.dailylife.domain.user.exception.NotFoundUserException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface UserPaginationRepository extends JpaRepository<User, Long> {

public default List<User> findAll(UserPagination pagination) {
if(pagination.getUserNickName()!=null){
return findUserName(pagination);
}
throw new NotFoundUserException();
}

Page<User> findByUserNameContaining(String keyword, Pageable pageable);

public default List<User> findUserName(UserPagination pagination) {
Page<User> page = this.findByUserNameContaining(pagination.getUserNickName(),PageRequest.of(pagination.getPg() - 1, pagination.getSz(),
Sort.Direction.DESC, "userNum"));
pagination.setRecordCount((int)page.getTotalElements());
return page.getContent();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
import com.dailylife.domain.user.dto.UserJoinRequest;
import com.dailylife.domain.user.dto.UserLoginRequest;
import com.dailylife.domain.user.dto.UserModifyRequest;
import com.dailylife.domain.user.dto.UserPagination;
import com.dailylife.domain.user.entity.User;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

public interface UserService {

Expand All @@ -21,4 +23,8 @@ public interface UserService {
void quit(Long userNum);

User getDetails(Long userNum);

List<User> findUser(UserPagination userPagination);


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
import com.dailylife.domain.user.dto.UserJoinRequest;
import com.dailylife.domain.user.dto.UserLoginRequest;
import com.dailylife.domain.user.dto.UserModifyRequest;
import com.dailylife.domain.user.dto.UserPagination;
import com.dailylife.domain.user.entity.User;
import com.dailylife.domain.user.exception.NotFoundUserException;
import com.dailylife.domain.user.exception.UserException;
import com.dailylife.domain.user.repository.UserPaginationRepository;
import com.dailylife.domain.user.repository.UserRepository;
import com.dailylife.global.fileUpload.SingleUpload;
import com.dailylife.global.jwt.service.JwtService;
Expand All @@ -17,13 +19,16 @@
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

private final UserRepository userRepository;

private final UserPaginationRepository userPaginationRepository;
private final SpringSecurityConfig springSecurity;
private final SingleUpload singleUpload;
private final JwtService jwtService;
Expand Down Expand Up @@ -91,5 +96,15 @@ public User getDetails(Long userNum) {

return userRepository.findByUserNum(userNum).orElseThrow(NotFoundUserException::new);
}

@Override
public List<User> findUser(UserPagination userPagination) {
List<User> user = userPaginationRepository.findAll(userPagination);
if (user.isEmpty()) throw new NotFoundUserException();
return user;

}


}

10 changes: 2 additions & 8 deletions src/main/java/com/dailylife/global/dto/ApplicationResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,26 @@
public class ApplicationResponse<T> {

private String message;
private boolean success;
private int httpCode;
private HttpStatus httpStatus;
private T data;
private LocalDateTime localDateTime;

public static <T> ApplicationResponse<T> create(String message, HttpStatus httpStatus,
public static <T> ApplicationResponse<T> create(String message, int httpCode,
T data){
return (ApplicationResponse<T>)
ApplicationResponse.builder()
.httpCode(httpStatus.value())
.httpCode(httpCode)
.localDateTime(LocalDateTime.now())
.message(message)
.httpStatus(httpStatus)
.data(data)
.success(true)
.build();
}

public static <T> ApplicationResponse<T> ok(){
return (ApplicationResponse<T>) ApplicationResponse.builder()
.data(null)
.success(true)
.localDateTime(LocalDateTime.now())
.message("성공")
.httpStatus(HttpStatus.OK)
.build();
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/dailylife/global/dto/ErrorResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public class ErrorResponse {

private int errorCode;
private String message;
private String localDateTime;


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

import lombok.Getter;

import java.time.LocalDateTime;

@Getter
public abstract class ApplicationException extends RuntimeException{

private final int errorCode;
private String localDateTime;

protected ApplicationException(int errorCode , String errorMessage) {
super(errorMessage);
this.errorCode = errorCode;
this.localDateTime = LocalDateTime.now().toString();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ApplicationExceptionHandler {

@ExceptionHandler(ApplicationException.class)
public ResponseEntity<ErrorResponse> globalRuntimeException(ApplicationException e) {
ErrorResponse errorResponse = new ErrorResponse(e.getErrorCode(), e.getMessage());
ErrorResponse errorResponse = new ErrorResponse(e.getErrorCode(), e.getMessage() , e.getLocalDateTime());
return ResponseEntity.ok(errorResponse);
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/dailylife/global/init/InitDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,19 @@ public void initDB() throws IOException {
UserJoinRequest userReq1 = UserJoinRequest.builder()
.userId("dl1")
.userPassword("dl")
.userName("dailyLife")
.userName("dailyLife1")
.userEmail("[email protected]").userJoinDate(LocalDateTime.now()).build();

UserJoinRequest userReq2 = UserJoinRequest.builder()
.userId("dl2")
.userPassword("dl")
.userName("dailyLife")
.userName("dailyLife2")
.userEmail("[email protected]").userJoinDate(LocalDateTime.now()).build();

UserJoinRequest userReq3 = UserJoinRequest.builder()
.userId("dl3")
.userPassword("dl")
.userName("dailyLife")
.userName("dailyLife3")
.userEmail("[email protected]").userJoinDate(LocalDateTime.now()).build();

userService.join(userReq1);
Expand Down

0 comments on commit f5f3a9a

Please sign in to comment.