Skip to content
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

Feat/user redis test #20

Merged
merged 6 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion .github/workflows/pr-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ jobs:
permissions:
write-all
#contents: read

steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
Expand All @@ -27,6 +26,14 @@ jobs:
java-version: '17'
distribution: 'temurin'

# Setup Redis
- name: Setup Redis
uses: shogo82148/[email protected]
with:
redis-version: 6.2.0
redis-port: 6379
auto-start: true

# Configure Gradle for optimal use in GiHub Actions, including caching of downloaded dependencies.
# See: https://github.com/gradle/actions/blob/main/setup-gradle/README.md
- name: Grant execute permission for gradlew
Expand All @@ -35,6 +42,20 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@417ae3ccd767c252f5661f1ace9f835f9654f2b5 # v3.1.0

# Wait for Redis to be ready
- name: Wait for Redis to be ready
run: |
for i in `seq 1 15`; do
if [ "$(redis-cli -h localhost ping)" = "PONG" ]; then
echo "Redis is up and running"
exit 0
fi
echo "Waiting for Redis..."
sleep 1
done
echo "Redis did not start in time"
exit 1

- name: Build with Gradle Wrapper
run: ./gradlew build

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import kutaverse.game.map.domain.Status;
import kutaverse.game.map.domain.User;
import kutaverse.game.map.dto.request.PostMapUserRequest;
import kutaverse.game.map.dto.response.GetMapUserResponse;
import kutaverse.game.map.dto.response.PostMapUserResponse;
import kutaverse.game.map.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
Expand All @@ -16,30 +19,55 @@ public class RedisUserController implements UserController{

private final UserService userService;

@PostMapping(produces= MediaType.APPLICATION_JSON_VALUE)
/**
* 맵 유저 저장
* @param postMapUserRequest 맵 유저 정보
* @return Mono<PostMapUserResponse> 저장된 맵 유저 정보
*/
@Override
public Mono<User> addUser(@RequestBody User user) {
return userService.create(user);
}
@PostMapping(produces= MediaType.APPLICATION_JSON_VALUE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이렇게 value 값을 지정하면 어떤 이점이 있나요? 저 형태가 아니면 예외를 발생시키나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

불필요한 부분 같습니다 제거 했습니다.

public Mono<PostMapUserResponse> addUser(@RequestBody PostMapUserRequest postMapUserRequest) {
return userService.create(postMapUserRequest);
}

/**
* 맵 유저 모두 조회
* @return Flux<GetMapUserResponse> Flux<맵 유저 정보></맵>
*/
@GetMapping()
@Override
public Flux<User> getAllUser() {
public Flux<GetMapUserResponse> getAllUser() {
return userService.findAll();
}

/**
* 맵 유저 조회
* @param userId 유저 Id
* @return Mono<GetMapUserResponse> 맵 유저 정보
*/
@GetMapping("{userId}")
@Override
public Mono<User> getUser(@PathVariable(value = "userId") String userId) {
public Mono<GetMapUserResponse> getUser(@PathVariable(value = "userId") String userId) {
return userService.findOne(userId);
}

/**
* 맵 유저 삭제
* @param userId 유저 Id
* @return Mono<Long> 삭제된 유저 Id
*/
@DeleteMapping("{userId}")
public Mono<Long> deleteUser(@PathVariable(value = "userId") String userId) {
return userService.deleteById(userId);
}


/**
* TODO 구현안됨
* 맵 유저 상태 변경
* @param userId 유저 Id
* @param status 변경할 상태
* @return 변경된 유저 정보
*/
@PostMapping("/state/{userId}")
public Mono<User> changeState(@PathVariable(value = "userId") String userId, @RequestParam("state")Status status) { return userService.changeState(userId,status); }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
package kutaverse.game.map.controller;

import kutaverse.game.map.domain.User;
import kutaverse.game.map.dto.request.PostMapUserRequest;
import kutaverse.game.map.dto.response.GetMapUserResponse;
import kutaverse.game.map.dto.response.PostMapUserResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public interface UserController {


Mono<User> addUser(User user);
Mono<PostMapUserResponse> addUser(PostMapUserRequest postMapUserRequest);


Flux<User> getAllUser();
Flux<GetMapUserResponse> getAllUser();


Mono<User> getUser(String id);
Mono<GetMapUserResponse> getUser(String id);


Mono<Long> deleteUser(String userId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kutaverse.game.map.dto;
package kutaverse.game.map.domain;

public enum MapRequestType {
SAVE,DELETE
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/kutaverse/game/map/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.TimeToLive;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Objects;

@Getter
@NoArgsConstructor
Expand All @@ -39,6 +42,7 @@ public class User {
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape= JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss")
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime localDateTime;

@Builder
Expand All @@ -51,7 +55,7 @@ public User(String userId, Double positionX, Double positionY, Double positionZ,
this.rotationYaw = Math.round(rotationYaw*1000.0)/1000.0;
this.rotationRoll = Math.round(rotationRoll*1000.0)/1000.0;
this.status = status;
this.localDateTime=LocalDateTime.now();
this.localDateTime = LocalDateTime.parse(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}

public void updateTime() {
Expand All @@ -61,4 +65,17 @@ public void updateTime() {
public void setStatus(Status status){
this.status=status;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(userId, user.userId) && Objects.equals(positionX, user.positionX) && Objects.equals(positionY, user.positionY) && Objects.equals(positionZ, user.positionZ) && Objects.equals(rotationPitch, user.rotationPitch) && Objects.equals(rotationYaw, user.rotationYaw) && Objects.equals(rotationRoll, user.rotationRoll) && status == user.status && Objects.equals(localDateTime, user.localDateTime);
}

@Override
public int hashCode() {
return Objects.hash(userId, positionX, positionY, positionZ, rotationPitch, rotationYaw, rotationRoll, status, localDateTime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package kutaverse.game.map.dto.request;

import kutaverse.game.map.domain.Status;
import kutaverse.game.map.domain.User;
import lombok.*;

@NoArgsConstructor
@Getter
@Setter
@AllArgsConstructor
@Builder
public class PostMapUserRequest {

private String userId;
private Double positionX;
private Double positionY;
private Double positionZ;
private Double rotationPitch;
private Double rotationYaw;
private Double rotationRoll;
private Status status;

/**
* service layer에서 엔티티 변환을 위해 필요합니다.
* @return user
*/
public User toEntity(){
return User.builder()
.userId(userId)
.positionX(positionX)
.positionY(positionY)
.positionZ(positionZ)
.rotationPitch(rotationPitch)
.rotationYaw(rotationYaw)
.rotationRoll(rotationRoll)
.status(status)
.build();
}

/**
* 테스트에서 필요합니다.
* @param user
* @return PostMapUserRequest
*/
public static PostMapUserRequest toEntity(User user){
return PostMapUserRequest.builder()
.userId(user.getUserId())
.positionX(user.getPositionX())
.positionY(user.getPositionY())
.positionZ(user.getPositionZ())
.rotationPitch(user.getRotationPitch())
.rotationRoll(user.getRotationRoll())
.rotationYaw(user.getRotationYaw())
.status(user.getStatus())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package kutaverse.game.map.dto.response;

import kutaverse.game.map.domain.Status;
import kutaverse.game.map.domain.User;
import lombok.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
@EqualsAndHashCode
public class GetMapUserResponse {

private String userId;
private Double positionX;
private Double positionY;
private Double positionZ;
private Double rotationPitch;
private Double rotationYaw;
private Double rotationRoll;
private Status status;

public static GetMapUserResponse toDto(User user){
return builder()
.userId(user.getUserId())
.positionX(user.getPositionX())
.positionY(user.getPositionY())
.positionZ(user.getPositionZ())
.rotationPitch(user.getRotationPitch())
.rotationRoll(user.getRotationRoll())
.rotationYaw(user.getRotationYaw())
.status(user.getStatus())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package kutaverse.game.map.dto.response;

import kutaverse.game.map.domain.Status;
import kutaverse.game.map.domain.User;
import kutaverse.game.map.dto.request.PostMapUserRequest;
import lombok.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ToString
public class PostMapUserResponse {

private String userId;
private Double positionX;
private Double positionY;
private Double positionZ;
private Double rotationPitch;
private Double rotationYaw;
private Double rotationRoll;
private Status status;

public static PostMapUserResponse toDto(User user){
return builder()
.userId(user.getUserId())
.positionX(user.getPositionX())
.positionY(user.getPositionY())
.positionZ(user.getPositionZ())
.rotationPitch(user.getRotationPitch())
.rotationRoll(user.getRotationRoll())
.rotationYaw(user.getRotationYaw())
.status(user.getStatus())
.build();
}
}
Loading
Loading