-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from My-Music-Note/diary
Diary
- Loading branch information
Showing
33 changed files
with
315 additions
and
102 deletions.
There are no files selected for viewing
6 changes: 3 additions & 3 deletions
6
...ack/gpt/controller/ChatGPTController.java → ...te/back/controller/ChatGPTController.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
59 changes: 59 additions & 0 deletions
59
src/main/java/my/music/note/back/controller/DiaryController.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,59 @@ | ||
package my.music.note.back.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import my.music.note.back.data.dto.diary.request.DiaryCreateRequest; | ||
import my.music.note.back.data.dto.diary.request.DiaryDeleteRequest; | ||
import my.music.note.back.data.dto.diary.request.DiaryModifyRequest; | ||
import my.music.note.back.data.dto.diary.response.FindDiaryResponse; | ||
import my.music.note.back.service.DiaryService; | ||
import my.music.note.back.service.TokenService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController("/api/diaries") | ||
@RequiredArgsConstructor | ||
public class DiaryController { | ||
|
||
private final DiaryService diaryService; | ||
|
||
private final TokenService tokenService; | ||
|
||
@PostMapping | ||
@ResponseStatus(HttpStatus.OK) | ||
public void createDiary(@RequestBody DiaryCreateRequest request) { | ||
diaryService.createDiary(request); | ||
} | ||
|
||
@DeleteMapping | ||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
public void deleteDiary(@RequestBody DiaryDeleteRequest request) { | ||
diaryService.deleteDiary(request); | ||
} | ||
|
||
@PutMapping | ||
@ResponseStatus(HttpStatus.OK) | ||
public void modifyDiary(@RequestBody DiaryModifyRequest request) { | ||
diaryService.modifyDiary(request); | ||
} | ||
|
||
@GetMapping("/{diaryId}") | ||
public ResponseEntity<FindDiaryResponse> FindDiaryResponse(@PathVariable Long diaryId) { | ||
|
||
FindDiaryResponse response = diaryService.findDiary(diaryId); | ||
return new ResponseEntity<>(response, HttpStatus.OK); | ||
} | ||
|
||
@GetMapping | ||
public ResponseEntity<List<FindDiaryResponse>> FindDiaryResponse(@RequestHeader("Authorization") String token) { | ||
|
||
Long userId = tokenService.getUserId(token); | ||
List<FindDiaryResponse> response = diaryService.findDiaries(userId); | ||
return new ResponseEntity<>(response, HttpStatus.OK); | ||
} | ||
|
||
|
||
|
||
} |
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
4 changes: 4 additions & 0 deletions
4
src/main/java/my/music/note/back/data/dto/diary/request/DiaryCreateRequest.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,4 @@ | ||
package my.music.note.back.data.dto.diary.request; | ||
|
||
public record DiaryCreateRequest(String content,Boolean isLongEntry) { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/my/music/note/back/data/dto/diary/request/DiaryDeleteRequest.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,4 @@ | ||
package my.music.note.back.data.dto.diary.request; | ||
|
||
public record DiaryDeleteRequest(Long diaryId) { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/my/music/note/back/data/dto/diary/request/DiaryModifyRequest.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,4 @@ | ||
package my.music.note.back.data.dto.diary.request; | ||
|
||
public record DiaryModifyRequest(Long id,String content) { | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/my/music/note/back/data/dto/diary/response/FindDiaryResponse.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 my.music.note.back.data.dto.diary.response; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public record FindDiaryResponse(Long id, String content, Boolean isLongEntry, LocalDateTime createdAt, | ||
LocalDateTime currentModifiedAt) { | ||
} | ||
|
2 changes: 1 addition & 1 deletion
2
.../note/back/gpt/dto/ChatCompletionDto.java → ...ta/dto/gpt/request/ChatCompletionDto.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package my.music.note.back.gpt.dto; | ||
package my.music.note.back.data.dto.gpt.request; | ||
|
||
import lombok.*; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
.../note/back/gpt/dto/ChatRequestMsgDto.java → ...ta/dto/gpt/request/ChatRequestMsgDto.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package my.music.note.back.gpt.dto; | ||
package my.music.note.back.data.dto.gpt.request; | ||
|
||
import lombok.*; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...k/jwt/dto/request/TokenCreateRequest.java → ...a/dto/jwt/request/TokenCreateRequest.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
2 changes: 1 addition & 1 deletion
2
...jwt/dto/response/TokenCreateResponse.java → ...dto/jwt/response/TokenCreateResponse.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package my.music.note.back.jwt.dto.response; | ||
package my.music.note.back.data.dto.jwt.response; | ||
|
||
public record TokenCreateResponse(String token) { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/my/music/note/back/data/dto/user/request/DeleteAccountRequest.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,4 @@ | ||
package my.music.note.back.data.dto.user.request; | ||
|
||
public record DeleteAccountRequest(Long id) { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/my/music/note/back/data/dto/user/request/FindUserRequest.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,4 @@ | ||
package my.music.note.back.data.dto.user.request; | ||
|
||
public record FindUserRequest(Long id) { | ||
} |
2 changes: 1 addition & 1 deletion
2
...r/dto/request/LoginOrRegisterRequest.java → .../user/request/LoginOrRegisterRequest.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
2 changes: 1 addition & 1 deletion
2
...k/user/dto/request/ModifyNameRequest.java → ...a/dto/user/request/ModifyNameRequest.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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package my.music.note.back.user.dto.request; | ||
package my.music.note.back.data.dto.user.request; | ||
|
||
public record ModifyNameRequest(String name) { | ||
} |
4 changes: 4 additions & 0 deletions
4
src/main/java/my/music/note/back/data/dto/user/response/FindUserResponse.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,4 @@ | ||
package my.music.note.back.data.dto.user.response; | ||
|
||
public record FindUserResponse(String name, String email, Boolean isAdmin) { | ||
} |
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,59 @@ | ||
package my.music.note.back.data.entity; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.NoArgsConstructor; | ||
import my.music.note.back.data.dto.diary.request.DiaryCreateRequest; | ||
import my.music.note.back.data.dto.diary.request.DiaryModifyRequest; | ||
import my.music.note.back.data.dto.diary.response.FindDiaryResponse; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Entity | ||
@Table(name = "diary") | ||
@NoArgsConstructor | ||
public class Diary { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id") | ||
private User user; | ||
|
||
private String content; | ||
|
||
private LocalDateTime createdAt; | ||
|
||
private LocalDateTime currentModifiedAt; | ||
|
||
private Boolean isLongEntry; | ||
|
||
private Boolean isDeleted; | ||
|
||
private Diary(LocalDateTime createdAt, String content, Boolean isLongEntry) { | ||
this.createdAt = createdAt; | ||
this.content = content; | ||
this.isLongEntry = isLongEntry; | ||
this.isDeleted = false; | ||
} | ||
|
||
public static Diary of(DiaryCreateRequest request) { | ||
return new Diary(LocalDateTime.now(), request.content(), request.isLongEntry()); | ||
} | ||
|
||
public void modifyDiaryContent(DiaryModifyRequest request) { | ||
this.content = request.content(); | ||
this.currentModifiedAt = LocalDateTime.now(); | ||
} | ||
|
||
public boolean isDeleted() { | ||
return this.isDeleted; | ||
} | ||
|
||
|
||
public FindDiaryResponse convertToFindDiaryResponse() { | ||
return new FindDiaryResponse(this.id, this.content, this.isLongEntry, this.createdAt, this.currentModifiedAt); | ||
} | ||
|
||
} |
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
13 changes: 13 additions & 0 deletions
13
src/main/java/my/music/note/back/data/repository/DiaryRepository.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 my.music.note.back.data.repository; | ||
|
||
import my.music.note.back.data.dto.diary.response.FindDiaryResponse; | ||
import my.music.note.back.data.entity.Diary; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface DiaryRepository extends JpaRepository<Diary, Long> { | ||
|
||
List<FindDiaryResponse> findAllByUserIdAndIsDeletedFalse(Long userId); | ||
|
||
} |
4 changes: 2 additions & 2 deletions
4
.../back/user/repository/UserRepository.java → .../back/data/repository/UserRepository.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
4 changes: 2 additions & 2 deletions
4
...note/back/gpt/service/ChatGPTService.java → ...sic/note/back/service/ChatGPTService.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
55 changes: 55 additions & 0 deletions
55
src/main/java/my/music/note/back/service/DiaryService.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,55 @@ | ||
package my.music.note.back.service; | ||
|
||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import my.music.note.back.data.dto.diary.request.DiaryCreateRequest; | ||
import my.music.note.back.data.dto.diary.request.DiaryDeleteRequest; | ||
import my.music.note.back.data.dto.diary.request.DiaryModifyRequest; | ||
import my.music.note.back.data.dto.diary.response.FindDiaryResponse; | ||
import my.music.note.back.data.entity.Diary; | ||
import my.music.note.back.data.repository.DiaryRepository; | ||
import org.springframework.stereotype.Service; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
@Transactional | ||
@RequiredArgsConstructor | ||
public class DiaryService { | ||
|
||
private final DiaryRepository diaryRepository; | ||
|
||
public void createDiary(DiaryCreateRequest diaryCreateRequest) { | ||
|
||
Diary diary = Diary.of(diaryCreateRequest); | ||
diaryRepository.save(diary); | ||
} | ||
|
||
public void deleteDiary(DiaryDeleteRequest diaryDeleteRequest) { | ||
diaryRepository.deleteById(diaryDeleteRequest.diaryId()); | ||
} | ||
|
||
public void modifyDiary(DiaryModifyRequest diaryModifyRequest) { | ||
|
||
Optional<Diary> optionalDiary = diaryRepository.findById(diaryModifyRequest.id()); | ||
|
||
Diary diary = optionalDiary.orElseThrow(IllegalArgumentException::new); | ||
diary.modifyDiaryContent(diaryModifyRequest); | ||
} | ||
|
||
public List<FindDiaryResponse> findDiaries(Long userId) { | ||
return diaryRepository.findAllByUserIdAndIsDeletedFalse(userId); | ||
} | ||
|
||
public FindDiaryResponse findDiary(Long diaryId) { | ||
Optional<Diary> optionalDiary = diaryRepository.findById(diaryId); | ||
Diary diary = optionalDiary.orElseThrow(IllegalArgumentException::new); | ||
|
||
if (diary.isDeleted()) { | ||
throw new IllegalArgumentException(); | ||
} | ||
|
||
return diary.convertToFindDiaryResponse(); | ||
} | ||
|
||
} |
Oops, something went wrong.