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
@@ -0,0 +1,76 @@
package com.mtvs.devlinkbackend.ether.controller;

import com.mtvs.devlinkbackend.config.JwtUtil;
import com.mtvs.devlinkbackend.ether.dto.EtherRegistRequestDTO;
import com.mtvs.devlinkbackend.ether.dto.EtherUpdateRequestDTO;
import com.mtvs.devlinkbackend.ether.entity.Ether;
import com.mtvs.devlinkbackend.ether.service.EtherService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ether")
public class EtherController {
private final EtherService etherService;
private final JwtUtil jwtUtil;

public EtherController(EtherService etherService, JwtUtil jwtUtil) {
this.etherService = etherService;
this.jwtUtil = jwtUtil;
}

// Create
@PostMapping
public ResponseEntity<Ether> registEther(
@RequestBody EtherRegistRequestDTO etherRegistRequestDTO,
@RequestHeader(name = "Authorization") String authorizationHeader) throws Exception {

String accountId = jwtUtil.getSubjectFromTokenWithoutAuth(authorizationHeader);
Ether newEther = etherService.registEther(etherRegistRequestDTO, accountId);
return ResponseEntity.ok(newEther);
}

// Read - Find by Ether ID
@GetMapping("/{etherId}")
public ResponseEntity<Ether> findEtherByEtherId(@PathVariable Long etherId) {
Ether ether = etherService.findEtherByEtherId(etherId);
return ether != null ? ResponseEntity.ok(ether) : ResponseEntity.notFound().build();
}

// Read - Find by Account ID
@GetMapping("/account")
public ResponseEntity<List<Ether>> findEthersByAccountId(
@RequestHeader(name = "Authorization") String authorizationHeader) throws Exception {

String accountId = jwtUtil.getSubjectFromTokenWithoutAuth(authorizationHeader);
List<Ether> ethers = etherService.findEthersByAccountId(accountId);
return ResponseEntity.ok(ethers);
}

// Read - Find by Reason
@GetMapping("/reason/{reason}")
public ResponseEntity<List<Ether>> findEthersByReason(@PathVariable String reason) {
List<Ether> ethers = etherService.findEthersByReason(reason);
return ResponseEntity.ok(ethers);
}

// Update
@PatchMapping
public ResponseEntity<Ether> updateEther(@RequestBody EtherUpdateRequestDTO etherUpdateRequestDTO) {
try {
Ether updatedEther = etherService.updateEther(etherUpdateRequestDTO);
return ResponseEntity.ok(updatedEther);
} catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().body(null);
}
}

// Delete
@DeleteMapping("/{etherId}")
public ResponseEntity<Void> deleteEtherByEtherId(@PathVariable Long etherId) {
etherService.deleteEtherByEtherId(etherId);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.mtvs.devlinkbackend.ether.dto;

import lombok.*;

@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class EtherRegistRequestDTO {
private Long amount;
private String reason;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.mtvs.devlinkbackend.ether.dto;

import lombok.*;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class EtherUpdateRequestDTO {
private Long etherId;
private Long amount;
private String reason;
}
50 changes: 50 additions & 0 deletions src/main/java/com/mtvs/devlinkbackend/ether/entity/Ether.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.mtvs.devlinkbackend.ether.entity;

import jakarta.persistence.*;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

@Table(name = "ETHER")
@Entity(name = "ETHER")
@NoArgsConstructor
public class Ether {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ETHER_ID")
private Long etherId;

@Column(name = "AMOUNT")
private Long amount;

@Column(name = "REASON")
private String reason;

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

@UpdateTimestamp
@Column(name = "MODIFIED_AT")
private LocalDateTime modifiedAt;

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

public Ether(String accountId, String reason, Long amount) {
this.accountId = accountId;
this.reason = reason;
this.amount = amount;
}

public void setAmount(Long amount) {
this.amount = amount;
}

public void setReason(String reason) {
this.reason = reason;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mtvs.devlinkbackend.ether.repository;

import com.mtvs.devlinkbackend.ether.entity.Ether;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface EtherRepository extends JpaRepository<Ether, Long> {
public List<Ether> findEthersByAccountId(String accountId);
public List<Ether> findEthersByReason(String reason);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.mtvs.devlinkbackend.ether.service;

import com.mtvs.devlinkbackend.ether.dto.EtherRegistRequestDTO;
import com.mtvs.devlinkbackend.ether.dto.EtherUpdateRequestDTO;
import com.mtvs.devlinkbackend.ether.entity.Ether;
import com.mtvs.devlinkbackend.ether.repository.EtherRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class EtherService {
private final EtherRepository etherRepository;

public EtherService(EtherRepository etherRepository) {
this.etherRepository = etherRepository;
}

@Transactional
public Ether registEther(EtherRegistRequestDTO etherRegistRequestDTO, String accountId) {
return etherRepository.save(
new Ether(
accountId,
etherRegistRequestDTO.getReason(),
etherRegistRequestDTO.getAmount())
);
}

public Ether findEtherByEtherId(Long etherId) {
return etherRepository.findById(etherId).orElse(null);
}

public List<Ether> findEthersByAccountId(String accountId) {
return etherRepository.findEthersByAccountId(accountId);
}

public List<Ether> findEthersByReason(String reason) {
return etherRepository.findEthersByReason(reason);
}

@Transactional
public Ether updateEther(EtherUpdateRequestDTO etherUpdateRequestDTO) {
Optional<Ether> ether = etherRepository.findById(etherUpdateRequestDTO.getEtherId());
if(ether.isPresent()) {
Ether foundEther = ether.get();
foundEther.setReason(etherUpdateRequestDTO.getReason());
foundEther.setAmount(etherUpdateRequestDTO.getAmount());
return foundEther;
} else
throw new IllegalArgumentException("잘못된 Ether Id로 호출, ETHER_ID : " + etherUpdateRequestDTO.getEtherId());
}

public void deleteEtherByEtherId(Long etherId) {
etherRepository.deleteById(etherId);
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/mtvs/devlinkbackend/oauth2/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import jakarta.persistence.*;
import lombok.Getter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

@Table(name = "USER")
@Entity(name = "USER")
Expand All @@ -11,12 +15,24 @@ public class User {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID")
private Long userId;

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

@Column(name = "EMAIL")
private String email;

@Column(name = "USER_NAME")
private String userName;

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

@UpdateTimestamp
@Column(name = "MODIFIED_AT")
private LocalDateTime modifiedAt;

@Column(name = "REFRESH_TOKEN")
private String refreshToken;
// 추후 추가 정보 필요시 Entity에 Column 추가 예정
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ResponseEntity<List<Question>> getQuestionsByAccountIdWithPaging(
}

// Update a question by ID
@PutMapping("/{id}")
@PatchMapping("/{id}")
public ResponseEntity<Question> updateQuestion(
@RequestBody QuestionUpdateRequestDTO questionUpdateRequestDTO,
@RequestHeader(name = "Authorization") String accessToken) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ResponseEntity<List<Reply>> findRepliesByAccountId(
return ResponseEntity.ok(replies);
}

@PutMapping
@PatchMapping
public ResponseEntity<Reply> updateReply(
@RequestBody ReplyUpdateRequestDTO replyUpdateRequestDTO,
@RequestHeader(name = "Authorization") String token) throws Exception {
Expand Down
95 changes: 95 additions & 0 deletions src/test/java/com/mtvs/devlinkbackend/ether/EtherCRUDTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.mtvs.devlinkbackend.ether;

import com.mtvs.devlinkbackend.ether.dto.EtherRegistRequestDTO;
import com.mtvs.devlinkbackend.ether.dto.EtherUpdateRequestDTO;
import com.mtvs.devlinkbackend.ether.service.EtherService;
import com.mtvs.devlinkbackend.question.dto.QuestionRegistRequestDTO;
import com.mtvs.devlinkbackend.question.dto.QuestionUpdateRequestDTO;
import com.mtvs.devlinkbackend.question.service.QuestionService;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.transaction.annotation.Transactional;

import java.util.stream.Stream;

@SpringBootTest
@Transactional
public class EtherCRUDTest {
@Autowired
private EtherService etherService;

private static Stream<Arguments> newEther() {
return Stream.of(
Arguments.of(new EtherRegistRequestDTO(10L, "이유0"), "계정1"),
Arguments.of(new EtherRegistRequestDTO(100L, "이유00"), "계정2")
);
}

private static Stream<Arguments> modifiedEther() {
return Stream.of(
Arguments.of(new EtherUpdateRequestDTO(1L,200L, "이유0")),
Arguments.of(new EtherUpdateRequestDTO(2L,500L , "이유00"))
);
}

@DisplayName("에테르 이력 추가 테스트")
@ParameterizedTest
@MethodSource("newEther")
@Order(0)
public void testCreateEther(EtherRegistRequestDTO etherRegistRequestDTO, String accountId) {
Assertions.assertDoesNotThrow(() -> etherService.registEther(etherRegistRequestDTO, accountId));
}

@DisplayName("PK로 에테르 이력 조회 테스트")
@ValueSource(longs = {1,2})
@ParameterizedTest
@Order(1)
public void testFindEtherByEtherId(long etherId) {
Assertions.assertDoesNotThrow(() ->
System.out.println("Ether = " + etherService.findEtherByEtherId(etherId)));
}

@DisplayName("계정 ID에 따른 에테르 이력 조회 테스트")
@ValueSource(strings = {"계정1", "계정2"})
@ParameterizedTest
@Order(3)
public void testFindEthersByAccountId(String accountId) {
Assertions.assertDoesNotThrow(() ->
System.out.println("Ether = " + etherService.findEthersByAccountId(accountId)));
}

@DisplayName("지급 이유에 따른 에테르 이력 조회 테스트")
@ValueSource(strings = {"Salary Bonus", "Expense Compensation"})
@ParameterizedTest
@Order(4)
public void testFindEthersByReason(String reason) {
Assertions.assertDoesNotThrow(() ->
System.out.println("Ether = " + etherService.findEthersByReason(reason)));
}

@DisplayName("에테르 이력 수정 테스트")
@MethodSource("modifiedEther")
@ParameterizedTest
@Order(5)
public void testUpdateQuestion(EtherUpdateRequestDTO etherUpdateRequestDTO) {
Assertions.assertDoesNotThrow(() ->
System.out.println(etherService.updateEther(etherUpdateRequestDTO)));
}

@DisplayName("에테르 이력 삭제 테스트")
@ValueSource(longs = {0,1})
@ParameterizedTest
@Order(6)
public void testDeleteEther(long etherId) {
Assertions.assertDoesNotThrow(() ->
etherService.deleteEtherByEtherId(etherId));
}
}