Browse Source

feat: course表完善属性;提供了httpService调用其他子系统的服务

mjh 4 years ago
parent
commit
536940aef3

+ 11 - 0
seecoder-portal-common/src/main/java/cn/seecoder/seecoderportalcommon/vo/CourseVO.java

@@ -31,6 +31,8 @@ public class CourseVO {
 
 
     private OffsetDateTime createdTime;
     private OffsetDateTime createdTime;
 
 
+    private Double price;
+
     private CourseState courseState;
     private CourseState courseState;
 
 
     public Long getId() {
     public Long getId() {
@@ -153,6 +155,14 @@ public class CourseVO {
         this.createdTime = createdTime;
         this.createdTime = createdTime;
     }
     }
 
 
+    public Double getPrice() {
+        return price;
+    }
+
+    public void setPrice(Double price) {
+        this.price = price;
+    }
+
     @Override
     @Override
     public String toString() {
     public String toString() {
         return "CourseVO{" +
         return "CourseVO{" +
@@ -170,6 +180,7 @@ public class CourseVO {
                 ", coderId=" + coderId +
                 ", coderId=" + coderId +
                 ", helperId=" + helperId +
                 ", helperId=" + helperId +
                 ", createdTime=" + createdTime +
                 ", createdTime=" + createdTime +
+                ", price=" + price +
                 ", courseState=" + courseState +
                 ", courseState=" + courseState +
                 '}';
                 '}';
     }
     }

+ 6 - 0
seecoder-portal-server/pom.xml

@@ -121,6 +121,12 @@
             <groupId>com.aliyun</groupId>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-core</artifactId>
             <artifactId>aliyun-java-sdk-core</artifactId>
         </dependency>
         </dependency>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>fastjson</artifactId>
+            <version>1.2.79</version>
+        </dependency>
     </dependencies>
     </dependencies>
 
 
 </project>
 </project>

+ 13 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/domain/Course.java

@@ -74,6 +74,10 @@ public class Course {
     @NotNull
     @NotNull
     private OffsetDateTime createdTime;
     private OffsetDateTime createdTime;
 
 
+    @Column
+    @NotNull
+    private Double price;
+
     @Column
     @Column
     @Enumerated(EnumType.STRING)
     @Enumerated(EnumType.STRING)
     @NotNull
     @NotNull
@@ -199,6 +203,14 @@ public class Course {
         this.createdTime = createdTime;
         this.createdTime = createdTime;
     }
     }
 
 
+    public Double getPrice() {
+        return price;
+    }
+
+    public void setPrice(Double price) {
+        this.price = price;
+    }
+
     @Override
     @Override
     public String toString() {
     public String toString() {
         return "Course{" +
         return "Course{" +
@@ -216,6 +228,7 @@ public class Course {
                 ", coderId=" + coderId +
                 ", coderId=" + coderId +
                 ", helperId=" + helperId +
                 ", helperId=" + helperId +
                 ", createdTime=" + createdTime +
                 ", createdTime=" + createdTime +
+                ", price=" + price +
                 ", courseState=" + courseState +
                 ", courseState=" + courseState +
                 '}';
                 '}';
     }
     }

+ 15 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/exception/ExceptionType.java

@@ -0,0 +1,15 @@
+package cn.seecoder.seecoderportalserver.exception;
+
+import lombok.Getter;
+import org.springframework.http.HttpStatus;
+
+@Getter
+public enum ExceptionType {
+    PARAM_ERROR(HttpStatus.BAD_REQUEST), UNAUTHORIZED(HttpStatus.UNAUTHORIZED), FORBIDDEN(HttpStatus.FORBIDDEN), NOT_FOUND(HttpStatus.NOT_FOUND), CONFLICT(HttpStatus.CONFLICT), ERROR(HttpStatus.INTERNAL_SERVER_ERROR);
+
+    private final HttpStatus status;
+
+    ExceptionType(HttpStatus status) {
+        this.status = status;
+    }
+}

+ 19 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/exception/PortalException.java

@@ -0,0 +1,19 @@
+package cn.seecoder.seecoderportalserver.exception;
+
+import lombok.Getter;
+
+@Getter
+public class PortalException extends RuntimeException{
+    private final ExceptionType type;
+    private final String msg;
+
+    public PortalException(ExceptionType type, String msg) {
+        this.type = type;
+        this.msg = msg;
+    }
+
+    @Override
+    public String getMessage() {
+        return msg;
+    }
+}

+ 2 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/mapper/impl/CourseMapperImpl.java

@@ -43,6 +43,7 @@ public class CourseMapperImpl implements CourseMapper {
         courseVO.setCoderId(course.getCoderId());
         courseVO.setCoderId(course.getCoderId());
         courseVO.setHelperId(course.getHelperId());
         courseVO.setHelperId(course.getHelperId());
         courseVO.setCreatedTime(course.getCreatedTime());
         courseVO.setCreatedTime(course.getCreatedTime());
+        courseVO.setPrice(course.getPrice());
         courseVO.setCourseState(course.getCourseState());
         courseVO.setCourseState(course.getCourseState());
 
 
         return courseVO;
         return courseVO;
@@ -70,6 +71,7 @@ public class CourseMapperImpl implements CourseMapper {
         course.setCoderId(courseVO.getCoderId());
         course.setCoderId(courseVO.getCoderId());
         course.setHelperId(courseVO.getHelperId());
         course.setHelperId(courseVO.getHelperId());
         course.setCreatedTime(courseVO.getCreatedTime());
         course.setCreatedTime(courseVO.getCreatedTime());
+        course.setPrice(courseVO.getPrice());
         course.setCourseState(courseVO.getCourseState());
         course.setCourseState(courseVO.getCourseState());
 
 
         return course;
         return course;

+ 12 - 1
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/service/CourseService.java

@@ -9,6 +9,9 @@ import cn.seecoder.seecoderportalserver.mapper.CourseMapper;
 import cn.seecoder.seecoderportalserver.repository.CourseRepository;
 import cn.seecoder.seecoderportalserver.repository.CourseRepository;
 import cn.seecoder.seecoderportalserver.repository.CourseSelectionRepository;
 import cn.seecoder.seecoderportalserver.repository.CourseSelectionRepository;
 import cn.seecoder.seecoderportalserver.repository.UserRepository;
 import cn.seecoder.seecoderportalserver.repository.UserRepository;
+import cn.seecoder.seecoderportalserver.service.Impl.CoderService;
+import cn.seecoder.seecoderportalserver.service.Impl.EvalService;
+import cn.seecoder.seecoderportalserver.service.Impl.HelperService;
 import org.slf4j.Logger;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
@@ -27,12 +30,18 @@ public class CourseService {
     private final CourseMapper courseMapper;
     private final CourseMapper courseMapper;
     private final UserRepository userRepository;
     private final UserRepository userRepository;
     private final CourseSelectionRepository courseSelectionRepository;
     private final CourseSelectionRepository courseSelectionRepository;
+    private final CoderService coderService;
+    private final HelperService helperService;
+    private final EvalService evalService;
 
 
-    public CourseService(CourseRepository courseRepository, CourseMapper courseMapper, UserRepository userRepository, CourseSelectionRepository courseSelectionRepository) {
+    public CourseService(CourseRepository courseRepository, CourseMapper courseMapper, UserRepository userRepository, CourseSelectionRepository courseSelectionRepository, CoderService coderService, HelperService helperService, EvalService evalService) {
         this.courseRepository = courseRepository;
         this.courseRepository = courseRepository;
         this.courseMapper = courseMapper;
         this.courseMapper = courseMapper;
         this.userRepository = userRepository;
         this.userRepository = userRepository;
         this.courseSelectionRepository = courseSelectionRepository;
         this.courseSelectionRepository = courseSelectionRepository;
+        this.coderService = coderService;
+        this.helperService = helperService;
+        this.evalService = evalService;
     }
     }
 
 
     public CourseVO getCourse(Long courseId) {
     public CourseVO getCourse(Long courseId) {
@@ -45,6 +54,8 @@ public class CourseService {
     }
     }
 
 
     public CourseVO createCourse(CourseVO courseVO) {
     public CourseVO createCourse(CourseVO courseVO) {
+        evalService.getAllExams();
+
         Optional<User> teacher = userRepository.findById(courseVO.getTeacher().getId());
         Optional<User> teacher = userRepository.findById(courseVO.getTeacher().getId());
         // 确认授课老师ID存在
         // 确认授课老师ID存在
         if (!teacher.isPresent()) {
         if (!teacher.isPresent()) {

+ 64 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/service/HttpService.java

@@ -0,0 +1,64 @@
+package cn.seecoder.seecoderportalserver.service;
+
+import cn.seecoder.seecoderportalserver.exception.ExceptionType;
+import cn.seecoder.seecoderportalserver.exception.PortalException;
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import org.apache.http.Consts;
+import org.apache.http.HttpEntity;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.springframework.stereotype.Service;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+
+import javax.servlet.http.HttpServletRequest;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+
+@Service
+public class HttpService {
+
+    public String sendPost(String uri, JSONObject requestBody, String token) throws Exception {
+        StringEntity requestEntity = new StringEntity(JSON.toJSONString(requestBody));
+        HttpPost httpPost = new HttpPost(uri);
+        httpPost.setHeader("Authorization", token);
+        httpPost.setHeader("Content-Type", "application/json;chartset=UTF-8");
+        httpPost.setEntity(requestEntity);
+        httpPost.setConfig(RequestConfig.DEFAULT);
+        CloseableHttpClient client = HttpClients.createDefault();
+        CloseableHttpResponse response = client.execute(httpPost);
+        HttpEntity entity = response.getEntity();
+        String result = EntityUtils.toString(entity);
+        client.close();
+        return result;
+    }
+
+    public String sendGet(String uri, List<NameValuePair> params, String token) throws Exception {
+        String requestParam = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
+        HttpGet httpGet = new HttpGet(uri + "?" + requestParam);
+        httpGet.setHeader("Authorization", token);
+        httpGet.setConfig(RequestConfig.DEFAULT);
+        CloseableHttpClient client = HttpClients.createDefault();
+        CloseableHttpResponse response = client.execute(httpGet);
+        HttpEntity entity = response.getEntity();
+        String result = EntityUtils.toString(entity);
+        client.close();
+        return result;
+    }
+
+    public String getToken() {
+        HttpServletRequest httpServletRequest = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
+        String token = Optional.ofNullable(httpServletRequest.getHeader("Authorization")).orElseThrow(() -> new PortalException(ExceptionType.UNAUTHORIZED, "缺少Token"));
+        return token;
+    }
+}

+ 62 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/service/Impl/CoderService.java

@@ -0,0 +1,62 @@
+package cn.seecoder.seecoderportalserver.service.Impl;
+
+import cn.seecoder.seecoderportalcommon.vo.CourseVO;
+import cn.seecoder.seecoderportalserver.service.HttpService;
+import cn.seecoder.seecoderportalserver.service.SubsystemService;
+import com.alibaba.fastjson.JSONObject;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.util.EntityUtils;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Service("coderService")
+@Transactional(rollbackFor = RuntimeException.class)
+public class CoderService implements SubsystemService {
+    HttpService httpService;
+
+    public CoderService(HttpService httpService) {
+        this.httpService = httpService;
+    }
+
+    @Override
+    public void createService(CourseVO courseVO){
+        String uri = "https://coder-server.seec.seecoder.cn/api/course";
+        String token = httpService.getToken();
+        JSONObject requestBody = new JSONObject();
+        requestBody.put("name", courseVO.getName());
+        requestBody.put("code", courseVO.getInvitationCode());
+        requestBody.put("price", courseVO.getPrice().toString());
+        String result = null;
+        try {
+            result = httpService.sendPost(uri, requestBody, token);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        System.out.println(result);
+    }
+
+    @Override
+    public void deleteService() {
+
+    }
+
+    public void getCoursesByTeacher(){
+        String uri = "https://coder-server.seec.seecoder.cn/api/course";
+        String token = httpService.getToken();
+        List<NameValuePair> param = new ArrayList<>();
+        String result = null;
+        try {
+            result = httpService.sendGet(uri, param, token);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        System.out.println(result);
+    }
+}

+ 46 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/service/Impl/EvalService.java

@@ -0,0 +1,46 @@
+package cn.seecoder.seecoderportalserver.service.Impl;
+
+import cn.seecoder.seecoderportalcommon.vo.CourseVO;
+import cn.seecoder.seecoderportalserver.service.HttpService;
+import cn.seecoder.seecoderportalserver.service.SubsystemService;
+import org.apache.http.NameValuePair;
+import org.apache.http.message.BasicNameValuePair;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Service
+@Transactional(rollbackFor = RuntimeException.class)
+public class EvalService implements SubsystemService {
+    HttpService httpService;
+
+    public EvalService(HttpService httpService) {
+        this.httpService = httpService;
+    }
+
+    @Override
+    public void createService(CourseVO courseVO) {
+
+    }
+
+    @Override
+    public void deleteService() {
+
+    }
+
+    public void getAllExams(){
+        String uri = "https://eval-server.seec.seecoder.cn/api/exam/list";
+        String token = httpService.getToken();
+        List<NameValuePair> params = new ArrayList<>();
+        params.add(new BasicNameValuePair("pageSize", "10"));
+        params.add(new BasicNameValuePair("pageNum", "1"));
+        try {
+            String result = httpService.sendGet(uri, params, token);
+            System.out.println(result);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 20 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/service/Impl/HelperService.java

@@ -0,0 +1,20 @@
+package cn.seecoder.seecoderportalserver.service.Impl;
+
+import cn.seecoder.seecoderportalcommon.vo.CourseVO;
+import cn.seecoder.seecoderportalserver.service.SubsystemService;
+import org.springframework.stereotype.Service;
+import org.springframework.transaction.annotation.Transactional;
+
+@Service
+@Transactional(rollbackFor = RuntimeException.class)
+public class HelperService implements SubsystemService {
+    @Override
+    public void createService(CourseVO courseVO) {
+
+    }
+
+    @Override
+    public void deleteService() {
+
+    }
+}

+ 11 - 0
seecoder-portal-server/src/main/java/cn/seecoder/seecoderportalserver/service/SubsystemService.java

@@ -0,0 +1,11 @@
+package cn.seecoder.seecoderportalserver.service;
+
+import cn.seecoder.seecoderportalcommon.vo.CourseVO;
+
+import java.io.UnsupportedEncodingException;
+
+public interface SubsystemService {
+    void createService(CourseVO courseVO) throws Exception;
+
+    void deleteService();
+}