Skip to content

Commit

Permalink
feat:change profile
Browse files Browse the repository at this point in the history
  • Loading branch information
SeoYoonJu committed Apr 2, 2024
1 parent 2a71907 commit 6ad57c5
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/modules/JWTLogIn.JWT.main.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions JWT/src/main/java/JWTLogIn/JWT/user/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

import JWTLogIn.JWT.user.dto.LogInDTO;
import JWTLogIn.JWT.user.dto.UserDTO;
import JWTLogIn.JWT.user.dto.UserRequestDTO;
import JWTLogIn.JWT.user.dto.UserResponseDTO;
import JWTLogIn.JWT.user.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Member;

import static org.springframework.data.jpa.domain.AbstractPersistable_.id;

@RestController
@RequestMapping("/tgwing.kr")
@RequiredArgsConstructor
Expand Down Expand Up @@ -59,4 +66,16 @@ public ResponseEntity<Void> deleteId(@PathVariable Long id) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}// 회원 탈퇴



//@RequestMapping(value = "/profile/{id}", method = RequestMethod.GET)
@PutMapping("/profile/{id}")
public ResponseEntity<UserResponseDTO> changeProfile(@PathVariable Long id, @RequestBody @Valid UserRequestDTO request){
UserDTO change = userService.changeUser(id, request);
if(change != null){
return ResponseEntity.ok().build(); //요거가 response다시
}
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}

}
1 change: 1 addition & 0 deletions JWT/src/main/java/JWTLogIn/JWT/user/dto/UserDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ public static UserEntity toUserEntity(UserDTO userDTO) {
.build();
}
}

18 changes: 18 additions & 0 deletions JWT/src/main/java/JWTLogIn/JWT/user/dto/UserRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package JWTLogIn.JWT.user.dto;

import JWTLogIn.JWT.user.entity.Enum.Status;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserRequestDTO {
private String name; // 이름
private Status status; // 재학/휴학 상태
private String semester; // 학년, 학기(3글자로 설정)
private String phoneNumber;
}
21 changes: 21 additions & 0 deletions JWT/src/main/java/JWTLogIn/JWT/user/dto/UserResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package JWTLogIn.JWT.user.dto;

import JWTLogIn.JWT.user.entity.Enum.Status;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class UserResponseDTO {
private Long id;
private String studentId; // 아이디
private String email; // 이메일
private String name; // 이름
private Status status; // 재학/휴학 상태
private String semester; // 학년, 학기(3글자로 설정)
private String phoneNumber;
}
3 changes: 3 additions & 0 deletions JWT/src/main/java/JWTLogIn/JWT/user/entity/UserEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import JWTLogIn.JWT.user.entity.Enum.Level;
import JWTLogIn.JWT.user.entity.Enum.Status;
import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.DynamicInsert;
Expand All @@ -17,6 +19,7 @@
@Table(name = "user")
public class UserEntity extends BaseEntity{
@Id
@Schema(hidden = true)
@GeneratedValue(strategy = GenerationType.IDENTITY) // 점직적 증가
private Long id; // 기본 키.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package JWTLogIn.JWT.user.repository;

import JWTLogIn.JWT.user.entity.Enum.Level;
import JWTLogIn.JWT.user.entity.Enum.Status;
import JWTLogIn.JWT.user.entity.UserEntity;
import jakarta.transaction.Transactional;
import org.springframework.data.domain.Page;
Expand All @@ -22,6 +23,7 @@ public interface UserRepository extends JpaRepository<UserEntity, Long> {
@Query("DELETE FROM UserEntity U WHERE U.id = :id")
void deleteUser(@Param("id") Long id);


// @Modifying
// @Transactional
// @Query("UPDATE UserEntity u SET " +
Expand All @@ -35,4 +37,10 @@ public interface UserRepository extends JpaRepository<UserEntity, Long> {
// "WHERE b.boardId = :boardId")

Page<UserEntity> findAll(Pageable pageable);

@Transactional
@Modifying
@Query("UPDATE UserEntity U SET U.name = :name, U.status = :status, U.semester = :semester, U.phoneNumber = :phoneNumber WHERE U.id = :id")
void changeUser(Long id, String name, Status status, String semester, String phoneNumber);

}
27 changes: 27 additions & 0 deletions JWT/src/main/java/JWTLogIn/JWT/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import JWTLogIn.JWT.user.dto.LogInDTO;
import JWTLogIn.JWT.user.dto.UserDTO;
import JWTLogIn.JWT.user.dto.UserRequestDTO;
import JWTLogIn.JWT.user.dto.UserResponseDTO;
import JWTLogIn.JWT.user.entity.Enum.Level;
import JWTLogIn.JWT.user.entity.Enum.Status;
import JWTLogIn.JWT.user.entity.UserEntity;
import JWTLogIn.JWT.user.repository.UserRepository;
import JWTLogIn.JWT.user.security.JwtUtil;
Expand Down Expand Up @@ -72,6 +75,30 @@ public Boolean deleteUser(Long id) {
}
}// 회원 삭제

public UserDTO changeUser(Long id, UserRequestDTO request){
Optional<UserEntity> user = userRepository.findById(id);
// 만약 사용자 정보가 존재한다면 업데이트를 수행
if (user.isPresent()) {
userRepository.changeUser(id, request.getName(), request.getStatus(), request.getSemester(), request.getPhoneNumber());
}
Optional<UserEntity> updatedUserOptional = userRepository.findById(id);

// 업데이트된 사용자 정보가 존재한다면 UserDTO로 변환하여 반환
if (updatedUserOptional.isPresent()) {
UserEntity updatedUser = updatedUserOptional.get();
return UserDTO.builder()
.id(updatedUser.getId())
.name(updatedUser.getName())
.status(updatedUser.getStatus())
.semester(updatedUser.getSemester())
.phoneNumber(updatedUser.getPhoneNumber())
.build();
}
else{
return null;
}
};


public List<UserDTO> findUserAll() {
List<UserEntity> allUser = userRepository.findAll();
Expand Down

0 comments on commit 6ad57c5

Please sign in to comment.