Skip to content
This repository has been archived by the owner on Nov 25, 2023. It is now read-only.

Commit

Permalink
update endpoints for pagination and sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
JPiotr committed Nov 12, 2023
1 parent 963d381 commit 7e47337
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 43 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/oblitus/serviceApp/Common/Response.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ public class Response {
protected String message;
protected String devMessage;
protected Map<String,?> data;
protected Map<String,PageInfo> meta;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.oblitus.serviceApp.Modules.Controllers;

import com.oblitus.serviceApp.Common.PageInfo;
import com.oblitus.serviceApp.Common.PageSortUtil;
import com.oblitus.serviceApp.Common.Response;
import com.oblitus.serviceApp.Modules.Admin.DTOs.UserDTO;
import com.oblitus.serviceApp.Modules.Admin.Responses.UserResponse;
Expand Down Expand Up @@ -113,13 +115,50 @@ public ResponseEntity<Response> addUser(@RequestBody @Validated UserDTO userDTO)
}

@GetMapping("/users")
public ResponseEntity<Response> getUsers(@RequestParam @Nullable @Validated String ruleName){
public ResponseEntity<Response> getUsers(@RequestParam @Nullable @Validated String ruleName,
@Nullable @RequestParam @Validated String sortField,
@Nullable @RequestParam @Validated Boolean desc,
@Nullable @RequestParam @Validated Integer page,
@Nullable @RequestParam @Validated Integer size){

PageSortUtil.preparePaginationAndSorting(sortField,desc,page,size);
if(PageSortUtil.pageable.isPaged()){
var userPage = modulesWrapper.adminModule.getAdminDAO().getUserService()
.getAll(PageSortUtil.pageable);

if(ruleName != null && Arrays.stream(ERule.values()).anyMatch(x-> Objects.equals(x.toString(), ruleName))){
return ResponseEntity.ok(
Response.builder()
.timestamp(LocalDateTime.now())
.message("All users with rule "+ruleName)
.data(Map.of("users",userPage.stream().map(mappersWrapper.userMapper)
.filter(userResponse -> userResponse.getRules().stream()
.anyMatch(ruleDTO -> Objects.equals(ruleDTO.name(), ruleName)))
.toList()))
.meta(Map.of("pageInfo",new PageInfo(userPage)))
.statusCode(HttpStatus.OK.value())
.status(HttpStatus.OK)
.build()
);
}
return ResponseEntity.ok(
Response.builder()
.timestamp(LocalDateTime.now())
.message("All existing users.")
.data(Map.of("users",userPage.stream().map(mappersWrapper.userMapper).toList()))
.meta(Map.of("pageInfo",new PageInfo(userPage)))
.statusCode(HttpStatus.OK.value())
.status(HttpStatus.OK)
.build()
);
}
if(ruleName != null && Arrays.stream(ERule.values()).anyMatch(x-> Objects.equals(x.toString(), ruleName))){
return ResponseEntity.ok(
Response.builder()
.timestamp(LocalDateTime.now())
.message("All users with rule "+ruleName)
.data(Map.of("users",modulesWrapper.adminModule.getAdminDAO().getUserService().getAll()
.data(Map.of("users",modulesWrapper.adminModule.getAdminDAO().getUserService()
.getAll(PageSortUtil.sort)
.stream().map(mappersWrapper.userMapper)
.filter(userResponse -> userResponse.getRules().stream()
.anyMatch(ruleDTO -> Objects.equals(ruleDTO.name(), ruleName)))
Expand All @@ -133,7 +172,8 @@ public ResponseEntity<Response> getUsers(@RequestParam @Nullable @Validated Stri
Response.builder()
.timestamp(LocalDateTime.now())
.message("All existing users.")
.data(Map.of("users",modulesWrapper.adminModule.getAdminDAO().getUserService().getAll()
.data(Map.of("users",modulesWrapper.adminModule.getAdminDAO().getUserService()
.getAll(PageSortUtil.sort)
.stream().map(mappersWrapper.userMapper).toList()))
.statusCode(HttpStatus.OK.value())
.status(HttpStatus.OK)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
package com.oblitus.serviceApp.Modules.Controllers;

import com.oblitus.serviceApp.Common.PageInfo;
import com.oblitus.serviceApp.Common.PageSortUtil;
import com.oblitus.serviceApp.Common.Response;
import com.oblitus.serviceApp.Modules.MappersWrapper;
import com.oblitus.serviceApp.Modules.ModulesWrapper;
import com.oblitus.serviceApp.Modules.Service.DTOs.*;
import com.oblitus.serviceApp.Modules.Service.Responses.TicketResponse;
import com.oblitus.serviceApp.Modules.Service.Ticket;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.aspectj.apache.bcel.classfile.Field;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.util.Pair;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -160,13 +173,33 @@ public ResponseEntity<Response> getComment(@PathVariable @Validated UUID id) {
}

@GetMapping("/comments")
public ResponseEntity<Response> getComments(){
public ResponseEntity<Response> getComments(@Nullable @RequestParam @Validated String sortField,
@Nullable @RequestParam @Validated Boolean desc,
@Nullable @RequestParam @Validated Integer page,
@Nullable @RequestParam @Validated Integer size){
PageSortUtil.preparePaginationAndSorting(sortField,desc,page,size);
if(PageSortUtil.pageable.isPaged()){
var commentPage = modulesWrapper.serviceModule.getServiceDAO().getCommentService()
.getAll(PageSortUtil.pageable);
return ResponseEntity.ok(
Response.builder()
.timestamp(LocalDateTime.now())
.message("All existing comments.")
.data(Map.of("comments",commentPage.stream().map(mappersWrapper.commentMapper).toList()
)
)
.meta(Map.of("pageInfo",new PageInfo(commentPage)))
.statusCode(HttpStatus.OK.value())
.status(HttpStatus.OK)
.build()
);
}
return ResponseEntity.ok(
Response.builder()
.timestamp(LocalDateTime.now())
.message("All existing comments.")
.data(Map.of("comments",modulesWrapper.serviceModule.getServiceDAO().getCommentService()
.getAll().stream().map(mappersWrapper.commentMapper).toList()
.getAll(PageSortUtil.sort).stream().map(mappersWrapper.commentMapper).toList()
)
)
.statusCode(HttpStatus.OK.value())
Expand Down Expand Up @@ -240,27 +273,35 @@ public ResponseEntity<Response> deleteComment(@RequestBody @Validated CommentDTO
);
}

@Deprecated
@DeleteMapping("/comment/{id}")
public ResponseEntity<Response> deleteCommentByID(@PathVariable @Validated UUID id){
return ResponseEntity.ok(
Response.builder()
.timestamp(LocalDateTime.now())
.message("Try to drop Comment")
.data(Map.of("result", modulesWrapper.serviceModule.getServiceDAO().getCommentService().delete(new CommentDTO(id))))
.statusCode(HttpStatus.OK.value())
.status(HttpStatus.OK)
.build()
);
}

@GetMapping("/comments/")
public ResponseEntity<Response> getTicketsComments(@Nonnull @RequestParam @Validated UUID ticketId){
public ResponseEntity<Response> getTicketsComments(@Nonnull @RequestParam @Validated UUID ticketId,
@Nullable @RequestParam @Validated String sortField,
@Nullable @RequestParam @Validated Boolean desc,
@Nullable @RequestParam @Validated Integer page,
@Nullable @RequestParam @Validated Integer size){
PageSortUtil.preparePaginationAndSorting(sortField,desc,page,size);
if(PageSortUtil.pageable.isPaged()) {
var commentPage = modulesWrapper.serviceModule.getServiceDAO().getCommentService()
.getAll(PageSortUtil.pageable);
return ResponseEntity.ok(
Response.builder()
.timestamp(LocalDateTime.now())
.message("All ticket's comments.")
.data(Map.of("comments",commentPage.stream().filter(
commentResponse -> ticketId.equals(commentResponse.getTicket().getUuid())
).map(mappersWrapper.commentMapper).collect(Collectors.toList())))
.meta(Map.of("pageInfo",new PageInfo(commentPage)))
.statusCode(HttpStatus.OK.value())
.status(HttpStatus.OK)
.build()
);
}
return ResponseEntity.ok(
Response.builder()
.timestamp(LocalDateTime.now())
.message("All ticket's comments.")
.data(Map.of("comments",modulesWrapper.serviceModule.getServiceDAO().getCommentService().getAll().stream().filter(
.data(Map.of("comments",modulesWrapper.serviceModule.getServiceDAO().getCommentService()
.getAll(PageSortUtil.sort).stream().filter(
commentResponse -> ticketId.equals(commentResponse.getTicket().getUuid())
).map(mappersWrapper.commentMapper).collect(Collectors.toList())))
.statusCode(HttpStatus.OK.value())
Expand Down Expand Up @@ -301,15 +342,35 @@ public ResponseEntity<Response> getTicket(@PathVariable @Validated UUID id) {
);
}
}

@GetMapping("/tickets")
public ResponseEntity<Response> getTickets(){
public ResponseEntity<Response> getTicketsPage(@Nullable @RequestParam @Validated String sortField,
@Nullable @RequestParam @Validated Boolean desc,
@Nullable @RequestParam @Validated Integer page,
@Nullable @RequestParam @Validated Integer size){
PageSortUtil.preparePaginationAndSorting(sortField,desc,size,page);
if(PageSortUtil.pageable.isPaged()){
var ticketsPage = modulesWrapper.serviceModule.getServiceDAO().getTicketService()
.getAll(PageSortUtil.pageable);
var ticketList = ticketsPage.stream().map(mappersWrapper.ticketMapper).toList();
return ResponseEntity.ok(
Response.builder()
.timestamp(LocalDateTime.now())
.message("All existing tickets.")
.data(Map.of("tickets", ticketList))
.meta(Map.of("pageInfo", new PageInfo(ticketsPage)))
.statusCode(HttpStatus.OK.value())
.status(HttpStatus.OK)
.build()
);
}

return ResponseEntity.ok(
Response.builder()
.timestamp(LocalDateTime.now())
.message("All existing tickets.")
.data(Map.of("tickets",modulesWrapper.serviceModule.getServiceDAO().getTicketService()
.getAll().stream().map(mappersWrapper.ticketMapper).toList()))
.data(Map.of("tickets",
modulesWrapper.serviceModule.getServiceDAO().getTicketService()
.getAll(PageSortUtil.sort).stream().map(mappersWrapper.ticketMapper).toList()))
.statusCode(HttpStatus.OK.value())
.status(HttpStatus.OK)
.build()
Expand All @@ -331,17 +392,39 @@ public ResponseEntity<Response> addTicket(@RequestBody @Validated TicketDTO tick
}

@GetMapping("/tickets/{userId}")
public ResponseEntity<Response> getUserTickets(@PathVariable @Validated UUID userId){
public ResponseEntity<Response> getUserTickets(@PathVariable @Validated UUID userId,
@Nullable @RequestParam @Validated String sortField,
@Nullable @RequestParam @Validated Boolean desc,
@Nullable @RequestParam @Validated Integer page,
@Nullable @RequestParam @Validated Integer size){
PageSortUtil.preparePaginationAndSorting(sortField,desc,size,page);
if(PageSortUtil.pageable.isPaged()){
var ticketsPage = modulesWrapper.serviceModule.getServiceDAO().getTicketService()
.getAll(PageSortUtil.pageable);
return ResponseEntity.ok(
Response.builder()
.timestamp(LocalDateTime.now())
.message("All user tickets.")
.data(Map.of("tickets", ticketsPage.stream().filter(
ticket -> userId.equals(ticket.getAssigned().getUuid())
|| userId.equals(ticket.getCreator().getUuid())
).map(mappersWrapper.ticketMapper).toList()))
.meta(Map.of("pageInfo", new PageInfo(ticketsPage)))
.statusCode(HttpStatus.OK.value())
.status(HttpStatus.OK)
.build()
);
}
return ResponseEntity.ok(
Response.builder()
.timestamp(LocalDateTime.now())
.message("All user tickets.")
.data(Map.of("tickets",modulesWrapper.serviceModule.getServiceDAO().getTicketService()
.getAll().stream().filter(
.getAll(PageSortUtil.sort).stream().filter(
ticket -> userId.equals(ticket.getAssigned().getUuid())
|| userId.equals(ticket.getCreator().getUuid())
).map(mappersWrapper.ticketMapper)
.collect(Collectors.toList())))
.toList()))
.statusCode(HttpStatus.OK.value())
.status(HttpStatus.OK)
.build()
Expand Down Expand Up @@ -394,21 +477,6 @@ public ResponseEntity<Response> deleteTicket(@RequestBody @Validated TicketDTO t
);
}

@Deprecated
@DeleteMapping("/ticket/{id}")
public ResponseEntity<Response> deleteTicketById(@PathVariable @Validated UUID id){
return ResponseEntity.ok(
Response.builder()
.timestamp(LocalDateTime.now())
.message("Try to drop Ticket")
.data(Map.of("result", modulesWrapper.serviceModule.getServiceDAO().getTicketService()
.delete(new TicketDTO(id))))
.statusCode(HttpStatus.OK.value())
.status(HttpStatus.OK)
.build()
);
}

@GetMapping("/activity/{id}")
public ResponseEntity<Response> getActivity(@PathVariable @Validated UUID id) {
try{
Expand Down

0 comments on commit 7e47337

Please sign in to comment.