Skip to content

Commit

Permalink
Merge pull request #16 from GDG-Hackathon-77ia/refactor/addRequestTime
Browse files Browse the repository at this point in the history
Refactor: add request with time
  • Loading branch information
GitJIHO authored Nov 11, 2024
2 parents 7ade8b9 + fbd9786 commit 3d5a387
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
Expand Down Expand Up @@ -74,10 +75,10 @@ public ResponseEntity<DailyResponseResponse> getOneResponseOfDailyQuestion(@Requ
return ResponseEntity.ok().body(dailyResponseResponse);
}

@Operation(summary = "본인이 모든 답변 조회", description = "본인이 작성한 모든 답변을 조회합니다.")
@GetMapping("/response/all")
public ResponseEntity<List<DailyResponseResponse>> getAllResponseOfDailyQuestion(@RequestAttribute("memberId") Long memberId) {
List<DailyResponseResponse> dailyResponseResponses = dailyResponseService.getAllResponseOfDailyQuestion(memberId);
@Operation(summary = "날짜별 본인이 모든 답변 조회", description = "localDate에 본인이 작성한 모든 답변을 조회합니다.")
@GetMapping("/response/all/{localDate}")
public ResponseEntity<List<DailyResponseResponse>> getAllResponseOfDailyQuestion(@RequestAttribute("memberId") Long memberId, @PathVariable("localDate") LocalDate localDate) {
List<DailyResponseResponse> dailyResponseResponses = dailyResponseService.getAllResponseOfDailyQuestionInLocalDate(memberId, localDate);
return ResponseEntity.ok().body(dailyResponseResponses);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Entity
Expand All @@ -27,6 +28,8 @@ public class DailyResponse {
@CreatedDate
private LocalDateTime responseDateTime;
@NotNull
private LocalDate responseDate;
@NotNull
private String response;
@ManyToOne
@JoinColumn(name = "member_id")
Expand All @@ -45,6 +48,11 @@ public DailyResponse(String response, Member member, DailyQuestion dailyQuestion
this.dailyQuestion = dailyQuestion;
}

@PrePersist
public void convertToReceivedDate() {
this.responseDate = this.responseDateTime.toLocalDate();
}

public boolean checkMember(Member member) {
return this.member.equals(member);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import com.gdg.kkia.member.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface DailyResponseRepository extends JpaRepository<DailyResponse, Long> {

List<DailyResponse> findAllByMember(Member member);
List<DailyResponse> findAllByMemberAndResponseDate(Member member, LocalDate localDate);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -53,11 +54,11 @@ public DailyResponseResponse getOneResponseOfDailyQuestion(Long memberId, Long d
return new DailyResponseResponse(dailyResponse.getId(), dailyResponse.getDailyQuestion().getQuestion(), dailyResponse.getResponse());
}

public List<DailyResponseResponse> getAllResponseOfDailyQuestion(Long memberId) {
public List<DailyResponseResponse> getAllResponseOfDailyQuestionInLocalDate(Long memberId, LocalDate localDate) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundException("memberId에 해당하는 멤버가 없습니다."));

return dailyResponseRepository.findAllByMember(member)
return dailyResponseRepository.findAllByMemberAndResponseDate(member, localDate)
.stream()
.map(DailyResponse -> new DailyResponseResponse(
DailyResponse.getId(),
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/com/gdg/kkia/diary/controller/DiaryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@RestController
Expand All @@ -21,17 +22,17 @@ public class DiaryController {

private final DiaryService diaryService;

@Operation(summary = "일기 작성", description = "사용자가 일기를 작성합니다.")
@Operation(summary = "일기 작성", description = "사용자가 일기를 작성합니다. 리스트형태로 여러개를 넣을 수 있습니다.")
@PostMapping
public ResponseEntity<StringTypeMessageResponse> writeDiary(@RequestAttribute("memberId") Long memberId, @RequestBody DiaryWriteRequest diaryWriteRequest) {
diaryService.writeDiary(memberId, diaryWriteRequest);
public ResponseEntity<StringTypeMessageResponse> writeDiary(@RequestAttribute("memberId") Long memberId, @RequestBody List<DiaryWriteRequest> diaryWriteRequests) {
diaryService.writeDiary(memberId, diaryWriteRequests);
return ResponseEntity.status(HttpStatus.CREATED).body(new StringTypeMessageResponse("일기가 작성되었습니다."));
}

@Operation(summary = "작성한 모든 일기 조회", description = "사용자가 작성했던 모든 일기를 조회합니다.")
@GetMapping()
public ResponseEntity<List<DiaryReadResponse>> getAllDiary(@RequestAttribute("memberId") Long memberId) {
List<DiaryReadResponse> diaryReadResponses = diaryService.getAllDiaryWrittenByMember(memberId);
@Operation(summary = "유저의 날짜별 작성 일기 조회", description = "localDate에 해당하는 날짜에 사용자가 작성했던 모든 일기를 조회합니다.")
@GetMapping("/all/{localDate}")
public ResponseEntity<List<DiaryReadResponse>> getAllDiary(@RequestAttribute("memberId") Long memberId, @PathVariable("localDate") LocalDate localDate) {
List<DiaryReadResponse> diaryReadResponses = diaryService.getAllDiaryWrittenByMemberInLocalDate(memberId, localDate);
return ResponseEntity.ok().body(diaryReadResponses);
}

Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/gdg/kkia/diary/dto/DiaryReadResponse.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.gdg.kkia.diary.dto;

import com.gdg.kkia.diary.entity.Diary;

import java.time.LocalDateTime;

public record DiaryReadResponse(
Long diaryId,
LocalDateTime localDateTime,
LocalDateTime writtenDateTime,
Diary.Type type,
String content
) {
}
3 changes: 3 additions & 0 deletions src/main/java/com/gdg/kkia/diary/dto/DiaryWriteRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.gdg.kkia.diary.dto;

import com.gdg.kkia.diary.entity.Diary;

public record DiaryWriteRequest(
Diary.Type type,
String content
) {
}
20 changes: 19 additions & 1 deletion src/main/java/com/gdg/kkia/diary/entity/Diary.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Entity
Expand All @@ -29,17 +30,28 @@ public class Diary {
@NotNull
@CreatedDate
private LocalDateTime writtenDatetime;
@NotNull
private LocalDate writtenDate;
@NotNull
@Enumerated(EnumType.STRING)
private Diary.Type type;
@ManyToOne
@JoinColumn(name = "member_id")
@NotNull
@OnDelete(action = OnDeleteAction.CASCADE)
private Member member;

public Diary(String content, Member member) {
public Diary(Diary.Type type, String content, Member member) {
this.type = type;
this.content = content;
this.member = member;
}

@PrePersist
public void convertToReceivedDate() {
this.writtenDate = this.writtenDatetime.toLocalDate();
}

public boolean checkMemberIsNotCorrect(Member member) {
return !this.member.equals(member);
}
Expand All @@ -50,4 +62,10 @@ public void updateDiary(String content) {
}
this.content = content;
}

public enum Type {
DAY,
EMOTION,
MEMO
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import com.gdg.kkia.diary.entity.Diary;
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface DiaryRepository extends JpaRepository<Diary, Long> {
List<Diary> findAllByMemberId(Long memberId);
List<Diary> findAllByMemberIdAndWrittenDate(Long memberId, LocalDate localDate);
}
16 changes: 10 additions & 6 deletions src/main/java/com/gdg/kkia/diary/service/DiaryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -23,21 +24,24 @@ public class DiaryService {
private final MemberRepository memberRepository;

@Transactional
public void writeDiary(Long memberId, DiaryWriteRequest diaryWriteRequest) {
public void writeDiary(Long memberId, List<DiaryWriteRequest> diaryWriteRequests) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new NotFoundException("id에 해당하는 멤버가 없습니다."));

Diary diary = new Diary(diaryWriteRequest.content(), member);
diaryRepository.save(diary);
for (DiaryWriteRequest diaryWriteRequest : diaryWriteRequests) {
Diary diary = new Diary(diaryWriteRequest.type(), diaryWriteRequest.content(), member);
diaryRepository.save(diary);
}
}

@Transactional(readOnly = true)
public List<DiaryReadResponse> getAllDiaryWrittenByMember(Long memberId) {
return diaryRepository.findAllByMemberId(memberId)
public List<DiaryReadResponse> getAllDiaryWrittenByMemberInLocalDate(Long memberId, LocalDate localDate) {
return diaryRepository.findAllByMemberIdAndWrittenDate(memberId, localDate)
.stream()
.map(Diary -> new DiaryReadResponse(
Diary.getId(),
Diary.getWrittenDatetime(),
Diary.getType(),
Diary.getContent()))
.collect(Collectors.toList());
}
Expand All @@ -54,7 +58,7 @@ public DiaryReadResponse getOneDiaryWrittenByMember(Long memberId, Long diaryId)
throw new UnauthorizedException("로그인한 사용자가 작성한 일기가 아닙니다.");
}

return new DiaryReadResponse(diary.getId(), diary.getWrittenDatetime(), diary.getContent());
return new DiaryReadResponse(diary.getId(), diary.getWrittenDatetime(), diary.getType(), diary.getContent());
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
@Tag(name = "인증", description = "인증 관련 API")
@Tag(name = "회원 관리", description = "회원 관리 관련 API")
public class MemberController {

private final MemberService memberService;
Expand Down

0 comments on commit 3d5a387

Please sign in to comment.