Skip to content

Commit

Permalink
Feat: #7 Photo 좋아요 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
minjeong073 committed Dec 19, 2023
1 parent c314d23 commit 467cfa8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.snowmanvillage.server.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.snowmanvillage.server.dto.PhotoLikeRequestDto;
import com.snowmanvillage.server.dto.PhotoRequestDto;
import com.snowmanvillage.server.dto.PhotoResponseDto;
import com.snowmanvillage.server.dto.PhotoUploadRequestDto;
Expand All @@ -16,6 +17,7 @@
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -70,4 +72,15 @@ public ResponseEntity<String> deletePhoto(@PathVariable(name = "photoId") Long p
return ResponseEntity.ok("포토 삭제 실패");
}
}

@PutMapping("/like")
public ResponseEntity<PhotoResponseDto> likePhoto(@RequestBody PhotoLikeRequestDto requestDto) {
return ResponseEntity.ok(photoService.likePhoto(requestDto.getPhoto_id()));
}

@PutMapping("/unlike")
public ResponseEntity<PhotoResponseDto> unlikePhoto(@RequestBody PhotoLikeRequestDto requestDto) {
return ResponseEntity.ok(photoService.unlikePhoto(requestDto.getPhoto_id()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.snowmanvillage.server.dto;

import lombok.Getter;

@Getter
public class PhotoLikeRequestDto {
private Long photo_id;
}
13 changes: 13 additions & 0 deletions src/main/java/com/snowmanvillage/server/service/PhotoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,17 @@ public String deletePhoto(Long photoId, String password) {
}
}

@Transactional
public PhotoResponseDto likePhoto(Long photoId) {
Photo photo = photoRepository.findById(photoId).orElseThrow(() -> new IllegalArgumentException("해당 포토가 없습니다."));
photo.setLikeCount(photo.getLikeCount() + 1);
return PhotoResponseDto.of(photo);
}

@Transactional
public PhotoResponseDto unlikePhoto(Long photoId) {
Photo photo = photoRepository.findById(photoId).orElseThrow(() -> new IllegalArgumentException("해당 포토가 없습니다."));
photo.setLikeCount(photo.getLikeCount() - 1);
return PhotoResponseDto.of(photo);
}
}

0 comments on commit 467cfa8

Please sign in to comment.