Skip to content

Commit

Permalink
Merge pull request #124 from BudgetBuddiesTeam/dev
Browse files Browse the repository at this point in the history
[v5] UserApi 추가 및 URL 변경
  • Loading branch information
wnd01jun authored Aug 16, 2024
2 parents 440b935 + deaa840 commit 782b53a
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.bbteam.budgetbuddies.domain.user.controller;

import com.bbteam.budgetbuddies.apiPayload.ApiResponse;
import com.bbteam.budgetbuddies.domain.consumptiongoal.dto.UserConsumptionGoalResponse;
import com.bbteam.budgetbuddies.domain.user.dto.UserDto;
import com.bbteam.budgetbuddies.domain.user.validation.ExistUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

public interface UserApi {
@Operation(summary = "[Admin] 기본 카테고리 생성 API ", description = "기본 카테고리가 없는 사용자에게 기본 카테고리를 생성합니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
})
@Parameters({
@Parameter(name = "userId", description = "기본 카테고리를 만들 사용자 ID 입니다. pathVariable",
in = ParameterIn.PATH),
})
ResponseEntity<List<UserConsumptionGoalResponse>> createConsumptionGoals(@PathVariable Long userId);

@Operation(summary = "[Admin] User 추가 API ", description = "서비스를 이용할 사용자를 추가합니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
})
@Parameters({
@Parameter(name = "phoneNumber", description = "휴대폰 번호. requestBody"),
@Parameter(name = "name", description = "사용자 이름. requestBody"),
@Parameter(name = "age", description = "사용자 나이. requestBody"),
@Parameter(name = "gender", description = "사용자 성별 / MALE, FEMALE, NONE requestBody"),
@Parameter(name = "email", description = "메일 주소. requestBody"),
@Parameter(name = "photoUrl", description = "사진 Url. 아마 사용 x requestBody"),
@Parameter(name = "consumptionPattern", description = "소비 패턴. 아마 사용 x requestBody")
})
ApiResponse<UserDto.ResponseUserDto> registerUser(@RequestBody UserDto.RegisterUserDto dto);
@Operation(summary = "[Admin] User 찾기 ", description = "ID를 기반으로 해당 사용자를 찾습니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
})
@Parameters({
@Parameter(name = "userId", description = "찾고자하는 사용자 id 입니다. pathVariable",
in = ParameterIn.PATH)
})
ApiResponse<UserDto.ResponseUserDto> findOne(@PathVariable("userId") @ExistUser Long userId);

@Operation(summary = "[Admin] 모든 User정보 찾기 ", description = "DB에 존재하는 모든 user 정보를 가져옵니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
})
ApiResponse<List<UserDto.ResponseUserDto>> findAll();

@Operation(summary = "[Admin] User 데이터 변경 ", description = "사용자의 email, name을 변경합니다.")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 성공"),
})
@Parameters({
@Parameter(name = "userId", description = "변경하고자하는 사용자 id 입니다. pathVariable",
in = ParameterIn.PATH),
@Parameter(name = "email", description = "변경할 사용자 email 주소입니다. requestBody"),
@Parameter(name = "name", description = "변경할 사용자 name 입니다. requestBody")

})
ApiResponse<UserDto.ResponseUserDto> changeOne(@PathVariable("userId") @ExistUser Long userId,
@RequestBody UserDto.ModifyUserDto dto);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@RequestMapping("/users")
@RequiredArgsConstructor
@Validated
public class UserController {
public class UserController implements UserApi{

private final UserService userService;

Expand All @@ -31,14 +31,20 @@ public ApiResponse<UserDto.ResponseUserDto> registerUser(@RequestBody UserDto.Re
return ApiResponse.onSuccess(userService.saveUser(dto));
}

@GetMapping("/{userId}/find")
@GetMapping("/find/{userid}")
public ApiResponse<UserDto.ResponseUserDto> findOne(@PathVariable("userId") @ExistUser Long userId) {
return ApiResponse.onSuccess(userService.findUser(userId));
}

@PutMapping("/{userId}/change")
@Override
@GetMapping("/findAll")
public ApiResponse<List<UserDto.ResponseUserDto>> findAll() {
return ApiResponse.onSuccess(userService.findAll());
}

@PutMapping("/modify/{userId}")
public ApiResponse<UserDto.ResponseUserDto> changeOne(@PathVariable("userId") @ExistUser Long userId,
@RequestParam("email") String email, @RequestParam("name") String name) {
return ApiResponse.onSuccess(userService.changeUser(userId, email, name));
@RequestBody UserDto.ModifyUserDto dto) {
return ApiResponse.onSuccess(userService.modifyUser(userId, dto));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bbteam.budgetbuddies.domain.user.dto;

import com.bbteam.budgetbuddies.enums.Gender;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.Min;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -35,6 +36,14 @@ public static class ResponseUserDto {
private Gender gender;
private String photoUrl;
private String consumptionPattern;
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm", timezone = "Asia/Seoul")
private LocalDateTime lastLoginAt;
}

@Getter
@Builder
public static class ModifyUserDto {
private String email;
private String name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ public interface UserService {

UserDto.ResponseUserDto findUser(Long userId);

UserDto.ResponseUserDto changeUser(Long userId, String email, String name);
UserDto.ResponseUserDto modifyUser(Long userId, UserDto.ModifyUserDto dto);

List<UserDto.ResponseUserDto> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,17 @@ public UserDto.ResponseUserDto findUser(Long userId) {

@Override
@Transactional
public UserDto.ResponseUserDto changeUser(Long userId, String email, String name) {
public UserDto.ResponseUserDto modifyUser(Long userId, UserDto.ModifyUserDto dto) {
User user = userRepository.findById(userId).orElseThrow(() -> new NoSuchElementException("No such user"));
user.changeUserDate(email, name);
user.changeUserDate(dto.getEmail(), dto.getName());
return UserConverter.toDto(user);
}

@Override
public List<UserDto.ResponseUserDto> findAll() {
List<User> users = userRepository.findAll();
return users.stream()
.map(UserConverter::toDto)
.toList();
}
}

0 comments on commit 782b53a

Please sign in to comment.