Skip to content

Commit

Permalink
Merge pull request #65 from DSM-Repo/refactor-project-#1
Browse files Browse the repository at this point in the history
pr :: 도서관 목록 조회 api 리팩토링
  • Loading branch information
ori0o0p authored Aug 17, 2024
2 parents be17f17 + f3d6c20 commit 966e3bb
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.example.whopper.global.annotation.OnlyStudent;
import com.example.whopper.global.annotation.OnlyTeacher;
import com.example.whopper.global.utils.DataResponseInfo;
import jakarta.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
Expand Down Expand Up @@ -59,7 +60,7 @@ public DataResponseInfo<LibraryResponse> studentFindLibrary(@RequestParam Intege

@OnlyTeacher
@GetMapping("/teacher")
public DataResponseInfo<LibraryResponse> teacherFindLibrary(@RequestParam Integer year) {
public DataResponseInfo<LibraryResponse> teacherFindLibrary(@RequestParam(required = false) @Nullable Integer year) {
return teacherFindLibraryUseCase.teacherFindLibrary(year);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.example.whopper.domain.library.application.dao;

import com.example.whopper.domain.file.application.usecase.PdfUseCase;
import com.example.whopper.domain.library.application.model.Library;
import com.example.whopper.domain.library.dao.LibraryMongoRepository;
import com.example.whopper.domain.library.domain.type.AccessRight;
import jakarta.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Optional;

@RequiredArgsConstructor
@Component
public class LibraryAdapter {

private final LibraryMongoRepository libraryMongoRepository;
private final PdfUseCase pdfUseCase;

public Optional<Library> getLibrary(AccessRight accessRight, @Nullable Integer year) {
if (year == null) {
return libraryMongoRepository.findFirstByAccessRightNot(accessRight)
.map(it -> {
var pdfFileUrl = pdfUseCase.getPdfFileUrl(it.getFilePath());
return it.toModel(pdfFileUrl);
}
);
}

return libraryMongoRepository.findFirstByAccessRightNotAndYear(
accessRight, year
).map(it -> {
var pdfFileUrl = pdfUseCase.getPdfFileUrl(it.getFilePath());
return it.toModel(pdfFileUrl);
}
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ public LibraryIndexResponse findLibraryIndex(String libraryId) {
return new LibraryIndexResponse(DataResponseInfo.of(library.getIndex()));
}
}

Original file line number Diff line number Diff line change
@@ -1,35 +1,25 @@
package com.example.whopper.domain.library.application.impl;

import com.example.whopper.domain.file.application.usecase.PdfUseCase;
import com.example.whopper.domain.library.application.dao.LibraryAdapter;
import com.example.whopper.domain.library.application.usecase.StudentFindLibraryUseCase;
import com.example.whopper.domain.library.dao.LibraryMongoRepository;
import com.example.whopper.domain.library.domain.type.AccessRight;
import com.example.whopper.domain.library.dto.LibraryResponse;
import com.example.whopper.global.utils.DataResponseInfo;
import jakarta.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class StudentFindLibraryService implements StudentFindLibraryUseCase {

private final LibraryMongoRepository libraryMongoRepository;
private final PdfUseCase pdfUseCase;
private final LibraryAdapter libraryAdapter;

public DataResponseInfo<LibraryResponse> studentFindLibrary(Integer year) {
var libraries = year == null
? libraryMongoRepository.findFirstByAccessRightNot(AccessRight.PRIVATE)
: libraryMongoRepository.findFirstByAccessRightNotAndYear(AccessRight.PRIVATE, year);
public DataResponseInfo<LibraryResponse> studentFindLibrary(@Nullable Integer year) {
var arthurLibrary = libraryAdapter.getLibrary(AccessRight.PRIVATE, year);

return DataResponseInfo.of(libraries.stream()
.map(library -> new LibraryResponse(
null,
null,
library.getYear(),
library.getGrade(),
library.getGeneration(),
pdfUseCase.getPdfFileUrl(library.getFilePath())
))
return DataResponseInfo.of(arthurLibrary.stream()
.map(LibraryResponse::from)
.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.example.whopper.domain.library.domain.type.AccessRight;
import com.example.whopper.domain.library.dto.LibraryResponse;
import com.example.whopper.global.utils.DataResponseInfo;
import com.mongodb.lang.Nullable;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -16,8 +17,8 @@ public class TeacherFindLibraryService implements TeacherFindLibraryUseCase {
private final LibraryMongoRepository libraryMongoRepository;
private final PdfUseCase pdfUseCase;

public DataResponseInfo<LibraryResponse> teacherFindLibrary(Integer year) {
var libraries = year == null
public DataResponseInfo<LibraryResponse> teacherFindLibrary(@Nullable Integer year) {
var libraries = (year == null)
? libraryMongoRepository.findFirstByAccessRightNot(AccessRight.PRIVATE)
: libraryMongoRepository.findFirstByAccessRightNotAndYear(AccessRight.PRIVATE, year);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.example.whopper.domain.library.application.model;

import com.example.whopper.domain.library.domain.DocumentIndex;
import com.example.whopper.domain.library.domain.type.AccessRight;
import lombok.Getter;

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

@Getter
public record Library(
String id,
Integer year,
Integer grade,
LocalDateTime createAt,
AccessRight accessRight,
List<DocumentIndex> books,
String pdfFileUrl
) {

public Integer getGeneration() {
return this.year - this.grade - 2013;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.whopper.domain.library.domain;

import com.example.whopper.domain.library.application.model.Library;
import com.example.whopper.domain.library.domain.type.AccessRight;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -39,4 +40,17 @@ public Integer getGeneration() {
public void changeAccessRight(AccessRight accessRight) {
this.accessRight = accessRight;
}

public Library toModel(String pdfFileUrl) {
return new Library(
id,
year,
grade,
createAt,
accessRight,
index,
pdfFileUrl
);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.example.whopper.domain.library.dto;

import com.example.whopper.domain.library.application.model.Library;
import com.example.whopper.domain.library.domain.type.AccessRight;

public record LibraryResponse(
Expand All @@ -10,4 +11,16 @@ public record LibraryResponse(
Integer generation,
String document_url
) {

public static LibraryResponse from(Library library) {
return new LibraryResponse(
library.id(),
library.accessRight(),
library.year(),
library.grade(),
library.getGeneration(),
library.pdfFileUrl()
);
}

}

0 comments on commit 966e3bb

Please sign in to comment.