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

1. 회원가입 요구사항 Update #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.maximus.spring_server.controller;

import com.maximus.spring_server.domain.Account;
Copy link
Member

Choose a reason for hiding this comment

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

표현층에 도메인층 의존성이 노출되면 안됩니다. 변경에 취약한 구조입니다.

import com.maximus.spring_server.service.AccountService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
public class AccountController {

private final AccountService accountService;

@RequestMapping(value = "/account/signup", method = RequestMethod.POST)
public ResponseEntity<?> signUp(@RequestBody Account account) throws Exception {
Copy link
Member

Choose a reason for hiding this comment

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

throws 필요한가요?

try {
return new ResponseEntity<>(accountService.saveUser(account), HttpStatus.CREATED);
}catch (Exception e){
return new ResponseEntity<>(accountService.saveUser(account),HttpStatus.BAD_REQUEST);
}


}


}
45 changes: 45 additions & 0 deletions src/main/java/com/maximus/spring_server/domain/Account.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.maximus.spring_server.domain;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Getter
@Table(name = "account")
@NoArgsConstructor
public class Account {

public Account(String username, String password) {
this.username = username;
this.password = password;
}

@Id
Copy link
Member

Choose a reason for hiding this comment

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

?? 여기에 있어야할 이유를 모르겠네요

@Column(name = "username", nullable = false)
private String username;

@Column(name = "password", nullable = false)
private String password;

public void validationUser() {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public void validationUser() {
public void validateUser() {

서술형으로 작성 잊지맙시다

checkUser(username);
checkPassword(password);
}


private void checkPassword(String password) {
if (password.isEmpty()) throw new IllegalArgumentException("패스워드를 입력해주세요");
}

private void checkUser(String username) {
if (username.isEmpty()) throw new IllegalArgumentException("닉네임을 입력해주세요");

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.maximus.spring_server.repository;

import com.maximus.spring_server.domain.Account;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface AccountRepository extends JpaRepository<Account,String>{

Account findByUsername(String username);


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.maximus.spring_server.service;

import com.maximus.spring_server.domain.Account;
import com.maximus.spring_server.repository.AccountRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class AccountService {

private final AccountRepository accountRepository;

@Transactional
public Account saveUser(Account account) throws Exception {
if (accountRepository.findByUsername(account.getUsername()) != null) {
Copy link
Member

Choose a reason for hiding this comment

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

accountRepository.existsByUsername 메소드를 추가하는건 어떨까요?

throw new Exception();

}
account.validationUser();
return accountRepository.save(account);

}
}
6 changes: 5 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@

spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=1234
spring.h2.console.enabled=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.maximus.spring_server.service;

import com.maximus.spring_server.domain.Account;
import com.maximus.spring_server.repository.AccountRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;

@ExtendWith(MockitoExtension.class)
class AccountServiceTest {

@Mock
private AccountRepository accountRepository;

@InjectMocks
private AccountService accountService;


@AfterEach
public void tearDown() throws Exception {
accountRepository.deleteAll();
}

@Test
public void 저장작동테스트() {
String username = "1234";
String password = "me2";

Account account = new Account(username, password);
//given
given(accountService.saveUser(account)).willReturn(account);
//when

Account account2 = accountService.saveUser(account);
assertThat(account.getUsername()).isEqualTo(username);
assertThat(account.getPassword()).isEqualTo(password);

assertThat(account2.getUsername()).isEqualTo(account.getUsername());
assertThat(account2.getPassword()).isEqualTo(account.getPassword());

assertThat(account2.getUsername()).isNotEmpty();
assertThat(account2.getPassword()).isNotEmpty();
Copy link
Member

Choose a reason for hiding this comment

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

반환된 객체는 어차피 given(..).willReturn(...) 로 직접 작성하신 구조인데, 테스트할 의미가 있나요?

그냥 accountRepository.save(..) 가 잘 동작했는지만 판단하면 될것 같습니다.


}
}