Skip to content

Commit

Permalink
Merge pull request #75 from GDSC-KNU/develop
Browse files Browse the repository at this point in the history
feat: UserLike, AnimalDetailResponse add some val
  • Loading branch information
fanta4715 authored Feb 23, 2024
2 parents 715750a + 2e5509d commit 6d75b45
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 21 deletions.
17 changes: 9 additions & 8 deletions src/main/java/com/gdsc/pikpet/controller/AnimalController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gdsc.pikpet.controller;

import com.gdsc.pikpet.config.security.UserSecurityDto;
import com.gdsc.pikpet.dto.response.AnimalDetailResponseDto;
import com.gdsc.pikpet.service.AnimalService;
import lombok.RequiredArgsConstructor;
Expand All @@ -8,22 +9,22 @@
import org.springframework.data.geo.Point;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/animals")
@RequestMapping("/api/animal")
@RequiredArgsConstructor
public class AnimalController {
private final AnimalService animalService;

@GetMapping("/{animalId}")
public ResponseEntity<AnimalDetailResponseDto> searchAnimalDetail(@PathVariable Long animalId) {
return ResponseEntity.ok(animalService.getAnimalDetail(animalId));
public ResponseEntity<AnimalDetailResponseDto> searchAnimalDetail(
Authentication authentication,
@PathVariable Long animalId
) {
return ResponseEntity.ok(animalService.getAnimalDetail((UserSecurityDto) authentication.getPrincipal(), animalId));
}
// @GetMapping("/location")
// // imageUrl을 통해 가져온 이미지를 통해 동물을 검색하고, 해당 동물이 유저 반경에 있다면 Page에 담아서 return
// public ResponseEntity<?> searchAnimal(
//
// )

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.gdsc.pikpet.config.security.UserSecurityDto;
import com.gdsc.pikpet.dto.UserLikeResponse;
import com.gdsc.pikpet.service.UserAccountService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -32,11 +33,15 @@ public ResponseEntity<?> updateUserInfo(Authentication authentication, @RequestB
}

@PostMapping("/likeAnimal")
public ResponseEntity<?> likeAnimal(Authentication authentication, @RequestParam Long animalId) {
String stateMessage = userAccountService.addlikeAnimal((UserSecurityDto) authentication.getPrincipal(), animalId);
return ResponseEntity.ok().body(stateMessage);
public ResponseEntity<UserLikeResponse> likeAnimal(
Authentication authentication,
@RequestParam Long animalId
) {
UserLikeResponse userLikeResponse = userAccountService.addlikeAnimal((UserSecurityDto) authentication.getPrincipal(), animalId);
return ResponseEntity.ok().body(userLikeResponse);
}

//TODO: fixMe: 좋아하는 "동물" 조회하도록 변경해야함
@GetMapping("/likeAnimal")
public ResponseEntity<?> getLikeAnimal(Authentication authentication) {
return ResponseEntity.ok().body(userAccountService.getLikeAnimal((UserSecurityDto) authentication.getPrincipal()));
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/gdsc/pikpet/dto/UserLikeResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.gdsc.pikpet.dto;

public record UserLikeResponse (
boolean isLiked,
String message
){
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.gdsc.pikpet.entity.animal.Animal;
import com.gdsc.pikpet.entity.animal.AnimalSize;
import com.gdsc.pikpet.entity.animal.AnimalColor;
import com.gdsc.pikpet.entity.animal.Breed;
import com.gdsc.pikpet.entity.animal.Color;
import com.gdsc.pikpet.entity.animal.Species;
import java.time.LocalDateTime;
Expand All @@ -16,16 +17,19 @@ public record AnimalDetailResponseDto(
Species species,
Gender gender,
AnimalSize size,
String deasease,
String disease,
// Shelter를 노출시키는 것이 좋을지에 대한 고민이 필요함. ShelterId만 노출시키는 것이 좋을 수 있음
Shelter shelter,
boolean isNeutralized,
boolean checkUp,
LocalDateTime captureDate,
LocalDateTime enthanasiaDate,
List<Color> color
LocalDateTime euthanasiaDate,
List<Color> colors,
Integer age,
Breed breed,
Boolean isLiked
) {
public static AnimalDetailResponseDto from(Animal animal) {
public static AnimalDetailResponseDto from(Animal animal, Boolean isLiked) {
return new AnimalDetailResponseDto(
animal.getId(),
animal.getImageUrl(),
Expand All @@ -40,7 +44,10 @@ public static AnimalDetailResponseDto from(Animal animal) {
animal.getEnthanasiaDate(),
animal.getAnimalColors().stream()
.map(AnimalColor::getColor)
.toList()
.toList(),
animal.getAge(),
animal.getBreed(),
isLiked
);
}
}
11 changes: 9 additions & 2 deletions src/main/java/com/gdsc/pikpet/service/AnimalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import com.gdsc.pikpet.dto.AnimalSimpleDto;
import com.gdsc.pikpet.dto.response.AnimalDetailResponseDto;
import com.gdsc.pikpet.entity.UserAccount;
import com.gdsc.pikpet.entity.UserLike;
import com.gdsc.pikpet.entity.animal.Animal;
import com.gdsc.pikpet.repository.AnimalRepository;
import com.gdsc.pikpet.repository.LikeRepository;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand All @@ -23,9 +25,14 @@ public class AnimalService {
private final AnimalRepository animalRepository;
private final LikeRepository likeRepository;
private final int PAGE_SIZE = 10;
public AnimalDetailResponseDto getAnimalDetail(Long animalId) {
public AnimalDetailResponseDto getAnimalDetail(UserSecurityDto userSecurityDto, Long animalId) {
UserAccount userAccount = userAccountService.getUserAccount(userSecurityDto);
Optional<Animal> animal = animalRepository.findById(animalId);
if (!animal.isPresent()) throw new IllegalArgumentException("존재하지 않는 동물입니다");
boolean isLiked = isLikedByUser(userAccount, animal.get());

return animalRepository.findById(animalId)
.map(AnimalDetailResponseDto::from)
.map((Animal a) -> AnimalDetailResponseDto.from(a, isLiked))
.orElseThrow(() -> new IllegalArgumentException("해당하는 동물을 찾을 수 없습니다 - animalId: " + animalId));
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/gdsc/pikpet/service/UserAccountService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gdsc.pikpet.service;

import com.gdsc.pikpet.config.security.UserSecurityDto;
import com.gdsc.pikpet.dto.UserLikeResponse;
import com.gdsc.pikpet.entity.animal.Animal;
import com.gdsc.pikpet.entity.Application;
import com.gdsc.pikpet.entity.UserAccount;
Expand Down Expand Up @@ -33,16 +34,16 @@ public UserSecurityDto updateUser(UserSecurityDto userSecurityDto) {
}

@Transactional
public String addlikeAnimal(UserSecurityDto userSecurityDto, Long animalId) {
public UserLikeResponse addlikeAnimal(UserSecurityDto userSecurityDto, Long animalId) {
UserAccount userAccount = getUserAccount(userSecurityDto);
Animal animal = animalRepository.findById(animalId).orElseThrow(() -> new IllegalArgumentException("동물을 찾을 수 없습니다 - id: " + animalId));
Optional<UserLike> existedUserLike = likeRepository.findByUserAccountAndAnimal(userAccount, animal);
if (existedUserLike.isPresent()) {
likeRepository.delete(existedUserLike.get());
return "관심동물 삭제 완료";
return new UserLikeResponse(false,"관심동물 삭제 완료");
}
likeRepository.save(UserLike.of(userAccount, animal));
return "관심동물 설정 완료";
return new UserLikeResponse(true,"관심동물 설정 완료");
}

@Transactional(readOnly = true)
Expand Down

0 comments on commit 6d75b45

Please sign in to comment.