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: Semester api v3 #1149

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import java.util.ArrayList;
import java.util.List;

import in.koreatech.koin.domain.timetableV3.model.Term;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
Expand All @@ -33,8 +36,18 @@ public class Semester {
@Column(name = "semester", nullable = false, unique = true)
private String semester;

@NotNull
@Column(name = "year", nullable = false)
private Integer year;

@Enumerated(EnumType.STRING)
@Column(name = "term", nullable = false, length = 20)
private Term term;

@Builder
public Semester(String semester) {
public Semester(String semester, Integer year, Term term) {
this.semester = semester;
this.year = year;
this.term = term;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package in.koreatech.koin.domain.timetableV3.controller;

import static in.koreatech.koin.domain.user.model.UserType.STUDENT;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;

import in.koreatech.koin.domain.timetableV3.dto.response.SemesterCheckResponseV3;
import in.koreatech.koin.domain.timetableV3.dto.response.SemesterResponseV3;
import in.koreatech.koin.global.auth.Auth;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "(Normal) Timetable: V3-시간표", description = "시간표 정보를 관리한다")
public interface TimetableApiV3 {
Soundbar91 marked this conversation as resolved.
Show resolved Hide resolved
@ApiResponses(
value = {
@ApiResponse(responseCode = "200"),
@ApiResponse(responseCode = "401", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "403", content = @Content(schema = @Schema(hidden = true))),
@ApiResponse(responseCode = "404", content = @Content(schema = @Schema(hidden = true)))
}
)
@Operation(summary = "학기 정보 조회")
@GetMapping("/v3/semesters")
ResponseEntity<List<SemesterResponseV3>> getSemesters();

@ApiResponses(
value = {
@ApiResponse(responseCode = "200"),
@ApiResponse(responseCode = "401", content = @Content(schema = @Schema(hidden = true)))
}
)
@Operation(summary = "학생 학기 정보 조회")
@SecurityRequirement(name = "Jwt Authentication")
@GetMapping("/v3/semesters/check")
ResponseEntity<SemesterCheckResponseV3> getStudentSemesters(
@Auth(permit = {STUDENT}) Integer userId
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package in.koreatech.koin.domain.timetableV3.controller;

import static in.koreatech.koin.domain.user.model.UserType.STUDENT;

import java.util.List;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import in.koreatech.koin.domain.timetableV3.dto.response.SemesterCheckResponseV3;
import in.koreatech.koin.domain.timetableV3.dto.response.SemesterResponseV3;
import in.koreatech.koin.domain.timetableV3.service.SemesterServiceV3;
import in.koreatech.koin.global.auth.Auth;
import lombok.RequiredArgsConstructor;

@RestController
@RequiredArgsConstructor
public class TimetableControllerV3 {

private final SemesterServiceV3 semesterServiceV3;

@GetMapping("/v3/semesters")
public ResponseEntity<List<SemesterResponseV3>> getSemesters() {
List<SemesterResponseV3> semesterResponseV3 = semesterServiceV3.getSemesters();
return ResponseEntity.ok(semesterResponseV3);
}

@GetMapping("/v3/semesters/check")
public ResponseEntity<SemesterCheckResponseV3> getStudentSemesters(
@Auth(permit = {STUDENT}) Integer userId
) {
SemesterCheckResponseV3 semesterCheckResponseV3 = semesterServiceV3.getStudentSemesters(userId);
return ResponseEntity.ok(semesterCheckResponseV3);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package in.koreatech.koin.domain.timetableV3.dto.response;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

import java.util.List;

import com.fasterxml.jackson.databind.PropertyNamingStrategies.SnakeCaseStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import in.koreatech.koin.domain.timetable.model.Semester;
import io.swagger.v3.oas.annotations.media.Schema;

@JsonNaming(value = SnakeCaseStrategy.class)
public record SemesterCheckResponseV3(
@Schema(description = "유저 id", example = "1", requiredMode = REQUIRED)
Integer userId,

@Schema(description = "유저 학기", requiredMode = REQUIRED)
List<InnerSemesterCheckResponse> semesters
) {
@JsonNaming(value = SnakeCaseStrategy.class)
public record InnerSemesterCheckResponse(
@Schema(description = "year", example = "2024", requiredMode = REQUIRED)
Integer year,

@Schema(description = "term", example = "1학기", requiredMode = REQUIRED)
String term
) {
public static InnerSemesterCheckResponse of(Semester semester) {
return new InnerSemesterCheckResponse(
semester.getYear(),
semester.getTerm().getDescription()
);
}
}

public static SemesterCheckResponseV3 of(Integer userId, List<Semester> semester) {
return new SemesterCheckResponseV3(
userId,
semester.stream()
.map(InnerSemesterCheckResponse::of)
.toList()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package in.koreatech.koin.domain.timetableV3.dto.response;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;

import in.koreatech.koin.domain.timetable.model.Semester;
import io.swagger.v3.oas.annotations.media.Schema;

@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
public record SemesterResponseV3(
@Schema(description = "year", example = "2024", requiredMode = REQUIRED)
Integer year,

@Schema(description = "term", example = "1학기", requiredMode = REQUIRED)
String term
) {
public static SemesterResponseV3 of(Semester semester) {
return new SemesterResponseV3(
semester.getYear(),
semester.getTerm().getDescription()
);
}
}
19 changes: 19 additions & 0 deletions src/main/java/in/koreatech/koin/domain/timetableV3/model/Term.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package in.koreatech.koin.domain.timetableV3.model;

import lombok.Getter;

@Getter
public enum Term {
WINTER("겨울학기", 1),
SECOND("2학기", 2),
SUMMER("여름학기", 3),
FIRST("1학기", 4);

private final String description;
private final int priority;

Term(String description, int priority) {
this.description = description;
this.priority = priority;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package in.koreatech.koin.domain.timetableV3.repository;

import java.util.List;

import org.springframework.data.repository.Repository;

import in.koreatech.koin.domain.timetable.model.Semester;

public interface SemesterRepositoryV3 extends Repository<Semester, Integer> {

List<Semester> findAll();

Semester save(Semester semester);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package in.koreatech.koin.domain.timetableV3.repository;

import org.springframework.data.repository.Repository;

import java.util.List;

import in.koreatech.koin.domain.timetableV2.model.TimetableFrame;

public interface TimetableFrameRepositoryV3 extends Repository<TimetableFrame, Integer> {

List<TimetableFrame> findByUserIdAndIsMainTrue(Integer userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package in.koreatech.koin.domain.timetableV3.service;

import java.util.Comparator;
import java.util.List;

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

import in.koreatech.koin.domain.timetable.model.Semester;
import in.koreatech.koin.domain.timetableV2.model.TimetableFrame;
import in.koreatech.koin.domain.timetableV3.dto.response.SemesterCheckResponseV3;
import in.koreatech.koin.domain.timetableV3.dto.response.SemesterResponseV3;
import in.koreatech.koin.domain.timetableV3.repository.SemesterRepositoryV3;
import in.koreatech.koin.domain.timetableV3.repository.TimetableFrameRepositoryV3;
import lombok.RequiredArgsConstructor;

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

private final TimetableFrameRepositoryV3 timetableFrameRepository;
private final SemesterRepositoryV3 semesterRepository;

public List<SemesterResponseV3> getSemesters() {
return semesterRepository.findAll().stream()
.sorted(Comparator.comparing(Semester::getYear).reversed()
.thenComparing(semester -> semester.getTerm().getPriority()))
.map(SemesterResponseV3::of)
.toList();
}

public SemesterCheckResponseV3 getStudentSemesters(Integer userId) {
List<Semester> semesters = timetableFrameRepository.findByUserIdAndIsMainTrue(userId).stream()
.map(TimetableFrame::getSemester)
.distinct()
.sorted(Comparator.comparing(Semester::getYear).reversed()
.thenComparing(semester -> semester.getTerm().getPriority()))
.toList();
return SemesterCheckResponseV3.of(userId, semesters);
}
}
30 changes: 30 additions & 0 deletions src/main/resources/db/migration/V108__alter__semester.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
alter table semester
add column year int unsigned,
add column term varchar(20);

update semester set year = 2019, term = 'SECOND' where semester = '20192';
update semester set year = 2019, term = 'SUMMER' where semester = '2019-여름';
update semester set year = 2019, term = 'WINTER' where semester = '2019-겨울';
update semester set year = 2020, term = 'FIRST' where semester = '20201';
update semester set year = 2020, term = 'SECOND' where semester = '20202';
update semester set year = 2020, term = 'SUMMER' where semester = '2020-여름';
update semester set year = 2020, term = 'WINTER' where semester = '2020-겨울';
update semester set year = 2021, term = 'FIRST' where semester = '20211';
update semester set year = 2021, term = 'SECOND' where semester = '20212';
update semester set year = 2021, term = 'SUMMER' where semester = '2021-여름';
update semester set year = 2021, term = 'WINTER' where semester = '2021-겨울';
update semester set year = 2022, term = 'FIRST' where semester = '20221';
update semester set year = 2022, term = 'SECOND' where semester = '20222';
update semester set year = 2022, term = 'SUMMER' where semester = '2022-여름';
update semester set year = 2022, term = 'WINTER' where semester = '2022-겨울';
update semester set year = 2023, term = 'FIRST' where semester = '20231';
update semester set year = 2023, term = 'SECOND' where semester = '20232';
update semester set year = 2023, term = 'SUMMER' where semester = '2023-여름';
update semester set year = 2023, term = 'WINTER' where semester = '2023-겨울';
update semester set year = 2024, term = 'FIRST' where semester = '20241';
update semester set year = 2024, term = 'SECOND' where semester = '20242';
update semester set year = 2024, term = 'SUMMER' where semester = '2024-여름';

ALTER TABLE semester
MODIFY COLUMN year INT UNSIGNED NOT NULL,
MODIFY COLUMN term VARCHAR(20) NOT NULL;
Soundbar91 marked this conversation as resolved.
Show resolved Hide resolved
Loading
Loading