Java的新项目学成在线笔记-day9(十五)

5.4 课程预览服务端 
5.4.1 Api定义 
此Api是课程管理前端请求服务端进行课程预览的Api 请求:课程Id
响应:课程预览Url 1、定义响应类型

[AppleScript] 纯文本查看 复制代码

?

1

2

3

4

5

6

7

8

@Data

 @ToString

@NoArgsConstructor public class CoursePublishResult extends ResponseResult {  

  String previewUrl;  

  public CoursePublishResult(ResultCode resultCode,String previewUrl) { 

       super(resultCode);   

     this.previewUrl = previewUrl;

    } }


2、接口定义如下
 

[AppleScript] 纯文本查看 复制代码

?

1

2

@ApiOperation("预览课程")

public CoursePublishResult preview(String id);


5.4.2 创建 Feign Client 
在课程管理工程创建CMS服务的Feign Client,通过此Client远程请求cms添加页面。
 

[AppleScript] 纯文本查看 复制代码

?

1

2

3

4

5

@FeignClient(value = XcServiceList.XC_SERVICE_MANAGE_CMS)

  public interface CmsPageClient{

     //保存页面 

    @PostMapping("/cms/page/save") 

    public CmsPageResult save(@RequestBody CmsPage cmsPage)}


5.4.3  Service 
1、配置添加页面参数信息
在application.yml中配置:
 

[AppleScript] 纯文本查看 复制代码

?

1

2

3

4

5

6

7

course‐publish:

 siteId: 5b30cba5f58b4411fc6cb1e5

  templateId: 5b345a6b94db44269cb2bfec  

previewUrl: [url]http://www.xuecheng.com/cms/preview/[/url

 pageWebPath: /course/detail/ 

 pagePhysicalPath: /course/detail/ 

 dataUrlPre: http://localhost:31200/course/courseview/


2、代码如下:
 

[AppleScript] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

@Value("${course‐publish.dataUrlPre}") 

   private String publish_dataUrlPre; 

   @Value("${course‐publish.pagePhysicalPath}")  

  private String publish_page_physicalpath; 

   @Value("${course‐publish.pageWebPath}") 

   private String publish_page_webpath;

    @Value("${course‐publish.siteId}")

   private String publish_siteId;

    @Value("${course‐publish.templateId}")  

  private String publish_templateId;  

  @Value("${course‐publish.previewUrl}") 

   private String previewUrl;  

       //根据id查询课程基本信息  

  public CourseBase findCourseBaseById(String courseId){   

     Optional<CourseBase>

 baseOptional = courseBaseRepository.findById(courseId);

        if(baseOptional.isPresent()){

[AppleScript] 纯文本查看 复制代码

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

CourseBase courseBase = baseOptional.get();  

         return courseBase;   

    }    

   ExceptionCast.cast(CourseCode.COURSE_GET_NOTEXISTS);   

    return null;

   }   

//课程预览 

  public CoursePublishResult preview(String courseId){ 

      CourseBase one = this.findCourseBaseById(courseId);  

       //发布课程预览页面  

     CmsPage cmsPage = new CmsPage()

      //站点     

  cmsPage.setSiteId(publish_siteId);//课程预览站点  

     //模板     

  cmsPage.setTemplateId(publish_templateId);   

    //页面名称  

     cmsPage.setPageName(courseId+".html")

      //页面别名   

    cmsPage.setPageAliase(one.getName());      

 //页面访问路径  

     cmsPage.setPageWebPath(publish_page_webpath)

      //页面存储路径   

    cmsPage.setPagePhysicalPath(publish_page_physicalpath)

      //数据url       

cmsPage.setDataUrl(publish_dataUrlPre+courseId);

       //远程请求cms保存页面信息    

   CmsPageResult cmsPageResult = cmsPageClient.save(cmsPage);   

    if(!cmsPageResult.isSuccess()){   

        return new CoursePublishResult(CommonCode.FAIL,null);   

    }    

   //页面id    

   String pageId = cmsPageResult.getCmsPage().getPageId();    

   //页面url   

    String pageUrl = previewUrl+pageId;  

     return new CoursePublishResult(CommonCode.SUCCESS,pageUrl)

    }


5.4.4 Controller 

[AppleScript] 纯文本查看 复制代码

?

1

2

3

4

@Override

@PostMapping("/preview/{id}") public CoursePublishResult

preview(@PathVariable("id") String id) {

    return courseService.preview(id); }

猜你喜欢

转载自blog.csdn.net/czbkzmj/article/details/89632602
今日推荐