Skip to content

Commit

Permalink
Merge pull request #7 from team-nabi/NABI-102--yejin--refactor--oauth…
Browse files Browse the repository at this point in the history
…2-google-kakao

Nabi 102  yejin  refactor  oauth2 google kakao
  • Loading branch information
born-A authored Nov 2, 2023
2 parents 0635adc + 4558c00 commit 9b50754
Show file tree
Hide file tree
Showing 31 changed files with 374 additions and 435 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/prgrms/nabimarketbe/domain/user/Role.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.prgrms.nabimarketbe.domain.user;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@Getter
@RequiredArgsConstructor
public enum Role {
GUEST("ROLE_GUEST"),

USER("ROLE_USER");

private final String key;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import lombok.RequiredArgsConstructor;

import org.prgrms.nabimarketbe.domain.user.dto.UserRequestDto;
import org.prgrms.nabimarketbe.domain.user.dto.UserResponseDto;
import org.prgrms.nabimarketbe.domain.user.dto.request.UserRequestDTO;
import org.prgrms.nabimarketbe.domain.user.dto.response.UserResponseDTO;
import org.prgrms.nabimarketbe.domain.user.service.UserService;
import org.prgrms.nabimarketbe.global.util.model.CommonResult;
import org.prgrms.nabimarketbe.global.util.model.ListResult;
Expand All @@ -14,40 +14,40 @@

@RequiredArgsConstructor
@RestController
@RequestMapping("/v1")
@RequestMapping("/api/v1/users")
public class UserController {
private final UserService userService;

private final ResponseFactory responseFactory;

@GetMapping("/user/id/{userId}")
public SingleResult<UserResponseDto> findUserById(@PathVariable Long userId) {
@GetMapping("/{userId}")
public SingleResult<UserResponseDTO> findUserById(@PathVariable Long userId) {
return responseFactory.getSingleResult(userService.findById(userId));
}

@GetMapping("/user/nickname/{nickname}")
public SingleResult<UserResponseDto> findUserByNickName(@PathVariable String nickname) {
return responseFactory.getSingleResult(userService.findByNickName(nickname));
@GetMapping("/{nickname}")
public SingleResult<UserResponseDTO> findUserByNickName(@PathVariable String nickname) {
return responseFactory.getSingleResult(userService.findByNickname(nickname));
}

@GetMapping("/users")
public ListResult<UserResponseDto> findAllUser() {
@GetMapping
public ListResult<UserResponseDTO> findAllUser() {
return responseFactory.getListResult(userService.findAllUser());
}

@PutMapping("/user")
@PutMapping
public SingleResult<Long> update (
@RequestParam Long userId,
@RequestParam String nickname
) {
UserRequestDto userRequestDto = UserRequestDto.builder()
UserRequestDTO userRequestDTO = UserRequestDTO.builder()
.nickName(nickname)
.build();

return responseFactory.getSingleResult(userService.update(userId, userRequestDto));
return responseFactory.getSingleResult(userService.update(userId, userRequestDTO));
}

@DeleteMapping("/user/{userId}")
@DeleteMapping("/{userId}")
public CommonResult delete(@PathVariable Long userId) {
userService.delete(userId);

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package org.prgrms.nabimarketbe.domain.user.dto;
package org.prgrms.nabimarketbe.domain.user.dto.request;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.prgrms.nabimarketbe.domain.user.entity.User;

@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class UserRequestDto {

public class UserRequestDTO {
private String email;
private String name;

private String nickName;

public User toEntity() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.prgrms.nabimarketbe.domain.user.dto.request;

import lombok.Builder;
import org.prgrms.nabimarketbe.domain.user.Role;
import org.prgrms.nabimarketbe.domain.user.entity.User;

@Builder
public record UserSignInRequestDTO(
String accountId,
String nickname,
String provider
) {
public User toEntity() {
return User.builder()
.accountId(accountId)
.nickname(nickname)
.provider(provider)
.role(Role.USER)
.build();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.prgrms.nabimarketbe.domain.user.dto.response;

import lombok.Getter;
import org.prgrms.nabimarketbe.domain.user.Role;
import org.prgrms.nabimarketbe.domain.user.entity.User;

import java.time.LocalDateTime;

@Getter
public class UserResponseDTO {
private final Long userId;

private final String accountId;

private final String nickName;

private final Role role;

private final LocalDateTime createdDate;

private final LocalDateTime modifiedDate;

public UserResponseDTO(User user) {
this.userId = user.getUserId();
this.accountId = user.getAccountId();
this.nickName = user.getNickname();
this.role = user.getRole();
this.createdDate = user.getCreatedDate();
this.modifiedDate = user.getModifiedDate();
}
}

This file was deleted.

This file was deleted.

This file was deleted.

40 changes: 25 additions & 15 deletions src/main/java/org/prgrms/nabimarketbe/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import org.prgrms.nabimarketbe.global.BaseEntity;
import org.prgrms.nabimarketbe.domain.user.Role;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Collections;

@Builder
@Entity
Expand All @@ -27,63 +27,73 @@ public class User extends BaseEntity implements UserDetails {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long userId;

@Column(nullable = false, unique = true, length = 30)
private String accountId;

@Column(name = "nick_name", nullable = false, length = 20)
private String nickname;

@Column(name = "user_email")
private String email;

@Column(name = "user_image_url")
private String image_url;
private String imageUrl;

@Column(length = 100)
private String provider;

@ElementCollection(fetch = FetchType.EAGER)
@Builder.Default
private List<String> roles = new ArrayList<>();
@Enumerated(EnumType.STRING)
@Column(name = "user_role")
private Role role;

private User(
String accountId,
String nickname,
String profileImageUrl,
Role role
) {
}

public void updateNickname(String nickname) {
this.nickname = nickname;
}

@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return this.roles
.stream().map(SimpleGrantedAuthority::new)
.collect(Collectors.toList());
return Collections.singletonList(new SimpleGrantedAuthority(this.role.toString()));
}

@Override
public String getPassword() {
return null;
}

@Override
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Override
public String getUsername() {
return String.valueOf(this.userId);
}

@Override
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Override
public boolean isAccountNonExpired() {
return true;
}

@Override
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Override
public boolean isAccountNonLocked() {
return true;
}

@Override
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Override
public boolean isCredentialsNonExpired() {
return true;
}

@Override
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
@Override
public boolean isEnabled() {
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
import java.util.Optional;

@Repository
public interface UserJpaRepo extends JpaRepository<User, Long> {
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByNickname(String name);

Optional<User> findByEmail(String email);
Optional<User> findByNicknameAndProvider(String name, String provider);

Optional<User> findByAccountIdAndProvider(String accountId, String provider);

Optional<User> findByAccountId(String oauthId);
}
Loading

0 comments on commit 9b50754

Please sign in to comment.