Skip to content

Commit

Permalink
[FEATURE] 학과 조회 API (#111)
Browse files Browse the repository at this point in the history
* refactor: dto 리팩토링 (#108)

* refactor: 코드 리팩토링 (#108)

* feat: 학과 조회 리포지토리 (#108)

* feat: 학과 조회 서비스 (#108)

* feat: 학과 조회 API (#108)

* feat: 시큐리티 권한 설정 (#108)
  • Loading branch information
hyunmin0317 authored Dec 10, 2024
1 parent 8e3c4f3 commit 4ee4256
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.smunity.server.domain.course.repository.course;


import com.smunity.server.domain.course.entity.Course;
import com.smunity.server.global.common.entity.enums.Category;

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

import com.smunity.server.domain.culture.dto.CultureResponseDto;
import com.smunity.server.domain.culture.service.CultureQueryService;
import com.smunity.server.domain.member.dto.SubjectResponseDto;
import com.smunity.server.global.common.dto.ListResponseDto;
import com.smunity.server.global.common.entity.enums.SubDomain;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -19,8 +19,8 @@ public class CultureController {
private final CultureQueryService cultureQueryService;

@GetMapping
public ResponseEntity<SubjectResponseDto<CultureResponseDto>> readCultures(@RequestParam(required = false) SubDomain subDomain) {
SubjectResponseDto<CultureResponseDto> responseDto = cultureQueryService.readCultures(subDomain);
public ResponseEntity<ListResponseDto<CultureResponseDto>> readCultures(@RequestParam(required = false) SubDomain subDomain) {
ListResponseDto<CultureResponseDto> responseDto = cultureQueryService.readCultures(subDomain);
return ResponseEntity.ok(responseDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import static com.smunity.server.domain.culture.entity.QCulture.culture;


@Repository
@RequiredArgsConstructor
public class CultureQueryRepositoryImpl implements CultureQueryRepository {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.smunity.server.domain.culture.dto.CultureResponseDto;
import com.smunity.server.domain.culture.entity.Culture;
import com.smunity.server.domain.culture.repository.CultureQueryRepository;
import com.smunity.server.domain.member.dto.SubjectResponseDto;
import com.smunity.server.global.common.dto.ListResponseDto;
import com.smunity.server.global.common.entity.enums.SubDomain;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -18,9 +18,9 @@ public class CultureQueryService {

private final CultureQueryRepository cultureQueryRepository;

public SubjectResponseDto<CultureResponseDto> readCultures(SubDomain subDomain) {
public ListResponseDto<CultureResponseDto> readCultures(SubDomain subDomain) {
List<Culture> cultures = cultureQueryRepository.findBySubDomain(subDomain);
List<CultureResponseDto> responseDtoList = CultureResponseDto.from(cultures);
return SubjectResponseDto.from(responseDtoList);
return ListResponseDto.from(responseDtoList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.smunity.server.domain.department.controller;

import com.smunity.server.domain.department.dto.DepartmentResponseDto;
import com.smunity.server.domain.department.service.DepartmentService;
import com.smunity.server.global.common.dto.ListResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/departments")
public class DepartmentController {

private final DepartmentService departmentService;

@GetMapping
public ResponseEntity<ListResponseDto<DepartmentResponseDto>> readDepartments(@RequestParam(required = false) Boolean isEditable) {
ListResponseDto<DepartmentResponseDto> responseDto = departmentService.readDepartments(isEditable);
return ResponseEntity.ok(responseDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.smunity.server.domain.department.dto;

import com.smunity.server.global.common.entity.Department;
import com.smunity.server.global.common.entity.enums.SubDomain;
import lombok.Builder;

import java.util.List;

@Builder
public record DepartmentResponseDto(
Long id,
String college,
String name,
SubDomain subDomain,
boolean isEditable,
String code
) {

public static DepartmentResponseDto from(Department department) {
return DepartmentResponseDto.builder()
.id(department.getId())
.college(department.getCollege())
.name(department.getName())
.subDomain(department.getSubDomain())
.isEditable(department.isEditable())
.code(department.getCode())
.build();
}


public static List<DepartmentResponseDto> from(List<Department> departments) {
return departments.stream().map(DepartmentResponseDto::from).toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.smunity.server.domain.department.repository;

import com.smunity.server.global.common.entity.Department;

import java.util.List;

public interface DepartmentQueryRepository {

List<Department> findByIsEditable(Boolean isEditable);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.smunity.server.domain.department.repository;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.smunity.server.global.common.entity.Department;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.smunity.server.global.common.entity.QDepartment.department;

@Repository
@RequiredArgsConstructor
public class DepartmentQueryRepositoryImpl implements DepartmentQueryRepository {

private final JPAQueryFactory query;

@Override
public List<Department> findByIsEditable(Boolean isEditable) {
return query.selectFrom(department)
.where(isEditableEq(isEditable))
.fetch();
}

private BooleanExpression isEditableEq(Boolean isEditable) {
return isEditable != null ? department.isEditable.eq(isEditable) : null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.smunity.server.domain.department.service;

import com.smunity.server.domain.department.dto.DepartmentResponseDto;
import com.smunity.server.domain.department.repository.DepartmentQueryRepository;
import com.smunity.server.global.common.dto.ListResponseDto;
import com.smunity.server.global.common.entity.Department;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

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

private final DepartmentQueryRepository departmentQueryRepository;

public ListResponseDto<DepartmentResponseDto> readDepartments(Boolean isEditable) {
List<Department> departments = departmentQueryRepository.findByIsEditable(isEditable);
List<DepartmentResponseDto> responseDtoList = DepartmentResponseDto.from(departments);
return ListResponseDto.from(responseDtoList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.smunity.server.domain.major.dto.MajorResponseDto;
import com.smunity.server.domain.major.service.MajorQueryService;
import com.smunity.server.domain.member.dto.SubjectResponseDto;
import com.smunity.server.global.common.dto.ListResponseDto;
import com.smunity.server.global.common.entity.enums.Category;
import com.smunity.server.global.security.annotation.AuthMember;
import lombok.RequiredArgsConstructor;
Expand All @@ -20,8 +20,8 @@ public class MajorController {
private final MajorQueryService majorQueryService;

@GetMapping
public ResponseEntity<SubjectResponseDto<MajorResponseDto>> readMajors(@AuthMember Long memberId, @RequestParam(required = false) Category category) {
SubjectResponseDto<MajorResponseDto> responseDto = majorQueryService.readMajors(memberId, category);
public ResponseEntity<ListResponseDto<MajorResponseDto>> readMajors(@AuthMember Long memberId, @RequestParam(required = false) Category category) {
ListResponseDto<MajorResponseDto> responseDto = majorQueryService.readMajors(memberId, category);
return ResponseEntity.ok(responseDto);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.smunity.server.domain.major.repository;


import com.smunity.server.domain.major.entity.Major;
import com.smunity.server.global.common.entity.Department;
import com.smunity.server.global.common.entity.enums.Category;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.smunity.server.domain.major.dto.MajorResponseDto;
import com.smunity.server.domain.major.entity.Major;
import com.smunity.server.domain.major.repository.MajorQueryRepository;
import com.smunity.server.domain.member.dto.SubjectResponseDto;
import com.smunity.server.global.common.dto.ListResponseDto;
import com.smunity.server.global.common.entity.Member;
import com.smunity.server.global.common.entity.enums.Category;
import com.smunity.server.global.common.repository.MemberRepository;
Expand All @@ -23,11 +23,11 @@ public class MajorQueryService {
private final MemberRepository memberRepository;
private final MajorQueryRepository majorQueryRepository;

public SubjectResponseDto<MajorResponseDto> readMajors(Long memberId, Category category) {
public ListResponseDto<MajorResponseDto> readMajors(Long memberId, Category category) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new GeneralException(ErrorCode.MEMBER_NOT_FOUND));
List<Major> majors = majorQueryRepository.findByDepartmentAndCategory(member.getDepartment(), category, member.getCompletedNumbers());
List<MajorResponseDto> responseDtoList = MajorResponseDto.from(majors);
return SubjectResponseDto.from(responseDtoList);
return ListResponseDto.from(responseDtoList);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package com.smunity.server.domain.member.dto;
package com.smunity.server.global.common.dto;

import lombok.Builder;

import java.util.List;

@Builder
public record SubjectResponseDto<T>(
public record ListResponseDto<T>(
int count,
List<T> content
) {

public static <T> SubjectResponseDto<T> from(List<T> responses) {
return SubjectResponseDto.<T>builder()
public static <T> ListResponseDto<T> from(List<T> responses) {
return ListResponseDto.<T>builder()
.count(responses.size())
.content(responses)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

// 경로별 인가 작업
http.authorizeHttpRequests(authorize -> authorize
// H2 콘솔과 Swagger UI 및 API 문서에 대한 접근 허용
.requestMatchers("/h2-console/**", "/swagger-ui/**", "/v3/api-docs/**").permitAll()
// H2 콘솔, Swagger UI 및 API 문서, Actuator 에 대한 접근 허용
.requestMatchers("/h2-console/**", "/swagger-ui/**", "/v3/api-docs/**", "/actuator/info").permitAll()

// 재학생 인증을 완료한 사용자 (ROLE_VERIFIED)
.requestMatchers("/api/v1/accounts/register").hasRole("VERIFIED")

// 모든 사용자
.requestMatchers("/api/v1/accounts/**", "/api/v1/auth/**", "/actuator/info").permitAll()
.requestMatchers("/api/v1/accounts/**", "/api/v1/auth/**", "/api/v1/departments").permitAll()
.requestMatchers(HttpMethod.GET, "/api/v1/questions/**").permitAll()

// 관리자 권한을 가진 사용자 (ROLE_ADMIN)
Expand Down

0 comments on commit 4ee4256

Please sign in to comment.