Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

export file feature #98

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.backend.elearning.domain.student.Student;
import com.backend.elearning.domain.user.User;
import com.backend.elearning.domain.user.UserGetVM;
import com.backend.elearning.utils.DateTimeUtils;

public record AnswerLecture(
Long id,
Expand All @@ -18,14 +19,25 @@ public record AnswerLecture(
public static final AnswerLecture fromModelStudent(StudentAnswer studentAnswer) {
Student student = studentAnswer.getStudent();
UserGetVM userGetVM = UserGetVM.fromModelStudent(student);
return new AnswerLecture(studentAnswer.getId(), studentAnswer.getContent(), studentAnswer.getCreatedAt().toString(),
studentAnswer.getUpdatedAt().toString(), userGetVM);
String createdAt = studentAnswer.getCreatedAt() != null ?
DateTimeUtils.convertLocalDateTimeToString(studentAnswer.getCreatedAt()) : "";
String updatedAt = studentAnswer.getUpdatedAt() != null ?
DateTimeUtils.convertLocalDateTimeToString(studentAnswer.getUpdatedAt()) : "";
return new AnswerLecture(studentAnswer.getId(), studentAnswer.getContent(),
createdAt,
updatedAt, userGetVM);
}

public static final AnswerLecture fromModelUser(UserAnswer userAnswer) {
User user = userAnswer.getUser();
UserGetVM userGetVM = UserGetVM.fromModel(user);
return new AnswerLecture(userAnswer.getId(), userAnswer.getContent(), userAnswer.getCreatedAt().toString(),
userAnswer.getUpdatedAt().toString(), userGetVM);
String createdAt = userAnswer.getCreatedAt() != null ?
DateTimeUtils.convertLocalDateTimeToString(userAnswer.getCreatedAt()) : "";
String updatedAt = userAnswer.getUpdatedAt() != null ?
DateTimeUtils.convertLocalDateTimeToString(userAnswer.getUpdatedAt()) : "";
return new AnswerLecture(userAnswer.getId(),
userAnswer.getContent(),
createdAt,
updatedAt, userGetVM);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ public class QuestionLecture extends AbstractAuditEntity {
@JoinColumn(name = "student_id")
private Student student;

@OneToMany(mappedBy = "question_lecture", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "questionLecture", cascade = CascadeType.ALL, orphanRemoval = true)
private List<UserAnswer> userAnswers = new ArrayList<>();


@OneToMany(mappedBy = "question_lecture", cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(mappedBy = "questionLecture", cascade = CascadeType.ALL, orphanRemoval = true)
private List<StudentAnswer> studentAnswers = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1")
Expand All @@ -36,5 +35,34 @@ public ResponseEntity<QuestionLectureVM> create (
return ResponseEntity.status(HttpStatus.OK).body(questionLectureVM);
}

@PutMapping("/question-lecture/{id}")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Updated", content =
@Content(schema = @Schema(implementation = QuestionLectureVM.class)))
})
public ResponseEntity<QuestionLectureVM> update (
@RequestBody QuestionLecturePostVM questionLecturePostVM,
@PathVariable("id") Long id
) {
QuestionLectureVM questionLectureVM = questionLectureService.update(questionLecturePostVM, id);
return ResponseEntity.status(HttpStatus.OK).body(questionLectureVM);
}

@DeleteMapping("/question-lecture/{id}")
public ResponseEntity<Void> delete (
@PathVariable("id") Long id
) {
questionLectureService.delete(id);
return ResponseEntity.noContent().build();
}

@GetMapping("/question-lecture/lectures/{lectureId}")

public ResponseEntity<List<QuestionLectureGetVM>> getByLectureId (
@PathVariable("lectureId") Long lectureId
) {
List<QuestionLectureGetVM> response = questionLectureService.getByLectureId(lectureId);
return ResponseEntity.status(HttpStatus.OK).body(response);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.backend.elearning.domain.lecture.LectureGetVM;
import com.backend.elearning.domain.user.UserGetVM;
import com.backend.elearning.utils.DateTimeUtils;

import java.util.List;

Expand All @@ -18,12 +19,16 @@ public record QuestionLectureGetVM(
public static QuestionLectureGetVM fromModel(QuestionLecture questionLecture, List<AnswerLecture> answers) {
LectureGetVM lectureGetVM = LectureGetVM.fromModel(questionLecture.getLecture());
UserGetVM userGetVM = UserGetVM.fromModelStudent(questionLecture.getStudent());
String createdAt = questionLecture.getCreatedAt() != null ?
DateTimeUtils.convertLocalDateTimeToString(questionLecture.getCreatedAt()) : "";
String updatedAt = questionLecture.getUpdatedAt() != null ?
DateTimeUtils.convertLocalDateTimeToString(questionLecture.getUpdatedAt()) : "";
return new QuestionLectureGetVM(questionLecture.getId(),
questionLecture.getTitle(),
questionLecture.getDescription(),
userGetVM,
lectureGetVM, questionLecture.getCreatedAt().toString(),
questionLecture.getUpdatedAt().toString(),
lectureGetVM, createdAt,
updatedAt,
answers);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ public interface QuestionLectureService {

QuestionLectureVM create(QuestionLecturePostVM questionLecturePostVM);

QuestionLectureVM update(QuestionLecturePostVM questionLecturePostVM, Long questionLectureId);

void delete(Long questionLectureId);

List<QuestionLectureGetVM> getByLectureId(Long lectureId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@
import com.backend.elearning.domain.questionLecture.userAnswer.UserAnswerRepo;
import com.backend.elearning.domain.student.Student;
import com.backend.elearning.domain.student.StudentRepository;
import com.backend.elearning.exception.BadRequestException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Service
Expand Down Expand Up @@ -52,6 +55,26 @@ public QuestionLectureVM create(QuestionLecturePostVM questionLecturePostVM) {
return QuestionLectureVM.fromModel(savedQuestionLecture);
}

@Override
public QuestionLectureVM update(QuestionLecturePostVM questionLecturePostVM, Long questionLectureId) {
QuestionLecture questionLecture = questionLectureRepo.findById(questionLectureId).orElseThrow();
questionLecture.setTitle(questionLecturePostVM.title());
questionLecture.setDescription(questionLecturePostVM.description());
questionLecture.setUpdatedAt(LocalDateTime.now());
QuestionLecture savedQuestionLecture = questionLectureRepo.saveAndFlush(questionLecture);

return QuestionLectureVM.fromModel(savedQuestionLecture);
}

@Override
public void delete(Long questionLectureId) {
QuestionLecture questionLecture = questionLectureRepo.findById(questionLectureId).orElseThrow();
if (questionLecture.getStudentAnswers().size() > 0 || questionLecture.getUserAnswers().size() > 0) {
throw new BadRequestException("Câu hỏi này đã có câu trả lời");
}
questionLectureRepo.deleteById(questionLectureId);
}

@Override
public List<QuestionLectureGetVM> getByLectureId(Long lectureId) {

Expand All @@ -70,8 +93,7 @@ public List<QuestionLectureGetVM> getByLectureId(Long lectureId) {
for (StudentAnswer studentAnswer : studentAnswers) {
answerLectures.add(AnswerLecture.fromModelStudent(studentAnswer));
}


answerLectures.sort(Comparator.comparing(AnswerLecture::createdAt));
return QuestionLectureGetVM.fromModel(questionLecture, answerLectures);
}).toList();
return questionLectureGetVMS;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
package com.backend.elearning.domain.questionLecture;

import com.backend.elearning.domain.lecture.LectureGetVM;
import com.backend.elearning.domain.student.Student;
import com.backend.elearning.domain.user.UserGetVM;
import com.backend.elearning.utils.DateTimeUtils;

public record QuestionLectureVM (
Long id,
String title,
String description,

UserGetVM student,
LectureGetVM lecture,
String createdAt,
String updatedAt
) {

public static final QuestionLectureVM fromModel (QuestionLecture questionLecture) {
Student student = questionLecture.getStudent();
LectureGetVM lectureGetVM = LectureGetVM.fromModel(questionLecture.getLecture());
UserGetVM userGetVM = UserGetVM.fromModelStudent(student);
String createdAt = questionLecture.getCreatedAt() != null ?
DateTimeUtils.convertLocalDateTimeToString(questionLecture.getCreatedAt()) : "";
String updatedAt = questionLecture.getUpdatedAt() != null ?
DateTimeUtils.convertLocalDateTimeToString(questionLecture.getUpdatedAt()) : "";
return new QuestionLectureVM(questionLecture.getId(),
questionLecture.getTitle(),
questionLecture.getDescription(),
userGetVM,
questionLecture.getCreatedAt().toString(),
questionLecture.getUpdatedAt().toString());
lectureGetVM,
createdAt,
updatedAt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1")
Expand All @@ -37,4 +34,25 @@ public ResponseEntity<AnswerLecture> create (
AnswerLecture studentAnswerVM = studentAnswerService.create(studentAnswerPostVM);
return ResponseEntity.status(HttpStatus.OK).body(studentAnswerVM);
}

@PutMapping("/student-answer/{id}")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Created", content =
@Content(schema = @Schema(implementation = AnswerLecture.class)))
})
public ResponseEntity<AnswerLecture> update (
@RequestBody StudentAnswerPostVM studentAnswerPostVM,
@PathVariable("id") Long id
) {
AnswerLecture studentAnswerVM = studentAnswerService.update(studentAnswerPostVM, id);
return ResponseEntity.status(HttpStatus.OK).body(studentAnswerVM);
}

@DeleteMapping("/student-answer/{id}")
public ResponseEntity<Void> delete (
@PathVariable("id") Long id
) {
studentAnswerService.delete(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.backend.elearning.domain.questionLecture.studentAnswer;

public record StudentAnswerPostVM(
Long id,
String content,
Long questionLectureId
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@

public interface StudentAnswerService {
AnswerLecture create(StudentAnswerPostVM studentAnswerPostVM);

AnswerLecture update(StudentAnswerPostVM studentAnswerPostVM, Long studentAnswerId);

void delete(Long answerLectureId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,18 @@ public AnswerLecture create(StudentAnswerPostVM studentAnswerPostVM) {
StudentAnswer savedStudentAnswer = studentAnswerRepo.saveAndFlush(studentAnswer);
return AnswerLecture.fromModelStudent(savedStudentAnswer);
}

@Override
public AnswerLecture update(StudentAnswerPostVM studentAnswerPostVM, Long studentAnswerId) {
StudentAnswer studentAnswer = studentAnswerRepo.findById(studentAnswerId).orElseThrow() ;
studentAnswer.setContent(studentAnswerPostVM.content());
studentAnswer.setUpdatedAt(LocalDateTime.now());
StudentAnswer savedStudentAnswer = studentAnswerRepo.saveAndFlush(studentAnswer);
return AnswerLecture.fromModelStudent(savedStudentAnswer);
}

@Override
public void delete(Long answerLectureId) {
studentAnswerRepo.deleteById(answerLectureId);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.backend.elearning.domain.questionLecture.userAnswer;

import com.backend.elearning.domain.questionLecture.AnswerLecture;
import com.backend.elearning.domain.questionLecture.studentAnswer.StudentAnswerPostVM;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1")
Expand All @@ -34,4 +32,26 @@ public ResponseEntity<AnswerLecture> create (
AnswerLecture userAnswerVM = userAnswerService.create(userAnswerPostVM);
return ResponseEntity.status(HttpStatus.OK).body(userAnswerVM);
}


@PutMapping("/user-answer/{id}")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Created", content =
@Content(schema = @Schema(implementation = AnswerLecture.class)))
})
public ResponseEntity<AnswerLecture> update (
@RequestBody UserAnswerPostVM userAnswerPostVM,
@PathVariable("id") Long id
) {
AnswerLecture studentAnswerVM = userAnswerService.update(userAnswerPostVM, id);
return ResponseEntity.status(HttpStatus.OK).body(studentAnswerVM);
}

@DeleteMapping("/user-answer/{id}")
public ResponseEntity<Void> delete (
@PathVariable("id") Long id
) {
userAnswerService.delete(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.backend.elearning.domain.questionLecture.userAnswer;

public record UserAnswerPostVM(
Long id,
String content,
Long questionLectureId
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@
public interface UserAnswerService {

AnswerLecture create(UserAnswerPostVM userAnswerPostVM);

AnswerLecture update(UserAnswerPostVM userAnswerPostVM, Long answerLectureId);
void delete(Long answerLectureId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,18 @@ public AnswerLecture create(UserAnswerPostVM userAnswerPostVM) {
UserAnswer savedUserAnswer = userAnswerRepo.saveAndFlush(userAnswer);
return AnswerLecture.fromModelUser(savedUserAnswer);
}

@Override
public AnswerLecture update(UserAnswerPostVM userAnswerPostVM, Long userAnswerId) {
UserAnswer userAnswer = userAnswerRepo.findById(userAnswerId).orElseThrow();
userAnswer.setContent(userAnswerPostVM.content());
userAnswer.setUpdatedAt(LocalDateTime.now());
UserAnswer savedUserAnswer = userAnswerRepo.saveAndFlush(userAnswer);
return AnswerLecture.fromModelUser(savedUserAnswer);
}

@Override
public void delete(Long answerLectureId) {
userAnswerRepo.deleteById(answerLectureId);
}
}
Loading
Loading