Skip to content
This repository has been archived by the owner on Oct 22, 2020. It is now read-only.

Commit

Permalink
[#163] feat: 어드민 관리 기능을 추가한다.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevLuffy committed May 22, 2020
1 parent dfc5caf commit 69fcace
Show file tree
Hide file tree
Showing 11 changed files with 308 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<AdminSearchResponse> searchAdmin(@PageableDefault Pageable pageable,
@Valid AdminSearchRequest request) {
return adminFacade.searchAdmin(pageable, request);
}

@PostMapping
public AdminCreateResponse createAdmin(@RequestBody AdminCreateRequest request) {
return adminFacade.createAdmin(request);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Admin> searchAdmin(Pageable pageable, AdminSearchClause clause) {
JPQLQuery<Admin> jpqlQuery = jpaQueryFactory.selectFrom(admin)
.where(clause.where())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.orderBy(admin.id.asc());

QueryResults<Admin> queryResults = jpqlQuery.fetchResults();

return new PageImpl<>(queryResults.getResults(), pageable, queryResults.getTotal());
}
}
Original file line number Diff line number Diff line change
@@ -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<Predicate> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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<AdminSearchResponse> searchAdmin(Pageable pageable, AdminSearchRequest request) {
Page<Admin> admins = adminService.searchAdmin(pageable, request);

List<AdminSearchResponse> 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);
}
}
Original file line number Diff line number Diff line change
@@ -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<Admin> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 69fcace

Please sign in to comment.