Skip to content

Commit

Permalink
feat: 로그인한 유저가 등록해놓은 동네 정보를 반환하는 기능 구현(#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
acceptor-gyu committed Jun 21, 2023
1 parent 8e3f679 commit fa2c0f9
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
6 changes: 6 additions & 0 deletions be/src/main/java/com/secondhand/region/dto/RegionDto.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.secondhand.region.dto;

import com.querydsl.core.annotations.QueryProjection;
import com.secondhand.region.entity.Region;
import lombok.Getter;

@Getter
Expand All @@ -14,4 +15,9 @@ public RegionDto(int id, String name) {
this.id = id;
this.name = name;
}

public RegionDto(Region region) {
this.id = region.getId();
this.name = region.getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
import com.secondhand.region.entity.Region;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface RegionRepository extends JpaRepository<Region, Integer>, RegionRepositoryCustom {

List<Region> findAllRegionsByIdIn(List<Integer> regionIds);
}
13 changes: 13 additions & 0 deletions be/src/main/java/com/secondhand/user/UserController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.secondhand.user;

import com.secondhand.region.dto.PostMyRegionDto;
import com.secondhand.user.dto.UserRegionsDto;
import com.secondhand.user.login.dto.LoggedInUser;
import com.secondhand.util.CustomResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -17,6 +18,18 @@ public class UserController {

private final UserService userService;

@GetMapping("/regions")
public ResponseEntity<CustomResponse<UserRegionsDto>> getMyRegion(@RequestAttribute LoggedInUser loggedInUser) {

return ResponseEntity
.ok()
.body(new CustomResponse(
"success",
200,
"지역 조회 성공",
userService.getMyRegion(loggedInUser.getId())));
}

@PutMapping("/regions")
public ResponseEntity<CustomResponse> updateMyRegion(@Validated @RequestBody PostMyRegionDto postMyRegionDto, @RequestAttribute LoggedInUser loggedInUser) {

Expand Down
27 changes: 27 additions & 0 deletions be/src/main/java/com/secondhand/user/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.secondhand.region.dto.PostMyRegionDto;
import com.secondhand.region.repository.RegionRepository;
import com.secondhand.region.validator.RegionValidator;
import com.secondhand.user.dto.UserRegionsDto;
import com.secondhand.user.entity.User;
import com.secondhand.user.login.dto.LoggedInUser;
import com.secondhand.user.repository.UserRepository;
Expand All @@ -15,6 +16,8 @@
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
Expand All @@ -26,6 +29,17 @@ public class UserService {
private final InterestRepository interestRepository;
private final RegionRepository regionRepository;

@Transactional
public UserRegionsDto getMyRegion(Long userId) {

User user = userRepository.findById(userId)
.orElseThrow(() -> new IllegalArgumentException("해당 유저가 존재하지 않습니다."));

List<Integer> regionIds = getUserRegionIds(user);

return new UserRegionsDto(regionRepository.findAllRegionsByIdIn(regionIds));
}

@Transactional
public void updateMyRegion(long userId, PostMyRegionDto postMyRegionDto) {
RegionValidator.isValidRegionId(postMyRegionDto, regionRepository);
Expand Down Expand Up @@ -61,4 +75,17 @@ public void deleteInterestPost(long postId, LoggedInUser loggedInUser) {

interestRepository.delete(interest);
}


private List<Integer> getUserRegionIds(User user) {

List<Integer> regionIds = new ArrayList<>();

regionIds.add(user.getFirstRegionId());

if (user.getSecondRegionId() != null) {
regionIds.add(user.getSecondRegionId());
}
return regionIds;
}
}
23 changes: 23 additions & 0 deletions be/src/main/java/com/secondhand/user/dto/UserRegionsDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.secondhand.user.dto;

import com.secondhand.region.dto.RegionDto;
import com.secondhand.region.entity.Region;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

@Getter
public class UserRegionsDto {

private List<RegionDto> regions;

public UserRegionsDto(List<Region> regions) {

this.regions = new ArrayList<>();

for (Region region : regions) {
this.regions.add(new RegionDto(region));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,12 @@ public class LoggedInUser {
private long githubId;
private String loginId;
private String profileUrl;
private int firstRegionId;
private Integer secondRegionId;

@Builder
public LoggedInUser(User user) {
this.id = user.getId();
this.githubId = user.getGithubId();
this.loginId = user.getLoginId();
this.profileUrl = user.getProfileUrl();
this.firstRegionId = user.getFirstRegionId();
this.secondRegionId = user.getSecondRegionId();
}
}

0 comments on commit fa2c0f9

Please sign in to comment.