Skip to content

Commit

Permalink
Merge pull request #70 from kookmin-sw/BE_Feature/#69-assignment
Browse files Browse the repository at this point in the history
Be feature/#69 assignment
  • Loading branch information
wjdwlghks authored May 12, 2024
2 parents fb050c8 + bed3dee commit 156eef7
Show file tree
Hide file tree
Showing 13 changed files with 268 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.project.capstone.assignment.controller;

import com.project.capstone.assignment.controller.dto.AssignmentResponse;
import com.project.capstone.assignment.controller.dto.CreateAssignmentRequest;
import com.project.capstone.assignment.service.AssignmentService;
import com.project.capstone.auth.domain.PrincipalDetails;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/assign")
@RequiredArgsConstructor
public class AssignmentController {

private final AssignmentService assignmentService;

// 과제 생성
@PostMapping("/create")
public ResponseEntity<?> createAssignment(@AuthenticationPrincipal PrincipalDetails details,
@RequestBody CreateAssignmentRequest request, @RequestParam Long clubId) {
assignmentService.createAssignment(details.getUserId(), request, clubId);
return ResponseEntity.ok().body("과제 생성 완료");
}

// 모임의 과제 조회
@GetMapping("/get")
public ResponseEntity<List<AssignmentResponse>> getAssignment(@RequestParam Long clubId) {
List<AssignmentResponse> assignmentResponseList = assignmentService.getAssignment(clubId);
return ResponseEntity.ok().body(assignmentResponseList);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.project.capstone.assignment.controller.dto;

import com.project.capstone.assignment.domain.Assignment;
import com.project.capstone.content.controller.dto.ContentResponse;
import com.project.capstone.content.domain.Content;
import java.util.ArrayList;
import java.util.List;

public record AssignmentResponse(
Long id,
String name,
String startDate,
String endDate,
List<ContentResponse> contentList
) {
public AssignmentResponse(Assignment assignment) {
this(assignment.getId(), assignment.getName(), assignment.getStartDate(), assignment.getEndDate(), createContentList(assignment.getContents()));
}

private static List<ContentResponse> createContentList(List<Content> contents) {
List<ContentResponse> contentResponseList = new ArrayList<>();
for (Content content : contents) {
contentResponseList.add(new ContentResponse(content));
}
return contentResponseList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.project.capstone.assignment.controller.dto;

public record CreateAssignmentRequest(
String name,
String startDate,
String endDate
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.project.capstone.assignment.domain;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.project.capstone.assignment.controller.dto.CreateAssignmentRequest;
import com.project.capstone.club.domain.Club;
import com.project.capstone.content.domain.Content;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Table(name = "assignment")
public class Assignment {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String name;

@Column(name = "start_date")
private String startDate;

@Column(name = "end_date")
private String endDate;

@JsonManagedReference
@OneToMany(mappedBy = "assignment")
List<Content> contents = new ArrayList<>();

@JsonBackReference
@ManyToOne
private Club club;

public Assignment(CreateAssignmentRequest request, Club club) {
this(null, request.name(), request.startDate(), request.endDate(), new ArrayList<>(), club);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.project.capstone.assignment.domain;

import com.project.capstone.club.domain.Club;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface AssignmentRepository extends JpaRepository<Assignment, Long> {
List<Assignment> findAssignmentsByClub(Club club);

Optional<Assignment> findAssignmentById(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.project.capstone.assignment.exception;

import com.project.capstone.common.exception.BaseException;
import com.project.capstone.common.exception.ExceptionType;

public class AssignmentException extends BaseException {
public AssignmentException(ExceptionType exceptionType) {
super(exceptionType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.project.capstone.assignment.exception;

import com.project.capstone.common.exception.ExceptionType;
import lombok.AllArgsConstructor;
import org.springframework.http.HttpStatus;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.NOT_FOUND;

@AllArgsConstructor
public enum AssignmentExceptionType implements ExceptionType {
ASSIGNMENT_NOT_FOUND(NOT_FOUND, 601, "해당 과제를 찾을 수 없습니다."),
NOT_EXIST_BOOK(BAD_REQUEST, 602, "대표책이 지정되지 않았습니다.")
;


private final HttpStatus status;
private final int exceptionCode;
private final String message;

@Override
public HttpStatus httpStatus() {
return status;
}

@Override
public int exceptionCode() {
return exceptionCode;
}

@Override
public String message() {
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.project.capstone.assignment.service;

import com.project.capstone.assignment.controller.dto.AssignmentResponse;
import com.project.capstone.assignment.controller.dto.CreateAssignmentRequest;
import com.project.capstone.assignment.domain.Assignment;
import com.project.capstone.assignment.domain.AssignmentRepository;
import com.project.capstone.club.domain.Club;
import com.project.capstone.club.domain.ClubRepository;
import com.project.capstone.club.exception.ClubException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static com.project.capstone.club.exception.ClubExceptionType.CLUB_NOT_FOUND;
import static com.project.capstone.club.exception.ClubExceptionType.UNAUTHORIZED_ACTION;

@Service
@RequiredArgsConstructor
@Slf4j
public class AssignmentService {

private final ClubRepository clubRepository;
private final AssignmentRepository assignmentRepository;
public void createAssignment(String userId, CreateAssignmentRequest request, Long clubId) {
Club club = clubRepository.findClubById(clubId).orElseThrow(
() -> new ClubException(CLUB_NOT_FOUND)
);
if (!club.getManagerId().toString().equals(userId)) {
throw new ClubException(UNAUTHORIZED_ACTION);
}
Assignment saved = assignmentRepository.save(new Assignment(request, club));

club.getAssignments().add(saved);
}


public List<AssignmentResponse> getAssignment(Long clubId) {
Club club = clubRepository.findClubById(clubId).orElseThrow(
() -> new ClubException(CLUB_NOT_FOUND)
);
List<Assignment> assignmentsByClub = club.getAssignments();
List<AssignmentResponse> assignmentResponseList = new ArrayList<>();
for (Assignment assignment : assignmentsByClub) {
assignmentResponseList.add(new AssignmentResponse(assignment));
}
return assignmentResponseList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ public record ClubResponse (
LocalDateTime createdAt,
int maximum,
int memberCnt,
Integer password,
PublicStatus publicstatus,
List<PostResponse> posts,
List<SimpleMemberResponse> memberList

) {
public ClubResponse(Club club) {
this(club.getId(), club.getBook() == null ? null : createBookResponse(club.getBook()), club.getManagerId(), club.getTopic(), club.getName(), club.getCreatedAt(), club.getMaximum(),
club.getMembers().size(), club.getPublicStatus(), createPostResponseList(club.getPosts()), createSimpleMemberResponse(club.getMembers()));
club.getMembers().size(), club.getPassword() == null ? null : club.getPassword(), club.getPublicStatus(), createPostResponseList(club.getPosts()), createSimpleMemberResponse(club.getMembers()));
}

private static List<PostResponse> createPostResponseList(List<Post> postList) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.project.capstone.club.domain;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.project.capstone.assignment.domain.Assignment;
import com.project.capstone.book.domain.Book;
import com.project.capstone.club.controller.dto.ClubCreateRequest;
import com.project.capstone.memberclub.domain.MemberClub;
Expand Down Expand Up @@ -60,13 +62,18 @@ public class Club {
@OneToMany(mappedBy = "club")
private List<Quiz> quizzes = new ArrayList<>();

@JsonManagedReference
@OneToMany(mappedBy = "club")
private List<Assignment> assignments = new ArrayList<>();

@JsonBackReference
@ManyToOne
@JoinColumn(name = "book_id")
private Book book;

public Club(ClubCreateRequest request, UUID memberId) {
this(null, memberId, request.topic(), request.name(), null, request.maximum(), request.publicStatus(), request.password(),
new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), null);
new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), new ArrayList<>(), null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public class ContentController {
@PostMapping("/create")
public ResponseEntity<?> createContent(@AuthenticationPrincipal PrincipalDetails details,
@RequestBody ContentCreateRequest request,
@RequestParam(required = false) Long clubId) {
contentService.createContent(details.getUserId(), request, clubId);
@RequestParam(required = false) Long clubId, @RequestParam(required = false) Long asId) {
contentService.createContent(details.getUserId(), request, clubId, asId);
return ResponseEntity.ok().body("컨텐츠 생성 완료");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.project.capstone.content.domain;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.project.capstone.assignment.domain.Assignment;
import com.project.capstone.book.domain.Book;
import com.project.capstone.club.domain.Club;
import com.project.capstone.member.domain.Member;
Expand Down Expand Up @@ -47,4 +48,8 @@ public class Content {
@JsonBackReference
@ManyToOne
private Club club;

@JsonBackReference
@ManyToOne
private Assignment assignment;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.project.capstone.content.service;

import com.project.capstone.assignment.domain.Assignment;
import com.project.capstone.assignment.domain.AssignmentRepository;
import com.project.capstone.assignment.exception.AssignmentException;
import com.project.capstone.assignment.exception.AssignmentExceptionType;
import com.project.capstone.book.domain.Book;
import com.project.capstone.book.domain.BookRepository;
import com.project.capstone.book.exception.BookException;
Expand Down Expand Up @@ -29,6 +33,8 @@
import java.util.UUID;
import java.util.stream.Collectors;

import static com.project.capstone.assignment.exception.AssignmentExceptionType.ASSIGNMENT_NOT_FOUND;
import static com.project.capstone.assignment.exception.AssignmentExceptionType.NOT_EXIST_BOOK;
import static com.project.capstone.book.exception.BookExceptionType.BOOK_NOT_FOUND;
import static com.project.capstone.club.exception.ClubExceptionType.CLUB_NOT_FOUND;
import static com.project.capstone.content.exception.ContentExceptionType.CONTENT_NOT_FOUND;
Expand All @@ -44,8 +50,9 @@ public class ContentService {
private final BookRepository bookRepository;
private final ClubRepository clubRepository;
private final MemberRepository memberRepository;
private final AssignmentRepository assignmentRepository;

public void createContent(String userId, ContentCreateRequest request, Long clubId) {
public void createContent(String userId, ContentCreateRequest request, Long clubId, Long asId) {
Member member = memberRepository.findMemberById(UUID.fromString(userId)).orElseThrow(
() -> new MemberException(MEMBER_NOT_FOUND)
);
Expand All @@ -62,7 +69,22 @@ public void createContent(String userId, ContentCreateRequest request, Long club
club = clubRepository.findClubById(clubId).orElseThrow(
() -> new ClubException(CLUB_NOT_FOUND)
);
if (club.getBook() == null) {
throw new AssignmentException(NOT_EXIST_BOOK);
}
}

Assignment assignment;
if (asId == null) {
assignment = null;
}
else {
assignment = assignmentRepository.findAssignmentById(asId).orElseThrow(
() -> new AssignmentException(ASSIGNMENT_NOT_FOUND)
);
}


Content saved = contentRepository.save(
Content.builder()
.type(request.contentType())
Expand All @@ -77,6 +99,9 @@ public void createContent(String userId, ContentCreateRequest request, Long club

member.getContents().add(saved);
book.getContents().add(saved);
if (assignment != null) {
assignment.getContents().add(saved);
}
if (club != null) {
club.getContents().add(saved);
}
Expand Down

0 comments on commit 156eef7

Please sign in to comment.