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; + } }