From 69fcace81de2d09ba7a4b4f8fec1cf03eb444781 Mon Sep 17 00:00:00 2001 From: thedevluffy Date: Fri, 22 May 2020 22:40:40 +0900 Subject: [PATCH 1/3] =?UTF-8?q?[#163]=20feat:=20=EC=96=B4=EB=93=9C?= =?UTF-8?q?=EB=AF=BC=20=EA=B4=80=EB=A6=AC=20=EA=B8=B0=EB=8A=A5=EC=9D=84=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/web/admin/AdminController.java | 33 +++++++++++++ .../web/admin/converter/AdminConverter.java | 26 ++++++++++ .../web/admin/dto/AdminCreateRequest.java | 19 +++++++ .../web/admin/dto/AdminCreateResponse.java | 19 +++++++ .../web/admin/dto/AdminSearchRequest.java | 20 ++++++++ .../web/admin/dto/AdminSearchResponse.java | 22 +++++++++ .../repository/AdminManageRepository.java | 36 ++++++++++++++ .../admin/repository/AdminSearchClause.java | 49 +++++++++++++++++++ .../admin/web/admin/service/AdminFacade.java | 39 +++++++++++++++ .../admin/web/admin/service/AdminService.java | 40 +++++++++++++++ .../tecobrary/domain/admin/entity/Admin.java | 5 ++ 11 files changed, 308 insertions(+) create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/AdminController.java create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/converter/AdminConverter.java create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminCreateRequest.java create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminCreateResponse.java create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminSearchRequest.java create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminSearchResponse.java create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/repository/AdminManageRepository.java create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/repository/AdminSearchClause.java create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/service/AdminFacade.java create mode 100644 tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/service/AdminService.java diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/AdminController.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/AdminController.java new file mode 100644 index 0000000..ee1d4ea --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/AdminController.java @@ -0,0 +1,33 @@ +package com.woowacourse.tecobrary.admin.web.admin; + +import com.woowacourse.tecobrary.admin.web.admin.dto.AdminCreateRequest; +import com.woowacourse.tecobrary.admin.web.admin.dto.AdminCreateResponse; +import com.woowacourse.tecobrary.admin.web.admin.dto.AdminSearchRequest; +import com.woowacourse.tecobrary.admin.web.admin.dto.AdminSearchResponse; +import com.woowacourse.tecobrary.admin.web.admin.service.AdminFacade; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.web.PageableDefault; +import org.springframework.web.bind.annotation.*; + +import javax.validation.Valid; + +@RequiredArgsConstructor +@RestController +@RequestMapping("/api/admin") +public class AdminController { + + private final AdminFacade adminFacade; + + @GetMapping + public Page searchAdmin(@PageableDefault Pageable pageable, + @Valid AdminSearchRequest request) { + return adminFacade.searchAdmin(pageable, request); + } + + @PostMapping + public AdminCreateResponse createAdmin(@RequestBody AdminCreateRequest request) { + return adminFacade.createAdmin(request); + } +} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/converter/AdminConverter.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/converter/AdminConverter.java new file mode 100644 index 0000000..1e5e06e --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/converter/AdminConverter.java @@ -0,0 +1,26 @@ +package com.woowacourse.tecobrary.admin.web.admin.converter; + +import com.woowacourse.tecobrary.admin.web.admin.dto.AdminCreateResponse; +import com.woowacourse.tecobrary.admin.web.admin.dto.AdminSearchResponse; +import com.woowacourse.tecobrary.domain.admin.entity.Admin; +import org.springframework.stereotype.Component; + +@Component +public class AdminConverter { + + public AdminSearchResponse convertToSearchResponse(Admin admin) { + return AdminSearchResponse.builder() + .id(admin.getId()) + .email(admin.getEmail()) + .name(admin.getName()) + .role(admin.getRole().name()) + .build(); + } + + public AdminCreateResponse convertToCreateResponse(Admin admin) { + return AdminCreateResponse.builder() + .id(admin.getId()) + .email(admin.getEmail()) + .build(); + } +} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminCreateRequest.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminCreateRequest.java new file mode 100644 index 0000000..4b5b0e5 --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminCreateRequest.java @@ -0,0 +1,19 @@ +package com.woowacourse.tecobrary.admin.web.admin.dto; + + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Getter +@ToString +public class AdminCreateRequest { + + private String email; + + public AdminCreateRequest(String email) { + this.email = email; + } +} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminCreateResponse.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminCreateResponse.java new file mode 100644 index 0000000..7b153de --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminCreateResponse.java @@ -0,0 +1,19 @@ +package com.woowacourse.tecobrary.admin.web.admin.dto; + + +import lombok.*; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Getter +@ToString +public class AdminCreateResponse { + + private Long id; + private String email; + + @Builder + public AdminCreateResponse(Long id, String email) { + this.id = id; + this.email = email; + } +} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminSearchRequest.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminSearchRequest.java new file mode 100644 index 0000000..f0b82f7 --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminSearchRequest.java @@ -0,0 +1,20 @@ +package com.woowacourse.tecobrary.admin.web.admin.dto; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Getter +@ToString +public class AdminSearchRequest { + + private String email; + private String name; + + public AdminSearchRequest(String email, String name) { + this.email = email; + this.name = name; + } +} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminSearchResponse.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminSearchResponse.java new file mode 100644 index 0000000..58f3869 --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminSearchResponse.java @@ -0,0 +1,22 @@ +package com.woowacourse.tecobrary.admin.web.admin.dto; + +import lombok.*; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@Getter +@ToString +public class AdminSearchResponse { + + private Long id; + private String email; + private String name; + private String role; + + @Builder + public AdminSearchResponse(Long id, String email, String name, String role) { + this.id = id; + this.email = email; + this.name = name; + this.role = role; + } +} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/repository/AdminManageRepository.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/repository/AdminManageRepository.java new file mode 100644 index 0000000..1e6b330 --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/repository/AdminManageRepository.java @@ -0,0 +1,36 @@ +package com.woowacourse.tecobrary.admin.web.admin.repository; + +import com.querydsl.core.QueryResults; +import com.querydsl.jpa.JPQLQuery; +import com.querydsl.jpa.impl.JPAQueryFactory; +import com.woowacourse.tecobrary.domain.admin.entity.Admin; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.support.QuerydslRepositorySupport; +import org.springframework.stereotype.Repository; + +import static com.woowacourse.tecobrary.domain.admin.entity.QAdmin.admin; + +@Repository +public class AdminManageRepository extends QuerydslRepositorySupport { + + private final JPAQueryFactory jpaQueryFactory; + + public AdminManageRepository(JPAQueryFactory jpaQueryFactory) { + super(Admin.class); + this.jpaQueryFactory = jpaQueryFactory; + } + + public Page searchAdmin(Pageable pageable, AdminSearchClause clause) { + JPQLQuery jpqlQuery = jpaQueryFactory.selectFrom(admin) + .where(clause.where()) + .offset(pageable.getOffset()) + .limit(pageable.getPageSize()) + .orderBy(admin.id.asc()); + + QueryResults queryResults = jpqlQuery.fetchResults(); + + return new PageImpl<>(queryResults.getResults(), pageable, queryResults.getTotal()); + } +} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/repository/AdminSearchClause.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/repository/AdminSearchClause.java new file mode 100644 index 0000000..893287e --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/repository/AdminSearchClause.java @@ -0,0 +1,49 @@ +package com.woowacourse.tecobrary.admin.web.admin.repository; + + +import com.nimbusds.oauth2.sdk.util.StringUtils; +import com.querydsl.core.types.Predicate; +import com.querydsl.core.types.dsl.BooleanExpression; +import lombok.Builder; + +import java.util.*; + +import static com.woowacourse.tecobrary.domain.admin.entity.QAdmin.admin; + +public class AdminSearchClause { + + private String name; + private String email; + + @Builder + public AdminSearchClause(String name, String email) { + this.name = name; + this.email = email; + } + + Predicate[] where(Predicate... predicates) { + List result = new ArrayList<>(Arrays.asList(predicates)); + result.addAll(Arrays.asList( + getNameExpression(), + getEmailExpression() + )); + return result.stream() + .filter(Objects::nonNull) + .toArray(Predicate[]::new); + } + + private BooleanExpression getNameExpression() { + return Optional.ofNullable(name) + .filter(StringUtils::isNotBlank) + .map(admin.name::like) + .orElse(null); + } + + private BooleanExpression getEmailExpression() { + return Optional.ofNullable(email) + .filter(StringUtils::isNotBlank) + .map(admin.email::like) + .orElse(null); + } + +} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/service/AdminFacade.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/service/AdminFacade.java new file mode 100644 index 0000000..5ff0aac --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/service/AdminFacade.java @@ -0,0 +1,39 @@ +package com.woowacourse.tecobrary.admin.web.admin.service; + +import com.woowacourse.tecobrary.admin.web.admin.converter.AdminConverter; +import com.woowacourse.tecobrary.admin.web.admin.dto.AdminCreateRequest; +import com.woowacourse.tecobrary.admin.web.admin.dto.AdminCreateResponse; +import com.woowacourse.tecobrary.admin.web.admin.dto.AdminSearchRequest; +import com.woowacourse.tecobrary.admin.web.admin.dto.AdminSearchResponse; +import com.woowacourse.tecobrary.domain.admin.entity.Admin; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageImpl; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +public class AdminFacade { + + private final AdminService adminService; + private final AdminConverter adminConverter; + + public Page searchAdmin(Pageable pageable, AdminSearchRequest request) { + Page admins = adminService.searchAdmin(pageable, request); + + List dtos = admins.stream() + .map(adminConverter::convertToSearchResponse) + .collect(Collectors.toList()); + + return new PageImpl<>(dtos, pageable, admins.getTotalElements()); + } + + public AdminCreateResponse createAdmin(AdminCreateRequest request) { + Admin admin = adminService.createAdmin(request); + return adminConverter.convertToCreateResponse(admin); + } +} diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/service/AdminService.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/service/AdminService.java new file mode 100644 index 0000000..37e2203 --- /dev/null +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/service/AdminService.java @@ -0,0 +1,40 @@ +package com.woowacourse.tecobrary.admin.web.admin.service; + +import com.woowacourse.tecobrary.admin.web.admin.dto.AdminCreateRequest; +import com.woowacourse.tecobrary.admin.web.admin.dto.AdminSearchRequest; +import com.woowacourse.tecobrary.admin.web.admin.repository.AdminManageRepository; +import com.woowacourse.tecobrary.admin.web.admin.repository.AdminSearchClause; +import com.woowacourse.tecobrary.domain.admin.entity.Admin; +import com.woowacourse.tecobrary.domain.admin.entity.AdminRole; +import com.woowacourse.tecobrary.domain.admin.repository.AdminRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class AdminService { + + private final AdminRepository adminRepository; + private final AdminManageRepository adminManageRepository; + + public Page searchAdmin(Pageable pageable, AdminSearchRequest request) { + AdminSearchClause clause = AdminSearchClause.builder() + .name(request.getName()) + .email(request.getEmail()) + .build(); + + return adminManageRepository.searchAdmin(pageable, clause); + } + + public Admin createAdmin(AdminCreateRequest request) { + Admin admin = Admin.builder() + .email(request.getEmail()) + .name("임시 이름") + .picture("임시 picture") + .build(); + admin.updateRole(AdminRole.ROLE_ADMIN); + return adminRepository.save(admin); + } +} diff --git a/tecobrary-domain/src/main/java/com/woowacourse/tecobrary/domain/admin/entity/Admin.java b/tecobrary-domain/src/main/java/com/woowacourse/tecobrary/domain/admin/entity/Admin.java index 17437f6..8ca4dae 100644 --- a/tecobrary-domain/src/main/java/com/woowacourse/tecobrary/domain/admin/entity/Admin.java +++ b/tecobrary-domain/src/main/java/com/woowacourse/tecobrary/domain/admin/entity/Admin.java @@ -41,4 +41,9 @@ public Admin updateUserInfo(String name, String picture) { this.picture = picture; return this; } + + public Admin updateRole(AdminRole role) { + this.role = role; + return this; + } } From 9db3248c298ea1e555588a38b7e0d3aebed3e3b0 Mon Sep 17 00:00:00 2001 From: thedevluffy Date: Fri, 22 May 2020 22:41:01 +0900 Subject: [PATCH 2/3] =?UTF-8?q?[#163]=20feat:=20spring=20security=20?= =?UTF-8?q?=EC=97=90=20=EC=96=B4=EB=93=9C=EB=AF=BC=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=EC=84=A4=EC=A0=95=EC=9D=84=20=EC=B6=94=EA=B0=80=ED=95=9C?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../woowacourse/tecobrary/admin/security/SecurityConfig.java | 4 +++- .../admin/security/extractor/GoogleUserExtractor.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/security/SecurityConfig.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/security/SecurityConfig.java index b835b7c..3d29953 100644 --- a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/security/SecurityConfig.java +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/security/SecurityConfig.java @@ -28,12 +28,14 @@ protected void configure(HttpSecurity http) throws Exception { .antMatchers("/", "/login", "/oauth2/authorization/**", "/static/**").permitAll() .antMatchers("/api/librarybook/**").hasAuthority(AdminRole.ROLE_ADMIN.name()) + .antMatchers("/api/admin/**").hasAuthority(AdminRole.ROLE_ADMIN.name()) .anyRequest().authenticated() .and().logout().logoutSuccessUrl("/").invalidateHttpSession(true) .and().oauth2Login().userInfoEndpoint().userService(userSecurityService); - if (environment.acceptsProfiles(Profiles.of("local"))) { + if (environment.acceptsProfiles(Profiles.of("local")) + || environment.acceptsProfiles(Profiles.of("localdev"))) { http.oauth2Login() .defaultSuccessUrl(DEVELOP_ORIGIN) .loginPage(DEVELOP_ORIGIN); diff --git a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/security/extractor/GoogleUserExtractor.java b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/security/extractor/GoogleUserExtractor.java index 780f0c3..14c2c13 100644 --- a/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/security/extractor/GoogleUserExtractor.java +++ b/tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/security/extractor/GoogleUserExtractor.java @@ -45,6 +45,6 @@ public Admin extract(OAuth2User oAuth2User) { Admin admin = adminRepository.findByEmail(email) .orElseThrow(() -> new IllegalArgumentException("등록된 유저가 아닙니다.")); - return admin.updateUserInfo(name, picture); + return adminRepository.save(admin.updateUserInfo(name, picture)); } } From b23d3ae63f1d868877b02d31b1e4d1ff8d9662f2 Mon Sep 17 00:00:00 2001 From: thedevluffy Date: Fri, 22 May 2020 22:41:20 +0900 Subject: [PATCH 3/3] =?UTF-8?q?[#163]=20feat:=20=ED=94=84=EB=A1=A0?= =?UTF-8?q?=ED=8A=B8=20=EC=96=B4=EB=93=9C=EB=AF=BC=20=EA=B4=80=EB=A6=AC=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=EC=9D=84=20=EA=B5=AC=ED=98=84=ED=95=9C?= =?UTF-8?q?=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tecobrary-admin/src/front/src/api/admin.js | 15 ++ .../src/front/src/views/user/admin.vue | 167 +++++++++++++++++- 2 files changed, 179 insertions(+), 3 deletions(-) create mode 100644 tecobrary-admin/src/front/src/api/admin.js diff --git a/tecobrary-admin/src/front/src/api/admin.js b/tecobrary-admin/src/front/src/api/admin.js new file mode 100644 index 0000000..c786cf5 --- /dev/null +++ b/tecobrary-admin/src/front/src/api/admin.js @@ -0,0 +1,15 @@ +import request from '@/utils/request' + +export const getAdmin = (query) => request({ + url: '/api/admin', + method: 'get', + params: Object.assign(query, { + page: query.page + }) +}) + +export const createAdmin = (data) => request({ + url: '/api/admin', + method: 'post', + data: data +}) diff --git a/tecobrary-admin/src/front/src/views/user/admin.vue b/tecobrary-admin/src/front/src/views/user/admin.vue index 3c154bb..5fd5905 100644 --- a/tecobrary-admin/src/front/src/views/user/admin.vue +++ b/tecobrary-admin/src/front/src/views/user/admin.vue @@ -1,5 +1,166 @@ \ No newline at end of file + + + \ No newline at end of file