Skip to content

Commit

Permalink
Merge pull request #29 from PetQ-A/feature/mypage
Browse files Browse the repository at this point in the history
Feature/mypage
  • Loading branch information
SeoYoonJu authored Aug 11, 2024
2 parents 405ad8a + fc57de4 commit 15a89a3
Show file tree
Hide file tree
Showing 20 changed files with 689 additions and 22 deletions.
88 changes: 88 additions & 0 deletions src/main/java/com/petqa/api/DiaryAPIController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.petqa.api;

import com.petqa.apiPayload.apiPayload.ApiResponse;
import com.petqa.dto.diary.DiaryRequestDTO;
import com.petqa.dto.diary.DiaryResponseDTO;
import com.petqa.service.diary.DiaryService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.hibernate.engine.spi.Resolution;
import java.time.LocalDate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.security.Principal;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/diary")
@RequiredArgsConstructor
public class DiaryAPIController {
private final DiaryService diaryService;


@PostMapping("/{diaryDate}")
public ResponseEntity<ApiResponse<DiaryResponseDTO>> writeDiary(Principal principal,
@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate diaryDate,
@RequestPart("request") DiaryRequestDTO.AllDto request,
@RequestPart(value = "petProfileImage", required = false) MultipartFile diaryImage) {

request.setImg(diaryImage);
String username = principal.getName();
DiaryResponseDTO written = diaryService.writeMyDiary(username, diaryDate, request);

return ResponseEntity.ok(ApiResponse.onSuccess(written));
}

@PatchMapping("/memo/{diaryDate}")
public ResponseEntity<ApiResponse<DiaryResponseDTO>> modifyMemo(Principal principal,
@PathVariable LocalDate diaryDate,
@RequestBody DiaryRequestDTO.MemoDto request){
String username = principal.getName();
DiaryResponseDTO modified = diaryService.modifyMyMemo(username, diaryDate, request);

return ResponseEntity.ok(ApiResponse.onSuccess(modified));
}

@PatchMapping("/picture/{diaryDate}")
public ResponseEntity<ApiResponse<DiaryResponseDTO>> modifyImage(Principal principal,
@PathVariable LocalDate diaryDate,
@RequestPart(value = "petProfileImage", required = false) MultipartFile diaryImage){
String username = principal.getName();
DiaryResponseDTO modified = diaryService.modifyMyImage(username, diaryDate, diaryImage);

return ResponseEntity.ok(ApiResponse.onSuccess(modified));
}

@DeleteMapping("/picture/{diaryDate}")
public ResponseEntity<ApiResponse<DiaryResponseDTO>> eraseImage(Principal principal,
@PathVariable LocalDate diaryDate){
String username = principal.getName();
DiaryResponseDTO modified = diaryService.eraseMyImage(username, diaryDate);

return ResponseEntity.ok(ApiResponse.onSuccess(modified));
}

@GetMapping("/{diaryDate}")
public ResponseEntity<ApiResponse<DiaryResponseDTO>> showDiary(Principal principal,
@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd")
LocalDate diaryDate){
String username = principal.getName();
DiaryResponseDTO eachDiary = diaryService.showMyDiary(username, diaryDate);

return ResponseEntity.ok(ApiResponse.onSuccess(eachDiary));
}

@GetMapping("/all")
public ResponseEntity<ApiResponse<List<DiaryResponseDTO>>> showAllDiary(Principal principal){
String username = principal.getName();
List<DiaryResponseDTO> allDiary = diaryService.showMyAllDiary(username);

return ResponseEntity.ok(ApiResponse.onSuccess(allDiary));
}

}
47 changes: 47 additions & 0 deletions src/main/java/com/petqa/api/MypageAPIController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.petqa.api;

import com.petqa.apiPayload.apiPayload.ApiResponse;
import com.petqa.base.Util;
import com.petqa.domain.User;
import com.petqa.dto.diary.DiaryRequestDTO;
import com.petqa.dto.diary.DiaryResponseDTO;
import com.petqa.dto.mypage.MypageRequestDTO;
import com.petqa.dto.user.UserResponseDTO;
import com.petqa.service.mypage.MypageService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

import static com.petqa.base.Util.getCurrentUser;

@RestController
@RequestMapping("/mypage")
@RequiredArgsConstructor
public class MypageAPIController {

private final MypageService mypageService;

@GetMapping("/profile")
public ResponseEntity<ApiResponse<UserResponseDTO.UserDetail>> showMyPage(){

User user = getCurrentUser();
UserResponseDTO.UserDetail info = mypageService.showMyInfo(user);

return ResponseEntity.ok(ApiResponse.onSuccess(info));
}

@PatchMapping("/profile")
public ResponseEntity<ApiResponse<UserResponseDTO.MypageDetail>> modifyMyPage(@RequestPart("request") MypageRequestDTO request,
@RequestPart(value = "petProfileImage", required = false) MultipartFile profileImage){
User user = getCurrentUser();
UserResponseDTO.MypageDetail info = mypageService.modifyMyInfo(user, request, profileImage);

return ResponseEntity.ok(ApiResponse.onSuccess(info));
}

}
16 changes: 16 additions & 0 deletions src/main/java/com/petqa/converter/DiaryConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.petqa.converter;

import com.petqa.domain.Diary;
import com.petqa.dto.diary.DiaryResponseDTO;

public class DiaryConverter {
public static DiaryResponseDTO responseDTO(Diary diary){
return DiaryResponseDTO.builder()
.img(diary.getImg())
.memo(diary.getMemo())
.createdAt(diary.getCreatedAt())
.modifiedAt(diary.getModifiedAt())
.date(diary.getDate())
.build();
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/petqa/converter/MypageConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.petqa.converter;

import com.petqa.domain.Pet;
import com.petqa.dto.user.UserResponseDTO;

public class MypageConverter {
public static UserResponseDTO.PetDetail convertToPetDetail(Pet pet) {
return UserResponseDTO.PetDetail.builder()
.id(pet.getId())
.name(pet.getName())
.petType(pet.getPetType())
.birth(pet.getBirth())
.profileImage(pet.getProfileImage())
.weight(pet.getWeight())
.gender(pet.getGender())
.build();
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/petqa/domain/Diary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.petqa.domain;

import com.petqa.domain.common.BaseEntity;
import com.petqa.domain.common.MutableBaseEntity;
import com.petqa.dto.diary.DiaryResponseDTO;
import jakarta.persistence.*;
import lombok.*;
import org.joda.time.DateTime;
import java.time.LocalDate;

import java.util.Base64;
import java.util.Date;

@Getter
@Entity
@Builder
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class Diary extends MutableBaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "diary_id")
private Long id;

private String img;

@Column(length = 18)
private String memo;

private LocalDate date; // 이름

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "user_id")
private User user;

// public DiaryResponseDTO toResponseDTO() {
// return DiaryResponseDTO.builder()
// .memo(memo)
// .date(date)
// .img(img)
// .build();
// }
}

1 change: 1 addition & 0 deletions src/main/java/com/petqa/domain/Pet.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@Getter
@Entity
@Builder
@Setter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
@DynamicInsert
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/petqa/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;

@Getter
@Setter
@Entity
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down Expand Up @@ -46,10 +47,17 @@ public class User extends MutableBaseEntity {
@OneToOne(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
private Pet pet;


@OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
private List<Diary> diary;



public void incrementQuestionCount() {
this.questionCount++;
}
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
private List<UserQuestion> userQuestionsList = new ArrayList<>();


}
55 changes: 55 additions & 0 deletions src/main/java/com/petqa/dto/diary/DiaryRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.petqa.dto.diary;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.petqa.domain.Diary;
import com.petqa.domain.User;
import jakarta.persistence.*;
import lombok.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;

import java.time.LocalDateTime;
import java.util.Date;


public class DiaryRequestDTO {

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class AllDto {
private MultipartFile img; // 여기서 img 필드가 MultipartFile 타입이어야 함
private String memo;

public Diary toEntity(User user, LocalDate diaryDate, String imgUrl) {
return Diary.builder()
.user(user)
.memo(memo)
.date(diaryDate)
.img(imgUrl)
.build();
}
}


@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class ImageDto {
private String img;
}

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class MemoDto {
private String memo;
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/petqa/dto/diary/DiaryResponseDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.petqa.dto.diary;

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
import java.time.LocalDate;

import java.time.LocalDateTime;
import java.util.Date;

@Getter
@Setter
@Builder
public class DiaryResponseDTO {
String img;
String memo;
LocalDateTime createdAt;
LocalDateTime modifiedAt;
LocalDate date; // 이름

}
15 changes: 15 additions & 0 deletions src/main/java/com/petqa/dto/mypage/MypageRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.petqa.dto.mypage;

import lombok.Getter;
import lombok.Setter;

import java.time.LocalDate;

@Getter
@Setter
public class MypageRequestDTO {
private String username;
private String petType;
private String petName;
private LocalDate petBirth;
}
12 changes: 12 additions & 0 deletions src/main/java/com/petqa/dto/user/UserResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ public static class UserDetail {

}

@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public static class MypageDetail {
private String username;
private PetDetail petDetail;
private String token; // JWT 토큰 필드 추가

// PetDetail 클래스와 기타 필요한 코드들...
}

@Getter
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/petqa/repository/DiaryRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.petqa.repository;

import com.petqa.domain.Diary;
import com.petqa.domain.User;
import java.time.LocalDate;
import org.springframework.data.jpa.repository.JpaRepository;

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

public interface DiaryRepository extends JpaRepository<Diary, Long> {
Optional<Diary> findByDate(LocalDate diaryDate);

Optional<Diary> findByUserAndDate(User user, LocalDate date);

Optional<List<Diary>> findByUser(User user);
}
8 changes: 8 additions & 0 deletions src/main/java/com/petqa/repository/MypageRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.petqa.repository;

import com.petqa.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MypageRepository extends JpaRepository<User, Long> {
boolean existsByUsername(String username);
}
Loading

0 comments on commit 15a89a3

Please sign in to comment.