11package me .agno .demo .controllers ;
22
33import java .math .BigDecimal ;
4+ import java .math .MathContext ;
45import java .time .Instant ;
56import java .util .function .Consumer ;
67
7- import com .fasterxml .jackson .core .JsonProcessingException ;
88import jakarta .persistence .EntityManager ;
99import jakarta .persistence .EntityManagerFactory ;
1010import jakarta .servlet .http .HttpServletRequest ;
11- import jakarta .servlet .http .HttpServletResponse ;
12- import me .agno .demo .DemoApplication ;
1311import me .agno .demo .model .Order ;
1412import me .agno .gridcore .IGridColumnCollection ;
1513import me .agno .gridcore .server .GridServer ;
1614import me .agno .gridcore .server .IGridServer ;
1715import me .agno .gridcore .utils .ItemsDTO ;
1816import org .springframework .beans .factory .annotation .Autowired ;
19- import org .springframework .http .HttpStatus ;
2017import org .springframework .http .MediaType ;
2118import org .springframework .http .ResponseEntity ;
2219import org .springframework .web .bind .annotation .GetMapping ;
2320import org .springframework .web .bind .annotation .RequestMapping ;
2421import org .springframework .web .bind .annotation .RestController ;
2522
2623@ RestController
27- @ RequestMapping (value = "/api/sampledata" )
24+ @ RequestMapping (value = { "/api/sampledata" , "/api/SampleData" } )
2825public class SampleDataController {
2926
3027 @ Autowired
3128 EntityManagerFactory entityManagerFactory ;
3229
33- @ GetMapping (value = "getordersgrid" , produces = MediaType .APPLICATION_JSON_VALUE )
34- public ResponseEntity <ItemsDTO <Order >> getUser (HttpServletRequest request ) throws JsonProcessingException {
30+ @ GetMapping (value = { "getordersgrid" , "GetOrdersGrid" } , produces = MediaType .APPLICATION_JSON_VALUE )
31+ public ResponseEntity <ItemsDTO <Order >> getOrdersGrid (HttpServletRequest request ) {
3532
3633 EntityManager em = entityManagerFactory .createEntityManager ();
3734
3835 Consumer <IGridColumnCollection <Order >> columns = c ->
3936 {
4037 c .add ("orderID" ,Integer .class );
4138 c .add ("orderDate" , Instant .class );
42- // c.add("customer.companyName", String.class);
43- // c.add("customer.contactName", String.class);
44- // c.add("customer.country", String.class, true);
39+ c .add ("customer.companyName" , String .class );
40+ c .add ("customer.contactName" , String .class );
41+ c .add ("customer.country" , String .class , true );
4542 c .add ("freight" , BigDecimal .class );
46- // c.add("customer.isVip",Boolean.class);
43+ c .add ("customer.isVip" ,Boolean .class );
4744 };
4845
4946 IGridServer <Order > server = new GridServer <Order >(em , Order .class , null , null ,
@@ -54,7 +51,41 @@ public ResponseEntity<ItemsDTO<Order>> getUser(HttpServletRequest request) throw
5451
5552 var items = server .getItemsToDisplay ();
5653 return ResponseEntity .ok (items );
57- //var result = DemoApplication.mapper.writeValueAsString(items);
58- //return items;
5954 }
60- }
55+
56+ @ GetMapping (value = {"getordersgridwithtotals" , "GetOrdersGridWithTotals" }, produces = MediaType .APPLICATION_JSON_VALUE )
57+ public ResponseEntity <ItemsDTO <Order >> getOrdersGridWithTotals (HttpServletRequest request ) {
58+
59+ EntityManager em = entityManagerFactory .createEntityManager ();
60+
61+ Consumer <IGridColumnCollection <Order >> columns = c ->
62+ {
63+ c .add ("orderID" ,Integer .class );
64+ c .add ("orderDate" , Instant .class ).max (true ).min (true );
65+ c .add ("customer.companyName" , String .class ).max (true ).min (true );
66+ c .add ("customer.contactName" , String .class ).max (true ).min (true );
67+ c .add ("freight" , BigDecimal .class ).sum (true ).average (true ).max (true ).min (true )
68+ .calculate ("Average 2" , x -> x .getGrid ().getItemsCount () == 0
69+ || x .get ("freight" ) == null || x .get ("freight" ).getSumValue ().getNumber ().isEmpty ()
70+ ? "" : x .get ("freight" ).getSumValue ().getNumber ().get ()
71+ .divide (BigDecimal .valueOf (x .getGrid ().getItemsCount ()), MathContext .DECIMAL32 ))
72+ .calculate ("Average 3" , x -> x .get ("orderID" ) == null || x .get ("orderID" ).getSumValue () == null
73+ || x .get ("orderID" ).getSumValue ().getNumber ().isEmpty ()
74+ || x .get ("orderID" ).getSumValue ().getNumber ().get () == BigDecimal .ZERO
75+ || x .get ("freight" ) == null || x .get ("freight" ).getSumValue () == null
76+ || x .get ("freight" ).getSumValue ().getNumber ().isEmpty ()
77+ ? "" : x .get ("freight" ).getSumValue ().getNumber ().get ()
78+ .divide (x .get ("orderID" ).getSumValue ().getNumber ().get (),MathContext .DECIMAL32 ));
79+ c .add ("customer.isVip" ,Boolean .class );
80+ };
81+
82+ IGridServer <Order > server = new GridServer <Order >(em , Order .class , null , null ,
83+ request .getParameterMap (), columns )
84+ .withPaging (10 )
85+ .sortable ()
86+ .filterable ();
87+
88+ var items = server .getItemsToDisplay ();
89+ return ResponseEntity .ok (items );
90+ }
91+ }
0 commit comments