Advertisement
Guest User

Country MVC

a guest
Dec 16th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.07 KB | None | 0 0
  1. // DOMAIN / MODEL
  2. @Data
  3. @Entity
  4. @NoArgsConstructor
  5. @AllArgsConstructor
  6. @Builder
  7. @Table(name = "t_area_country")
  8. public class Country {
  9.  
  10.     @Id
  11.     @GeneratedValue(generator = "UUID")
  12.     @GenericGenerator(
  13.             name = "UUID",
  14.             strategy = "org.hibernate.id.UUIDGenerator")
  15.     @Column(name = "id", updatable = false, nullable = false)
  16.     @Type(type = "org.hibernate.type.PostgresUUIDType")
  17.     private UUID id;
  18.  
  19.     private String code;
  20.     private String name;
  21.     private Boolean status;
  22.  
  23.     @JsonIgnore
  24.     @CreationTimestamp
  25.     private LocalDateTime createAt;
  26.  
  27.     @JsonIgnore
  28.     @UpdateTimestamp
  29.     private LocalDateTime modifiedAt;
  30. }
  31.  
  32. //DTO
  33. @Data
  34. @NoArgsConstructor
  35. @AllArgsConstructor
  36. @Builder
  37. public class CountryDto {
  38.  
  39.     private UUID id;
  40.     private String code;
  41.     private String name;
  42.  
  43.  
  44.  // repository
  45. public interface CountryRepo extends JpaRepository<Country, UUID> {
  46.  
  47.     Country findByName(String name);
  48.  
  49.     Country findByCode(String code);
  50.  
  51.     @Query(value = "SELECT * FROM t_area_country OFFSET ?1 LIMIT ?2", nativeQuery = true)
  52.     List<Country> findAllPaging(Integer offset, Integer limit);
  53.  
  54.     @Query(value = "SELECT * FROM t_area_country WHERE LOWER(name) LIKE %?1% OFFSET ?2 LIMIT ?3", nativeQuery = true)
  55.     List<Country> findByNamePaging(String name, Integer offset, Integer limit);
  56.  
  57.     @Query(value = "SELECT * FROM t_area_country WHERE LOWER(code) LIKE %?1% OFFSET ?2 LIMIT ?3", nativeQuery = true)
  58.     List<Country> findByCodePaging(String code, Integer offset, Integer limit);
  59.  
  60.     @Query(value = "SELECT COUNT(*) FROM t_area_country WHERE LOWER(code) LIKE %?1%", nativeQuery = true)
  61.     Integer countByCode(String code);
  62.  
  63.     @Query(value = "SELECT COUNT(*) FROM t_area_country WHERE LOWER(name) LIKE %?1%", nativeQuery = true)
  64.     Integer countByName(String name);
  65. }
  66.  
  67. //service interface
  68. public interface CountryService extends BaseService<Object, CountryDto, UUID> {
  69.  
  70.     Object findByCode(String code, String orderBy, Integer page, Integer limit);
  71.  
  72.     Object findByName(String name, String orderBy, Integer page, Integer limit);
  73.  
  74.     Object findAllPaging(String orderBy, Integer page, Integer limit);
  75. }
  76.  
  77. //service implement
  78. @Service
  79. public class CountryServiceImpl implements CountryService {
  80.  
  81.     @Autowired
  82.     private CountryRepo countryRepo;
  83.  
  84.     @Autowired
  85.     private MessageResponseImpl message;
  86.  
  87.     @Autowired
  88.     private CountrySerializer countrySerializer;
  89.  
  90.     @Autowired
  91.     private Validation validation;
  92.  
  93.     @Autowired
  94.     private HelperFunction helper;
  95.  
  96.     @Override
  97.     public Object findAll() {
  98.         List<CountryDto> countryDtoList = countrySerializer.toDto(countryRepo.findAll());
  99.         if (countryDtoList.isEmpty()) {
  100.             return message.response(D_EMPTY, countryDtoList, null, HttpStatus.OK);
  101.         } else {
  102.             return message.response(S_LOAD_DATA, countryDtoList, null, HttpStatus.OK);
  103.         }
  104.     }
  105.  
  106.     @Override
  107.     public Object save(CountryDto countryDto) {
  108.         if (countryDto.getName() == null || countryDto.getCode() == null) {
  109.             return message.response(A_NOT_COMPLETE, null, null, HttpStatus.OK);
  110.         } else {
  111.             countryDto.setName(formatText(countryDto.getName()));
  112.             countryDto.setStatus(true);
  113.             if (!validation.validateNumeric(countryDto.getCode()))
  114.                 return message.response("Kode tidak diizinkan", null, null, HttpStatus.OK);
  115.             if (!validation.validateAlphabet(countryDto.getName()))
  116.                 return message.response("Nama tidak di izinkan", null, null, HttpStatus.OK);
  117.             if (countryRepo.findByCode(countryDto.getCode()) != null)
  118.                 return message.response(isExist(COUNTRY + " Code"), null, null, HttpStatus.OK);
  119.  
  120.  
  121.             Country countryName = countryRepo.findByName(countryDto.getName());
  122.             if (countryName == null) {
  123.                 return message.response(S_SAVE, countryRepo.save(countrySerializer.toEntity(countryDto)), null, HttpStatus.CREATED);
  124.             } else {
  125.                 return message.response(isExist(COUNTRY + " Name"), null, null, HttpStatus.OK);
  126.             }
  127.         }
  128.     }
  129.  
  130.     @Override
  131.     public Object edit(CountryDto countryDto) {
  132.         if (countryDto.getId() == null || countryDto.getName() == null || countryDto.getCode() == null) {
  133.             return message.response(A_NOT_COMPLETE, null, null, HttpStatus.OK);
  134.         } else {
  135.             countryDto.setName(formatText(countryDto.getName()));
  136.             Optional<Country> countryId = countryRepo.findById(countryDto.getId());
  137.             if (countryId.isPresent()) {
  138.                 if (countryId.get().getName().equals(countryDto.getName())) {
  139.                     return message.response(S_UPDATE, countryRepo.save(countrySerializer.toEntity(countryDto)), null, HttpStatus.OK);
  140.                 } else {
  141.                     Country countryName = countryRepo.findByName(countryDto.getName());
  142.                     Country countryCode = countryRepo.findByCode(countryDto.getCode());
  143.                     if (countryName == null || countryCode == null) {
  144.                         Country country = countryRepo.save(countrySerializer.toEntity(countryDto));
  145.                         return message.response(S_UPDATE, countrySerializer.toDto(country), null, HttpStatus.CREATED);
  146.                     } else {
  147.                         return message.response(A_IS_EXIST, null, null, HttpStatus.OK);
  148.                     }
  149.                 }
  150.             } else {
  151.                 return message.response(D_NOT_FOUND, null, null, HttpStatus.OK);
  152.             }
  153.         }
  154.     }
  155.  
  156.     @Override
  157.     public Object del(UUID id) {
  158.         if (id == null)
  159.             return message.response(A_NOT_COMPLETE, null, null, HttpStatus.OK);
  160.  
  161.         Optional<Country> country = countryRepo.findById(id);
  162.         if (!country.isPresent())
  163.             return message.response(D_NOT_FOUND, null, null, HttpStatus.OK);
  164.  
  165.         country.get().setStatus(false);
  166.         countryRepo.save(country.get());
  167.         return message.response(S_DELETE, true, null, HttpStatus.OK);
  168.     }
  169.  
  170.     @Override
  171.     public Object findById(UUID id) {
  172.         if (id == null)
  173.             return message.response(A_NOT_COMPLETE, null, null, HttpStatus.OK);
  174.         Optional<Country> country = countryRepo.findById(id);
  175.         if (country.isPresent()) {
  176.             return message.response(S_LOAD_DATA, countrySerializer.toDto(country.get()), null, HttpStatus.OK);
  177.         } else {
  178.             return message.response(D_NOT_FOUND, null, null, HttpStatus.OK);
  179.         }
  180.     }
  181.  
  182.     @Override
  183.     public Object findByCode(String code, String orderBy, Integer page, Integer limit) {
  184.         if (code == null)
  185.             return message.response(A_NOT_COMPLETE, null, null, HttpStatus.OK);
  186.  
  187.         List<Country> country = countryRepo.findByCodePaging(code, helper.setOffset(page, limit), limit);
  188.         Integer count = countryRepo.countByCode(code);
  189.         Object meta = message.meta("Code", orderBy, page, limit, count);
  190.         if (country.isEmpty()) {
  191.             return message.response(isEmpty(COUNTRY), new ArrayList<Country>(), meta, HttpStatus.OK);
  192.         } else {
  193.             return message.response(S_LOAD_DATA, sorting(country, orderBy), meta, HttpStatus.OK);
  194.         }
  195.     }
  196.  
  197.     @Override
  198.     public Object findByName(String name, String orderBy, Integer page, Integer limit) {
  199.         if (name == null)
  200.             return message.response(A_NOT_COMPLETE, null, null, HttpStatus.OK);
  201.  
  202.         List<Country> country = countryRepo.findByNamePaging(name.toLowerCase(), helper.setOffset(page, limit), limit);
  203.         Integer count = countryRepo.countByName(name.toLowerCase());
  204.         Object meta = message.meta("Name", orderBy, page, limit, count);
  205.         if (country.isEmpty()) {
  206.             return message.response(isEmpty(COUNTRY), new ArrayList<Country>(), meta, HttpStatus.OK);
  207.         } else {
  208.             return message.response(S_LOAD_DATA, sorting(country, orderBy), meta, HttpStatus.OK);
  209.  
  210.         }
  211.     }
  212.  
  213.     @Override
  214.     public Object findAllPaging(String orderBy, Integer page, Integer limit) {
  215.         List<Country> countries = countryRepo.findAllPaging(helper.setOffset(page, limit), limit);
  216.         Long count = countryRepo.count();
  217.         Object meta = message.meta(null, orderBy, page, limit, count.intValue());
  218.         if (countries.isEmpty()) {
  219.             return message.response(isEmpty(COUNTRY), new ArrayList<Country>(), meta, HttpStatus.OK);
  220.         } else {
  221.             return message.response(S_LOAD_DATA, sorting(countries, orderBy), meta, HttpStatus.OK);
  222.         }
  223.     }
  224.  
  225.     private List<CountryDto> sorting(List<Country> countries, String orderBy) {
  226.         switch (orderBy) {
  227.             case "id":
  228.                 countries.sort(Comparator.comparing(Country::getId));
  229.                 break;
  230.             case "name":
  231.                 countries.sort(Comparator.comparing(Country::getName));
  232.                 break;
  233.             case "code":
  234.                 countries.sort(Comparator.comparing(Country::getCode));
  235.                 break;
  236.             case "status":
  237.                 countries.sort(Comparator.comparing(Country::getStatus));
  238.                 break;
  239.             default:
  240.                 countries.sort(Comparator.comparing(Country::getId));
  241.                 break;
  242.         }
  243.         return countrySerializer.toDto(countries);
  244.     }
  245. }
  246.  
  247.  
  248. //controller/delivery
  249. @RestController
  250. @RequestMapping(value = "/v1/master/country",
  251.         consumes = MediaType.APPLICATION_JSON_VALUE,
  252.         produces = MediaType.APPLICATION_JSON_VALUE)
  253. public class CountryController {
  254.  
  255.     @Autowired
  256.     private CountryServiceImpl countryService;
  257.  
  258.     @GetMapping
  259.     public Object find(
  260.             @RequestParam(value = "orderBy", defaultValue = "name") String orderBy,
  261.             @RequestParam(value = "page", defaultValue = "1") Integer page,
  262.             @RequestParam(value = "limit", defaultValue = "10") Integer limit,
  263.             @RequestParam(value = "name", required = false) String name,
  264.             @RequestParam(value = "code", required = false) String code,
  265.             @RequestParam(value = "show", required = false) String show,
  266.             @RequestParam(value = "id", required = false) String id
  267.     ) {
  268.         if (code != null) return countryService.findByCode(code, orderBy, page, limit);
  269.         if (name != null) return countryService.findByName(name, orderBy, page, limit);
  270.         if (show != null && show.equals("all")) return countryService.findAll();
  271.         if (id !=null) return countryService.findById(UUID.fromString(id));
  272.         return countryService.findAllPaging(orderBy, page, limit);
  273.     }
  274.  
  275.     @PostMapping
  276.     public Object save(@RequestBody CountryDto countryDto) {
  277.         return countryService.save(countryDto);
  278.     }
  279.  
  280.     @PutMapping
  281.     public Object edit(@RequestBody CountryDto countryDto) {
  282.         return countryService.edit(countryDto);
  283.     }
  284.  
  285.     @DeleteMapping(value = "/{id}")
  286.     public Object del(@PathVariable("id") String id) {
  287.         return countryService.del(UUID.fromString(id));
  288.     }
  289. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement