Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#36] Refactor: 사용자가 회원가입 할 때 대학도 저장하도록 한다. #37

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
Expand Down Expand Up @@ -90,6 +91,7 @@ public ResponseEntity<String> reissueToken(@Parameter @CookieValue(name = "Autho
@ApiResponse(responseCode = "204", description = "로그아웃 성공, AccessToken이 필요합니다.",
content = @Content(schema = @Schema(implementation = String.class)))
})
@SecurityRequirement(name = "bearer-key")
@PostMapping("/logout")
public ResponseEntity<String> logout(@Login @Schema(hidden = true) LoginUser loginUser, HttpServletResponse response) {
authService.logout(loginUser);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package com.backend.domain.auth.dto.request;

import com.backend.domain.university.entity.University;
import com.backend.domain.user.entity.GroupType;
import com.backend.domain.user.entity.User;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotBlank;
import org.springframework.security.crypto.password.PasswordEncoder;

public record JoinRequestDto(@NotEmpty String password, @NotEmpty String email, @NotEmpty String type,
@NotEmpty String typeName, @NotEmpty String proofImageUrl) {
public User toEntity(PasswordEncoder passwordEncoder) {
public record JoinRequestDto(@NotBlank String password, @NotBlank String email, @NotBlank String type,
@NotBlank String typeName, @NotBlank String proofImageUrl, @NotBlank Long univId) {
public User toEntity(PasswordEncoder passwordEncoder, University university) {
return User.builder()
.email(email)
.password(passwordEncoder.encode(this.password))
.type(GroupType.create(type))
.typeName(typeName)
.proofImageUrl(proofImageUrl)
.university(university)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import com.backend.domain.auth.dto.LoginUser;
import com.backend.domain.auth.dto.request.JoinRequestDto;
import com.backend.domain.auth.dto.request.LoginRequestDto;
import com.backend.domain.university.entity.University;
import com.backend.domain.university.repository.UniversityRepository;
import com.backend.jwt.service.JwtProvider;
import com.backend.jwt.token.RefreshToken;
import com.backend.jwt.token.Token;
Expand All @@ -23,6 +25,7 @@
public class AuthService {

private final UserRepository userRepository;
private final UniversityRepository universityRepository;
private final JwtProvider jwtProvider;
private final PasswordEncoder passwordEncoder;

Expand Down Expand Up @@ -51,7 +54,9 @@ public void join(JoinRequestDto joinRequestDto) {
throw new BusinessException(ErrorCode.ALREADY_EXIST_EMAIL);
}
// if (userRepository.existsByNickname(joinRequestDto.getNickname())) {}
userRepository.save(joinRequestDto.toEntity(passwordEncoder));
University university = universityRepository.findById(joinRequestDto.univId())
.orElseThrow(() -> new BusinessException(ErrorCode.UNIV_NOT_FOUND));
userRepository.save(joinRequestDto.toEntity(passwordEncoder, university));
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

public interface ContractRepository extends JpaRepository<Contract, Long> {

@Query("SELECT c FROM Contract c JOIN c.university u JOIN u.user user " +
@Query("SELECT c FROM Contract c JOIN c.university u JOIN u.users user " +
"WHERE user.id = :userId AND c.store.storeId = :storeId")
Optional<Contract> findContractByUserIdAndStoreId(@Param("userId") Long userId, @Param("storeId") Long storeId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand All @@ -20,6 +21,7 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/popups")
@SecurityRequirement(name = "bearer-key")
public class PopupController {

private final PopupService popupService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import java.util.List;

public interface PopupRepository extends JpaRepository<Popup, Long> {
@Query("SELECT p FROM Popup p JOIN p.contract c JOIN c.university u JOIN u.user user WHERE user.id = :userId")
@Query("SELECT p FROM Popup p JOIN p.contract c JOIN c.university u JOIN u.users user WHERE user.id = :userId")
Page<Popup> findPopupsPageByUserId(@Param("userId") Long userId, Pageable pageable);

@Query("SELECT p FROM Popup p JOIN p.contract c JOIN c.university u JOIN u.user user WHERE user.id = :userId")
@Query("SELECT p FROM Popup p JOIN p.contract c JOIN c.university u JOIN u.users user WHERE user.id = :userId")
List<Popup> findPopupsByUserId(@Param("userId") Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -38,6 +39,7 @@ public ResponseEntity<String> uploadImage(@RequestPart MultipartFile file) {
@ApiResponse(responseCode = "401", description = "토큰이 올바르지 않을 때 예외가 발생합니다.",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))
})
@SecurityRequirement(name = "bearer-key")
@DeleteMapping("/delete")
public ResponseEntity<String> deleteImage(@Login LoginUser loginUser, @RequestPart String fileName) {
s3Service.deleteImage(loginUser, fileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.backend.domain.university.entity.University;

public record getUnivResponseDto(String univName, String email) {
public record getUnivResponseDto(Long universityId, String univName, String email) {
public static getUnivResponseDto from(University university) {
return new getUnivResponseDto(university.getName(), university.getEmail());
return new getUnivResponseDto(university.getUniversityId(), university.getName(), university.getEmail());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public class University {
@OneToMany(mappedBy = "university")
private List<Contract> contracts = new ArrayList<>();

@OneToOne(mappedBy = "university")
private User user;
@OneToMany(mappedBy = "university")
private List<User> users = new ArrayList<>();
}
5 changes: 3 additions & 2 deletions src/main/java/com/backend/domain/user/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,22 @@ public class User extends BaseEntity {

private String proofImageUrl;

@OneToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "university_id")
private University university;

@OneToMany(mappedBy = "user")
private List<Pick> picks = new ArrayList<>();

@Builder
public User(String email, String password, GroupType type, String typeName, String refreshToken, String proofImageUrl) {
public User(String email, String password, GroupType type, String typeName, String refreshToken, String proofImageUrl, University university) {
this.email = email;
this.password = password;
this.type = type;
this.typeName = typeName;
this.refreshToken = refreshToken;
this.proofImageUrl = proofImageUrl;
this.university = university;
}

public void updateRefreshToken(String refreshToken) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/backend/error/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public enum ErrorCode {
STORE_NOT_FOUND(NOT_FOUND, "해당 가게를 찾을 수 없습니다."),
INVALID_DATE(BAD_REQUEST, "잘못된 날짜 형식입니다."),
POPUP_NOT_FOUND(NOT_FOUND, "팝업이 존재하지 않습니다."),
INVALID_POPUP(BAD_REQUEST, "잘못된 팝업 요청입니다.");
INVALID_POPUP(BAD_REQUEST, "잘못된 팝업 요청입니다."),
UNIV_NOT_FOUND(BAD_REQUEST, "해당 대학을 찾을 수 없습니다.");

private final int code;
private final String message;
Expand Down
Loading