Skip to content

Commit

Permalink
feat: user api 호출 시 유저 생성하도록 로직 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
yunuo46 committed May 10, 2024
1 parent 7d7c09f commit 95662d3
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package getaguitar.site.demo.Controller;
import com.fasterxml.jackson.core.JsonProcessingException;
import getaguitar.site.demo.Dto.ResUserNameDto;
import getaguitar.site.demo.Dto.ResUserDto;
import getaguitar.site.demo.Service.APIService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
Expand All @@ -17,7 +17,7 @@ public class APIController {

@GetMapping("/user")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public ResUserNameDto getUser() throws JsonProcessingException {
public ResUserDto getUser() throws JsonProcessingException {
return apiService.getUser();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import getaguitar.site.demo.Dto.StopUser.ResStopUserDto;
import getaguitar.site.demo.Dto.StopUser.ReqStopUserDto;
import getaguitar.site.demo.Dto.NewUser.ReqNewUserDto;
import getaguitar.site.demo.Dto.NewUser.ResNewUserDto;
import getaguitar.site.demo.Entity.UserEntity;
import getaguitar.site.demo.Service.MapService;

Expand All @@ -31,7 +30,7 @@ public MoveUserDto moveUser(MoveUserDto position) {

@MessageMapping("/new")
@SendTo("/topic/map/new")
public ResNewUserDto newUser(@Payload ReqNewUserDto newUser) {
public UserEntity newUser(@Payload ReqNewUserDto newUser) {
return mapService.createUser(newUser);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
@Getter
@Setter
public class ResNewUserDto {
private int id;
private String username;
private int x;
private int y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@NoArgsConstructor
@Getter
@Setter
public class ResUserNameDto {
public class ResUserDto {
private int id;
private String username;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@

@Repository
public interface UserRepository extends JpaRepository<UserEntity, Integer> {
UserEntity findByUsername(String username);
}
4 changes: 2 additions & 2 deletions src/main/java/getaguitar/site/demo/Service/APIService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package getaguitar.site.demo.Service;

import getaguitar.site.demo.Dto.ResUserNameDto;
import getaguitar.site.demo.Dto.ResUserDto;

public interface APIService {
ResUserNameDto getUser();
ResUserDto getUser();
}
19 changes: 16 additions & 3 deletions src/main/java/getaguitar/site/demo/Service/APIServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
package getaguitar.site.demo.Service;
import getaguitar.site.demo.Dto.ResUserNameDto;
import getaguitar.site.demo.Dto.ResUserDto;
import getaguitar.site.demo.Entity.UserEntity;
import getaguitar.site.demo.Repository.UserRepository;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.RandomStringUtils;

@Service
public class APIServiceImpl implements APIService{
private final UserRepository userRepository;
public APIServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}

@Override
public ResUserNameDto getUser(){
public ResUserDto getUser(){
String username = RandomStringUtils.randomAlphanumeric(5);
return new ResUserNameDto(username);
int id = userRepository.save(UserEntity.builder()
.username(username)
.x(400)
.y(300)
.direction("down")
.build()).getId();
return new ResUserDto(id, username);
}
}
2 changes: 1 addition & 1 deletion src/main/java/getaguitar/site/demo/Service/MapService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.List;

public interface MapService {
ResNewUserDto createUser(ReqNewUserDto newUser);
UserEntity createUser(ReqNewUserDto newUser);
ResStopUserDto stopUser(ReqStopUserDto stopUser);
MoveUserDto moveUser(MoveUserDto moveUser);
List<UserEntity> getAllUser();
Expand Down
13 changes: 3 additions & 10 deletions src/main/java/getaguitar/site/demo/Service/MapServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,9 @@ public MapServiceImpl(UserRepository userRepository) {
}

@Override
public ResNewUserDto createUser(ReqNewUserDto newUser){
String username = newUser.getUsername();
int id = userRepository.save(UserEntity.builder()
.username(username)
.x(400)
.y(300)
.direction("down")
.build()).getId();
return new ResNewUserDto(id, username,400,300,"down");
public UserEntity createUser(ReqNewUserDto newUser){
int id = newUser.getId();
return userRepository.findById(id).orElseThrow();
}

@Override
Expand All @@ -43,7 +37,6 @@ public List<UserEntity> getAllUser(){
public MoveUserDto moveUser(MoveUserDto userInfo) {
System.out.println("move");
int id = userInfo.getId();
System.out.println(id);
UserEntity user = userRepository.findById(id).orElseThrow();

String username = userInfo.getUsername();
Expand Down
6 changes: 3 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ spring.jpa.show-sql=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# LocalTest
spring.datasource.url=jdbc:mysql://localhost:3306/getaguitar?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=8320
# spring.datasource.url=jdbc:mysql://localhost:3306/getaguitar?useSSL=false&allowPublicKeyRetrieval=true
# spring.datasource.username=root
# spring.datasource.password=8320

0 comments on commit 95662d3

Please sign in to comment.