Skip to content

Commit

Permalink
vinh/add submit exam method
Browse files Browse the repository at this point in the history
  • Loading branch information
AnataAria committed Nov 6, 2023
1 parent 6597dad commit 763d4a1
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.group1.drawingcouseselling.controller;

import com.group1.drawingcouseselling.model.dto.CourseContentCompletionDto;
import com.group1.drawingcouseselling.model.dto.ExamDto;
import com.group1.drawingcouseselling.service.CourseContentCompletionService;
import com.group1.drawingcouseselling.service.JwtService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;
Expand Down Expand Up @@ -36,4 +38,11 @@ public ResponseEntity<List<CourseContentCompletionDto>> getCustomerCourseComplet
String email = jwtService.extractUserEmail(authorization.substring(7));
return ResponseEntity.ok(courseContentCompletionService.checkCourseContentCompleted(id, email));
}
@GetMapping("/course-content-completion/submit-exam")
public ResponseEntity<ExamDto>submitExam(@RequestParam(value = "id")BigDecimal id,
@RequestParam(value = "file")MultipartFile file,
@RequestHeader(value = "Authorization") String authorization){
String email = jwtService.extractUserEmail(authorization.substring(7));
return ResponseEntity.ok(courseContentCompletionService.submitCourseContentExam(id, file, email));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.group1.drawingcouseselling.model.dto;

import com.group1.drawingcouseselling.model.enums.EExamStatus;
import lombok.Builder;

import java.math.BigDecimal;
@Builder
public record ExamDto(BigDecimal id, Character score, String artLink, EExamStatus examStatus, String comment) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ public void setUpdateDate(Date updateDate) {
this.updateDate = updateDate;
}

public ECourseContentType getCourseContentType() {
return courseContentType;
}

public void setCourseContentType(ECourseContentType courseContentType) {
this.courseContentType = courseContentType;
}

@Override
public CourseContent covertDtoToEntity(CourseContentDto data) {
CourseContent lesson = new CourseContent();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.group1.drawingcouseselling.model.entity;

import com.group1.drawingcouseselling.model.enums.ECourseContentType;
import jakarta.persistence.*;

import java.math.BigDecimal;
Expand All @@ -22,6 +23,9 @@ public class CourseContentCompletion {
private Date finishDate;
@Column(name = "done")
private Boolean isDone;
@JoinColumn(name = "content_exam")
@OneToOne(fetch = FetchType.LAZY, cascade = {CascadeType.DETACH, CascadeType.PERSIST}, targetEntity = Exam.class)
private Exam exam;
@PreUpdate
@PrePersist
private void courseCompleted(){
Expand Down Expand Up @@ -69,4 +73,12 @@ public CourseContent getCourseContent() {
public void setCourseContent(CourseContent courseContent) {
this.courseContent = courseContent;
}

public Exam getExam() {
return exam;
}

public void setExam(Exam exam) {
this.exam = exam;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.group1.drawingcouseselling.model.entity;

import com.group1.drawingcouseselling.model.enums.EExamStatus;
import jakarta.persistence.*;

import java.math.BigDecimal;
Expand All @@ -8,14 +9,40 @@
public class Exam {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id", columnDefinition = "bigint")
@Column(name = "id", columnDefinition = "bigint")
private BigDecimal id;
@Column(name="title")
private String title;
@Column(name="description", columnDefinition = "text")
private String description;
@Column(name="videoLink")
private String videoLink;
@OneToOne(targetEntity = Instructor.class, cascade = CascadeType.DETACH)
@JoinColumn(name = "assignment_instructor")
private Instructor assignmentInstructor;
@OneToOne(targetEntity = CourseContentCompletion.class, cascade = {CascadeType.DETACH, CascadeType.PERSIST}, fetch = FetchType.LAZY)
@JoinColumn(name = "course_content_completion_id")
private CourseContentCompletion courseContentCompletion;
@Column(name = "score")
private Character score;
@Column(name = "art_url", nullable = false)
private String artLinked;
@Column(name = "instructor_comment")
private String instructorComment;
@Enumerated(EnumType.STRING)
private EExamStatus submitStatus;
@PreUpdate
private void updateScoreStatus(){
if(score != null){
courseContentCompletion.setDone(switch (score) {
case 'A', 'B', 'C' -> true;
default -> false;
});

}else {
courseContentCompletion.setDone(false);
submitStatus = EExamStatus.SUBMITTED;
}
}

@PrePersist
private void updateExamDefaultStatus(){
if(score == null) submitStatus = EExamStatus.SUBMITTED;
}

public BigDecimal getId() {
return id;
Expand All @@ -25,27 +52,51 @@ public void setId(BigDecimal id) {
this.id = id;
}

public String getTitle() {
return title;
public Instructor getAssignmentInstructor() {
return assignmentInstructor;
}

public void setAssignmentInstructor(Instructor assignmentInstructor) {
this.assignmentInstructor = assignmentInstructor;
}

public CourseContentCompletion getCourseContentCompletion() {
return courseContentCompletion;
}

public void setCourseContentCompletion(CourseContentCompletion courseContentCompletion) {
this.courseContentCompletion = courseContentCompletion;
}

public Character getScore() {
return score;
}

public void setScore(Character score) {
this.score = score;
}

public String getArtLinked() {
return artLinked;
}

public void setTitle(String title) {
this.title = title;
public void setArtLinked(String artLinked) {
this.artLinked = artLinked;
}

public String getDescription() {
return description;
public EExamStatus getSubmitStatus() {
return submitStatus;
}

public void setDescription(String description) {
this.description = description;
public void setSubmitStatus(EExamStatus submitStatus) {
this.submitStatus = submitStatus;
}

public String getVideoLink() {
return videoLink;
public String getInstructorComment() {
return instructorComment;
}

public void setVideoLink(String videoLink) {
this.videoLink = videoLink;
public void setInstructorComment(String instructorComment) {
this.instructorComment = instructorComment;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.group1.drawingcouseselling.service;

import com.group1.drawingcouseselling.model.dto.CourseContentCompletionDto;
import com.group1.drawingcouseselling.model.dto.ExamDto;
import org.springframework.web.multipart.MultipartFile;

import java.math.BigDecimal;
import java.util.List;
Expand All @@ -11,4 +13,5 @@ public interface CourseContentCompletionService {
public BigDecimal getQuantityCustomerAccessCourseContent(BigDecimal courseContentID);
public Integer getTotalCourseContentLearnedOnCourse(BigDecimal customerID, BigDecimal courseID);
public Boolean checkCourseContentCompleted(BigDecimal courseContentID, String customerEmail);
public ExamDto submitCourseContentExam(BigDecimal courseContentID, MultipartFile submitFile, String email);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.group1.drawingcouseselling.service.impl;

import com.group1.drawingcouseselling.exception.CourseNotFoundException;
import com.group1.drawingcouseselling.exception.CourseNotOwnedException;
import com.group1.drawingcouseselling.exception.UserNotFoundException;
import com.group1.drawingcouseselling.exception.*;
import com.group1.drawingcouseselling.model.dto.CourseContentCompletionDto;
import com.group1.drawingcouseselling.model.dto.ExamDto;
import com.group1.drawingcouseselling.model.entity.CourseContentCompletion;
import com.group1.drawingcouseselling.model.entity.Exam;
import com.group1.drawingcouseselling.model.enums.EExamStatus;
import com.group1.drawingcouseselling.model.enums.ES3;
import com.group1.drawingcouseselling.repository.CourseContentCompletionRepository;
import com.group1.drawingcouseselling.service.CourseContentCompletionService;
import com.group1.drawingcouseselling.service.CourseContentService;
import com.group1.drawingcouseselling.service.CourseService;
import com.group1.drawingcouseselling.service.CustomerService;
import com.group1.drawingcouseselling.repository.ExamRepository;
import com.group1.drawingcouseselling.service.*;
import com.group1.drawingcouseselling.util.Tool;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

Expand All @@ -23,6 +26,8 @@ public class CourseContentCompletionServiceImpl implements CourseContentCompleti
private final CourseContentService courseContentService;
private final CourseService courseService;
private final CourseContentCompletionRepository courseContentCompletionRepository;
private final MetadataService metadataService;
private final ExamRepository examRepository;
@Override
public CourseContentCompletionDto markCourseContentCompletion(BigDecimal courseContentID, String customerEmail){
var customerInfo = customerService.searchCustomerByEmail(customerEmail);
Expand Down Expand Up @@ -78,4 +83,58 @@ public Boolean checkCourseContentCompleted(BigDecimal courseContentID, String em
var result = courseContentCompletionRepository.searchCourseContentCompletionByCourseContentAndCustomerID(courseContentID,customer.getId());
return result !=null;
}
@Override
public ExamDto submitCourseContentExam(BigDecimal courseContentID, MultipartFile submitFile, String email){
String s3Path = "https://ademyimage.s3.ap-southeast-1.amazonaws.com/images/";
var customer = customerService.searchCustomerByEmail(email).orElseThrow(()
-> new UserNotFoundException("Not found customer with this email"));
var courseContent = courseContentService.getCourseContentById(courseContentID);
var course = courseService.getCourseByCourseContentID(courseContentID);
customer.getCourseList().stream().parallel().filter(c -> c.getId().equals(course.getId())).findFirst().orElseThrow(() -> new CourseNotOwnedException("You haven't owned this course"));
var currentCourseContentCompletion = courseContentCompletionRepository.searchCourseContentCompletionByCourseContentAndCustomerID(courseContent.getId(), customer.getId());
Exam exam = null;
if(currentCourseContentCompletion == null){
currentCourseContentCompletion = new CourseContentCompletion();
currentCourseContentCompletion.setCustomer(customer);
currentCourseContentCompletion.setCourseContent(courseContent);
currentCourseContentCompletion.setDone(false);
exam = new Exam();
currentCourseContentCompletion.setExam(exam);
exam.setAssignmentInstructor(course.getInstuctor());
try {
var name = course.getId().toString() + "_" + courseContent.getId().toString()
+ "_" + customer.getId().toString() + "_" + "ccontentexam";
var currentFile = Tool.changeMultipartFileName(name, submitFile);
var path = metadataService.upload(currentFile, ES3.images);
exam.setArtLinked(s3Path + path);
exam.setCourseContentCompletion(currentCourseContentCompletion);
} catch (IOException e) {
throw new SomethingWentWrongExceptions("Something went wrong with uploading file to S3");
}
}else {
exam = currentCourseContentCompletion.getExam();
if(exam.getSubmitStatus() == EExamStatus.PASSED) throw new ConditionNotMetException("You have already completed the exam");
else if(exam.getSubmitStatus() == EExamStatus.SUBMITTED) throw new ConditionNotMetException("You have already submit your work");
try{
var name = course.getId().toString() + "_" + courseContent.getId().toString()
+ "_" + customer.getId().toString() + "_" + "ccontentexam";
var currentFile = Tool.changeMultipartFileName(name, submitFile);
var path = metadataService.upload(currentFile, ES3.images);
exam.setArtLinked(s3Path + path);
exam.setSubmitStatus(EExamStatus.SUBMITTED);
exam.setScore(null);
exam.setInstructorComment("");
}catch (IOException e){
throw new SomethingWentWrongExceptions("Something went wrong with uploading file to S3");
}

}
courseContentCompletionRepository.save(currentCourseContentCompletion);
return ExamDto.builder()
.id(currentCourseContentCompletion.getExam().getId())
.comment(exam.getInstructorComment())
.artLink(exam.getArtLinked())
.examStatus(exam.getSubmitStatus())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.group1.drawingcouseselling.util;

import com.group1.drawingcouseselling.exception.SomethingWentWrongExceptions;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Tool {
Expand All @@ -10,7 +16,20 @@ public static String getYouTubeId(String youTubeUrl) {
if (matcher.find()) {
return matcher.group();
} else {
return "error";
throw new SomethingWentWrongExceptions("Could get link ");
}
}
public static MultipartFile changeMultipartFileName(String name, MultipartFile currentFile){
String originalFileName = currentFile.getOriginalFilename();
String fileExtension = StringUtils.getFilenameExtension(originalFileName);

String newFileName = name + "." + fileExtension;

try {
byte[] fileBytes = currentFile.getBytes();
return new MockMultipartFile(newFileName, newFileName, currentFile.getContentType(), fileBytes);
} catch (IOException e) {
throw new SomethingWentWrongExceptions("Something wrong in processing of change name of file");
}
}
}

0 comments on commit 763d4a1

Please sign in to comment.