Skip to content

Commit

Permalink
exportfile feature
Browse files Browse the repository at this point in the history
  • Loading branch information
guma2k2 committed Dec 15, 2024
1 parent 4db83d9 commit dd4384c
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ResponseEntity<Void> removeCourse(@PathVariable("courseId") Long courseId

@DeleteMapping("/admin/promotions/{promotionId}")
public ResponseEntity<Void> remove(
@PathVariable("courseId") Long promotionId) {
@PathVariable("courseId") Long promotionId) {

promotionService.deletePromotion(promotionId);
return ResponseEntity.noContent().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,15 @@ public ResponseEntity<byte[]> exportData(@RequestParam("year") int year,
return new ResponseEntity<>(datas, headers, HttpStatus.OK);
}

@PostMapping("/statistic/course/export")
public ResponseEntity<byte[]> exportData(@RequestParam("from") String from,
@RequestParam("to") String to){
byte[] datas = statisticService.exportByCourse(from, to);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename=statistics.xlsx");
return new ResponseEntity<>(datas, headers, HttpStatus.OK);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,55 @@ public byte[] export(int year, Integer month) {
return outputStream.toByteArray();

}

public byte[] exportByCourse(String from, String to) {
List<StatisticCourse> datas = getByTime(from, to);
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Statistics");

// Create Header Row
Row headerRow = sheet.createRow(0);
CellStyle headerStyle = workbook.createCellStyle();
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerStyle.setFont(headerFont);

Cell headerCell = headerRow.createCell(0);
headerCell.setCellValue("Name course");
headerCell.setCellStyle(headerStyle);

headerCell = headerRow.createCell(1);
headerCell.setCellValue("Quantity");
headerCell.setCellStyle(headerStyle);

headerCell = headerRow.createCell(2);
headerCell.setCellValue("Total");
headerCell.setCellStyle(headerStyle);

// Populate Data Rows
int rowIdx = 1;
for (StatisticCourse stat : datas) {
Row row = sheet.createRow(rowIdx++);
row.createCell(0).setCellValue(stat.getCourse());
row.createCell(1).setCellValue(stat.getQuantity());
row.createCell(2).setCellValue(stat.getPrice());

}

// Auto-size Columns
sheet.autoSizeColumn(0);
sheet.autoSizeColumn(1);
sheet.autoSizeColumn(2);

// Write to ByteArrayOutputStream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
workbook.write(outputStream);
workbook.close();

} catch (IOException e) {
throw new RuntimeException(e);
}
return outputStream.toByteArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void addToCart_ShouldReturnCartListGetVM_WhenCourseIsAddedToCart() throws Except
CourseListGetVM courseListGetVM = new CourseListGetVM(
1L, "Course Title", "Course Headline", "Beginner",
"course-slug", "10h 30m", 25, 4.5, 100,
"image-url", 1999L, false, "Author Name"
"image-url", 1999L, 1999L ,false, "Author Name"
);
CartListGetVM cartListGetVM = new CartListGetVM(1L, courseListGetVM, false);
when(cartService.addCourseToCart(courseId)).thenReturn(cartListGetVM);
Expand All @@ -76,12 +76,12 @@ void listCartForCustomer_ShouldReturnListOfCartListGetVM() throws Exception {
CourseListGetVM course1 = new CourseListGetVM(
1L, "Course Title 1", "Course Headline 1", "Beginner",
"course-slug-1", "10h 30m", 25, 4.5, 100,
"image-url-1", 1999L, false, "Author Name 1"
"image-url-1", 1999L,1999L, false, "Author Name 1"
);
CourseListGetVM course2 = new CourseListGetVM(
2L, "Course Title 2", "Course Headline 2", "Intermediate",
"course-slug-2", "8h 15m", 20, 4.2, 80,
"image-url-2", 2999L, false, "Author Name 2"
"image-url-2", 2999L, 1999L,false, "Author Name 2"
);
List<CartListGetVM> cartList = List.of(
new CartListGetVM(1L, course1, false),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ void testGetPageableCourseByCategoryId() throws Exception {

List<CourseListGetVM> mockCourseList = Arrays.asList(
new CourseListGetVM(1L, "Course 1", "Headline 1", "Beginner", "course-1",
"10 hours", 12, 4.5, 100, "image1.jpg", 1000L, true, "John Doe"),
"10 hours", 12, 4.5, 100, "image1.jpg", 1000L,1999L, true, "John Doe"),
new CourseListGetVM(2L, "Course 2", "Headline 2", "Intermediate", "course-2",
"15 hours", 20, 4.7, 200, "image2.jpg", 1500L, false, "Jane Doe")
"15 hours", 20, 4.7, 200, "image2.jpg", 1500L,1999L, false, "Jane Doe")
);

// Mock the behavior of courseService
Expand Down Expand Up @@ -120,6 +120,7 @@ void testGetCourseById() throws Exception {
"2024-09-10",
true,
1000L,
1999L,
CourseStatus.PUBLISHED.name(),
1,
2,
Expand Down Expand Up @@ -181,6 +182,7 @@ void testCreateCourse() throws Exception {
"2024-09-10",
true,
1000L,
1999L,
CourseStatus.PUBLISHED.name(),
1,
2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ void createReview_ShouldReturnReview_WhenReviewCreated() throws Exception {
.andExpect(jsonPath("$.student.photo").value("photoUrl"))
.andExpect(jsonPath("$.created_at").value("2024-08-29T10:00:00Z"))
.andExpect(jsonPath("$.updated_at").value("2024-08-29T10:00:00Z"))
.andExpect(jsonPath("$.status").value(true));
.andExpect(jsonPath("$.status").value(ReviewStatus.PUBLISHED.name()));
}

@Test
Expand Down Expand Up @@ -114,7 +114,7 @@ void updateReview_ShouldReturnUpdatedReview_WhenReviewUpdated() throws Exception
.andExpect(jsonPath("$.student.photo").value("photoUrl"))
.andExpect(jsonPath("$.created_at").value("2024-08-30T10:00:00Z"))
.andExpect(jsonPath("$.updated_at").value("2024-08-30T10:00:00Z"))
.andExpect(jsonPath("$.status").value(true));
.andExpect(jsonPath("$.status").value(ReviewStatus.PUBLISHED.name()));
}

@Test
Expand Down Expand Up @@ -167,7 +167,7 @@ void getPageableReviews_ShouldReturnPagedReviews_WhenValidParams() throws Except
.andExpect(jsonPath("$.content[0].course.image").value("imageUrl"))
.andExpect(jsonPath("$.content[0].createdAt").value("2024-08-29T10:00:00Z"))
.andExpect(jsonPath("$.content[0].updatedAt").value("2024-08-29T10:00:00Z"))
.andExpect(jsonPath("$.content[0].status").value(true));
.andExpect(jsonPath("$.content[0].status").value(ReviewStatus.PUBLISHED.name()));
}

// @Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void addCourseToCart_shouldAddCourseToCart_whenCourseIsNotInCart() {
1500, // ratingCount
"https://example.com/images/java.png", // image
4999L,
1999L,
false,// price
"John Doe" // createdBy
);
Expand Down Expand Up @@ -117,7 +118,7 @@ void addCourseToCart_shouldReturnExistedCart_whenCourseIsAlreadyInCart() {
4.7, // averageRating
1500, // ratingCount
"https://example.com/images/java.png", // image
4999L,
4999L,1999L,
false,// price
"John Doe" // createdBy
);
Expand Down Expand Up @@ -245,13 +246,13 @@ void listCartForUser_shouldReturnCartListForUser() {
4.7, // averageRating
1500, // ratingCount
"https://example.com/images/java.png", // image
4999L,
4999L,1999L,
false,// price
"John Doe" // createdBy
);
CourseListGetVM courseListGetVM2 = new CourseListGetVM(
2L, "Advanced Java", "Deep dive into Java", "Advanced", "advanced-java",
"15h 45m", 35, 4.9, 3000, "https://example.com/images/java_advanced.png", 7999L, false,"Jane Smith"
"15h 45m", 35, 4.9, 3000, "https://example.com/images/java_advanced.png", 7999L, 1999L, false,"Jane Smith"
);

List<Cart> carts = List.of(cart1, cart2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ void createReviewForProduct_shouldReturnReviewVM_whenReviewIsCreatedSuccessfully
.course(course)
.content(reviewPostVM.content())
.ratingStar(reviewPostVM.ratingStar())
.status(ReviewStatus.UNDER_REVIEW)
.createdAt(LocalDateTime.now())
.updatedAt(LocalDateTime.now())
.build();
Expand All @@ -78,6 +79,7 @@ void createReviewForProduct_shouldReturnReviewVM_whenReviewIsCreatedSuccessfully
.id(1L)
.student(student)
.course(course)
.status(ReviewStatus.UNDER_REVIEW)
.content(reviewPostVM.content())
.ratingStar(reviewPostVM.ratingStar())
.createdAt(reviewToSave.getCreatedAt())
Expand Down Expand Up @@ -119,6 +121,7 @@ void updateReview_shouldReturnUpdatedReviewVM_whenReviewIsUpdatedSuccessfully()
.id(reviewId)
.content("Original content")
.student(student)
.status(ReviewStatus.UNDER_REVIEW)
.course(course)
.ratingStar(5)
.createdAt(LocalDateTime.now().minusDays(1))
Expand All @@ -129,6 +132,7 @@ void updateReview_shouldReturnUpdatedReviewVM_whenReviewIsUpdatedSuccessfully()
.id(reviewId)
.student(student)
.course(course)
.status(ReviewStatus.UNDER_REVIEW)
.content(reviewPostVM.content())
.ratingStar(reviewPostVM.ratingStar())
.createdAt(existingReview.getCreatedAt())
Expand Down

0 comments on commit dd4384c

Please sign in to comment.