feat: implement dynamic training course tab system and modular PDF data migration scripts
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
package com.sisvietnamvn.web;
|
||||
|
||||
import com.sisvietnamvn.web.domain.Category;
|
||||
import com.sisvietnamvn.web.domain.PageStatus;
|
||||
import com.sisvietnamvn.web.domain.Post;
|
||||
import com.sisvietnamvn.web.domain.PostLayout;
|
||||
import com.sisvietnamvn.web.repository.CategoryRepository;
|
||||
import com.sisvietnamvn.web.repository.PostRepository;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
|
||||
@SpringBootTest(classes = SisvietnamvnApp.class)
|
||||
public class SeedCourseTest {
|
||||
|
||||
@Autowired
|
||||
private CategoryRepository categoryRepository;
|
||||
|
||||
@Autowired
|
||||
private PostRepository postRepository;
|
||||
|
||||
@Test
|
||||
public void seedNoiSoiCourse() throws IOException {
|
||||
System.out.println("=========================================");
|
||||
System.out.println("STARTING SEED COURSE OPERATION...");
|
||||
System.out.println("=========================================");
|
||||
|
||||
// 1. Find or create the Category 'dao-tao'
|
||||
String categorySlug = "dao-tao";
|
||||
Category category = categoryRepository.findBySlug(categorySlug).orElseGet(() -> {
|
||||
Category cat = new Category();
|
||||
// Generate id dynamically
|
||||
Long nextId = categoryRepository.count() + 1;
|
||||
cat.setId(nextId);
|
||||
cat.setName("Đào tạo");
|
||||
cat.setSlug(categorySlug);
|
||||
cat.setDescription("Chương trình đào tạo y khoa");
|
||||
cat.setCreatedBy("system");
|
||||
cat.setCreatedDate(Instant.now());
|
||||
cat = categoryRepository.save(cat);
|
||||
System.out.println("Created new Category 'dao-tao' with ID: " + cat.getId());
|
||||
return cat;
|
||||
});
|
||||
|
||||
// 2. Read content from parsed files
|
||||
String content = Files.readString(Paths.get("/tmp/course_content.html"));
|
||||
String excerpt = Files.readString(Paths.get("/tmp/course_excerpt.html"));
|
||||
|
||||
// 3. Find or create the Post
|
||||
String postSlug = "chuong-trinh-chung-chi-dao-tao-ky-thuat-chuyen-mon-noi-soi-da-day-noi-soi-dai-trang-va-noi-soi-dieu-tri-co-ban-khoa-02";
|
||||
Optional<Post> postOpt = postRepository.findBySlug(postSlug);
|
||||
|
||||
Post post;
|
||||
if (postOpt.isPresent()) {
|
||||
post = postOpt.get();
|
||||
System.out.println("Found existing Post with ID: " + post.getId() + ", updating...");
|
||||
} else {
|
||||
post = new Post();
|
||||
// Generate id dynamically
|
||||
Long nextId = postRepository.count() + 1000L;
|
||||
post.setId(nextId);
|
||||
post.setSlug(postSlug);
|
||||
post.setCreatedBy("system");
|
||||
post.setCreatedDate(Instant.now());
|
||||
System.out.println("Creating new Post with ID: " + post.getId());
|
||||
}
|
||||
|
||||
post.setTitle("CHƯƠNG TRÌNH CHỨNG CHỈ ĐÀO TẠO KỸ THUẬT CHUYÊN MÔN: NỘI SOI DẠ DÀY, NỘI SOI ĐẠI TRÀNG VÀ NỘI SOI ĐIỀU TRỊ CƠ BẢN, KHÓA 02");
|
||||
post.setContent(content);
|
||||
post.setExcerpt(excerpt);
|
||||
post.setLocation("Giảng đường 3A, lầu 3, khu A, Bệnh viện Đại học Y Dược Thành phố Hồ Chí Minh (215 Hồng Bàng, Phường Chợ Lớn, Thành phố Hồ Chí Minh)");
|
||||
post.setMetaDescription("38.000.000 VNĐ");
|
||||
post.setEventTime(Instant.parse("2026-07-27T07:30:00Z"));
|
||||
post.setStatus(PageStatus.PUBLISHED);
|
||||
post.setLayout(PostLayout.TRAINING);
|
||||
post.setCategory(category);
|
||||
post.setFeaturedImage("https://console.bvdaihoc.com.vn/uploads/KHDT/T%E1%BB%95%20S%E1%BB%B1%20Ki%E1%BB%87n%202026/07/N%E1%BB%98I-SOI-D%E1%BA%A0-D%C3%80Y,-N%E1%BB%98I-SOI-%C4%90%E1%BA%A0I-TR%C3%80NG-V%C3%80-N%E1%BB%98I-SOI-%C4%90I%E1%BB%80U-TR%E1%BB%8A-C%C6%A0-B%E1%BA%A2N-(1).gif");
|
||||
|
||||
postRepository.save(post);
|
||||
|
||||
System.out.println("=========================================");
|
||||
System.out.println("SEED COURSE COMPLETED SUCCESSFULLY!");
|
||||
System.out.println("URL path: /dao-tao/" + postSlug);
|
||||
System.out.println("=========================================");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user