This repository has been archived by the owner on Oct 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dfc5caf
commit 69fcace
Showing
11 changed files
with
308 additions
and
0 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
tecobrary-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/AdminController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...min/src/main/java/com/woowacourse/tecobrary/admin/web/admin/converter/AdminConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminCreateRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...dmin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminCreateResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminSearchRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...dmin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/dto/AdminSearchResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...main/java/com/woowacourse/tecobrary/admin/web/admin/repository/AdminManageRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
...src/main/java/com/woowacourse/tecobrary/admin/web/admin/repository/AdminSearchClause.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
39 changes: 39 additions & 0 deletions
39
...ry-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/service/AdminFacade.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...y-admin/src/main/java/com/woowacourse/tecobrary/admin/web/admin/service/AdminService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters