Skip to content

Commit

Permalink
[#54] temp: 한방 쿼리 완성
Browse files Browse the repository at this point in the history
  • Loading branch information
kwj1270 committed Oct 11, 2021
1 parent d7acfa9 commit 3d2d6d5
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 12 deletions.
46 changes: 46 additions & 0 deletions src/main/java/com/study/realworld/domain/follow/TestAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.study.realworld.domain.follow;

import com.study.realworld.domain.follow.application.FollowService;
import com.study.realworld.domain.follow.domain.FollowQueryDslRepository;
import com.study.realworld.domain.follow.dto.FollowableDto;
import com.study.realworld.domain.user.domain.persist.User;
import com.study.realworld.domain.user.domain.persist.UserRepository;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.transaction.annotation.Transactional;
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.RestController;

@Transactional(readOnly = true)
@RestController
public class TestAPI {
private final FollowQueryDslRepository queryDslRepository;
private final UserRepository userRepository;
private final FollowService followService;

public TestAPI(final FollowQueryDslRepository queryDslRepository, final UserRepository userRepository, final FollowService followService) {
this.queryDslRepository = queryDslRepository;
this.userRepository = userRepository;
this.followService = followService;
}

@PostMapping("follow/{targetId}")
public ResponseEntity<FollowableDto> follow(@AuthenticationPrincipal Long id, @PathVariable Long targetId) {
User user = userRepository.findById(id).get();
User user1 = userRepository.findById(targetId).get();
followService.following(id, targetId);
FollowableDto followableDto = queryDslRepository.existMeAndFollowing(user, user1);
return ResponseEntity.ok().body(followableDto);
}

@GetMapping("test/{targetId}")
public ResponseEntity<FollowableDto> test(@AuthenticationPrincipal Long id, @PathVariable Long targetId) {
User user = userRepository.findById(id).get();
User user1 = userRepository.findById(targetId).get();
FollowableDto followableDto = queryDslRepository.existMeAndFollowing(user, user1);
return ResponseEntity.ok().body(followableDto);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.study.realworld.domain.follow.domain;

import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.study.realworld.domain.follow.dto.FollowableDto;
import com.study.realworld.domain.user.domain.persist.User;
import org.springframework.stereotype.Repository;

import java.util.Objects;

import static com.study.realworld.domain.follow.domain.QFollow.follow;
import static com.study.realworld.domain.user.domain.persist.QUser.user;

@Repository
public class FollowQueryDslRepository {
Expand All @@ -16,14 +18,25 @@ public class FollowQueryDslRepository {
public FollowQueryDslRepository(final JPAQueryFactory jpaQueryFactory) {
this.query = jpaQueryFactory;
}

public FollowableDto existMeAndFollowing(final User me, final User following) {
final Integer count = query.selectOne()
.from(follow)
.where(follow.following.eq(following), follow.follower.eq(me))
return query.select(Projections.constructor(FollowableDto.class, user,
ExpressionUtils.isNotNull(
JPAExpressions.selectOne()
.from(follow)
.where(follow.following.eq(following), follow.follower.eq(me))
)
))
.from(user)
.where(user.eq(me))
.fetchFirst();
boolean followable = !Objects.isNull(count);
return FollowableDto.fromUserAndFollowable(me, followable);

// final Integer count = query.selectOne()
// .from(follow)
// .where(follow.following.eq(following), follow.follower.eq(me))
// .fetchFirst();
// boolean followable = !Objects.isNull(count);
// return FollowableDto.fromUserAndFollowable(me, followable);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ public class Followings {
public Followings() {
}

public Followings(final Set<Follow> followings) {
this.followings = followings;
}

public void add(final Follow following) {
validateArgumentNull(following);
followings.add(following);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public static FollowableDto fromUserAndFollowable(final User me, final boolean f
FollowableDto() {
}

public FollowableDto(final User me, final boolean followable) {
this.username = me.username();
this.bio = me.bio();
this.image = me.image();
this.followable = followable;
}

private FollowableDto(final Name username, final Bio bio, final Image image, final Boolean followable) {
this.username = username;
this.bio = bio;
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/com/study/realworld/http/follow.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
### JOIN
POST localhost:8080/api/users
Content-Type: application/json

{
"user":{
"username": "Jacob",
"email": "jake@jake.jake",
"password": "jakejake"
}
}

### JOIN2
POST localhost:8080/api/users
Content-Type: application/json

{
"user":{
"username": "Jacob2",
"email": "jake2@jake.jake",
"password": "jakejake2"
}
}

### LOGIN
POST localhost:8080/api/users/login
Content-Type: application/json

{
"user":{
"email": "jake@jake.jake",
"password": "jakejake"
}
}

> {%
client.global.set("Authorization", response.body.user.token.accessToken);
client.log("생성된 Authorization : " + client.global.get("Authorization"));
%}

### Followable
GET localhost:8080/test/2
Content-Type: application/json
Authorization: {{Authorization}}

### follow
POST localhost:8080/follow/2
Content-Type: application/json
Authorization: {{Authorization}}

### Followable
GET localhost:8080/test/2
Content-Type: application/json
Authorization: {{Authorization}}

0 comments on commit 3d2d6d5

Please sign in to comment.