-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/COT-165-attendance-record-order
- Loading branch information
Showing
22 changed files
with
438 additions
and
384 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/org/cotato/csquiz/api/attendance/controller/AttendanceExcelController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.cotato.csquiz.api.attendance.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import lombok.RequiredArgsConstructor; | ||
import org.cotato.csquiz.common.poi.ExcelView; | ||
import org.cotato.csquiz.domain.attendance.service.AttendanceExcelService; | ||
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; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
@RestController | ||
@RequestMapping("/v2/api/attendances/records/excel") | ||
@RequiredArgsConstructor | ||
public class AttendanceExcelController { | ||
|
||
private final AttendanceExcelService attendanceExcelService; | ||
|
||
@Operation(summary = "세션별 출석 기록 엑셀 다운로드 API") | ||
@GetMapping(params = "generationId") | ||
public ModelAndView downloadAttendanceRecordsAsExcelBySessions(@RequestParam(name = "generationId") Long generationId) { | ||
return new ModelAndView(new ExcelView(), attendanceExcelService.getAttendanceRecordsExcelDataByGeneration(generationId)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
src/main/java/org/cotato/csquiz/api/member/dto/DeleteGenerationMemberRequest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.cotato.csquiz.common.poi; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Builder | ||
@Getter | ||
@AllArgsConstructor | ||
public class CellData { | ||
|
||
private String value; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/org/cotato/csquiz/common/poi/ExcelColumnName.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package org.cotato.csquiz.common.poi; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ExcelColumnName { | ||
|
||
String headerName() default ""; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.cotato.csquiz.common.poi; | ||
|
||
import java.util.List; | ||
|
||
public interface ExcelData { | ||
|
||
List<CellData> headers(); | ||
|
||
List<CellData> datas(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.cotato.csquiz.common.poi; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.util.Map; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.poi.ss.usermodel.Workbook; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.view.document.AbstractXlsxView; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ExcelView extends AbstractXlsxView { | ||
|
||
@Override | ||
protected void buildExcelDocument(Map<String, Object> model, Workbook workbook, HttpServletRequest request, | ||
HttpServletResponse response) { | ||
new ExcelWriter(model, workbook, response).create(); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/main/java/org/cotato/csquiz/common/poi/ExcelWriter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package org.cotato.csquiz.common.poi; | ||
|
||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import lombok.RequiredArgsConstructor; | ||
import org.apache.poi.ss.usermodel.Cell; | ||
import org.apache.poi.ss.usermodel.Row; | ||
import org.apache.poi.ss.usermodel.Sheet; | ||
import org.apache.poi.ss.usermodel.Workbook; | ||
import org.springframework.http.HttpHeaders; | ||
|
||
@RequiredArgsConstructor | ||
public class ExcelWriter { | ||
|
||
public static final String FILE_NAME = "fileName"; | ||
public static final String SHEETS = "sheets"; | ||
|
||
private final Map<String, Object> data; | ||
private final Workbook workbook; | ||
private final HttpServletResponse response; | ||
|
||
@SuppressWarnings("unchecked") | ||
public void create() { | ||
setFileName(response, (String) data.get(FILE_NAME)); | ||
|
||
Map<String, ?> dataBySheet = (Map<String, ?>) data.get(SHEETS); | ||
|
||
for (Entry<String, ?> sheetAndDatas : dataBySheet.entrySet()) { | ||
String sheetName = sheetAndDatas.getKey(); | ||
Sheet sheet = workbook.createSheet(sheetName); | ||
createHeaderRow(sheet, sheetAndDatas.getValue()); | ||
createDataRow(sheet, sheetAndDatas.getValue()); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private void createHeaderRow(Sheet sheet, Object datas) { | ||
Row headerRow = sheet.createRow(0); | ||
List<ExcelData> excelData = (List<ExcelData>) datas; | ||
int cellNumber = 0; | ||
ExcelData cellData = excelData.get(0); | ||
List<CellData> headers = cellData.headers(); | ||
|
||
for (CellData header : headers) { | ||
Cell cell = headerRow.createCell(cellNumber++); | ||
cell.setCellValue(header.getValue()); | ||
} | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
private void createDataRow(Sheet sheet, Object datas) { | ||
List<ExcelData> excelData = (List<ExcelData>) datas; | ||
|
||
int rowNumber = 1; | ||
for (ExcelData columnValue : excelData) { | ||
Row row = sheet.createRow(rowNumber++); | ||
createData(row, columnValue.datas()); | ||
} | ||
} | ||
|
||
private void createData(Row row, List<CellData> datas) { | ||
int columnNumber = 0; | ||
for (CellData cellData : datas) { | ||
Cell cell = row.createCell(columnNumber++); | ||
cell.setCellValue(cellData.getValue()); | ||
} | ||
} | ||
|
||
private void setFileName(HttpServletResponse response, String fileName) { | ||
String format = "attachment; filename=\"%s\""; | ||
String encodedFilename = String.format(format, URLEncoder.encode(fileName +".xlsx", StandardCharsets.UTF_8)); | ||
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, encodedFilename); | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
src/main/java/org/cotato/csquiz/domain/attendance/poi/AttendanceRecordExcelData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package org.cotato.csquiz.domain.attendance.poi; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.cotato.csquiz.common.poi.CellData; | ||
import org.cotato.csquiz.common.poi.ExcelColumnName; | ||
import org.cotato.csquiz.common.poi.ExcelData; | ||
import org.cotato.csquiz.domain.attendance.entity.AttendanceRecord; | ||
import org.cotato.csquiz.domain.attendance.enums.AttendanceResult; | ||
import org.cotato.csquiz.domain.attendance.vo.AttendRecordStatics; | ||
import org.cotato.csquiz.domain.auth.entity.Member; | ||
import org.cotato.csquiz.domain.generation.entity.Session; | ||
|
||
@Data | ||
@Builder | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public class AttendanceRecordExcelData implements ExcelData { | ||
|
||
private static final String NAME_VALUE = "이름"; | ||
private static final String SESSION_COUNTS = "세션 수"; | ||
private static final String OFFLINE = "대면"; | ||
private static final String ONLINE = "비대면"; | ||
private static final String LATE = "지각"; | ||
private static final String ABSENT = "결석"; | ||
|
||
@ExcelColumnName(headerName = "이름") | ||
private String name; | ||
|
||
private List<AttendRecord> records; | ||
|
||
@ExcelColumnName(headerName = "세션 수") | ||
private String attendanceCounts; | ||
|
||
@ExcelColumnName(headerName = "대면") | ||
private String offline; | ||
|
||
@ExcelColumnName(headerName = "비대면") | ||
private String online; | ||
|
||
@ExcelColumnName(headerName = "지각") | ||
private String late; | ||
|
||
@ExcelColumnName(headerName = "결석") | ||
private String absent; | ||
|
||
public static AttendanceRecordExcelData of(Member member, int size, List<AttendRecord> records) { | ||
return AttendanceRecordExcelData.builder() | ||
.name(member.getName()) | ||
.records(records) | ||
.attendanceCounts(String.valueOf(size)) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public List<CellData> headers() { | ||
List<CellData> headers = new ArrayList<>(); | ||
headers.add(CellData.builder().value(NAME_VALUE).build()); | ||
|
||
for (AttendRecord record : records) { | ||
headers.add(CellData.builder().value(record.sessionName).build()); | ||
} | ||
headers.add(CellData.builder().value(SESSION_COUNTS).build()); | ||
headers.add(CellData.builder().value(OFFLINE).build()); | ||
headers.add(CellData.builder().value(ONLINE).build()); | ||
headers.add(CellData.builder().value(LATE).build()); | ||
headers.add(CellData.builder().value(ABSENT).build()); | ||
|
||
return headers; | ||
} | ||
|
||
@Override | ||
public List<CellData> datas() { | ||
List<CellData> datas = new ArrayList<>(); | ||
datas.add(CellData.builder().value(name).build()); | ||
|
||
for (AttendRecord record : records) { | ||
datas.add(CellData.builder().value(record.result().getDescription()).build()); | ||
} | ||
datas.add(CellData.builder().value(attendanceCounts).build()); | ||
|
||
AttendRecordStatics recordStatics = AttendRecordStatics.from(records); | ||
|
||
datas.add(CellData.builder().value(String.valueOf(recordStatics.offline())).build()); | ||
datas.add(CellData.builder().value(String.valueOf(recordStatics.online())).build()); | ||
datas.add(CellData.builder().value(String.valueOf(recordStatics.late())).build()); | ||
datas.add(CellData.builder().value(String.valueOf(recordStatics.absent())).build()); | ||
|
||
return datas; | ||
} | ||
|
||
public record AttendRecord( | ||
String sessionName, | ||
AttendanceResult result | ||
) { | ||
public static AttendRecord of(Session session, AttendanceRecord attendanceRecord) { | ||
return new AttendRecord( | ||
session.getTitle(), | ||
attendanceRecord.getAttendanceResult() | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.