Skip to content

Commit

Permalink
Merge pull request #33 from yooonwodyd/weekly
Browse files Browse the repository at this point in the history
유저관련 CRUD, 테스트 작성 #24 #22
  • Loading branch information
yooonwodyd authored Oct 4, 2024
2 parents efdac3a + 21cf8a0 commit 0df7e23
Show file tree
Hide file tree
Showing 33 changed files with 792 additions and 18 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
implementation 'org.springframework.boot:spring-boot-starter-security'

// Lombok
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/helpmeCookies/global/config/JpaConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.helpmeCookies.global.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

@Configuration
@EnableJpaAuditing
public class JpaConfig {
}
17 changes: 11 additions & 6 deletions src/main/java/com/helpmeCookies/global/jwt/JwtUser.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.helpmeCookies.global.jwt;

import java.util.Collection;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import lombok.Builder;
Expand All @@ -12,6 +14,8 @@
@Getter
public class JwtUser implements UserDetails {
private Long id;
private String username;
private Collection<? extends GrantedAuthority> authorities;

public static JwtUser of(Long id) {
return JwtUser.builder()
Expand All @@ -20,8 +24,9 @@ public static JwtUser of(Long id) {
}

@Override
// 임시 기본 권한을 USER로 설정
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
return List.of(new SimpleGrantedAuthority("ROLE_" + "USER"));
}

@Override
Expand All @@ -31,27 +36,27 @@ public String getPassword() {

@Override
public String getUsername() {
return null;
return this.username;
}

@Override
public boolean isAccountNonExpired() {
return false;
return true;
}

@Override
public boolean isAccountNonLocked() {
return false;
return true;
}

@Override
public boolean isCredentialsNonExpired() {
return false;
return true;
}

@Override
public boolean isEnabled() {
return false;
return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.helpmeCookies.user.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.helpmeCookies.global.jwt.JwtProvider;
import com.helpmeCookies.global.jwt.JwtUser;
import com.helpmeCookies.user.dto.request.BusinessArtistReq;
import com.helpmeCookies.user.dto.request.StudentArtistReq;
import com.helpmeCookies.user.dto.response.BusinessArtistRes;
import com.helpmeCookies.user.dto.response.StudentArtistRes;
import com.helpmeCookies.user.service.ArtistService;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class ArtistController {
private final ArtistService artistService;
private final JwtProvider jwtProvider;

@PostMapping("/v1/artists/students")
public ResponseEntity<StudentArtistRes> registerStudents(
@RequestBody StudentArtistReq studentArtistReq,
@AuthenticationPrincipal JwtUser jwtUser
) {
StudentArtistRes response = StudentArtistRes.from(artistService.registerStudentsArtist(studentArtistReq, jwtUser.getId()));
return ResponseEntity.ok(response);
}

@PostMapping("/v1/artists/bussinesses")
public ResponseEntity<BusinessArtistRes> registerbussinsess(
@RequestBody BusinessArtistReq businessArtistReq,
@AuthenticationPrincipal JwtUser jwtUser
) {
BusinessArtistRes response = BusinessArtistRes.from(artistService.registerBusinessArtist(businessArtistReq, jwtUser.getId()));
return ResponseEntity.ok(response);
}

// 작가 목록 조회(페이징)
// TODO: 6주차 회의 이후 추가
@GetMapping("/v1/artists")
public String getArtists() {
return "ok";
}

// 작가 프로필 조회
// TODO: 6주차 회의 이후 추가
@GetMapping("/v1/artists/{userId}")
public String getArtist() {
return "ok";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@ public JwtToken signup() {
.birthdate("1999-01-01")
.address("서울시 강남구")
.phone("010-1234-5678")
.hashTags(List.of(HashTag.autumn, HashTag.winter))
.hashTags(List.of(HashTag.DREAMLIKE, HashTag.VIBRANCE))
.build();
userRepository.save(user);
return jwtProvider.createToken(JwtUser.of(user.getId()));
}

// 임시 로그인 url. 로그인한 유저의 정보의 일부를 반환한다.
@GetMapping("/test/login")
@GetMapping("/login")
public String login(@AuthenticationPrincipal JwtUser jwtUser) {
User user = userRepository.findById(jwtUser.getId()).get();
User user = userRepository.findById(jwtUser.getId()).orElseThrow();
return user.getEmail();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.helpmeCookies.user.controller;

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;

import com.helpmeCookies.global.jwt.JwtUser;
import com.helpmeCookies.user.dto.UserDto;
import com.helpmeCookies.user.service.UserService;

import lombok.RequiredArgsConstructor;

@Controller
@RequiredArgsConstructor
public class UserController {
private final UserService userService;

// TODO: 이후 추가되는 요구사항에 따라 별도의 UserRes로 반환
// TODO: 구매 판매 내역에 대한 추가 정보 필요. Product 도메인 완성이후 추가
@GetMapping("/v1/users/{userId}")
public UserDto getUsers(
@PathVariable Long userId
) {
return userService.getUser(userId);
}

//유저 팔로우 목록 조회
@GetMapping("/v1/users/{userId}/follows")
public String getFollows() {

return "ok";
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/helpmeCookies/user/dto/ArtistInfoDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.helpmeCookies.user.dto;

public record ArtistInfoDto(
Long totalFollowers,
Long totalLikes,
String about
) {
public static ArtistInfoDto of(Long totalFollowers, Long totalLikes, String about) {
return new ArtistInfoDto(totalFollowers, totalLikes, about);
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/helpmeCookies/user/dto/BusinessArtistDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.helpmeCookies.user.dto;

import com.helpmeCookies.user.entity.BusinessArtist;

public record BusinessArtistDto(
Long id,
Long userId,
String businessNumber,
String openDate,
String headName,
ArtistInfoDto artistInfo
) {
public static BusinessArtistDto fromEntity(BusinessArtist businessArtist){
return new BusinessArtistDto(
businessArtist.getId(),
businessArtist.getUserId(),
businessArtist.getBusinessNumber(),
businessArtist.getOpenDate(),
businessArtist.getHeadName(),
ArtistInfoDto.of(
businessArtist.getArtistInfo().getTotalFollowers(),
businessArtist.getArtistInfo().getTotalLikes(),
businessArtist.getArtistInfo().getAbout()
)
);
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/helpmeCookies/user/dto/StudentArtistDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.helpmeCookies.user.dto;

import com.helpmeCookies.user.entity.StudentArtist;

public record StudentArtistDto(
Long id,
Long userId,
String schoolEmail,
String schoolName,
String major,
ArtistInfoDto artistInfo
) {
public static StudentArtistDto fromEntity(StudentArtist studentArtist) {
return new StudentArtistDto(
studentArtist.getId(),
studentArtist.getUserId(),
studentArtist.getSchoolEmail(),
studentArtist.getSchoolName(),
studentArtist.getMajor(),
ArtistInfoDto.of(
studentArtist.getArtistInfo().getTotalFollowers(),
studentArtist.getArtistInfo().getTotalLikes(),
studentArtist.getArtistInfo().getAbout()
)
);
}
}
31 changes: 31 additions & 0 deletions src/main/java/com/helpmeCookies/user/dto/UserDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.helpmeCookies.user.dto;

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

import com.helpmeCookies.product.entity.HashTag;
import com.helpmeCookies.user.entity.User;

public record UserDto(
Long id,
String nickname,
String email,
String birthdate,
String phone,
String address,
LocalDateTime createdAt,
List<HashTag> hashTags
) {
public static UserDto fromEntity(User user) {
return new UserDto(
user.getId(),
user.getNickname(),
user.getEmail(),
user.getBirthdate(),
user.getPhone(),
user.getAddress(),
user.getCreatedAt(),
user.getHashTags()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.helpmeCookies.user.dto.request;

import com.helpmeCookies.user.dto.ArtistInfoDto;

public record BusinessArtistReq(
String businessNumber,
String openDate,
String headName
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.helpmeCookies.user.dto.request;

import com.helpmeCookies.user.dto.StudentArtistDto;

public record StudentArtistReq(
String schoolEmail,
String schoolName,
String major
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.helpmeCookies.user.dto.response;

import com.helpmeCookies.user.dto.ArtistInfoDto;

public record ArtistInfoRes(
Long totalFollowers,
Long totalLikes,
String about
) {
public static ArtistInfoRes from(ArtistInfoDto artistInfoDto) {
return new ArtistInfoRes(
artistInfoDto.totalFollowers(),
artistInfoDto.totalLikes(),
artistInfoDto.about());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.helpmeCookies.user.dto.response;

import com.helpmeCookies.user.dto.BusinessArtistDto;

public record BusinessArtistRes(
String businessNumber,
String openDate,
String headName,
ArtistInfoRes artistInfo
) {
public static BusinessArtistRes from(BusinessArtistDto businessArtistDto) {
return new BusinessArtistRes(
businessArtistDto.businessNumber(),
businessArtistDto.openDate(),
businessArtistDto.headName(),
ArtistInfoRes.from(businessArtistDto.artistInfo())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.helpmeCookies.user.dto.response;

import com.helpmeCookies.user.dto.StudentArtistDto;

public record StudentArtistRes(
String schoolEmail,
String schoolName,
String major,
ArtistInfoRes artistInfo
) {
public static StudentArtistRes from(StudentArtistDto studentArtistDto) {
return new StudentArtistRes(
studentArtistDto.schoolEmail(),
studentArtistDto.schoolName(),
studentArtistDto.major(),
ArtistInfoRes.from(studentArtistDto.artistInfo())
);
}
}
Loading

0 comments on commit 0df7e23

Please sign in to comment.