Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mtvs.devlinkbackend.character.dto.response.UserCharacterSingleResponseDTO;
import com.mtvs.devlinkbackend.character.dto.request.UserCharacterUpdateRequestDTO;
import com.mtvs.devlinkbackend.character.service.UserCharacterService;
import com.mtvs.devlinkbackend.character.service.UserCharacterViewService;
import com.mtvs.devlinkbackend.util.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand All @@ -13,13 +14,15 @@

@RestController
@RequestMapping("/api/character")
public class UserCharacterController {
public class UserCharacterCommandController {
private final UserCharacterService userCharacterService;
private final JwtUtil jwtUtil;
private final UserCharacterViewService userCharacterViewService;

public UserCharacterController(UserCharacterService userCharacterService, JwtUtil jwtUtil) {
public UserCharacterCommandController(UserCharacterService userCharacterService, JwtUtil jwtUtil, UserCharacterViewService userCharacterViewService) {
this.userCharacterService = userCharacterService;
this.jwtUtil = jwtUtil;
this.userCharacterViewService = userCharacterViewService;
}

@Operation(summary = "캐릭터 등록", description = "새로운 캐릭터를 등록합니다.")
Expand All @@ -35,32 +38,15 @@ public ResponseEntity<UserCharacterSingleResponseDTO> registerCharacter(
return new ResponseEntity<>(userCharacter, HttpStatus.CREATED);
}

@Operation(summary = "캐릭터 조회", description = "계정 ID로 캐릭터를 조회합니다.")
@ApiResponse(responseCode = "200", description = "캐릭터가 성공적으로 조회되었습니다.")
@ApiResponse(responseCode = "404", description = "해당 계정 ID로 캐릭터를 찾을 수 없습니다.")
@GetMapping
public ResponseEntity<UserCharacterSingleResponseDTO> getCharacter(
@RequestHeader(name = "Authorization") String authorizationHeader) throws Exception {

String accountId = jwtUtil.getSubjectFromAuthHeaderWithoutAuth(authorizationHeader);
UserCharacterSingleResponseDTO userCharacter = userCharacterService.findCharacterByAccountId(accountId);
if (userCharacter == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(userCharacter, HttpStatus.OK);
}

@Operation(summary = "캐릭터 수정", description = "계정 ID로 캐릭터를 수정합니다.")
@ApiResponse(responseCode = "200", description = "캐릭터가 성공적으로 수정되었습니다.")
@ApiResponse(responseCode = "404", description = "해당 계정 ID로 캐릭터를 찾을 수 없습니다.")
@PatchMapping
public ResponseEntity<UserCharacterSingleResponseDTO> updateCharacter(
@RequestBody UserCharacterUpdateRequestDTO userCharacterUpdateRequestDTO,
@RequestHeader(name = "Authorization") String authorizationHeader) throws Exception {
@RequestBody UserCharacterUpdateRequestDTO userCharacterUpdateRequestDTO) throws Exception {

String accountId = jwtUtil.getSubjectFromAuthHeaderWithoutAuth(authorizationHeader);
try {
UserCharacterSingleResponseDTO updatedCharacter = userCharacterService.updateCharacter(userCharacterUpdateRequestDTO, accountId);
UserCharacterSingleResponseDTO updatedCharacter = userCharacterService.updateCharacter(userCharacterUpdateRequestDTO);
return new ResponseEntity<>(updatedCharacter, HttpStatus.OK);
} catch (IllegalArgumentException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.mtvs.devlinkbackend.character.controller;

import com.mtvs.devlinkbackend.character.dto.response.UserCharacterListResponseDTO;
import com.mtvs.devlinkbackend.character.dto.response.UserCharacterSingleResponseDTO;
import com.mtvs.devlinkbackend.character.service.UserCharacterViewService;
import com.mtvs.devlinkbackend.util.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/character")
public class UserCharacterQueryController {
private final JwtUtil jwtUtil;
private final UserCharacterViewService userCharacterViewService;

public UserCharacterQueryController(JwtUtil jwtUtil, UserCharacterViewService userCharacterViewService) {
this.jwtUtil = jwtUtil;
this.userCharacterViewService = userCharacterViewService;
}

@Operation(summary = "캐릭터 조회", description = "캐릭터 ID로 캐릭터를 조회합니다.")
@ApiResponse(responseCode = "200", description = "캐릭터가 성공적으로 조회되었습니다.")
@ApiResponse(responseCode = "404", description = "해당 계정 ID로 캐릭터를 찾을 수 없습니다.")
@GetMapping("/{characterId}")
public ResponseEntity<UserCharacterSingleResponseDTO> getCharacterByCharacterId(
@PathVariable(name = "characterId") Long characterId) throws Exception {

UserCharacterSingleResponseDTO userCharacter = userCharacterViewService.findCharacterByCharacterId(characterId);
if (userCharacter == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(userCharacter, HttpStatus.OK);
}

@Operation(summary = "측정 캐릭터 userId로 조회", description = "유저 ID로 캐릭터를 조회합니다.")
@ApiResponse(responseCode = "200", description = "캐릭터가 성공적으로 조회되었습니다.")
@ApiResponse(responseCode = "404", description = "해당 계정 ID로 캐릭터를 찾을 수 없습니다.")
@GetMapping("/user/{userId}")
public ResponseEntity<UserCharacterSingleResponseDTO> getCharacterByUserId(
@PathVariable(name = "userId") Long userId) throws Exception {

UserCharacterSingleResponseDTO userCharacter = userCharacterViewService.findCharacterByUserId(userId);
if (userCharacter == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(userCharacter, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mtvs.devlinkbackend.character.dto.request;

import com.mtvs.devlinkbackend.character.entity.CustomInfo;
import lombok.*;

import java.util.List;
Expand All @@ -9,5 +10,9 @@
@NoArgsConstructor
@ToString
public class UserCharacterRegistRequestDTO {
private List<Integer> status;
private Long userId;
private Long guildId;
private List<Long> teamIdList;
private List<CustomInfo> customList;
private String characterPicture;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mtvs.devlinkbackend.character.dto.request;

import com.mtvs.devlinkbackend.character.entity.CustomInfo;
import lombok.*;

import java.util.List;
Expand All @@ -9,6 +10,9 @@
@NoArgsConstructor
@ToString
public class UserCharacterUpdateRequestDTO {
private Long characterId;
private List<Integer> status;
private Long userId;
private Long guildId;
private List<Long> teamIdList;
private List<CustomInfo> customList;
private String characterPicture;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mtvs.devlinkbackend.character.dto.response;

import com.mtvs.devlinkbackend.character.entity.UserCharacter;
import lombok.*;

import java.util.List;

@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class UserCharacterListResponseDTO {
private List<UserCharacter> data;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.mtvs.devlinkbackend.character.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
public class CustomInfo {
@Column(name = "CUSTOM_TYPE")
private Integer customType;

@Column(name = "CUSTOM_NAME")
private String customName;

@Column(name = "CUSTOM_TEXTURE")
private String customTexture;

@Column(name = "CUSTOM_INDEX")
private Integer customIndex;
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package com.mtvs.devlinkbackend.character.entity;

import com.mtvs.devlinkbackend.util.IntegerListConverter;
import com.mtvs.devlinkbackend.util.LongListConverter;
import com.mtvs.devlinkbackend.util.converter.LongListConverter;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;
import java.util.List;

@Table(name = "USER_CHARACTER")
@Entity(name = "UserCharacter")
@Getter
@Setter
public class UserCharacter {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CHARACTER_ID")
private Long characterId;

@Column(name = "ACCOUNT_ID", unique = true)
private String accountId;

@Column(name = "GUILD_ID")
private Long guildId;

Expand All @@ -30,26 +31,28 @@ public class UserCharacter {
private String characterPicture;

@ElementCollection
@CollectionTable(name = "STATUS_LIST", joinColumns = @JoinColumn(name = "CHARACTER_ID"))
@Column(name = "STATUS")
private List<Integer> status;
@CollectionTable(name = "CUSTOM_LIST", joinColumns = @JoinColumn(name = "CHARACTER_ID"))
private List<CustomInfo> customList;

@Column(name = "USER_ID")
private Long userId;

public UserCharacter() {
}
@CreationTimestamp
@Column(name = "CREATED_AT", updatable = false)
private LocalDateTime createdAt;

public UserCharacter(String accountId, List<Integer> status) {
this.accountId = accountId;
this.status = status;
}
@UpdateTimestamp
@Column(name = "MODIFIED_AT")
private LocalDateTime modifiedAt;

public void setAccountId(String accountId) {
this.accountId = accountId;
public UserCharacter() {
}

public void setStatus(List<Integer> status) {
this.status = status;
public UserCharacter(Long guildId, List<Long> teamIdList, String characterPicture, List<CustomInfo> customList, Long userId) {
this.guildId = guildId;
this.teamIdList = teamIdList;
this.characterPicture = characterPicture;
this.customList = customList;
this.userId = userId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@

@Repository
public interface UserCharacterRepository extends JpaRepository<UserCharacter, Long> {
UserCharacter findByAccountId(String accountId);
void deleteByAccountId(String accountId);
void deleteByUserId(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mtvs.devlinkbackend.character.repository;

import com.mtvs.devlinkbackend.character.entity.UserCharacter;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserCharacterViewRepository extends JpaRepository<UserCharacter, Long> {
UserCharacter findByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,41 +5,52 @@
import com.mtvs.devlinkbackend.character.dto.request.UserCharacterUpdateRequestDTO;
import com.mtvs.devlinkbackend.character.entity.UserCharacter;
import com.mtvs.devlinkbackend.character.repository.UserCharacterRepository;
import com.mtvs.devlinkbackend.character.repository.UserCharacterViewRepository;
import com.mtvs.devlinkbackend.user.command.model.entity.User;
import com.mtvs.devlinkbackend.user.query.repository.UserViewRepository;
import jakarta.transaction.Transactional;
import org.springframework.stereotype.Service;

@Service
public class UserCharacterService {
private final UserCharacterRepository userCharacterRepository;
private final UserViewRepository userViewRepository;
private final UserCharacterViewRepository userCharacterViewRepository;

public UserCharacterService(UserCharacterRepository userCharacterRepository) {
public UserCharacterService(UserCharacterRepository userCharacterRepository, UserViewRepository userViewRepository, UserCharacterViewRepository userCharacterViewRepository) {
this.userCharacterRepository = userCharacterRepository;
this.userViewRepository = userViewRepository;
this.userCharacterViewRepository = userCharacterViewRepository;
}

@Transactional
public UserCharacterSingleResponseDTO registCharacter(UserCharacterRegistRequestDTO userCharacterRegistRequestDTO, String accountId) {
return new UserCharacterSingleResponseDTO(userCharacterRepository.save(new UserCharacter(
accountId,
userCharacterRegistRequestDTO.getStatus()
userCharacterRegistRequestDTO.getGuildId(),
userCharacterRegistRequestDTO.getTeamIdList(),
userCharacterRegistRequestDTO.getCharacterPicture(),
userCharacterRegistRequestDTO.getCustomList(),
userCharacterRegistRequestDTO.getUserId()
)));
}

public UserCharacterSingleResponseDTO findCharacterByAccountId(String accountId) {
return new UserCharacterSingleResponseDTO(userCharacterRepository.findByAccountId(accountId));
}

@Transactional
public UserCharacterSingleResponseDTO updateCharacter(UserCharacterUpdateRequestDTO userCharacterUpdateRequestDTO, String accountId) {
UserCharacter userCharacter = userCharacterRepository.findByAccountId(accountId);
public UserCharacterSingleResponseDTO updateCharacter(UserCharacterUpdateRequestDTO userCharacterUpdateRequestDTO) {
UserCharacter userCharacter = userCharacterViewRepository.findByUserId(userCharacterUpdateRequestDTO.getUserId());
if(userCharacter == null)
throw new IllegalArgumentException("잘못된 계정으로 캐릭터 수정 시도");

userCharacter.setStatus(userCharacterUpdateRequestDTO.getStatus());
userCharacter.setGuildId(userCharacterUpdateRequestDTO.getGuildId());
userCharacter.setCharacterPicture(userCharacter.getCharacterPicture());
userCharacter.setCustomList(userCharacterUpdateRequestDTO.getCustomList());
userCharacter.setTeamIdList(userCharacterUpdateRequestDTO.getTeamIdList());

return new UserCharacterSingleResponseDTO(userCharacter);
}

@Transactional
public void deleteCharacterByAccountId(String accountId) {
userCharacterRepository.deleteByAccountId(accountId);
User foundUser = userViewRepository.findUserByEpicAccountId(accountId);
userCharacterRepository.deleteByUserId(foundUser.getUserId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.mtvs.devlinkbackend.character.service;

import com.mtvs.devlinkbackend.character.dto.response.UserCharacterSingleResponseDTO;
import com.mtvs.devlinkbackend.character.repository.UserCharacterRepository;
import com.mtvs.devlinkbackend.character.repository.UserCharacterViewRepository;
import com.mtvs.devlinkbackend.user.command.model.entity.User;
import com.mtvs.devlinkbackend.user.query.repository.UserViewRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserCharacterViewService {
private final UserViewRepository userViewRepository;
private final UserCharacterViewRepository userCharacterViewRepository;

public UserCharacterViewService(UserViewRepository userViewRepository, UserCharacterViewRepository userCharacterViewRepository, UserCharacterRepository userCharacterRepository) {
this.userViewRepository = userViewRepository;
this.userCharacterViewRepository = userCharacterViewRepository;
}

public UserCharacterSingleResponseDTO findCharacterByAccountId(String accountId) {
User foundUser = userViewRepository.findUserByEpicAccountId(accountId);
return new UserCharacterSingleResponseDTO(userCharacterViewRepository.findByUserId(foundUser.getUserId()));
}

public UserCharacterSingleResponseDTO findCharacterByUserId(Long userId) {
return new UserCharacterSingleResponseDTO(userCharacterViewRepository.findByUserId(userId));
}

public UserCharacterSingleResponseDTO findCharacterByCharacterId(Long characterId) {
return new UserCharacterSingleResponseDTO(userCharacterViewRepository.findById(characterId).orElse(null));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mtvs.devlinkbackend.user.command.model.entity;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.mtvs.devlinkbackend.util.StringListConverter;
import com.mtvs.devlinkbackend.util.converter.StringListConverter;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down
Loading