Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
cedae0d
test: study page CRUD 단위테스트 작성
wive10 Oct 21, 2024
78f5f1d
Merge branch 'feature/22' of https://github.com/SmashStudy/ESquad-BE …
wive10 Oct 21, 2024
0c4e71b
Merge branch 'develop' of https://github.com/SmashStudy/ESquad-BE int…
wive10 Oct 22, 2024
2da3210
Merge branch 'develop' of https://github.com/SmashStudy/ESquad-BE int…
wive10 Oct 22, 2024
5d34bfa
Merge branch 'develop' of https://github.com/SmashStudy/ESquad-BE int…
wive10 Oct 22, 2024
18ebb99
pull: pull받음
wive10 Oct 22, 2024
4a817a8
fix: validation annotation 추가
wive10 Oct 22, 2024
f73f5db
fix: validation 검증 추가
wive10 Oct 22, 2024
b9513a0
fix: Dto에 to, from 함수 추가
wive10 Oct 22, 2024
86bbaf4
fix: Exception AOP 생성
wive10 Oct 22, 2024
c229895
fix: Dto to, from 적용 및 Exception AOP 적용
wive10 Oct 22, 2024
fd18a79
fix: Exception AOP 적용 및 수정
wive10 Oct 22, 2024
e4eb8eb
Merge branch 'develop' of https://github.com/SmashStudy/ESquad-BE int…
wive10 Oct 22, 2024
fb35bfd
fix: Error Code 수정
wive10 Oct 24, 2024
8d8c20f
fix: 예외 처리 적용
wive10 Oct 24, 2024
fb0006f
remove: 불필요한 레포지토리 삭제
wive10 Oct 24, 2024
4bdfdac
fix: 바뀐 예외에 따라 바뀐 테스트 코드
wive10 Oct 24, 2024
3e6d790
Merge branch 'develop' of https://github.com/SmashStudy/ESquad-BE int…
wive10 Oct 24, 2024
61ff16a
fix: 주석 삭제
wive10 Oct 25, 2024
ddce011
fix: front와의 통신에서 수정할 점 보안
wive10 Oct 29, 2024
cdf33f9
remove: 주석 및 로그
wive10 Oct 30, 2024
56c2584
refact: BookCreateException 생성
wive10 Oct 31, 2024
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 @@ -11,8 +11,8 @@
import com.esquad.esquadbe.studypage.entity.Book;
import com.esquad.esquadbe.studypage.repository.BookRepository;
import com.esquad.esquadbe.team.entity.TeamSpace;
import com.esquad.esquadbe.team.entity.repository.TeamSpaceRepository;
import com.esquad.esquadbe.team.exception.TeamNotFoundException;
import com.esquad.esquadbe.team.repository.TeamRepository;
import com.esquad.esquadbe.user.entity.User;
import com.esquad.esquadbe.user.exception.UserNotFoundException;
import com.esquad.esquadbe.user.exception.UserUsernameException;
Expand All @@ -36,7 +36,7 @@ public class QuestionService {
private final UserRepository userRepository;
private final QuestionRepository questionRepository;
private final BookRepository bookRepository;
private final TeamSpaceRepository teamSpaceRepository;
private final TeamRepository teamRepository;
private final S3FileService s3FileService;

private User getUser(String username) {
Expand All @@ -50,7 +50,7 @@ private Book getBook(Long bookId) {
}

private TeamSpace getTeamspace(Long teamSpaceId) {
return teamSpaceRepository.findById(teamSpaceId)
return teamRepository.findById(teamSpaceId)
.orElseThrow(TeamNotFoundException::new);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ public class BookController {

@GetMapping("/search")
public ResponseEntity<List<BookSearchResultItemDto>> searchTitle(@RequestParam("query") String query) {
log.info("받은 입력 값: {}", query);

List<BookSearchResultItemDto> bookList = bookService.resultList(query);

return ResponseEntity.status(200).body(bookList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.esquad.esquadbe.studypage.service.StudyPageService;
import com.esquad.esquadbe.studypage.service.StudyPageUserService;
import com.esquad.esquadbe.studypage.service.StudyRemindService;
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
Expand All @@ -19,7 +19,7 @@

@Slf4j
@RestController
@RequestMapping("/api/{teamId}/study-pages")
@RequestMapping("/api")
public class StudyPageController {

private final BookService bookService;
Expand All @@ -35,100 +35,50 @@ public StudyPageController(BookService bookService, StudyPageService studyPageSe
this.studyPageUserService = studyPageUserService;
}

// Create
@PostMapping("/")
public ResponseEntity<String> createStudyPage(
@PostMapping("/{teamId}/study-pages")
public ResponseEntity<Long> createStudyPage(
@PathVariable("teamId") Long teamId,
@RequestBody StudyPageCreateDto dto) {

log.info("Creating a new study page for teamId: {}", teamId);

@RequestBody @Valid StudyPageCreateDto dto) {
Long bookId = bookService.createBookInfo(dto.getBookDto());
Long studyPageId = studyPageService.createStudyPage(teamId, bookId, dto.getStudyInfoDto());

studyRemindService.createRemind(studyPageId, dto.getReminds());
studyPageUserService.createStudyPageUser(studyPageId, dto.getUserIds());
studyPageUserService.createStudyPageUser(teamId, studyPageId, dto.getUserIds());

return ResponseEntity.status(HttpStatus.CREATED).body("Study page created successfully.");
return ResponseEntity.status(HttpStatus.CREATED).body(studyPageId);
}

// Read List
@GetMapping("/")
@GetMapping("/{teamId}/study-pages")
public ResponseEntity<List<StudyPageReadDto>> getStudyPages(@PathVariable("teamId") Long teamId) {
log.info("Fetching study pages for teamId: {}", teamId);

List<StudyPageReadDto> studyPages = studyPageService.readStudyPages(teamId);
return ResponseEntity.status(HttpStatus.OK).body(studyPages);
return ResponseEntity.ok(studyPages);
}

@GetMapping("/{studyId}")
@GetMapping("/{teamId}/study-pages/{studyId}")
public ResponseEntity<StudyInfoDto> getStudyPageInfo(
@PathVariable("teamId") Long teamId,
@PathVariable("studyId") Long studyId) {
log.info("Fetching study page info: teamId = {}, studyId = {}", teamId, studyId);

try {
StudyInfoDto studyInfoDto= studyPageService.readStudyPageInfo(studyId);
return ResponseEntity.ok(studyInfoDto);

} catch (EntityNotFoundException ex) {
log.error("Study page not found: studyId = {}, teamId = {}", studyId, teamId, ex);
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(null);

} catch (IllegalArgumentException ex) {
log.error("Invalid argument provided for teamId = {} or studyId = {}", teamId, studyId, ex);
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(null);

} catch (Exception ex) {
log.error("Unexpected error while fetching study page: studyId = {}, teamId = {}", studyId, teamId, ex);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(null);
}

StudyInfoDto studyInfoDto = studyPageService.readStudyPageInfo(studyId);
return ResponseEntity.ok(studyInfoDto);
}

// Update
@PostMapping("/{studyId}")
public ResponseEntity<String> updateStudyPage(
@PostMapping("/{teamId}/study-pages/{studyId}")
public ResponseEntity<Long> updateStudyPage(
@PathVariable("teamId") Long teamId,
@PathVariable("studyId") Long studyId,
@RequestBody UpdateStudyPageRequestDto request) {

log.info("Updating study page with studyId: {}", studyId);
@RequestBody @Valid UpdateStudyPageRequestDto request) {

boolean isUpdated = studyPageService.updateStudyPage(studyId, request);

if (isUpdated) {
return ResponseEntity.status(HttpStatus.OK).body("Study page updated successfully.");
} else {
return ResponseEntity.status(HttpStatus.FORBIDDEN).body("You don't have authorization to update this study page.");
}
return ResponseEntity.ok(studyPageService.updateStudyPage(studyId, request));
}

// Delete
@DeleteMapping("/{studyId}")
@DeleteMapping("/{teamId}/study-pages/{studyId}")
public ResponseEntity<String> deleteStudyPage(
@PathVariable("studyId") Long studyId,
@RequestParam("name") String studyPageName) {

log.info("Attempting to delete study page with ID: {} and name: {}", studyId, studyPageName);

try {
log.info("Successfully deleted study page with ID: {}", studyId);
studyPageService.deleteStudyPage(studyId, studyPageName);
return ResponseEntity.status(HttpStatus.OK).body("Study page deleted successfully.");

} catch (EntityNotFoundException e) {
log.warn("Study page not found: {}", e.getMessage());
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());

} catch (IllegalArgumentException e) {
log.error("Invalid argument: {}", e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage());

} catch (Exception e) {
log.error("Error occurred while deleting study page: {}", e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("An error occurred while deleting the study page.");
}
studyPageService.deleteStudyPage(studyId, studyPageName);
return ResponseEntity.ok("Study page deleted successfully.");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.esquad.esquadbe.studypage.dto;

import com.esquad.esquadbe.studypage.entity.Book;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -10,13 +12,35 @@
@AllArgsConstructor
@Getter
public class BookSearchResultItemDto {
@NotNull
private String title;
@NotNull
private String link;
@NotNull
private String image;
@NotNull
private String author;
@NotNull
private String discount;
@NotNull
private String publisher;
@NotNull
private String isbn;
@NotNull
private String description;
@NotNull
private String pubdate;
};

public Book to() {

return Book.builder()
.title(title)
.imgPath(image)
.author(author)
.publisher(publisher)
.isbn(isbn)
.description(description)
.pubDate(pubdate)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.esquad.esquadbe.studypage.dto;

import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -12,9 +13,14 @@
@AllArgsConstructor
@Getter
public class BookSearchResultListDto {
@NotNull
private String lastBuildDate;
@NotNull
private int total;
@NotNull
private int start;
@NotNull
private int display;
@NotNull
private List<BookSearchResultItemDto> items;
}
40 changes: 33 additions & 7 deletions src/main/java/com/esquad/esquadbe/studypage/dto/StudyInfoDto.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,47 @@
package com.esquad.esquadbe.studypage.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import com.esquad.esquadbe.studypage.entity.Book;
import com.esquad.esquadbe.studypage.entity.StudyPage;
import com.esquad.esquadbe.team.entity.TeamSpace;
import jakarta.validation.constraints.NotNull;
import lombok.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@ToString
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Builder
public class StudyInfoDto {
@NotNull
private String studyPageName;
@NotNull
private LocalDate startDate;
@NotNull
private LocalDate endDate;
@NotNull
private String description;

//create
public StudyPage from(TeamSpace teamSpace, Book book) {
return StudyPage.builder()
.teamSpace(teamSpace)
.book(book)
.studyPageName(studyPageName)
.startDate(startDate)
.endDate(endDate)
.description(description)
.build();
}

//update
public static StudyInfoDto to(StudyPage studyPage) {
return StudyInfoDto.builder()
.studyPageName(studyPage.getStudyPageName())
.startDate(studyPage.getStartDate())
.endDate(studyPage.getEndDate())
.description(studyPage.getDescription())
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.esquad.esquadbe.studypage.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -12,9 +14,13 @@
@AllArgsConstructor
@Getter
public class StudyPageCreateDto {

private BookSearchResultItemDto bookDto;

private StudyInfoDto studyInfoDto;
@NotNull
private List<StudyRemindDto> reminds;

private List<Long> userIds;
}

Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
package com.esquad.esquadbe.studypage.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import com.esquad.esquadbe.studypage.entity.StudyPage;
import jakarta.validation.constraints.NotBlank;
import lombok.*;

@ToString
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Builder
public class StudyPageReadDto {
@NotBlank
private Long id;
private String image;
@NotBlank
private String title;

public StudyPageReadDto from(StudyPage studyPage) {
return StudyPageReadDto.builder()
.id(studyPage.getId())
.image(studyPage.getBook().getImgPath())
.title(studyPage.getStudyPageName())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.esquad.esquadbe.studypage.dto;

import com.esquad.esquadbe.notification.entity.AlertDayType;
import com.esquad.esquadbe.studypage.entity.StudyPage;
import com.esquad.esquadbe.studypage.entity.StudyRemind;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -13,7 +16,18 @@
@AllArgsConstructor
@Getter
public class StudyRemindDto {
@NotBlank
private AlertDayType dayType;
@NotBlank
private LocalTime timeAt;
private String description;

public StudyRemind from (StudyPage studyPage) {
return StudyRemind.builder()
.studyPage(studyPage)
.dayType(dayType)
.timeAt(timeAt)
.description(description)
.build();
}
}
Loading