Skip to content

Commit

Permalink
feat: admin API 개발 (#206)
Browse files Browse the repository at this point in the history
* add: 비밀번호 추가

* add: dto 추가

* chore: 화이트리스트에 추가

* feat: 어드민 API 구현

* del: 사용하지 않는 메서드 삭제
  • Loading branch information
mikekks authored Sep 5, 2024
1 parent 9724a20 commit c34e152
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ private void setHttp(HttpSecurity http) throws Exception {
.requestMatchers(new AntPathRequestMatcher("/external/image/presigned")).permitAll()
.requestMatchers(new AntPathRequestMatcher("/api/auth/oauth2/apple")).permitAll()
.requestMatchers(new AntPathRequestMatcher("/api/auth/create-token")).permitAll()
.requestMatchers(new AntPathRequestMatcher("/admin/**")).permitAll()
.anyRequest().authenticated()
)
.headers(headers -> headers
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.layer.domain.admin.controller;

import java.time.LocalDateTime;

import org.layer.domain.admin.controller.dto.AdminRetrospectsGetResponse;
import org.layer.domain.admin.controller.dto.AdminSpacesGetResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "어드민", description = "어드민 관련 API")
public interface AdminApi {
@Operation(summary = "스페이스 관련 데이터 조회", description = "")
@Parameters({
@Parameter(name = "startDate", description = "검색 시작 시간", example = "2024-09-05T15:30:45", required = true),
@Parameter(name = "endDate", description = "검색 종료 시간", example = "2024-09-13T15:30:45", required = true),
@Parameter(name = "password", description = "비밀번호 [카톡방으로 공유]", example = "[카톡방으로 공유]", required = true)
})
ResponseEntity<AdminSpacesGetResponse> getSpaceData(@RequestParam("startDate") LocalDateTime startDate,
@RequestParam("endDate") LocalDateTime endDate, @RequestParam("password") String password);

@Operation(summary = "회고 관련 데이터 조회", description = "")
@Parameters({
@Parameter(name = "startDate", description = "검색 시작 시간", example = "2024-09-05T15:30:45", required = true, schema = @Schema(type = "string")),
@Parameter(name = "endDate", description = "검색 종료 시간", example = "2024-09-13T15:30:45", required = true, schema = @Schema(type = "string")),
@Parameter(name = "password", description = "비밀번호 [카톡방으로 공유]", example = "[카톡방으로 공유]", required = true, schema = @Schema(type = "string", format = "string"))})
ResponseEntity<AdminRetrospectsGetResponse> getRetrospectData(@RequestParam("startDate") LocalDateTime startDate,
@RequestParam("endDate") LocalDateTime endDate, @RequestParam("password") String password);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.layer.domain.admin.controller;

import java.time.LocalDateTime;

import org.layer.common.annotation.MemberId;
import org.layer.domain.admin.controller.dto.AdminRetrospectsGetResponse;
import org.layer.domain.admin.controller.dto.AdminSpacesGetResponse;
import org.layer.domain.admin.service.AdminService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
@RequestMapping("/admin")
public class AdminController implements AdminApi {
private final AdminService adminService;

@Override
@GetMapping("/space")
public ResponseEntity<AdminSpacesGetResponse> getSpaceData(
@RequestParam("startDate") LocalDateTime startDate,
@RequestParam("endDate") LocalDateTime endDate,
@RequestParam("password") String password) {

return ResponseEntity.ok(adminService.getSpaceData(startDate, endDate, password));
}

@Override
@GetMapping("/retrospect")
public ResponseEntity<AdminRetrospectsGetResponse> getRetrospectData(
@RequestParam("startDate") LocalDateTime startDate,
@RequestParam("endDate") LocalDateTime endDate,
@RequestParam("password") String password) {


return ResponseEntity.ok(adminService.getRetrospectData(startDate, endDate, password));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.layer.domain.admin.controller.dto;

import java.util.List;

import org.layer.domain.retrospect.dto.AdminRetrospectGetResponse;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Getter
public class AdminRetrospectsGetResponse {

private final List<AdminRetrospectGetResponse> retrospects;
private final Integer totalCount;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package org.layer.domain.admin.controller.dto;

import java.util.List;

import org.layer.domain.space.dto.AdminSpaceGetResponse;

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

@RequiredArgsConstructor
@Getter
@Schema(name = "AdminSpacesGetResponse", description = "Admin 스페이스 조회")
public class AdminSpacesGetResponse {

@Schema(description = "스페이스 객체", example = "")
private final List<AdminSpaceGetResponse> spaces;

@Schema(description = "총 개수", example = "30")
private final Integer totalCount;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.layer.domain.admin.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import org.layer.domain.admin.controller.dto.AdminRetrospectsGetResponse;
import org.layer.domain.admin.controller.dto.AdminSpacesGetResponse;
import org.layer.domain.retrospect.dto.AdminRetrospectGetResponse;
import org.layer.domain.retrospect.repository.RetrospectAdminRepository;
import org.layer.domain.space.dto.AdminSpaceGetResponse;
import org.layer.domain.space.entity.Space;
import org.layer.domain.space.repository.SpaceAdminRepository;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class AdminService {

private final SpaceAdminRepository spaceAdminRepository;
private final RetrospectAdminRepository retrospectAdminRepository;

@Value("${admin.password}")
private String password;

public AdminSpacesGetResponse getSpaceData(LocalDateTime startDate, LocalDateTime endDate, String requestPassword){

if(!requestPassword.equals(password)){
throw new IllegalArgumentException("비밀번호가 틀렸습니다.");
}

List<AdminSpaceGetResponse> spaces = spaceAdminRepository.findAllByCreatedAtAfterAndCreatedAtBefore(startDate, endDate);

return new AdminSpacesGetResponse(spaces, spaces.size());
}

public AdminRetrospectsGetResponse getRetrospectData(LocalDateTime startDate, LocalDateTime endDate, String requestPassword){

if(!requestPassword.equals(password)){
throw new IllegalArgumentException("비밀번호가 틀렸습니다.");
}

List<AdminRetrospectGetResponse> retrospects = retrospectAdminRepository.findAllByCreatedAtAfterAndCreatedAtBefore(startDate, endDate);

return new AdminRetrospectsGetResponse(retrospects, retrospects.size());
}
}
7 changes: 5 additions & 2 deletions layer-api/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spring:
config:
import: optional:file:/config/application-secret.properties
import: application-secret.properties
datasource:
url: ${DEV_DB_URL}
username: ${DEV_DB_NAME}
Expand Down Expand Up @@ -77,4 +77,7 @@ openai:
systemContent: ${OPENAI_SYSTEM_CONTENT}
apiKey: ${OPENAI_API_KEY}
model: ${OPENAI_MODEL}
maxTokens: ${OPENAI_MAX_TOKENS}
maxTokens: ${OPENAI_MAX_TOKENS}

admin:
password: ${ADMIN_PASSWORD}
5 changes: 4 additions & 1 deletion layer-api/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,7 @@ openai:
systemContent: ${OPENAI_SYSTEM_CONTENT}
apiKey: ${OPENAI_API_KEY}
model: ${OPENAI_MODEL}
maxTokens: ${OPENAI_MAX_TOKENS}
maxTokens: ${OPENAI_MAX_TOKENS}

admin:
password: ${ADMIN_PASSWORD}
5 changes: 4 additions & 1 deletion layer-api/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,7 @@ openai:
systemContent: ${OPENAI_SYSTEM_CONTENT}
apiKey: ${OPENAI_API_KEY}
model: ${OPENAI_MODEL}
maxTokens: ${OPENAI_MAX_TOKENS}
maxTokens: ${OPENAI_MAX_TOKENS}

admin:
password: ${ADMIN_PASSWORD}
5 changes: 4 additions & 1 deletion layer-api/src/main/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,7 @@ openai:
systemContent: ${OPENAI_SYSTEM_CONTENT}
apiKey: ${OPENAI_API_KEY}
model: ${OPENAI_MODEL}
maxTokens: ${OPENAI_MAX_TOKENS}
maxTokens: ${OPENAI_MAX_TOKENS}

admin:
password: ${ADMIN_PASSWORD}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.layer.domain.retrospect.dto;

import lombok.Getter;

@Getter
public class AdminRetrospectGetResponse {
private final String retrospectTitle;
private final String retrospectCreator;

public AdminRetrospectGetResponse(String retrospectTitle, String retrospectCreator) {
this.retrospectTitle = retrospectTitle;
this.retrospectCreator = retrospectCreator;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.layer.domain.retrospect.repository;

import java.time.LocalDateTime;
import java.util.List;

import org.layer.domain.retrospect.dto.AdminRetrospectGetResponse;
import org.layer.domain.retrospect.entity.Retrospect;
import org.layer.domain.space.dto.AdminSpaceGetResponse;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface RetrospectAdminRepository extends JpaRepository<Retrospect, Long> {

@Query
("SELECT new org.layer.domain.retrospect.dto.AdminRetrospectGetResponse(r.title, m.name) "
+ "FROM Retrospect r "
+ "JOIN Space s ON r.spaceId = s.id "
+ "JOIN Member m ON s.leaderId = m.id "
+ "WHERE s.createdAt >= :startDate "
+ "AND s.createdAt <= :endDate"
)
List<AdminRetrospectGetResponse> findAllByCreatedAtAfterAndCreatedAtBefore(
@Param("startDate") LocalDateTime startDate,
@Param("endDate") LocalDateTime endDate);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.layer.domain.space.dto;

import lombok.Getter;

@Getter
public class AdminSpaceGetResponse {

private final String spaceName;
private final String spaceLeaderName;

public AdminSpaceGetResponse(String spaceName, String spaceLeaderName) {
this.spaceName = spaceName;
this.spaceLeaderName = spaceLeaderName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.layer.domain.space.repository;

import java.time.LocalDateTime;
import java.util.List;

import org.layer.domain.space.dto.AdminSpaceGetResponse;
import org.layer.domain.space.entity.Space;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface SpaceAdminRepository extends JpaRepository<Space, Long> {

@Query
("SELECT new org.layer.domain.space.dto.AdminSpaceGetResponse(s.name, m.name) "
+ "FROM Space s "
+ "JOIN Member m ON s.leaderId = m.id "
+ "WHERE s.createdAt >= :startDate "
+ "AND s.createdAt <= :endDate"
)
List<AdminSpaceGetResponse> findAllByCreatedAtAfterAndCreatedAtBefore(
@Param("startDate") LocalDateTime startDate,
@Param("endDate") LocalDateTime endDate
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import org.layer.domain.space.entity.Space;
import org.layer.domain.space.exception.SpaceException;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

import static org.layer.common.exception.SpaceExceptionType.NOT_FOUND_SPACE;

Expand All @@ -16,10 +12,4 @@ default Space findByIdOrThrow(Long spaceId) {
return findById(spaceId)
.orElseThrow(() -> new SpaceException(NOT_FOUND_SPACE));
}

List<Space> findByIdIn(List<Long> ids);

@Query("select s from Space s inner join MemberSpaceRelation ms on s.id = ms.space.id where ms.memberId = :memberId")
List<Space> findByMemberId(@Param("memberId") Long memberId);

}

0 comments on commit c34e152

Please sign in to comment.