Skip to content

Commit

Permalink
fix: 버그 수정 (#149)
Browse files Browse the repository at this point in the history
유저의 스터디 그룹 정보를 받아올 수 없던 버그 등 테스트 과정에서 발생한 버그 수정
  • Loading branch information
zionhann committed Mar 9, 2024
1 parent 9003d80 commit 4b59aed
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public ResponseEntity<TeamReportDto> getTeamReports(
@RequestAttribute Claims claims) {
if (Role.isAuthorized(claims, Role.ADMIN)) {
TeamReportDto res = teamService.getTeamReports(id, claims.getSubject());

return ResponseEntity.ok(res);
}
throw new ForbiddenException();
Expand Down Expand Up @@ -111,6 +110,7 @@ public ResponseEntity<List<UserDto.UserInfo>> getUnmatchedUsers(@RequestAttribut
public void deleteForm(@RequestParam String sid, @RequestAttribute Claims claims) {
if (Role.isAuthorized(claims, Role.ADMIN)) {
userService.deleteUserForm(sid);
return;
}
throw new ForbiddenException();
}
Expand All @@ -120,6 +120,7 @@ public void deleteForm(@RequestParam String sid, @RequestAttribute Claims claims
public void editUser(@RequestBody UserDto.UserEdit form, @RequestAttribute Claims claims) {
if (Role.isAuthorized(claims, Role.ADMIN)) {
userService.editUser(form);
return;
}
throw new ForbiddenException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package edu.handong.csee.histudy.controller;

import edu.handong.csee.histudy.controller.form.ReportForm;
import edu.handong.csee.histudy.domain.AcademicTerm;
import edu.handong.csee.histudy.domain.Role;
import edu.handong.csee.histudy.domain.StudyGroup;
import edu.handong.csee.histudy.domain.User;
import edu.handong.csee.histudy.dto.CourseDto;
import edu.handong.csee.histudy.dto.ReportDto;
import edu.handong.csee.histudy.dto.UserDto;
import edu.handong.csee.histudy.exception.ForbiddenException;
import edu.handong.csee.histudy.exception.NoCurrentTermFoundException;
import edu.handong.csee.histudy.exception.UserNotFoundException;
import edu.handong.csee.histudy.repository.AcademicTermRepository;
import edu.handong.csee.histudy.repository.StudyGroupRepository;
import edu.handong.csee.histudy.repository.UserRepository;
Expand Down Expand Up @@ -156,18 +151,7 @@ public ResponseEntity<Map<String, String>> uploadImage(
@RequestParam MultipartFile image,
@RequestAttribute Claims claims) {
if (Role.isAuthorized(claims, Role.MEMBER)) {
AcademicTerm currentTerm =
academicTermRepository
.findCurrentSemester()
.orElseThrow(NoCurrentTermFoundException::new);
User user =
userRepository
.findUserByEmail(claims.getSubject())
.orElseThrow(UserNotFoundException::new);
StudyGroup studyGroup =
studyGroupRepository.findByUserAndTerm(user, currentTerm).orElseThrow();

String filename = imageService.getImagePaths(image, studyGroup.getTag(), reportIdOr);
String filename = imageService.getImagePaths(claims.getSubject(), image, reportIdOr);
Map<String, String> response = Map.of("imagePath", filename);
return ResponseEntity.ok(response);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class StudyApplicant extends BaseTime {
@JoinColumn(name = "user_id")
private User user;

@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.PERSIST)
@JoinColumn(name = "study_group_id")
private StudyGroup studyGroup;

Expand Down
34 changes: 18 additions & 16 deletions src/main/java/edu/handong/csee/histudy/domain/StudyGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,12 @@ public static StudyGroup of(Integer tag, AcademicTerm current, List<StudyApplica
return new StudyGroup(tag, current, applicants.toArray(StudyApplicant[]::new));
}

public static StudyGroup of(Integer tag, AcademicTerm current) {
return new StudyGroup(tag, current);
}

protected StudyGroup(Integer tag, AcademicTerm academicTerm, StudyApplicant... applicants) {
this.tag = tag;
this.academicTerm = academicTerm;
assignMembers(applicants);
}

protected StudyGroup(Integer tag, AcademicTerm academicTerm) {
this.tag = tag;
this.academicTerm = academicTerm;
}

private List<Course> findCommonCourses(StudyApplicant... applicants) {
if (!this.courses.isEmpty()) {
this.courses.clear();
Expand Down Expand Up @@ -94,13 +85,13 @@ private List<Course> findCommonCourses(StudyApplicant... applicants) {
public StudyGroup assignMembers(StudyApplicant... applicants) {
Arrays.stream(applicants)
.forEach(
form -> {
if (isInSameGroup(form)) {
applicant -> {
if (isInSameGroup(applicant)) {
return;
} else if (isAlreadyInOtherGroup(form)) {
form.leaveGroup();
} else if (isAlreadyInOtherGroup(applicant)) {
applicant.leaveGroup();
}
GroupMember.of(this, form);
GroupMember.of(this, applicant);
});
assignCommonCourses(applicants);
return this;
Expand All @@ -114,14 +105,15 @@ public void removeMember(User member) {
}

private boolean isAlreadyInOtherGroup(StudyApplicant applicant) {
return applicant != null && !applicant.getStudyGroup().equals(this);
return applicant.getStudyGroup() != null && !applicant.getStudyGroup().equals(this);
}

public boolean isInSameGroup(StudyApplicant applicant) {
return applicant != null && applicant.getStudyGroup().equals(this);
return applicant.getStudyGroup() != null && applicant.getStudyGroup().equals(this);
}

protected void assignCommonCourses(StudyApplicant... applicants) {
if (isSameMemberExact(applicants)) return;
if (this.members.isEmpty()) {
this.courses.clear();
this.tag = -1;
Expand All @@ -132,6 +124,16 @@ protected void assignCommonCourses(StudyApplicant... applicants) {
.forEach(course -> new GroupCourse(this, course));
}

private boolean isSameMemberExact(StudyApplicant... applicants) {
if (this.courses.isEmpty()) {
return false;
}
Set<User> users =
Arrays.stream(applicants).map(StudyApplicant::getUser).collect(Collectors.toSet());
Set<User> members = this.members.stream().map(GroupMember::getUser).collect(Collectors.toSet());
return members.containsAll(users);
}

private boolean isNotInGroupCourse(Course course) {
return this.courses.stream().map(GroupCourse::getCourse).noneMatch(c -> c.equals(course));
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/edu/handong/csee/histudy/dto/TeamDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public TeamDto(
.map(
user -> {
StudyApplicant applicant = applicantMap.get(user);
return new UserDto.UserInfo(user, applicant);
return (applicant == null)
? new UserDto.UserInfo(user)
: new UserDto.UserInfo(user, applicant);
})
.toList();
this.reports = reports.size();
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/edu/handong/csee/histudy/dto/UserDto.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package edu.handong.csee.histudy.dto;

import edu.handong.csee.histudy.domain.StudyApplicant;
import edu.handong.csee.histudy.domain.PreferredCourse;
import edu.handong.csee.histudy.domain.StudyApplicant;
import edu.handong.csee.histudy.domain.StudyPartnerRequest;
import edu.handong.csee.histudy.domain.User;
import edu.handong.csee.histudy.jwt.JwtPair;
Expand Down Expand Up @@ -112,16 +112,20 @@ public UserInfo(User user, StudyApplicant applicant) {
this.email = user.getEmail();
this.group = (applicant.getStudyGroup() == null) ? null : applicant.getStudyGroup().getTag();
this.friends =
applicant.getPartnerRequests().stream()
.filter(StudyPartnerRequest::isAccepted)
.map(StudyPartnerRequest::getReceiver)
.map(UserBasic::new)
.toList();
(applicant.getPartnerRequests() == null)
? Collections.emptyList()
: applicant.getPartnerRequests().stream()
.filter(StudyPartnerRequest::isAccepted)
.map(StudyPartnerRequest::getReceiver)
.map(UserBasic::new)
.toList();
this.courses =
applicant.getPreferredCourses().stream()
.sorted(Comparator.comparing(PreferredCourse::getPriority))
.map(c -> new CourseDto.BasicCourseInfo(c.getCourse()))
.toList();
(applicant.getPreferredCourses() == null)
? Collections.emptyList()
: applicant.getPreferredCourses().stream()
.sorted(Comparator.comparing(PreferredCourse::getPriority))
.map(c -> new CourseDto.BasicCourseInfo(c.getCourse()))
.toList();
}

public UserInfo(User user) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@
public interface StudyApplicantRepository extends JpaRepository<StudyApplicant, Long> {

@Query(
"select s from StudyApplicant s join fetch s.partnerRequests "
+ "where s.user = :applicant and s.academicTerm = :currentTerm")
"select s from StudyApplicant s left join fetch s.preferredCourses "
+ "where s.user = :applicant and s.academicTerm = :currentTerm")
Optional<StudyApplicant> findByUserAndTerm(User applicant, AcademicTerm currentTerm);

@Query(
"select s from StudyApplicant s join fetch s.partnerRequests "
"select s from StudyApplicant s left join fetch s.preferredCourses "
+ "where s.academicTerm = :currentTerm and s.studyGroup is null")
List<StudyApplicant> findUnassignedApplicants(AcademicTerm currentTerm);

@Query(
"select s from StudyApplicant s join fetch s.partnerRequests "
"select s from StudyApplicant s join fetch s.user "
+ "where s.academicTerm = :currentTerm and s.studyGroup is not null")
List<StudyApplicant> findAssignedApplicants(AcademicTerm currentTerm);

@Query(
"select s from StudyApplicant s join fetch s.partnerRequests "
"select s from StudyApplicant s left join fetch s.preferredCourses "
+ "where s.academicTerm = :currentTerm")
List<StudyApplicant> findAllByTerm(AcademicTerm currentTerm);

@Query(
"select s from StudyApplicant s join fetch s.partnerRequests "
"select s from StudyApplicant s left join fetch s.preferredCourses "
+ "where s.studyGroup = :studyGroup")
List<edu.handong.csee.histudy.domain.StudyApplicant> findAllByStudyGroup(@Param("studyGroup") StudyGroup group);
List<StudyApplicant> findAllByStudyGroup(@Param("studyGroup") StudyGroup group);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public interface StudyGroupRepository extends JpaRepository<StudyGroup, Long> {
List<StudyGroup> findAllByAcademicTerm(@Param("academicTerm") AcademicTerm academicTerm);

@Query(
"select s from StudyGroup s where :user member of s.members and s.academicTerm = :currentTerm")
"select s from StudyGroup s "
+ "join s.members m "
+ "where m.user = :user and s.academicTerm = :currentTerm")
Optional<StudyGroup> findByUserAndTerm(
@Param("user") User user, @Param("currentTerm") AcademicTerm currentTerm);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@
import edu.handong.csee.histudy.dto.CourseDto;
import edu.handong.csee.histudy.dto.CourseIdDto;
import edu.handong.csee.histudy.exception.NoCurrentTermFoundException;
import edu.handong.csee.histudy.exception.StudyGroupNotFoundException;
import edu.handong.csee.histudy.exception.UserNotFoundException;
import edu.handong.csee.histudy.repository.AcademicTermRepository;
import edu.handong.csee.histudy.repository.CourseRepository;
import edu.handong.csee.histudy.repository.StudyGroupRepository;
import edu.handong.csee.histudy.repository.UserRepository;
import edu.handong.csee.histudy.repository.*;
import edu.handong.csee.histudy.util.CSVResolver;
import edu.handong.csee.histudy.util.CourseCSV;
import java.io.IOException;
Expand All @@ -24,6 +22,7 @@ public class CourseService {
private final UserRepository userRepository;
private final AcademicTermRepository academicTermRepository;
private final StudyGroupRepository studyGroupRepository;
private final StudyApplicantRepository studyApplicantRepository;

public void readCourseCSV(MultipartFile file) throws IOException {
CSVResolver resolver = CSVResolver.of(file.getInputStream());
Expand Down Expand Up @@ -65,10 +64,13 @@ public List<CourseDto.CourseInfo> getTeamCourses(String email) {
User user = userRepository.findUserByEmail(email).orElseThrow(UserNotFoundException::new);
AcademicTerm currentTerm =
academicTermRepository.findCurrentSemester().orElseThrow(NoCurrentTermFoundException::new);
StudyGroup studyGroup =
studyGroupRepository
.findByUserAndTerm(user, currentTerm)
.orElseThrow(StudyGroupNotFoundException::new);

StudyGroup studyGroup = studyGroupRepository.findByUserAndTerm(user, currentTerm).orElseThrow();
List<Course> courses =
studyGroup.getCourses().stream().map(GroupCourse::getCourse).toList();
studyGroupRepository.findByUserAndTerm(user, currentTerm).orElseThrow();
List<Course> courses = studyGroup.getCourses().stream().map(GroupCourse::getCourse).toList();

return courses.stream().map(CourseDto.CourseInfo::new).toList();
}
Expand Down
26 changes: 21 additions & 5 deletions src/main/java/edu/handong/csee/histudy/service/ImageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import static org.springframework.util.ResourceUtils.isUrl;

import edu.handong.csee.histudy.domain.AcademicTerm;
import edu.handong.csee.histudy.domain.ReportImage;
import edu.handong.csee.histudy.exception.FileTransferException;
import edu.handong.csee.histudy.exception.ReportNotFoundException;
import edu.handong.csee.histudy.repository.StudyReportRepository;
import edu.handong.csee.histudy.domain.StudyGroup;
import edu.handong.csee.histudy.domain.User;
import edu.handong.csee.histudy.exception.*;
import edu.handong.csee.histudy.repository.*;
import edu.handong.csee.histudy.util.ImagePathMapper;
import edu.handong.csee.histudy.util.Utils;
import java.io.File;
Expand Down Expand Up @@ -34,11 +36,24 @@ public class ImageService {
@Value("${custom.resource.location}")
private String imageBaseLocation;

private final AcademicTermRepository academicTermRepository;
private final UserRepository userRepository;
private final StudyReportRepository studyReportRepository;
private final StudyApplicantRepository studyApplicantRepository;

private final ImagePathMapper imagePathMapper;
private final StudyGroupRepository studyGroupRepository;

public String getImagePaths(
MultipartFile imageAsFormData, Integer tag, Optional<Long> reportIdOr) {
String email, MultipartFile imageAsFormData, Optional<Long> reportIdOr) {
AcademicTerm currentTerm =
academicTermRepository.findCurrentSemester().orElseThrow(NoCurrentTermFoundException::new);
User user = userRepository.findUserByEmail(email).orElseThrow(UserNotFoundException::new);
StudyGroup studyGroup =
studyGroupRepository
.findByUserAndTerm(user, currentTerm)
.orElseThrow(StudyGroupNotFoundException::new);

if (reportIdOr.isPresent()) {
Long id = reportIdOr.get();
Optional<String> sameResource = getSameContent(imageAsFormData, id);
Expand All @@ -58,7 +73,8 @@ public String getImagePaths(
// e.g. 2023-2-group1-report_20230923_123456.jpg
String pathname =
String.format(
"%d-%d-group%02d-report_%s%s", year, semester, tag, formattedDateTime, extension);
"%d-%d-group%02d-report_%s%s",
year, semester, studyGroup.getTag(), formattedDateTime, extension);
String savedImagePath = saveImage(imageAsFormData, pathname);

return imagePathMapper.getFullPath(savedImagePath);
Expand Down
Loading

0 comments on commit 4b59aed

Please sign in to comment.