Skip to content

Commit

Permalink
Merge pull request #247 from team9502/feature/user
Browse files Browse the repository at this point in the history
feat: ๊ตฌ์ง์ž ํƒˆํ‡ด
  • Loading branch information
daeundada authored Jun 30, 2024
2 parents 1ea4b2a + 58c3dfc commit a41dbcc
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public ResponseEntity<GlobalApiResponse<Void>> updateUserProfile(
@ApiResponses({
@ApiResponse(responseCode = "200", description = "๊ธฐ์—… ์‚ฌ์šฉ์ž ์ „์ฒด ์กฐํšŒ ์„ฑ๊ณต",
content = @Content(mediaType = "application/json",
examples = @ExampleObject(value = "{ \"message\": \"๊ธฐ์—… ์‚ฌ์šฉ์ž ์ „์ฒด ์กฐํšŒ ์„ฑ๊ณต\", \"data\": { \"cpUsers\": [{ \"cpUserId\": 1, \"cpName\": \"๊ณ ์–‘์ดํƒ•ํ›„๋ฃจ\", \"reviewCount\": 3, \"averageRating\": 3.0 }], \"totalCpUserCount\": 1, \"currentPage\": 0, \"totalPages\": 1 } }"))),
examples = @ExampleObject(value = "{ \"message\": \"๊ธฐ์—… ์‚ฌ์šฉ์ž ์ „์ฒด ์กฐํšŒ ์„ฑ๊ณต\", \"data\": { \"cpUsers\": [{ \"cpUserId\": 1, \"cpName\": \"๊ณ ์–‘์ดํƒ•ํ›„๋ฃจ\", \"reviewCount\": 3, \"averageRating\": 3.0 }], \"totalCpUserCount\": 1, \"currentPage\": 0, \"totalPages\": 1, \"viewCount\": null } }"))),
@ApiResponse(responseCode = "400", description = "์ž˜๋ชป๋œ ์š”์ฒญ ๊ฐ’",
content = @Content(mediaType = "application/json",
examples = @ExampleObject(value = "{\"message\": \"์ž˜๋ชป๋œ ์š”์ฒญ ๊ฐ’์ž…๋‹ˆ๋‹ค.\" }"))),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public class CpUserResponseDTO {
private Integer reviewCount;

private Float averageRating;

private Integer viewCount;
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,13 @@ public void usePointsForBanner(Long cpUserId) {
}

private CpUserResponseDTO convertToDTO(CompanyUser companyUser) {

return new CpUserResponseDTO(
companyUser.getCpUserId(),
companyUser.getCpName(),
companyUser.getReviewCount(),
companyUser.getAverageRating()
companyUser.getAverageRating(),
companyUser.getViewCount()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface SavedPointRepository extends JpaRepository<SavedPoint, Long>, S
List<SavedPoint> findByPointPointId(Long pointId);

List<SavedPoint> findSavedPointsWithCursor(Long pointId, Long cursorId, int limit);

void deleteByPointPointId(Long pointId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface UsedPointRepository extends JpaRepository<UsedPoint, Long>, Use
List<UsedPoint> findByPointPointId(Long pointId);

List<UsedPoint> findTop3ByUpTypeOrderByCreatedAtDesc(UpType upType, Pageable pageable);

void deleteByPointPointId(Long pointId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ public List<UsedPointDetailResponseDTO> getPublicLatestBannerUsage() {
.collect(Collectors.toList());
}

@Transactional
public void deletePointData(Point point) {

if (point != null) {
Long pointId = point.getPointId();
savedPointRepository.deleteByPointPointId(pointId);
usedPointRepository.deleteByPointPointId(pointId);
pointRepository.delete(point);
}
}


/*
๊ณตํ†ต ๋กœ์ง์„ ๋ฉ”์„œ๋“œ๋กœ ์ถ”์ถœ
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface BoardScrapsRepository extends JpaRepository<BoardScrap, Long> {
boolean existsByUser_UserIdAndBoard_BoardId(Long userId, Long boardId);

void deleteByUser_UserIdAndBoard_BoardId(Long userId, Long boardId);

void deleteByUser_UserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface CpUserScrapRepository extends JpaRepository<CpUserScrap, Long>
void deleteByUser_UserIdAndCompanyUser_CpUserId(Long userId, Long cpUserId);

Page<CpUserScrap> findByUser_UserId(Long userId, Pageable pageable);

void deleteByUser_UserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public interface JobScrapRepository extends JpaRepository<JobScrap, Long> {
Page<JobScrap> findByUser_UserId(Long userId, Pageable pageable);

void deleteByUser_UserIdAndJobBoard_JobBoardId(Long userId, Long jobBoardId);

void deleteByUser_UserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,14 @@ public CpUserScrapListResponseDTO getAllCpUserScrap(User user, int page, int siz
cpUserScrapPage.getSize()
);
}

@Transactional
public void deleteAllScrapsForUser(User user) {

Long userId = user.getUserId();

boardScrapsRepository.deleteByUser_UserId(userId);
jobScrapRepository.deleteByUser_UserId(userId);
cpUserScrapRepository.deleteByUser_UserId(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import team9502.sinchulgwinong.domain.user.dto.request.UserDeleteRequestDTO;
import team9502.sinchulgwinong.domain.user.dto.request.UserPasswordUpdateRequestDTO;
import team9502.sinchulgwinong.domain.user.dto.request.UserProfileUpdateRequestDTO;
import team9502.sinchulgwinong.domain.user.dto.response.UserProfileResponseDTO;
Expand Down Expand Up @@ -128,4 +129,38 @@ public ResponseEntity<GlobalApiResponse<Void>> updateUserProfile(
SUCCESS_USER_PASSWORD_UPDATED.getMessage(),
null));
}

@DeleteMapping
@Operation(summary = "ํšŒ์› ํƒˆํ‡ด", description = "๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž์˜ ํšŒ์› ํƒˆํ‡ด๋ฅผ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์Šคํฌ๋žฉ, ๊ฒŒ์‹œ๊ธ€, ํฌ์ธํŠธ๊ฐ€ ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "ํšŒ์› ํƒˆํ‡ด ์„ฑ๊ณต",
content = @Content(mediaType = "application/json",
examples = @ExampleObject(value = "{\"message\": \"๊ตฌ์ง์ž ํšŒ์› ํƒˆํ‡ด ์„ฑ๊ณต\", \"data\": null }"))),
@ApiResponse(responseCode = "400", description = "์š”์ฒญ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ",
content = @Content(mediaType = "application/json",
examples = {
@ExampleObject(name = "๋น„๋ฐ€๋ฒˆํ˜ธ ๋ถˆ์ผ์น˜",
value = "{\"message\": \"์ž…๋ ฅํ•œ ๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ๊ธฐ์กด ๋น„๋ฐ€๋ฒˆํ˜ธ์™€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.\", \"data\": null }"),
@ExampleObject(name = "์ž˜๋ชป๋œ ์‚ฌ์šฉ์ž ์œ ํ˜•",
value = "{\"message\": \"์ž˜๋ชป๋œ ์‚ฌ์šฉ์ž ์œ ํ˜•์ž…๋‹ˆ๋‹ค.\", \"data\": null }")
})),
@ApiResponse(responseCode = "404", description = "์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ",
content = @Content(mediaType = "application/json",
examples = @ExampleObject(value = "{\"message\": \"์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.\", \"data\": null }"))),
@ApiResponse(responseCode = "500", description = "์„œ๋ฒ„ ๋‚ด๋ถ€ ์˜ค๋ฅ˜",
content = @Content(mediaType = "application/json",
examples = @ExampleObject(value = "{\"message\": \"์„œ๋ฒ„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.\", \"data\": null }")))
})
public ResponseEntity<GlobalApiResponse<Void>> deleteUser(
@AuthenticationPrincipal UserDetailsImpl userDetails,
@RequestBody UserDeleteRequestDTO requestDTO) {

userService.deleteUser(userDetails.getUserId(), requestDTO);

return ResponseEntity.status(SUCCESS_USER_DELETED.getHttpStatus())
.body(
GlobalApiResponse.of(
SUCCESS_USER_DELETED.getMessage(),
null));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package team9502.sinchulgwinong.domain.user.dto.request;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class UserDeleteRequestDTO {

@Schema(description = "๋น„๋ฐ€๋ฒˆํ˜ธ", example = "Password1!")
private String password;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.DynamicUpdate;
import team9502.sinchulgwinong.domain.board.entity.Board;
import team9502.sinchulgwinong.domain.oauth.enums.SocialType;
import team9502.sinchulgwinong.domain.point.CommonPoint;
import team9502.sinchulgwinong.domain.point.entity.Point;
import team9502.sinchulgwinong.global.entity.BaseTimeEntity;

import java.util.List;

@Entity
@Getter
@Builder
Expand Down Expand Up @@ -50,4 +53,7 @@ public class User extends BaseTimeEntity implements CommonPoint {
@Enumerated(EnumType.STRING)
@Column(nullable = false, length = 20)
private SocialType loginType;

@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private List<Board> boards;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team9502.sinchulgwinong.domain.board.repository.BoardRepository;
import team9502.sinchulgwinong.domain.email.service.EmailVerificationService;
import team9502.sinchulgwinong.domain.point.service.PointService;
import team9502.sinchulgwinong.domain.scrap.service.ScrapService;
import team9502.sinchulgwinong.domain.user.dto.request.UserDeleteRequestDTO;
import team9502.sinchulgwinong.domain.user.dto.request.UserPasswordUpdateRequestDTO;
import team9502.sinchulgwinong.domain.user.dto.request.UserProfileUpdateRequestDTO;
import team9502.sinchulgwinong.domain.user.dto.response.UserProfileResponseDTO;
Expand All @@ -21,6 +25,9 @@ public class UserService {
private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder;
private final EmailVerificationService emailVerificationService;
private final BoardRepository boardRepository;
private final PointService pointService;
private final ScrapService scrapService;

@Transactional(readOnly = true)
public UserProfileResponseDTO getUserProfile(Long userId) {
Expand Down Expand Up @@ -92,4 +99,22 @@ public void updateUserPassword(Long userId, UserPasswordUpdateRequestDTO request
userRepository.save(user);
}

@Transactional
public void deleteUser(Long userId, UserDeleteRequestDTO requestDTO) {

User user = userRepository.findById(userId)
.orElseThrow(() -> new ApiException(ErrorCode.USER_NOT_FOUND));

if (!passwordEncoder.matches(requestDTO.getPassword(), user.getPassword())) {
throw new ApiException(ErrorCode.PASSWORD_MISMATCH);
}

boardRepository.deleteAll(user.getBoards());

pointService.deletePointData(user.getPoint());

scrapService.deleteAllScrapsForUser(user);

userRepository.delete(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public enum SuccessCode {
SUCCESS_USER_PROFILE_READ(HttpStatus.OK, "๊ตฌ์ง์ž ํ”„๋กœํ•„ ์กฐํšŒ ์„ฑ๊ณต"),
SUCCESS_USER_PROFILE_UPDATED(HttpStatus.OK, "๊ตฌ์ง์ž ํ”„๋กœํ•„ ์ˆ˜์ • ์„ฑ๊ณต"),
SUCCESS_USER_PASSWORD_UPDATED(HttpStatus.OK, "๊ตฌ์ง์ž ๋น„๋ฐ€๋ฒˆํ˜ธ ์ˆ˜์ • ์„ฑ๊ณต"),
SUCCESS_USER_DELETED(HttpStatus.OK, "๊ตฌ์ง์ž ํšŒ์› ํƒˆํ‡ด ์„ฑ๊ณต"),

// SocialLogin
SUCCESS_SOCIAL_LOGIN(HttpStatus.OK, "์†Œ์…œ ๋กœ๊ทธ์ธ ์„ฑ๊ณต"),
Expand Down

0 comments on commit a41dbcc

Please sign in to comment.