-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
π μ’μμ μμ² λ° μ·¨μ κΈ°λ₯ μμ± #55
Merged
Merged
Changes from 13 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
26ff537
feat: like schema μΆκ°
shoeone96 21cf1fd
feat: Like entity μΆκ°
shoeone96 2adc901
feat: μ’μμ μμ²κ³Ό μ·¨μλ₯Ό μν api κ°λ°
shoeone96 be4aca6
feat: μ’μμ μμ²κ³Ό μμ μ μ λ¬λλ λ°μ΄ν° μ μ
shoeone96 3efe23f
feat: μ’μμ db μ°κ²°μ μν repository μμ±
shoeone96 73e4061
feat: μ’μμ μλΉμ€ λ‘μ§ μμ±
shoeone96 48706da
test: like μμ² api test
shoeone96 76b2cf6
feat: like μμ² μλΉμ€ λ‘μ§ ν
μ€νΈ
shoeone96 d0b8b0a
test: like entity test μ½λ
shoeone96 4cf1359
fix: sql error ꡬ문 μμ
shoeone96 8352b24
style: test DisplayName μμ
shoeone96 d9dcc5f
refactor: WeatherDto μμλ‘ μ μΈ
shoeone96 73c7193
style: style check
shoeone96 153bcfc
style: κ³΅ν΅ μ€νμΌ μμ
shoeone96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
29 changes: 29 additions & 0 deletions
29
src/main/java/com/backendoori/ootw/like/controller/LikeController.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,29 @@ | ||
package com.backendoori.ootw.like.controller; | ||
|
||
import com.backendoori.ootw.like.dto.controller.LikeRequest; | ||
import com.backendoori.ootw.like.dto.controller.LikeResponse; | ||
import com.backendoori.ootw.like.service.LikeService; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/api/v1/likes") | ||
public class LikeController { | ||
|
||
private final LikeService likeService; | ||
|
||
@PostMapping("") | ||
public ResponseEntity<LikeResponse> pushLike(Authentication authentication, | ||
@Valid @RequestBody LikeRequest requestDto) { | ||
Long userId = (Long) authentication.getPrincipal(); | ||
return ResponseEntity.ok(likeService.requestLike(userId, requestDto.postId())); | ||
} | ||
|
||
} |
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,58 @@ | ||
package com.backendoori.ootw.like.domain; | ||
|
||
import com.backendoori.ootw.common.BaseEntity; | ||
import com.backendoori.ootw.post.domain.Post; | ||
import com.backendoori.ootw.user.domain.User; | ||
import io.jsonwebtoken.lang.Assert; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Table(name = "likes") | ||
@Entity | ||
@Getter | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
public class Like extends BaseEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id") | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id", nullable = false) | ||
private User user; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "post_id", nullable = false) | ||
private Post post; | ||
|
||
@Column(name = "status", columnDefinition = "tinyint") | ||
private Boolean status; | ||
|
||
private Like(Long id, User user, Post post, Boolean status) { | ||
Assert.notNull(user); | ||
Assert.notNull(post); | ||
Assert.notNull(status); | ||
this.id = id; | ||
this.user = user; | ||
this.post = post; | ||
this.status = status; | ||
} | ||
|
||
public void updateStatus() { | ||
this.status = !status; | ||
} | ||
|
||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/backendoori/ootw/like/dto/controller/LikeRequest.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,14 @@ | ||
package com.backendoori.ootw.like.dto.controller; | ||
|
||
import jakarta.validation.constraints.Min; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record LikeRequest( | ||
@Min(value = 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
@NotNull(message = NULL_MESSAGE) | ||
Long postId | ||
) { | ||
|
||
private static final String NULL_MESSAGE = "λ°λμ postId κ°μ΄ null μ λλ€."; | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/backendoori/ootw/like/dto/controller/LikeResponse.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,20 @@ | ||
package com.backendoori.ootw.like.dto.controller; | ||
|
||
import com.backendoori.ootw.like.domain.Like; | ||
|
||
public record LikeResponse( | ||
Long likeId, | ||
Long userId, | ||
Long postId, | ||
boolean status | ||
) { | ||
|
||
public static LikeResponse from(Like like) { | ||
return new LikeResponse( | ||
like.getId(), | ||
like.getUser().getId(), | ||
like.getPost().getId(), | ||
like.getStatus()); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/backendoori/ootw/like/repository/LikeRepository.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 com.backendoori.ootw.like.repository; | ||
|
||
import java.util.Optional; | ||
import com.backendoori.ootw.like.domain.Like; | ||
import com.backendoori.ootw.post.domain.Post; | ||
import com.backendoori.ootw.user.domain.User; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface LikeRepository extends JpaRepository<Like, Long> { | ||
|
||
Optional<Like> findByUserAndPost(User user, Post post); | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/backendoori/ootw/like/service/LikeService.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,60 @@ | ||
package com.backendoori.ootw.like.service; | ||
|
||
import java.util.NoSuchElementException; | ||
import com.backendoori.ootw.exception.UserNotFoundException; | ||
import com.backendoori.ootw.like.domain.Like; | ||
import com.backendoori.ootw.like.dto.controller.LikeResponse; | ||
import com.backendoori.ootw.like.repository.LikeRepository; | ||
import com.backendoori.ootw.post.domain.Post; | ||
import com.backendoori.ootw.post.repository.PostRepository; | ||
import com.backendoori.ootw.user.domain.User; | ||
import com.backendoori.ootw.user.repository.UserRepository; | ||
import jakarta.transaction.Transactional; | ||
import lombok.RequiredArgsConstructor; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class LikeService { | ||
|
||
private static final String POST_NOT_FOUND_MESSAGE = "ν΄λΉ κ²μκΈμ΄ μ‘΄μ¬νμ§ μμ΅λλ€."; | ||
private static final String LIKE_NOT_FOUND_MESSAGE = "μ°ΎμΌμλ μ’μμ μ λ³΄κ° μ‘΄μ¬νμ§ μμ΅λλ€."; | ||
|
||
private final UserRepository userRepository; | ||
private final PostRepository postRepository; | ||
private final LikeRepository likeRepository; | ||
|
||
@Transactional | ||
public LikeResponse requestLike(Long userId, Long postId) { | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ¬κΈ° κ°νμ μμ΄λ λ κ² κ°μμ! |
||
User user = userRepository.findById(userId) | ||
.orElseThrow(UserNotFoundException::new); | ||
|
||
Post post = postRepository.findById(postId) | ||
.orElseThrow(() -> new NoSuchElementException(POST_NOT_FOUND_MESSAGE)); | ||
|
||
likeRepository.findByUserAndPost(user, post).ifPresentOrElse( | ||
Like::updateStatus, | ||
likeNotExist(user, post) | ||
); | ||
|
||
Like like = likeRepository.findByUserAndPost(user, post) | ||
.orElseThrow(() -> new NoSuchElementException(LIKE_NOT_FOUND_MESSAGE)); | ||
|
||
return LikeResponse.from(like); | ||
} | ||
|
||
@NotNull | ||
private Runnable likeNotExist(User user, Post post) { | ||
return () -> { | ||
likeRepository.save( | ||
Like.builder() | ||
.user(user) | ||
.post(post) | ||
.status(true) | ||
.build()); | ||
}; | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mappingμ΄ λ³λλ‘ νμ μμΌλ©΄ μμ°λ κ²λ μ’μ κ² κ°μμ