Skip to content

Commit

Permalink
Merge pull request #23 from My-Music-Note/dev
Browse files Browse the repository at this point in the history
merge to main
  • Loading branch information
masiljangajji authored Dec 3, 2024
2 parents 6d7664f + 053f447 commit 8161ca2
Show file tree
Hide file tree
Showing 33 changed files with 315 additions and 102 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package my.music.note.back.gpt.controller;
package my.music.note.back.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import my.music.note.back.gpt.dto.ChatCompletionDto;
import my.music.note.back.gpt.service.ChatGPTService;
import my.music.note.back.data.dto.gpt.request.ChatCompletionDto;
import my.music.note.back.service.ChatGPTService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/my/music/note/back/controller/DiaryController.java
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);
}



}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package my.music.note.back.user.controller;
package my.music.note.back.controller;

import lombok.RequiredArgsConstructor;
import my.music.note.back.jwt.dto.response.TokenCreateResponse;
import my.music.note.back.jwt.service.TokenService;
import my.music.note.back.user.dto.request.DeleteAccountRequest;
import my.music.note.back.user.dto.request.FindUserRequest;
import my.music.note.back.user.dto.request.LoginOrRegisterRequest;
import my.music.note.back.user.dto.request.ModifyNameRequest;
import my.music.note.back.user.dto.response.FindUserResponse;
import my.music.note.back.user.entity.User;
import my.music.note.back.user.service.UserService;
import my.music.note.back.data.dto.jwt.response.TokenCreateResponse;
import my.music.note.back.data.dto.user.request.DeleteAccountRequest;
import my.music.note.back.data.dto.user.request.FindUserRequest;
import my.music.note.back.data.dto.user.request.LoginOrRegisterRequest;
import my.music.note.back.data.dto.user.request.ModifyNameRequest;
import my.music.note.back.data.dto.user.response.FindUserResponse;
import my.music.note.back.data.entity.User;
import my.music.note.back.service.TokenService;
import my.music.note.back.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -31,23 +31,22 @@ public ResponseEntity<TokenCreateResponse> loginOrRegister(@RequestBody LoginOrR
return new ResponseEntity<>(response, HttpStatus.OK);
}


@DeleteMapping
@ResponseStatus(HttpStatus.NO_CONTENT)
public void deleteAccount(@CookieValue String token) {
public void deleteAccount(@RequestHeader("Authorization") String token) {
Long userId = tokenService.getUserId(token);
userService.deleteUser(new DeleteAccountRequest(userId));
}

@PutMapping
@ResponseStatus(HttpStatus.OK)
public void modifyName(@CookieValue String token, @RequestBody ModifyNameRequest request) {
public void modifyName(@RequestHeader("Authorization") String token, @RequestBody ModifyNameRequest request) {
Long userId = tokenService.getUserId(token);
userService.modifyName(request, userId);
}

@GetMapping
public ResponseEntity<FindUserResponse> findUserInfo(@CookieValue String token) {
public ResponseEntity<FindUserResponse> findUser(@RequestHeader("Authorization") String token) {

Long userId = tokenService.getUserId(token);

Expand Down
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) {
}
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) {
}
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) {
}
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) {
}

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.*;

Expand Down
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.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package my.music.note.back.jwt.dto.request;
package my.music.note.back.data.dto.jwt.request;

public record TokenCreateRequest(String providerId, boolean isAdmin) {

Expand Down
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) {
}
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) {
}
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) {
}
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 LoginOrRegisterRequest(String name, String email, String provider, String providerId) {

Expand Down
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) {
}
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) {
}
59 changes: 59 additions & 0 deletions src/main/java/my/music/note/back/data/entity/Diary.java
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);
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package my.music.note.back.user.entity;
package my.music.note.back.data.entity;

import jakarta.persistence.*;
import lombok.NoArgsConstructor;
import my.music.note.back.user.dto.request.LoginOrRegisterRequest;
import my.music.note.back.user.dto.request.ModifyNameRequest;
import my.music.note.back.user.dto.response.FindUserResponse;
import my.music.note.back.data.dto.user.request.LoginOrRegisterRequest;
import my.music.note.back.data.dto.user.request.ModifyNameRequest;
import my.music.note.back.data.dto.user.response.FindUserResponse;

import java.time.LocalDateTime;

Expand All @@ -25,21 +25,21 @@ private User(String name, LocalDateTime createdAt, String email, String provider

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

String name;
private String name;

LocalDateTime createdAt;
private LocalDateTime createdAt;

String email;
private String email;

String provider;
private String provider;

String providerId;
private String providerId;

Boolean isAdmin;
private Boolean isAdmin;

Boolean isDeleted;
private Boolean isDeleted;

public Long getUserId() {
return id;
Expand Down
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);

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package my.music.note.back.user.repository;
package my.music.note.back.data.repository;

import my.music.note.back.user.entity.User;
import my.music.note.back.data.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package my.music.note.back.gpt.service;
package my.music.note.back.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import my.music.note.back.config.ChatGPTConfig;
import my.music.note.back.gpt.dto.ChatCompletionDto;
import my.music.note.back.data.dto.gpt.request.ChatCompletionDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/my/music/note/back/service/DiaryService.java
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();
}

}
Loading

0 comments on commit 8161ca2

Please sign in to comment.