11package com .boylegu .springboot_vue .controller ;
22
3+ import com .boylegu .springboot_vue .entities .Persons ;
4+ import org .springframework .beans .factory .annotation .Autowired ;
35import org .springframework .web .bind .annotation .RequestMapping ;
46import org .springframework .web .bind .annotation .RequestMethod ;
7+ import org .springframework .web .bind .annotation .RequestParam ;
58import org .springframework .web .bind .annotation .RestController ;
9+ import org .springframework .beans .factory .annotation .Value ;
610import org .springframework .http .ResponseEntity ;
711import org .springframework .http .HttpStatus ;
8- import org .springframework .beans . factory . annotation . Autowired ;
12+ import org .springframework .http . MediaType ;
913import org .springframework .data .domain .Pageable ;
1014import org .springframework .data .domain .Sort ;
1115import org .springframework .data .domain .Sort .Direction ;
1216import org .springframework .data .domain .PageRequest ;
1317
1418import com .boylegu .springboot_vue .controller .pagination .PaginationMultiTypeValuesHelper ;
1519import com .boylegu .springboot_vue .dao .PersonsRepository ;
20+ import com .boylegu .springboot_vue .controller .pagination .PaginationFormatting ;
1621
1722import java .util .*;
1823
2126@ RequestMapping ("/api/persons" )
2227public class MainController {
2328
24- /**
25- * @api {GET} /api/persons/sex Get all sexList
26- * @apiName GetAllSexList
27- * @apiGroup Info Manage
28- * @apiVersion 1.0.0
29- * @apiExample {httpie} Example usage:
30- * <p>
31- * http /api/persons/sex
32- * @apiSuccess {String} label
33- * @apiSuccess {String} value
34- */
35-
3629 @ Autowired
3730 private PersonsRepository personsRepository ;
3831
32+ @ Value (("${com.boylegu.paginatio.max-per-page}" ))
33+ Integer maxPerPage ;
34+
3935 @ RequestMapping (value = "/sex" , method = RequestMethod .GET )
4036 public ResponseEntity <?> getSexAll () {
4137
38+ /*
39+ * @api {GET} /api/persons/sex Get all sexList
40+ * @apiName GetAllSexList
41+ * @apiGroup Info Manage
42+ * @apiVersion 1.0.0
43+ * @apiExample {httpie} Example usage:
44+ * <p>
45+ * http /api/persons/sex
46+ * @apiSuccess {String} label
47+ * @apiSuccess {String} value
48+ */
49+
4250 ArrayList <Map <String , String >> results = new ArrayList <>();
4351
4452 for (Object value : personsRepository .findSex ()) {
53+
4554 Map <String , String > sex = new HashMap <>();
4655
4756 sex .put ("label" , value .toString ());
57+
4858 sex .put ("value" , value .toString ());
59+
4960 results .add (sex );
5061 }
62+
5163 ResponseEntity <ArrayList <Map <String , String >>> responseEntity = new ResponseEntity <>(results ,
5264 HttpStatus .OK );
65+
5366 return responseEntity ;
5467 }
5568
56- @ RequestMapping (value = "/" , method = RequestMethod .GET )
57- public Map <String , PaginationMultiTypeValuesHelper > getPersonsAll () {
69+ @ RequestMapping (method = RequestMethod .GET , produces = MediaType .APPLICATION_JSON_VALUE )
70+ public Map <String , PaginationMultiTypeValuesHelper > getPersonsAll (
71+ @ RequestParam (value = "page" , required = false ) Integer pages ,
72+ @ RequestParam ("sex" ) String sex ,
73+ @ RequestParam ("email" ) String email
74+ ) {
75+
76+ /*
77+ * @api {GET} /api/persons Get all or a part of person info
78+ * @apiName GetAllInfoList
79+ * @apiGroup Info Manage
80+ * @apiVersion 1.0.0
81+ *
82+ * @apiExample {httpie} Example usage: (support combinatorial search)
83+ *
84+ * All person:
85+ * http /api/persons
86+ *
87+ * You can according to 'sex | email' or 'sex & email'
88+ * http /api/persons?sex=xxx&email=xx
89+ * http /api/persons?sex=xxx
90+ * http /api/persons?email=xx
91+ *
92+ * @apiParam {String} sex
93+ * @apiParam {String} email
94+ *
95+ * @apiSuccess {String} create_datetime
96+ * @apiSuccess {String} email
97+ * @apiSuccess {String} id
98+ * @apiSuccess {String} phone
99+ * @apiSuccess {String} sex
100+ * @apiSuccess {String} username
101+ * @apiSuccess {String} zone
102+ */
103+
104+ if (pages == null ) {
105+
106+ pages = 1 ;
58107
59- int page = 1 , size = 5 ;
60- Sort sort = new Sort (Direction .ASC , "id" );
61- Pageable pageable = new PageRequest (page , size , sort );
108+ }
62109
63- PaginationMultiTypeValuesHelper multiValue = new PaginationMultiTypeValuesHelper ();
64- Map <String , PaginationMultiTypeValuesHelper > results = new HashMap <>();
110+ Sort sort = new Sort (Direction .ASC , "id" );
65111
66- Integer count = personsRepository .findAll (pageable ).getSize ();
67- Integer page_number = personsRepository .findAll (pageable ).getNumber ();
112+ Pageable pageable = new PageRequest (pages - 1 , maxPerPage , sort );
68113
69- Object content = personsRepository .findAll (pageable ).getContent ();
70- Long total = personsRepository .count ();
71- multiValue .setCount (count );
72- multiValue .setPage (page_number );
73- multiValue .setResults (content );
74- multiValue .setTotal (total );
114+ PaginationFormatting paginInstance = new PaginationFormatting ();
75115
76- results .put ("data" , multiValue );
77- return results ;
116+ return paginInstance .filterQuery (sex , email , pageable );
78117 }
79118
80119}
0 commit comments