forked from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Be feature/#69 assignment
- Loading branch information
Showing
13 changed files
with
268 additions
and
5 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
backend/src/main/java/com/project/capstone/assignment/controller/AssignmentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
backend/src/main/java/com/project/capstone/assignment/controller/dto/AssignmentResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...src/main/java/com/project/capstone/assignment/controller/dto/CreateAssignmentRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
46 changes: 46 additions & 0 deletions
46
backend/src/main/java/com/project/capstone/assignment/domain/Assignment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
backend/src/main/java/com/project/capstone/assignment/domain/AssignmentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/com/project/capstone/assignment/exception/AssignmentException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
backend/src/main/java/com/project/capstone/assignment/exception/AssignmentExceptionType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
backend/src/main/java/com/project/capstone/assignment/service/AssignmentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters