Skip to content

Commit

Permalink
Merge pull request #170 from theMomentTeam/feature/improve-hotfix
Browse files Browse the repository at this point in the history
Feature/improve hotfix
  • Loading branch information
siwony authored Jun 19, 2021
2 parents c02c79c + 9ecb0e1 commit 5886d1e
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

import com.moment.the.admin.AdminDomain;
import com.moment.the.admin.repository.AdminRepository;
import com.moment.the.exceptionAdvice.exception.NoImprovementException;
import com.moment.the.admin.service.AdminServiceImpl;
import com.moment.the.exceptionAdvice.exception.AccessNotFoundException;
import com.moment.the.exceptionAdvice.exception.UserNotFoundException;
import com.moment.the.improvement.ImprovementDomain;
import com.moment.the.improvement.dto.ImprovementDto;
import com.moment.the.improvement.dto.ImprovementViewAllDto;
import com.moment.the.improvement.repository.ImprovementRepository;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -27,10 +26,8 @@ public class ImprovementService {
// Create improvement.
@Transactional
public ImprovementDomain save(ImprovementDto improvementDto){
// 현재 user 정보를 가져오기
String UserEmail = getUserEmail();
try {
AdminDomain adminDomain = adminRepository.findByAdminId(UserEmail);
AdminDomain adminDomain = adminRepository.findByAdminId(AdminServiceImpl.getUserEmail());
return improvementRepository.save(improvementDto.ToEntity(adminDomain));
} catch (UserNotFoundException e){
throw new UserNotFoundException();
Expand All @@ -48,42 +45,23 @@ public List<ImprovementViewAllDto> read(){
// Update improvement.
@Transactional
public void update(ImprovementDto improvementDto, Long improveIdx){
// 현재 user 정보를 가져오기
try {
String UserEmail = getUserEmail();
AdminDomain adminDomain = adminRepository.findByAdminId(UserEmail);
} catch (UserNotFoundException e){
System.err.println("UserNotFoundException 이 발생했습니다.");
}
// 개선 사례 가져오기
try {
ImprovementDomain improvementDomain = improvementRepository.findByImproveIdx(improveIdx);
ImprovementDomain improvementDomain = improvementRepository.findByImproveIdx(improveIdx);
if(improvementDomain.getAdminDomain().getAdminId().equals(AdminServiceImpl.getUserEmail())){
improvementDomain.update(improvementDto);
} catch (NoImprovementException e){
System.err.println("NoImprovementException 이 발생했습니다.");
} else {
throw new AccessNotFoundException();
}
}

// Delete improvement.
@Transactional
public void delete(Long improveIdx){
try {
ImprovementDomain selectImprove = improvementRepository.findByImproveIdx(improveIdx);
ImprovementDomain selectImprove = improvementRepository.findByImproveIdx(improveIdx);
if(selectImprove.getAdminDomain().getAdminId().equals(AdminServiceImpl.getUserEmail())){
improvementRepository.delete(selectImprove);
} catch (NoImprovementException e){
System.err.println("NoImprovementException 이 발생했습니다.");
}
}

// Current UserEmail을 가져오기.
public String getUserEmail() {
String userEmail;
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if(principal instanceof UserDetails) {
userEmail = ((UserDetails) principal).getUsername();
} else {
userEmail = principal.toString();
throw new AccessNotFoundException();
}
return userEmail;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.moment.the.improvement.service;

import com.moment.the.admin.AdminDomain;
import com.moment.the.admin.dto.AdminDto;
import com.moment.the.admin.repository.AdminRepository;
import com.moment.the.admin.service.AdminService;
import com.moment.the.improvement.dto.ImprovementDto;
import com.moment.the.improvement.repository.ImprovementRepository;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;

import java.awt.*;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@Transactional
class ImprovementServiceTest {
@Autowired
private ImprovementRepository improvementRepository;
@Autowired
private ImprovementService improvementService;
@Autowired
private AdminService adminService;
@Autowired
private AdminRepository adminRepository;
@AfterEach
void deleteData(){
improvementRepository.deleteAll();
}

// test 편의를 위한 회원가입 매서드
void adminSignUp(String adminId, String password, String adminName) throws Exception {
AdminDto adminDto = new AdminDto(adminId, password, adminName);
adminService.signUp(adminDto);
}

// test 편의를 위한 로그인 매서드
AdminDomain adminLogin(String adminId, String password) throws Exception {
AdminDomain adminDomain = adminRepository.findByAdminId(adminId);
UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(
adminDomain.getAdminId(),
adminDomain.getAdminPwd(),
List.of(new SimpleGrantedAuthority("ROLE_USER")));
SecurityContext context = SecurityContextHolder.getContext();
context.setAuthentication(token);

return adminDomain;
}

@Test
void 실제개선사례작성() throws Exception {
//Given admin
String email = "asdf@gsm";
String pw = "1234";
String name = "jihwan";
adminSignUp(email, pw, name);
adminLogin(email, pw);

//Given improvement
ImprovementDto improvementDto = new ImprovementDto();
improvementDto.setImproveContent("Hello world");
improvementDto.setImproveContent("it's jihwan");

//when
improvementService.save(improvementDto);

//then
assertEquals(false, improvementRepository.findByImproveContent("it's jihwan") == null);
assertEquals(true, improvementRepository.findByImproveContent("it's jihwan").getAdminDomain().getAdminId().equals("asdf@gsm"));
}

}

0 comments on commit 5886d1e

Please sign in to comment.