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

[Feat] GET /managers/stores API 추가 #97

Merged
merged 2 commits into from
Feb 11, 2024
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
2 changes: 2 additions & 0 deletions src/main/java/com/ecolink/core/auth/token/UserPrincipal.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class UserPrincipal implements Serializable {
private final Set<? extends GrantedAuthority> authorities;

// Manager
private final Long managerId;
private final List<Long> managingStores;

public static UserPrincipal of(User user, Avatar avatar, Set<? extends GrantedAuthority> authoritySet,
Expand All @@ -60,6 +61,7 @@ public static UserPrincipal of(User user, Avatar avatar, Set<? extends GrantedAu
.avatarId(avatar.getId())
.nickname(avatar.getNickname())
.authorities(authoritySet)
.managerId(manager != null ? manager.getId() : null)
.managingStores(manager != null ? manager.getManagingStores() : List.of())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.ecolink.core.manager.controller;

import java.util.List;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ecolink.core.auth.token.UserPrincipal;
import com.ecolink.core.bookmark.dto.response.StoreInfoDto;
import com.ecolink.core.common.response.ApiResponse;
import com.ecolink.core.manager.dto.request.ManagerApplicationRequest;
import com.ecolink.core.manager.service.ManagerService;
Expand Down Expand Up @@ -37,4 +41,15 @@ public ApiResponse<Void> applyManger(
return ApiResponse.ok();
}

@Tag(name = "${swagger.tag.my-page}")
@Operation(summary = "관리 매장 조회 API - 인증 필요",
description = "관리 매장 조회 API - 인증 필요",
security = {@SecurityRequirement(name = "session-token")})
@PreAuthorize("hasRole('MANAGER')")
@GetMapping("/stores")
public ApiResponse<List<StoreInfoDto>> getManagingStores(
@AuthenticationPrincipal UserPrincipal principal) {
return ApiResponse.ok(managerService.getManagingStores(principal.getManagerId()));
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ecolink.core.manager.repository;

import java.util.List;
import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -8,19 +9,27 @@

import com.ecolink.core.manager.constant.ManagerStatus;
import com.ecolink.core.manager.domain.Manager;
import com.ecolink.core.store.domain.Store;
import com.ecolink.core.user.domain.User;

public interface ManagerRepository extends JpaRepository<Manager, Long> {

@Query("select m from Manager m "
+ "join fetch m.storeRegistrations sr "
+ "join fetch sr.store "
+ "where m.user = :user")
+ "left join fetch m.storeRegistrations sr "
+ "left join fetch sr.store "
+ "where m.user = :user")
Optional<Manager> findByUser(@Param("user") User user);

@Query("select (count(m) > 0) from Manager m "
+ "where m.user.id = :id "
+ "and m.status = :status ")
boolean existsByUserAndStatus(@Param("id") Long id,@Param("status") ManagerStatus status);
+ "where m.user.id = :id "
+ "and m.status = :status ")
boolean existsByUserAndStatus(@Param("id") Long id, @Param("status") ManagerStatus status);

@Query("select s from Manager m "
+ "left join m.storeRegistrations sr "
+ "on sr.status = com.ecolink.core.manager.constant.RegistrationStatus.ACTIVE "
+ "left join sr.store s "
+ "where m.id = :managerId")
List<Store> findStoresByManager(@Param("managerId") Long id);

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.ecolink.core.manager.service;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ecolink.core.auth.token.UserPrincipal;
import com.ecolink.core.bookmark.dto.response.StoreInfoDto;
import com.ecolink.core.common.error.ErrorCode;
import com.ecolink.core.common.error.exception.EntityNotFoundException;
import com.ecolink.core.common.error.exception.ManagerApplicationException;
Expand Down Expand Up @@ -36,7 +39,7 @@ public Long applyManager(ManagerApplicationRequest request, UserPrincipal princi
}

/**
* 대표 등록 신청 진행중인 지 여부 확인하는 메서드
* 대표 등록 신청 진행중인지 여부 확인하는 메서드
*/
public boolean checkIfPending(Long userId) {
return managerRepository.existsByUserAndStatus(userId, ManagerStatus.PENDING);
Expand All @@ -47,4 +50,9 @@ public Manager getByUser(User user) {
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.MANAGER_NOT_FOUND));
}

@Transactional
public List<StoreInfoDto> getManagingStores(Long managerId) {
return managerRepository.findStoresByManager(managerId).stream().map(StoreInfoDto::of).toList();
}

}
Loading