Skip to content

Commit

Permalink
[FEATURE] 현재 학기 API (#164)
Browse files Browse the repository at this point in the history
* refactor: 패키지 변경 (#163)

* feat: 학기 엔티티 (#163)

* feat: 학기 리포지토리 (#163)

* feat: 현재 학기 API (#163)

* feat: 현재 학기 API 권한 설정 (#163)

* chore: API 설명 추가 (#163)
  • Loading branch information
hyunmin0317 authored Jan 21, 2025
1 parent c7a435d commit 2aeb999
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.smunity.server.domain.major.entity;

import com.smunity.server.domain.major.entity.enums.Grade;
import com.smunity.server.domain.major.entity.enums.Semester;
import com.smunity.server.global.common.entity.Department;
import com.smunity.server.global.common.entity.enums.Category;
import com.smunity.server.global.common.entity.enums.Semester;
import jakarta.persistence.*;
import lombok.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.smunity.server.domain.term.controller;

import com.smunity.server.domain.term.dto.TermResponseDto;
import com.smunity.server.domain.term.service.TermService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/terms")
@Tag(name = "10 Term API", description = "학기 관련 API")
public class TermController {

private final TermService termService;

@GetMapping("/current")
@Operation(summary = "현재 학기 조회", description = "현재 학기를 조회합니다.")
public ResponseEntity<TermResponseDto> readCurrentTerm() {
TermResponseDto responseDto = termService.readCurrentTerm();
return ResponseEntity.ok(responseDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.smunity.server.domain.term.dto;

import com.smunity.server.global.common.entity.Term;
import lombok.Builder;

@Builder
public record TermResponseDto(
Long id,
int year,
String semester
) {

public static TermResponseDto from(Term term) {
return TermResponseDto.builder()
.id(term.getId())
.year(term.getYear())
.semester(term.getSemester().getName())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.smunity.server.domain.term.service;

import com.smunity.server.domain.term.dto.TermResponseDto;
import com.smunity.server.global.common.entity.Term;
import com.smunity.server.global.common.repository.TermRepository;
import com.smunity.server.global.exception.GeneralException;
import com.smunity.server.global.exception.code.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

private final TermRepository termRepository;

public TermResponseDto readCurrentTerm() {
Term term = termRepository.findFirstByOrderByIdDesc()
.orElseThrow(() -> new GeneralException(ErrorCode.TERM_NOT_FOUND));
return TermResponseDto.from(term);
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/smunity/server/global/common/entity/Term.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.smunity.server.global.common.entity;

import com.smunity.server.global.common.entity.enums.Semester;
import jakarta.persistence.*;
import lombok.Getter;

@Entity
@Getter
@Table(name = "common_term")
public class Term {

@Id
@Column(name = "term_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(name = "year_value")
private Integer year;

@Enumerated(EnumType.STRING)
private Semester semester;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.smunity.server.domain.major.entity.enums;
package com.smunity.server.global.common.entity.enums;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.smunity.server.global.common.repository;

import com.smunity.server.global.common.entity.Term;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface TermRepository extends JpaRepository<Term, Long> {

Optional<Term> findFirstByOrderByIdDesc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ public enum ErrorCode {
QUESTION_NOT_FOUND(404, "QUESTION001", "해당 질문을 찾을 수 없습니다."),

// Answer Errors
ANSWER_NOT_FOUND(404, "ANSWER001", "해당 답변을 찾을 수 없습니다.");
ANSWER_NOT_FOUND(404, "ANSWER001", "해당 답변을 찾을 수 없습니다."),

// Term Errors
TERM_NOT_FOUND(404, "TERM001", "해당 학기를 찾을 수 없습니다.");

private final int value;
private final String code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public SecurityFilterChain jwtFilterChain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(authorize -> authorize
// 모든 사용자
.requestMatchers("/h2-console/**", "/actuator/prometheus").permitAll()
.requestMatchers("/api/v1/accounts/login", "/api/v1/accounts/refresh").permitAll()
.requestMatchers("/api/v1/auth/**", "/api/v1/departments", "/api/v1/members/count").permitAll()
.requestMatchers("/api/v1/accounts/login", "/api/v1/accounts/refresh", "/api/v1/auth/**").permitAll()
.requestMatchers("/api/v1/terms/**", "/api/v1/departments", "/api/v1/members/count").permitAll()
.requestMatchers(HttpMethod.GET, "/api/v1/questions/**").permitAll()

// 재학생 인증을 완료한 사용자 (ROLE_VERIFIED)
Expand Down

0 comments on commit 2aeb999

Please sign in to comment.