Skip to content

Commit

Permalink
Group by search
Browse files Browse the repository at this point in the history
  • Loading branch information
CaioCesarRocha committed May 5, 2021
1 parent 8debc80 commit 23c45be
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.semanaspringreact.dsvendas.controllers;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -9,6 +11,8 @@
import org.springframework.web.bind.annotation.RestController;

import com.semanaspringreact.dsvendas.dto.SaleDTO;
import com.semanaspringreact.dsvendas.dto.SaleSucessDTO;
import com.semanaspringreact.dsvendas.dto.SaleSumDTO;
import com.semanaspringreact.dsvendas.service.SaleService;

@RestController
Expand All @@ -24,4 +28,14 @@ public ResponseEntity<Page<SaleDTO>> findAll(Pageable pageable){
return ResponseEntity.ok(list);
}

@GetMapping(value="/amount-by-seller")
public ResponseEntity<List<SaleSumDTO>> amountGroupedBySeller(){
List<SaleSumDTO> list = service.amountGroupedBySeller();
return ResponseEntity.ok(list);
}
@GetMapping(value="/sucess-by-seller")
public ResponseEntity<List<SaleSucessDTO>> sucessGroupedBySeller(){
List<SaleSucessDTO> list = service.sucessGroupedBySeller();
return ResponseEntity.ok(list);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.semanaspringreact.dsvendas.dto;

import java.io.Serializable;

import com.semanaspringreact.dsvendas.entities.Seller;

public class SaleSucessDTO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String sellerName;
private Long visited;
private Long deals;

public SaleSucessDTO() {

}

public SaleSucessDTO(Seller seller, Long visited, Long deals) {
this.sellerName = seller.getName();
this.visited = visited;
this.deals = deals;
}

public String getSellerName() {
return sellerName;
}

public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}

public Long getVisited() {
return visited;
}

public void setVisited(Long visited) {
this.visited = visited;
}

public Long getDeals() {
return deals;
}

public void setDeals(Long deals) {
this.deals = deals;
}








}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.semanaspringreact.dsvendas.dto;

import java.io.Serializable;

import com.semanaspringreact.dsvendas.entities.Seller;

public class SaleSumDTO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
private String sellerName;
private Double sum;

public SaleSumDTO() {

}

public SaleSumDTO(Seller seller, Double sum) {
this.sellerName = seller.getName();
this.sum = sum;
}

public String getSellerName() {
return sellerName;
}

public void setSellerName(String sellerName) {
this.sellerName = sellerName;
}

public Double getSum() {
return sum;
}

public void setSum(Double sum) {
this.sum = sum;
}


}
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package com.semanaspringreact.dsvendas.repositories;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import com.semanaspringreact.dsvendas.dto.SaleSucessDTO;
import com.semanaspringreact.dsvendas.dto.SaleSumDTO;
import com.semanaspringreact.dsvendas.entities.Sale;

public interface SaleRepository extends JpaRepository<Sale, Long>{



@Query("SELECT new com.semanaspringreact.dsvendas.dto.SaleSumDTO(obj.seller, SUM(obj.amount))"
+ "FROM Sale AS obj GROUP BY obj.seller")
List<SaleSumDTO> amountGroupedBySeller();

@Query("SELECT new com.semanaspringreact.dsvendas.dto.SaleSucessDTO(obj.seller, SUM(obj.visited), SUM(obj.deals))"
+ "FROM Sale AS obj GROUP BY obj.seller")
List<SaleSucessDTO> sucessGroupedBySeller();
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package com.semanaspringreact.dsvendas.service;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.semanaspringreact.dsvendas.dto.SaleDTO;
import com.semanaspringreact.dsvendas.dto.SaleSucessDTO;
import com.semanaspringreact.dsvendas.dto.SaleSumDTO;
import com.semanaspringreact.dsvendas.entities.Sale;
import com.semanaspringreact.dsvendas.repositories.SaleRepository;
import com.semanaspringreact.dsvendas.repositories.SellerRepository;
Expand All @@ -27,5 +31,15 @@ public Page<SaleDTO> findAll(Pageable pageable){
Page<Sale> result = repository.findAll(pageable);
return result.map(x -> new SaleDTO(x));
}

@Transactional(readOnly = true)
public List<SaleSumDTO> amountGroupedBySeller(){
return repository.amountGroupedBySeller();
}

@Transactional(readOnly = true)
public List<SaleSucessDTO> sucessGroupedBySeller(){
return repository.sucessGroupedBySeller();
}

}

0 comments on commit 23c45be

Please sign in to comment.