#24 simple sns: 코드 최적화
This commit is contained in:
@@ -64,7 +64,7 @@ public class PostController {
|
||||
}
|
||||
|
||||
@GetMapping("/{postId}/likes")
|
||||
public Response<Integer> likeCount(@PathVariable Integer postId, Authentication authentication) {
|
||||
public Response<Long> likeCount(@PathVariable Integer postId, Authentication authentication) {
|
||||
return Response.success(postService.likeCount(postId));
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,11 @@ import com.example.sns.controller.response.AlarmResponse;
|
||||
import com.example.sns.controller.response.Response;
|
||||
import com.example.sns.controller.response.UserJoinResponse;
|
||||
import com.example.sns.controller.response.UserLoginResponse;
|
||||
import com.example.sns.exception.ErrorCode;
|
||||
import com.example.sns.exception.SnsApplicationException;
|
||||
import com.example.sns.model.User;
|
||||
import com.example.sns.service.UserService;
|
||||
import com.example.sns.util.ClassUtils;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -36,6 +39,13 @@ public class UserController {
|
||||
|
||||
@GetMapping("/alarm")
|
||||
public Response<Page<AlarmResponse>> alarm(Pageable pageable, Authentication authentication) {
|
||||
return Response.success(userService.alarmList(authentication.getName(), pageable).map(AlarmResponse::fromAlarm));
|
||||
User user = ClassUtils.getSafeCastInstance(authentication.getPrincipal(), User.class)
|
||||
.orElseThrow(
|
||||
() -> new SnsApplicationException(
|
||||
ErrorCode.INTERNAL_SERVER_ERROR,
|
||||
"Casting to User class failed"
|
||||
)
|
||||
);
|
||||
return Response.success(userService.alarmList(user.getId(), pageable).map(AlarmResponse::fromAlarm));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ import java.sql.Timestamp;
|
||||
public class Alarm {
|
||||
|
||||
private Integer id;
|
||||
private User user;
|
||||
private AlarmType alarmType;
|
||||
private AlarmArgs args;
|
||||
|
||||
@@ -22,7 +21,6 @@ public class Alarm {
|
||||
public static Alarm fromEntity(AlarmEntity entity) {
|
||||
return new Alarm(
|
||||
entity.getId(),
|
||||
User.fromEntity(entity.getUser()),
|
||||
entity.getAlarmType(),
|
||||
entity.getArgs(),
|
||||
entity.getRegisteredAt(),
|
||||
|
||||
@@ -29,7 +29,7 @@ public class AlarmEntity {
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Integer id;
|
||||
|
||||
@ManyToOne
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JoinColumn(name = "user_id")
|
||||
private UserEntity user;
|
||||
|
||||
|
||||
@@ -10,5 +10,5 @@ import org.springframework.stereotype.Repository;
|
||||
@Repository
|
||||
public interface AlarmEntityRepository extends JpaRepository<AlarmEntity, Integer> {
|
||||
|
||||
Page<AlarmEntity> findAllByUser(UserEntity user, Pageable pageable);
|
||||
Page<AlarmEntity> findAllByUserId(Integer userId, Pageable pageable);
|
||||
}
|
||||
|
||||
@@ -5,10 +5,19 @@ import com.example.sns.model.entity.PostEntity;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
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;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Repository
|
||||
public interface CommentEntityRepository extends JpaRepository<CommentEntity, Integer> {
|
||||
|
||||
Page<CommentEntity> findAllByPost(PostEntity post, Pageable pageable);
|
||||
|
||||
@Transactional
|
||||
@Modifying
|
||||
@Query("UPDATE CommentEntity entity SET deleted_at = NOW() WHERE entity.post = :post")
|
||||
void deleteAllByPost(@Param("post") PostEntity postEntity);
|
||||
}
|
||||
|
||||
@@ -4,9 +4,11 @@ import com.example.sns.model.entity.LikeEntity;
|
||||
import com.example.sns.model.entity.PostEntity;
|
||||
import com.example.sns.model.entity.UserEntity;
|
||||
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;
|
||||
import org.springframework.stereotype.Repository;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -16,8 +18,15 @@ public interface LikeEntityRepository extends JpaRepository<LikeEntity, Integer>
|
||||
|
||||
Optional<LikeEntity> findByUserAndPost(UserEntity user, PostEntity post);
|
||||
|
||||
@Query(value = "SELECT COUNT(*) FROM LikeEntity entity WHERE entity.post =:post")
|
||||
Integer countByPost(@Param("post") PostEntity post);
|
||||
// @Query(value = "SELECT COUNT(*) FROM LikeEntity entity WHERE entity.post =:post")
|
||||
// Integer countByPost(@Param("post") PostEntity post);
|
||||
|
||||
long countByPost(PostEntity post);
|
||||
|
||||
List<LikeEntity> findAllByPost(PostEntity post);
|
||||
|
||||
@Transactional
|
||||
@Modifying
|
||||
@Query("UPDATE LikeEntity entity SET deleted_at = NOW() WHERE entity.post = :post")
|
||||
void deleteAllByPost(@Param("post") PostEntity postEntity);
|
||||
}
|
||||
|
||||
@@ -62,6 +62,8 @@ public class PostService {
|
||||
);
|
||||
}
|
||||
|
||||
likeEntityRepository.deleteAllByPost(postEntity);
|
||||
commentEntityRepository.deleteAllByPost(postEntity);
|
||||
postEntityRepository.delete(postEntity);
|
||||
}
|
||||
|
||||
@@ -100,7 +102,7 @@ public class PostService {
|
||||
);
|
||||
}
|
||||
|
||||
public int likeCount(Integer postId) {
|
||||
public long likeCount(Integer postId) {
|
||||
PostEntity postEntity = getPostEntityOrException(postId);
|
||||
|
||||
// count like
|
||||
|
||||
@@ -69,12 +69,12 @@ public class UserService {
|
||||
return JwtTokenUtils.generateToken(username, secretKey, expiredTimeMs);
|
||||
}
|
||||
|
||||
public Page<Alarm> alarmList(String username, Pageable pageable) {
|
||||
UserEntity userEntity = userEntityRepository.findByUsername(username)
|
||||
.orElseThrow(
|
||||
() -> new SnsApplicationException(USER_NOT_FOUND, String.format("%s not founded", username))
|
||||
);
|
||||
public Page<Alarm> alarmList(Integer userId, Pageable pageable) {
|
||||
// UserEntity userEntity = userEntityRepository.findByUsername(username)
|
||||
// .orElseThrow(
|
||||
// () -> new SnsApplicationException(USER_NOT_FOUND, String.format("%s not founded", username))
|
||||
// );
|
||||
|
||||
return alarmEntityRepository.findAllByUser(userEntity, pageable).map(Alarm::fromEntity);
|
||||
return alarmEntityRepository.findAllByUserId(userId, pageable).map(Alarm::fromEntity);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.example.sns.util;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class ClassUtils {
|
||||
|
||||
public static <T>Optional<T> getSafeCastInstance(Object o, Class<T> clazz) {
|
||||
return clazz != null && clazz.isInstance(o) ? Optional.of(clazz.cast(o)) : Optional.empty();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user