-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9429444
commit fc5b9c4
Showing
11 changed files
with
168 additions
and
9 deletions.
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
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,23 @@ | ||
package com.photo.domain; | ||
|
||
import lombok.Getter; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
import javax.persistence.EntityListeners; | ||
import javax.persistence.MappedSuperclass; | ||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@MappedSuperclass | ||
@EntityListeners(AuditingEntityListener.class) | ||
public abstract class BaseTimeEntity { | ||
|
||
@CreatedDate | ||
private LocalDateTime createdDate; | ||
|
||
@LastModifiedDate | ||
private LocalDateTime modifiedDate; | ||
|
||
} |
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,35 @@ | ||
package com.photo.domain.follow; | ||
|
||
import com.photo.domain.BaseTimeEntity; | ||
import com.photo.domain.user.User; | ||
import lombok.*; | ||
|
||
import javax.persistence.*; | ||
|
||
|
||
|
||
@NoArgsConstructor | ||
@Getter | ||
@Entity | ||
@Table( | ||
uniqueConstraints = { | ||
@UniqueConstraint( | ||
name = "follow_uk", | ||
columnNames = {"fromUserId", "toUserId"} | ||
) | ||
} | ||
) | ||
public class Follow extends BaseTimeEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private int id; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "fromUserId") | ||
private User fromUser; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "toUserId") | ||
private User toUser; | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/photo/domain/follow/FollowRepository.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,18 @@ | ||
package com.photo.domain.follow; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.repository.query.Param; | ||
|
||
public interface FollowRepository extends JpaRepository<Follow, Integer> { | ||
|
||
@Modifying | ||
@Query(value = "INSERT INTO follow(fromUserId,toUserId,createdDate) VALUES(:fromUserId,:toUserId,now())", nativeQuery = true) | ||
void cFollow(@Param(value = "fromUserId") Integer fromUserId, @Param(value = "toUserId") Integer toUserId); | ||
|
||
@Modifying | ||
@Query(value = "DELETE FROM follow WHERE fromUserId =:fromUserId AND toUserId =:toUserId", nativeQuery = true) | ||
void cUnFollow(@Param(value = "fromUserId") Integer fromUserId, @Param(value = "toUserId") Integer toUserId); | ||
|
||
} |
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
19 changes: 19 additions & 0 deletions
19
src/main/java/com/photo/handler/exception/CustomApiException.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,19 @@ | ||
package com.photo.handler.exception; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.Map; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class CustomApiException extends RuntimeException { | ||
private static final long serialVersionUID = 1L; | ||
|
||
private String message; | ||
private Map<String, String> errors; | ||
|
||
public CustomApiException(String message) { | ||
this.message = message; | ||
} | ||
} |
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.photo.service; | ||
|
||
import com.photo.domain.follow.FollowRepository; | ||
import com.photo.handler.exception.CustomApiException; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class FollowService { | ||
|
||
private final FollowRepository followRepository; | ||
|
||
|
||
@Transactional | ||
public void follow(int fromUserId, int toUserId) { | ||
try{ | ||
followRepository.cFollow(fromUserId,toUserId); | ||
}catch (Exception e) { | ||
throw new CustomApiException("이미 팔로우 하고 있는 유저입니다."); | ||
} | ||
} | ||
|
||
@Transactional | ||
public void unFollow(int fromUserId, int toUserId) { | ||
followRepository.cUnFollow(fromUserId,toUserId); | ||
} | ||
} |
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,32 @@ | ||
package com.photo.web.api; | ||
|
||
import com.photo.config.auth.CustomUserDetails; | ||
import com.photo.service.FollowService; | ||
import com.photo.web.dto.ResDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class FollowApiController { | ||
|
||
private final FollowService followService; | ||
|
||
@PostMapping("/api/follow/{toUserId}") | ||
public ResponseEntity<?> follow(@AuthenticationPrincipal CustomUserDetails customUserDetails, @PathVariable int toUserId) { | ||
followService.follow(customUserDetails.getUser().getId(),toUserId); | ||
return new ResponseEntity<>(new ResDto<>(1,"팔로우 성공", null), HttpStatus.OK); | ||
} | ||
|
||
@DeleteMapping("/api/follow/{toUserId}") | ||
public ResponseEntity<?> unFollow(@AuthenticationPrincipal CustomUserDetails customUserDetails, @PathVariable int toUserId) { | ||
followService.unFollow(customUserDetails.getUser().getId(), toUserId); | ||
return new ResponseEntity<>(new ResDto<>(1,"언팔로우 성공", null), HttpStatus.OK); | ||
} | ||
} |
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