-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: master
Are you sure you want to change the base?
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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; | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
} |
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
서술형으로 작성 잊지맙시다 |
||||||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. accountRepository.existsByUsername 메소드를 추가하는건 어떨까요? |
||
throw new Exception(); | ||
|
||
} | ||
account.validationUser(); | ||
return accountRepository.save(account); | ||
|
||
} | ||
} |
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 반환된 객체는 어차피 그냥 |
||
|
||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
표현층에 도메인층 의존성이 노출되면 안됩니다. 변경에 취약한 구조입니다.