Skip to content
This repository has been archived by the owner on Aug 13, 2022. It is now read-only.

[#21] Item domain 개발 #27

Open
wants to merge 20 commits into
base: feature/23
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#### 이벤트
- 선착순 이벤트
- 핫딜
#### CI/CD


## 기술적인 문제 해결 과정
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
33 changes: 16 additions & 17 deletions src/main/java/me/jjeda/mall/accounts/Service/AccountService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package me.jjeda.mall.accounts.Service;

import lombok.RequiredArgsConstructor;
import me.jjeda.mall.accounts.domain.Account;
import me.jjeda.mall.accounts.domain.AccountAdapter;
import me.jjeda.mall.accounts.domain.AccountAndDtoAdapter;
import me.jjeda.mall.accounts.domain.AccountAndUserAdapter;
import me.jjeda.mall.accounts.domain.AccountStatus;
import me.jjeda.mall.accounts.dto.AccountDto;
import me.jjeda.mall.accounts.repository.AccountRepository;
Expand All @@ -15,25 +17,21 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;
import javax.persistence.EntityNotFoundException;

@Service
@RequiredArgsConstructor
public class AccountService implements UserDetailsService {

private AccountRepository accountRepository;
private final AccountRepository accountRepository;

private PasswordEncoder passwordEncoder;
private final PasswordEncoder passwordEncoder;

public AccountService(AccountRepository accountRepository, PasswordEncoder passwordEncoder) {
this.accountRepository = accountRepository;
this.passwordEncoder = passwordEncoder;
}

public Account saveAccount(AccountDto dto) {
Account account = dto.toEntity();
public AccountDto saveAccount(AccountDto dto) {
Account account = AccountAndDtoAdapter.dtoToEntity(dto);
account.setPassword(passwordEncoder.encode(dto.getPassword()));

return accountRepository.save(account);
return AccountAndDtoAdapter.entityToDto(accountRepository.save(account));
}

public PagedResources findAllAccountWithStatus(AccountStatus status, Pageable pageable, PagedResourcesAssembler<Account> pagedResourcesAssembler) {
Expand All @@ -42,8 +40,9 @@ public PagedResources findAllAccountWithStatus(AccountStatus status, Pageable pa

}

public Optional<Account> getAccount(Long id) {
return accountRepository.findById(id);
public AccountDto getAccount(Long id) {
Account account = accountRepository.findById(id).orElseThrow(EntityNotFoundException::new);
return AccountAndDtoAdapter.entityToDto(account);
}

@Transactional
Expand All @@ -53,19 +52,19 @@ public void changeAccountStatus(Long id, AccountStatus status) {
}

@Transactional
public Account updateAccount(Long id, AccountDto accountDto) {
public AccountDto updateAccount(Long id, AccountDto accountDto) {
Account account = accountRepository.findById(id).get();
account.update(accountDto);

return account;
return AccountAndDtoAdapter.entityToDto(account);
}

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Account account = accountRepository.findByEmail(username)
.orElseThrow(() -> new UsernameNotFoundException(username));

return AccountAdapter.from(account);
return AccountAndUserAdapter.from(AccountAndDtoAdapter.entityToDto(account));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import org.springframework.boot.autoconfigure.security.servlet.PathRequest;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
Expand All @@ -13,7 +12,7 @@
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;

@Configuration
@EnableWebSecurity
Expand All @@ -33,8 +32,8 @@ public SecurityConfig(AccountService accountService, PasswordEncoder passwordEnc
* TokenStore -> Redis
*/
@Bean
public TokenStore tokenStore(RedisConnectionFactory redisConnectionFactory) {
return new RedisTokenStore(redisConnectionFactory);
public TokenStore tokenStore() {
return new InMemoryTokenStore();
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package me.jjeda.mall.accounts.controller;

import me.jjeda.mall.accounts.Service.AccountService;
import me.jjeda.mall.accounts.domain.Account;
import me.jjeda.mall.accounts.domain.AccountStatus;
import me.jjeda.mall.accounts.dto.AccountDto;
import me.jjeda.mall.common.CurrentUser;
Expand All @@ -22,35 +21,35 @@
@RequestMapping("/api/accounts")
public class AccountController {

private AccountService accountService;
private final AccountService accountService;

public AccountController(AccountService accountService) {
this.accountService = accountService;
}

@PostMapping
public ResponseEntity createAccount(@RequestBody @Valid AccountDto requestAccount) {
Account account = accountService.saveAccount(requestAccount);
URI uri = ControllerLinkBuilder.linkTo(AccountController.class).slash(account.getId()).toUri();
AccountDto accountDto = accountService.saveAccount(requestAccount);
URI uri = ControllerLinkBuilder.linkTo(AccountController.class).slash(accountDto.getId()).toUri();

return ResponseEntity.created(uri).body(account);
return ResponseEntity.created(uri).body(accountDto);
}

@GetMapping
public ResponseEntity getAccount(@CurrentUser Account account) {
return ResponseEntity.ok(account);
public ResponseEntity getAccount(@CurrentUser AccountDto accountDto) {
return ResponseEntity.ok(accountService.getAccount(accountDto.getId()));
}

@DeleteMapping
public ResponseEntity withdrawFromMembership(@CurrentUser Account account) {
accountService.changeAccountStatus(account.getId(), AccountStatus.DELETED);
public ResponseEntity withdrawFromMembership(@CurrentUser AccountDto accountDto) {
accountService.changeAccountStatus(accountDto.getId(), AccountStatus.DELETED);

return ResponseEntity.ok().build();
}

@PutMapping
public ResponseEntity updateAccount(@RequestBody @Valid AccountDto accountDto, @CurrentUser Account account) {
Account updateAccount = accountService.updateAccount(account.getId(), accountDto);
public ResponseEntity updateAccount(@RequestBody @Valid AccountDto accountDto, @CurrentUser AccountDto currentUser) {
AccountDto updateAccount = accountService.updateAccount(currentUser.getId(), accountDto);

return ResponseEntity.ok(updateAccount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import me.jjeda.mall.accounts.Service.AccountService;
import me.jjeda.mall.accounts.domain.Account;
import me.jjeda.mall.accounts.domain.AccountStatus;
import me.jjeda.mall.accounts.dto.AccountDto;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.PagedResources;
Expand All @@ -14,47 +15,56 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Optional;
import javax.persistence.EntityNotFoundException;

@Controller
@RequestMapping("/admin/accounts")
public class AdminAccountController {

private AccountService accountService;
private final AccountService accountService;

public AdminAccountController(AccountService accountService) {
this.accountService = accountService;
}

@GetMapping
public ResponseEntity getAccountWithStatus(@RequestParam AccountStatus status, Pageable pageable, PagedResourcesAssembler<Account> pagedResourcesAssembler) {
PagedResources pagedResources = accountService.findAllAccountWithStatus(status, pageable, pagedResourcesAssembler);
public ResponseEntity getAccountWithStatus(@RequestParam AccountStatus status,
Pageable pageable,
PagedResourcesAssembler<Account> pagedResourcesAssembler) {
PagedResources pagedResources
= accountService.findAllAccountWithStatus(status, pageable, pagedResourcesAssembler);

return ResponseEntity.ok(pagedResources);
}

@GetMapping("/{id}")
public ResponseEntity getAccount(@PathVariable Long id) {
Optional<Account> account = accountService.getAccount(id);
if (account.isEmpty()) {
AccountDto accountDto;

try {
accountDto = accountService.getAccount(id);
} catch (EntityNotFoundException e) {
return ResponseEntity.notFound().build();
}

return ResponseEntity.ok(account);
return ResponseEntity.ok(accountDto);
}

@DeleteMapping("/{id}")
public ResponseEntity changeAccountStatus(@PathVariable Long id, @RequestParam AccountStatus status) {
Optional<Account> account = accountService.getAccount(id);
AccountDto accountDto;

if (account.isEmpty()) {
try {
accountDto = accountService.getAccount(id);
} catch (EntityNotFoundException e) {
return ResponseEntity.notFound().build();
} else if(status.equals(AccountStatus.DELETED)) {
}

if(status.equals(AccountStatus.DELETED)) {
//관리자는 탈퇴 처리 할 수없다.
return ResponseEntity.badRequest().build();
}
accountService.changeAccountStatus(id,status);

return ResponseEntity.ok(account);
return ResponseEntity.ok(accountService.getAccount(accountDto.getId()));
}
}
17 changes: 17 additions & 0 deletions src/main/java/me/jjeda/mall/accounts/domain/Account.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package me.jjeda.mall.accounts.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import me.jjeda.mall.accounts.dto.AccountDto;
import me.jjeda.mall.common.model.Address;
import me.jjeda.mall.orders.domain.Order;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
Expand All @@ -17,7 +19,10 @@
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Entity
Expand All @@ -34,12 +39,20 @@ public class Account {

private String nickname;

@Column(unique = true)
private String email;

private String password;

private String phone;

/**
* SELLER 의 경우 팔고있는 상품
*/
@OneToMany(mappedBy = "account")
@JsonIgnore
private List<Order> orders = new ArrayList<>();

@Enumerated(EnumType.STRING)
private AccountStatus status;

Expand All @@ -66,4 +79,8 @@ public void update(AccountDto accountDto) {
this.email = accountDto.getEmail();
this.modifiedAt = LocalDateTime.now();
}

public void insertOrder(Order order) {
this.orders.add(order);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package me.jjeda.mall.accounts.domain;

import me.jjeda.mall.accounts.dto.AccountDto;

import java.time.LocalDateTime;
import java.util.Objects;

public class AccountAndDtoAdapter {


public static AccountDto entityToDto(Account account) {
return AccountDto.builder()
.id(account.getId())
.nickname(account.getNickname())
.email(account.getEmail())
.password(account.getPassword())
.phone(account.getPhone())
.accountRole(account.getAccountRole())
.address(account.getAddress())
.createAt(account.getCreatedAt())
.status(account.getStatus())
.build();
}

public static Account dtoToEntity(AccountDto accountDto) {

Account account = Account.builder()
.nickname(accountDto.getNickname())
.email(accountDto.getEmail())
.password(accountDto.getPassword())
.address(accountDto.getAddress())
.phone(accountDto.getPhone())
.accountRole(accountDto.getAccountRole())
.createdAt(LocalDateTime.now())
.status(AccountStatus.NORMAL)
.build();

if (Objects.nonNull(accountDto.getId())) {
account.setId(accountDto.getId());
account.setCreatedAt(accountDto.getCreateAt());
account.setStatus(accountDto.getStatus());
}

return account;
}
}
Loading