-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from DDD-3/develop
master <= develop
- Loading branch information
Showing
35 changed files
with
945 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.ddd.airplane.accounts; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnore; | ||
import lombok.*; | ||
|
||
import java.io.Serializable; | ||
import java.util.Set; | ||
|
||
@Getter @Setter @EqualsAndHashCode(of = "email") | ||
@Builder @NoArgsConstructor @AllArgsConstructor | ||
class Account implements Serializable { | ||
private String email; | ||
@JsonIgnore | ||
private String password; | ||
private String nickname; | ||
@JsonIgnore | ||
private Set<AccountRole> roles; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/ddd/airplane/accounts/AccountAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.ddd.airplane.accounts; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.userdetails.User; | ||
|
||
import java.util.Collection; | ||
import java.util.stream.Collectors; | ||
|
||
class AccountAdapter extends User { | ||
private Account account; | ||
|
||
AccountAdapter(Account account) { | ||
super(account.getEmail(), account.getPassword(), authorities(account.getRoles())); | ||
this.account = account; | ||
} | ||
|
||
private static Collection<? extends GrantedAuthority> authorities(Collection<AccountRole> roles) { | ||
return roles.stream().map(AccountRole::toAuthority).collect(Collectors.toList()); | ||
} | ||
|
||
public Account getAccount() { | ||
return account; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/ddd/airplane/accounts/AccountAlreadyRegisteredException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.ddd.airplane.accounts; | ||
|
||
import com.ddd.airplane.common.AlreadyRegisteredException; | ||
|
||
class AccountAlreadyRegisteredException extends AlreadyRegisteredException { | ||
AccountAlreadyRegisteredException(String message) { | ||
super(message); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/ddd/airplane/accounts/AccountApiController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.ddd.airplane.accounts; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.validation.Valid; | ||
|
||
@AllArgsConstructor | ||
@RestController | ||
@RequestMapping("/api") | ||
public class AccountApiController { | ||
private final AccountService accountService; | ||
|
||
@PostMapping("/v1/accounts") | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public Account createAccount(@RequestBody @Valid AccountDto accountDto) { | ||
return accountService.createAccount(accountDto); | ||
} | ||
|
||
@GetMapping("/v1/accounts/{email}") | ||
@ResponseStatus(HttpStatus.OK) | ||
public Account getAccount( | ||
@PathVariable String email, | ||
@CurrentAccount Account account | ||
) { | ||
return accountService.getAccount(email); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.ddd.airplane.accounts; | ||
|
||
import lombok.Builder; | ||
import lombok.Data; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
|
||
@Data @Builder | ||
public class AccountDto { | ||
@NotBlank | ||
private String email; | ||
@NotBlank | ||
private String password; | ||
private String nickname; | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/com/ddd/airplane/accounts/AccountRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.ddd.airplane.accounts; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.springframework.dao.EmptyResultDataAccessException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Set; | ||
|
||
@AllArgsConstructor | ||
@Repository | ||
public class AccountRepository { | ||
private final JdbcTemplate jdbcTemplate; | ||
|
||
Account findByEmail(String email) { | ||
try { | ||
return jdbcTemplate.queryForObject( | ||
AccountSql.FIND_BY_EMAIL, | ||
new Object[]{email}, | ||
(rs, rowNum) -> Account.builder() | ||
.email(rs.getString("email")) | ||
.password(rs.getString("password")) | ||
.nickname(rs.getString("nickname")) | ||
.roles(Set.of(AccountRole.ROLE_USER)) | ||
.build() | ||
); | ||
} catch (EmptyResultDataAccessException e) { | ||
return null; | ||
} | ||
} | ||
|
||
Account save(Account account) { | ||
jdbcTemplate.update( | ||
AccountSql.SAVE, | ||
account.getEmail(), | ||
account.getPassword(), | ||
account.getNickname() | ||
); | ||
|
||
return Account.builder() | ||
.email(account.getEmail()) | ||
.nickname(account.getNickname()) | ||
.build(); | ||
} | ||
|
||
void truncate() { | ||
jdbcTemplate.update( | ||
AccountSql.TRUNCATE | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.ddd.airplane.accounts; | ||
|
||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
|
||
public enum AccountRole { | ||
ROLE_USER; | ||
|
||
public GrantedAuthority toAuthority() { | ||
return new SimpleGrantedAuthority(name()); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/com/ddd/airplane/accounts/AccountService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.ddd.airplane.accounts; | ||
|
||
import lombok.AllArgsConstructor; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
|
||
@AllArgsConstructor | ||
@Service | ||
public class AccountService implements UserDetailsService { | ||
private final AccountRepository accountRepository; | ||
private final ModelMapper modelMapper; | ||
private final PasswordEncoder passwordEncoder; | ||
|
||
public Account createAccount(AccountDto accountDto) { | ||
String email = accountDto.getEmail(); | ||
|
||
if (accountRepository.findByEmail(email) != null) { | ||
throw new AccountAlreadyRegisteredException(email); | ||
} | ||
|
||
Account account = modelMapper.map(accountDto, Account.class); | ||
account.setPassword(passwordEncoder.encode(account.getPassword())); | ||
|
||
return accountRepository.save(account); | ||
} | ||
|
||
Account getAccount(String email) { | ||
Account account = accountRepository.findByEmail(email); | ||
if (account == null) { | ||
account = new Account(); | ||
} | ||
|
||
return account; | ||
} | ||
|
||
public void deleteAll() { | ||
accountRepository.truncate(); | ||
} | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String username) { | ||
Account account = accountRepository.findByEmail(username); | ||
if (account == null) { | ||
throw new UsernameNotFoundException(username); | ||
} | ||
|
||
return new AccountAdapter(account); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.ddd.airplane.accounts; | ||
|
||
class AccountSql { | ||
static final String FIND_BY_EMAIL = "SELECT email, password, nickname FROM accounts WHERE email = ?"; | ||
static final String SAVE = "INSERT INTO accounts (email, password, nickname) VALUES (?, ?, ?)"; | ||
static final String TRUNCATE = "TRUNCATE TABLE accounts"; | ||
} | ||
|
15 changes: 15 additions & 0 deletions
15
src/main/java/com/ddd/airplane/accounts/CurrentAccount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.ddd.airplane.accounts; | ||
|
||
|
||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@AuthenticationPrincipal(expression = "#this == 'anonymousUser' ? null : account") | ||
@interface CurrentAccount { | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/ddd/airplane/common/AlreadyRegisteredException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.ddd.airplane.common; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
|
||
@ResponseStatus(HttpStatus.CONFLICT) | ||
public class AlreadyRegisteredException extends RuntimeException { | ||
public AlreadyRegisteredException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.ddd.airplane.common; | ||
|
||
import lombok.Getter; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Getter | ||
public class AppProperties { | ||
@Value("${client.id}") private String clientId; | ||
@Value("${client.secret}") private String clientSecret; | ||
} |
Oops, something went wrong.