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

πŸ”€ :: (#275) Excel μΆ”κ°€ #276

Closed
wants to merge 14 commits into from
Closed
5 changes: 5 additions & 0 deletions yapaghetti-infrastructure/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ dependencies {

//cool sms
implementation 'net.nurigo:javaSDK:2.2'

// excel poi
implementation 'org.apache.poi:poi:3.16'
implementation 'org.apache.poi:poi-ooxml:3.16'
osangu marked this conversation as resolved.
Show resolved Hide resolved

}

test {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package kr.hs.entrydsm.yapaghetti.domain.teacher.excel;

import kr.hs.entrydsm.yapaghetti.domain.student.persistence.StudentPersistenceAdapter;
import kr.hs.entrydsm.yapaghetti.domain.teacher.api.dto.response.StudentElementByGradeClassNum;
import kr.hs.entrydsm.yapaghetti.domain.teacher.excel.exception.ExcelOException;
import lombok.AccessLevel;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@Component
public class CreateExcelImpl implements CreateExcelInterface {
osangu marked this conversation as resolved.
Show resolved Hide resolved

private final StudentPersistenceAdapter studentPersistenceAdapter;
@Override
osangu marked this conversation as resolved.
Show resolved Hide resolved
public void execute(HttpServletResponse response) {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("학생 정보");
osangu marked this conversation as resolved.
Show resolved Hide resolved

int rowNum = 0;
String fileName = "전ꡐ생 ν˜„ν™©";
osangu marked this conversation as resolved.
Show resolved Hide resolved

Row headerRow = sheet.createRow(rowNum++);
headerRow.createCell(0).setCellValue("ν•™λ²ˆ");
headerRow.createCell(1).setCellValue("이름");
headerRow.createCell(2).setCellValue("ν”Όλ“œλ°± μƒνƒœ");
headerRow.createCell(3).setCellValue("λŒ€κΈ° λ¬Έμ„œ μ—¬λΆ€");
headerRow.createCell(4).setCellValue("곡개 λ¬Έμ„œ μ—¬λΆ€");
osangu marked this conversation as resolved.
Show resolved Hide resolved

List<StudentElementByGradeClassNum> excelElement = studentPersistenceAdapter
.queryStudentListByGradeAndClassNumAndDocStatus(null, null, null);
osangu marked this conversation as resolved.
Show resolved Hide resolved

for (StudentElementByGradeClassNum element : excelElement) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(element.getGcn());
row.createCell(1).setCellValue(element.getName());
row.createCell(2).setCellValue(element.isFeedbackStatus() ? "O" : null);
row.createCell(3).setCellValue(element.isSubmitted() ? "O" : null);
row.createCell(4).setCellValue(element.isPublicStatus() ? "O" : null);
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p5;

excelElement.stream()
  .forEach(element -> {
    ...
})


try {
String time = LocalDateTime.now()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p3; νƒ€μž„μ‘΄ κ΄€λ¦¬λŠ” λ”°λ‘œ ν•„μš” μ—†μ„κΉŒμš”?

.format(DateTimeFormatter.ofPattern("yyyyλ…„MMμ›”dd일_HHμ‹œmmλΆ„_"));
eogus2513 marked this conversation as resolved.
Show resolved Hide resolved
osangu marked this conversation as resolved.
Show resolved Hide resolved
String formatFileName = new String((time + fileName + ".xlsx")
.getBytes("KSC5601"), "8859_1");
osangu marked this conversation as resolved.
Show resolved Hide resolved

response.setContentType("application/vnd.ms-excel");
osangu marked this conversation as resolved.
Show resolved Hide resolved
response.setHeader("Content-Disposition", "attachment; fileName=\"" + formatFileName + "\"");

workbook.write(response.getOutputStream());
workbook.close();
} catch (IOException e) {
throw ExcelOException.EXCEPTION;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package kr.hs.entrydsm.yapaghetti.domain.teacher.excel;

import javax.servlet.http.HttpServletResponse;

public interface CreateExcelInterface {
void execute(HttpServletResponse response);
}
osangu marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package kr.hs.entrydsm.yapaghetti.domain.teacher.excel.exception;

import kr.hs.entrydsm.yapaghetti.error.ErrorProperty;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public enum ExcelErrorCode implements ErrorProperty {

EXCEL_IO_EXCEPTION(400, "엑셀을 λ‹€μš΄λ‘œλ“œ ν•  수 μ—†μŠ΅λ‹ˆλ‹€.");
osangu marked this conversation as resolved.
Show resolved Hide resolved

private final int status;
private final String message;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kr.hs.entrydsm.yapaghetti.domain.teacher.excel.exception;

import kr.hs.entrydsm.yapaghetti.error.YapaghettiException;

public class ExcelOException extends YapaghettiException {
osangu marked this conversation as resolved.
Show resolved Hide resolved

public static final YapaghettiException EXCEPTION = new ExcelOException();
osangu marked this conversation as resolved.
Show resolved Hide resolved

private ExcelOException() {
super(ExcelErrorCode.EXCEL_IO_EXCEPTION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import kr.hs.entrydsm.yapaghetti.domain.teacher.api.dto.response.CompanyListResponse;
import kr.hs.entrydsm.yapaghetti.domain.teacher.api.dto.response.NewCompanyResponse;
import kr.hs.entrydsm.yapaghetti.domain.teacher.api.dto.response.StudentDetailResponse;
import kr.hs.entrydsm.yapaghetti.domain.teacher.excel.CreateExcelInterface;
import kr.hs.entrydsm.yapaghetti.domain.teacher.presentation.dto.request.WebCreateCompanyRequest;
import kr.hs.entrydsm.yapaghetti.domain.teacher.api.dto.response.StudentListResponse;
import kr.hs.entrydsm.yapaghetti.domain.teacher.presentation.dto.request.WebCreateFeedbackRequest;
Expand All @@ -38,6 +39,8 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.UUID;
Expand All @@ -58,6 +61,7 @@ public class TeacherWebAdapter {
private final QueryStudentDetailPort queryStudentDetailPort;
private final QueryCompanyListPort queryCompanyListPort;
private final DeleteFeedbackPort deleteFeedbackPort;
private final CreateExcelInterface createExcelUseCase;

@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/feedback/{student-id}")
Expand Down Expand Up @@ -162,4 +166,8 @@ public void deleteFeedback(@RequestBody @Valid WebDeleteFeedbackRequest request)
);
}

@GetMapping("/excel")
public void getExcel(HttpServletResponse response) {
createExcelUseCase.execute(response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.antMatchers(HttpMethod.DELETE, "/teachers/company/{company-id}").hasRole(TEACHER)
.antMatchers(HttpMethod.DELETE, "/teachers/student/{student-id}").hasRole(TEACHER)
.antMatchers(HttpMethod.DELETE, "/teachers/feedback").hasRole(TEACHER)
.antMatchers(HttpMethod.GET, "/teachers/excel").hasRole(TEACHER)

// tags
.antMatchers(HttpMethod.POST, "/tags").hasRole(TEACHER)
Expand Down