Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DOMAIN / MODEL
- @Data
- @Entity
- @NoArgsConstructor
- @AllArgsConstructor
- @Builder
- @Table(name = "t_area_country")
- public class Country {
- @Id
- @GeneratedValue(generator = "UUID")
- @GenericGenerator(
- name = "UUID",
- strategy = "org.hibernate.id.UUIDGenerator")
- @Column(name = "id", updatable = false, nullable = false)
- @Type(type = "org.hibernate.type.PostgresUUIDType")
- private UUID id;
- private String code;
- private String name;
- private Boolean status;
- @JsonIgnore
- @CreationTimestamp
- private LocalDateTime createAt;
- @JsonIgnore
- @UpdateTimestamp
- private LocalDateTime modifiedAt;
- }
- //DTO
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- @Builder
- public class CountryDto {
- private UUID id;
- private String code;
- private String name;
- // repository
- public interface CountryRepo extends JpaRepository<Country, UUID> {
- Country findByName(String name);
- Country findByCode(String code);
- @Query(value = "SELECT * FROM t_area_country OFFSET ?1 LIMIT ?2", nativeQuery = true)
- List<Country> findAllPaging(Integer offset, Integer limit);
- @Query(value = "SELECT * FROM t_area_country WHERE LOWER(name) LIKE %?1% OFFSET ?2 LIMIT ?3", nativeQuery = true)
- List<Country> findByNamePaging(String name, Integer offset, Integer limit);
- @Query(value = "SELECT * FROM t_area_country WHERE LOWER(code) LIKE %?1% OFFSET ?2 LIMIT ?3", nativeQuery = true)
- List<Country> findByCodePaging(String code, Integer offset, Integer limit);
- @Query(value = "SELECT COUNT(*) FROM t_area_country WHERE LOWER(code) LIKE %?1%", nativeQuery = true)
- Integer countByCode(String code);
- @Query(value = "SELECT COUNT(*) FROM t_area_country WHERE LOWER(name) LIKE %?1%", nativeQuery = true)
- Integer countByName(String name);
- }
- //service interface
- public interface CountryService extends BaseService<Object, CountryDto, UUID> {
- Object findByCode(String code, String orderBy, Integer page, Integer limit);
- Object findByName(String name, String orderBy, Integer page, Integer limit);
- Object findAllPaging(String orderBy, Integer page, Integer limit);
- }
- //service implement
- @Service
- public class CountryServiceImpl implements CountryService {
- @Autowired
- private CountryRepo countryRepo;
- @Autowired
- private MessageResponseImpl message;
- @Autowired
- private CountrySerializer countrySerializer;
- @Autowired
- private Validation validation;
- @Autowired
- private HelperFunction helper;
- @Override
- public Object findAll() {
- List<CountryDto> countryDtoList = countrySerializer.toDto(countryRepo.findAll());
- if (countryDtoList.isEmpty()) {
- return message.response(D_EMPTY, countryDtoList, null, HttpStatus.OK);
- } else {
- return message.response(S_LOAD_DATA, countryDtoList, null, HttpStatus.OK);
- }
- }
- @Override
- public Object save(CountryDto countryDto) {
- if (countryDto.getName() == null || countryDto.getCode() == null) {
- return message.response(A_NOT_COMPLETE, null, null, HttpStatus.OK);
- } else {
- countryDto.setName(formatText(countryDto.getName()));
- countryDto.setStatus(true);
- if (!validation.validateNumeric(countryDto.getCode()))
- return message.response("Kode tidak diizinkan", null, null, HttpStatus.OK);
- if (!validation.validateAlphabet(countryDto.getName()))
- return message.response("Nama tidak di izinkan", null, null, HttpStatus.OK);
- if (countryRepo.findByCode(countryDto.getCode()) != null)
- return message.response(isExist(COUNTRY + " Code"), null, null, HttpStatus.OK);
- Country countryName = countryRepo.findByName(countryDto.getName());
- if (countryName == null) {
- return message.response(S_SAVE, countryRepo.save(countrySerializer.toEntity(countryDto)), null, HttpStatus.CREATED);
- } else {
- return message.response(isExist(COUNTRY + " Name"), null, null, HttpStatus.OK);
- }
- }
- }
- @Override
- public Object edit(CountryDto countryDto) {
- if (countryDto.getId() == null || countryDto.getName() == null || countryDto.getCode() == null) {
- return message.response(A_NOT_COMPLETE, null, null, HttpStatus.OK);
- } else {
- countryDto.setName(formatText(countryDto.getName()));
- Optional<Country> countryId = countryRepo.findById(countryDto.getId());
- if (countryId.isPresent()) {
- if (countryId.get().getName().equals(countryDto.getName())) {
- return message.response(S_UPDATE, countryRepo.save(countrySerializer.toEntity(countryDto)), null, HttpStatus.OK);
- } else {
- Country countryName = countryRepo.findByName(countryDto.getName());
- Country countryCode = countryRepo.findByCode(countryDto.getCode());
- if (countryName == null || countryCode == null) {
- Country country = countryRepo.save(countrySerializer.toEntity(countryDto));
- return message.response(S_UPDATE, countrySerializer.toDto(country), null, HttpStatus.CREATED);
- } else {
- return message.response(A_IS_EXIST, null, null, HttpStatus.OK);
- }
- }
- } else {
- return message.response(D_NOT_FOUND, null, null, HttpStatus.OK);
- }
- }
- }
- @Override
- public Object del(UUID id) {
- if (id == null)
- return message.response(A_NOT_COMPLETE, null, null, HttpStatus.OK);
- Optional<Country> country = countryRepo.findById(id);
- if (!country.isPresent())
- return message.response(D_NOT_FOUND, null, null, HttpStatus.OK);
- country.get().setStatus(false);
- countryRepo.save(country.get());
- return message.response(S_DELETE, true, null, HttpStatus.OK);
- }
- @Override
- public Object findById(UUID id) {
- if (id == null)
- return message.response(A_NOT_COMPLETE, null, null, HttpStatus.OK);
- Optional<Country> country = countryRepo.findById(id);
- if (country.isPresent()) {
- return message.response(S_LOAD_DATA, countrySerializer.toDto(country.get()), null, HttpStatus.OK);
- } else {
- return message.response(D_NOT_FOUND, null, null, HttpStatus.OK);
- }
- }
- @Override
- public Object findByCode(String code, String orderBy, Integer page, Integer limit) {
- if (code == null)
- return message.response(A_NOT_COMPLETE, null, null, HttpStatus.OK);
- List<Country> country = countryRepo.findByCodePaging(code, helper.setOffset(page, limit), limit);
- Integer count = countryRepo.countByCode(code);
- Object meta = message.meta("Code", orderBy, page, limit, count);
- if (country.isEmpty()) {
- return message.response(isEmpty(COUNTRY), new ArrayList<Country>(), meta, HttpStatus.OK);
- } else {
- return message.response(S_LOAD_DATA, sorting(country, orderBy), meta, HttpStatus.OK);
- }
- }
- @Override
- public Object findByName(String name, String orderBy, Integer page, Integer limit) {
- if (name == null)
- return message.response(A_NOT_COMPLETE, null, null, HttpStatus.OK);
- List<Country> country = countryRepo.findByNamePaging(name.toLowerCase(), helper.setOffset(page, limit), limit);
- Integer count = countryRepo.countByName(name.toLowerCase());
- Object meta = message.meta("Name", orderBy, page, limit, count);
- if (country.isEmpty()) {
- return message.response(isEmpty(COUNTRY), new ArrayList<Country>(), meta, HttpStatus.OK);
- } else {
- return message.response(S_LOAD_DATA, sorting(country, orderBy), meta, HttpStatus.OK);
- }
- }
- @Override
- public Object findAllPaging(String orderBy, Integer page, Integer limit) {
- List<Country> countries = countryRepo.findAllPaging(helper.setOffset(page, limit), limit);
- Long count = countryRepo.count();
- Object meta = message.meta(null, orderBy, page, limit, count.intValue());
- if (countries.isEmpty()) {
- return message.response(isEmpty(COUNTRY), new ArrayList<Country>(), meta, HttpStatus.OK);
- } else {
- return message.response(S_LOAD_DATA, sorting(countries, orderBy), meta, HttpStatus.OK);
- }
- }
- private List<CountryDto> sorting(List<Country> countries, String orderBy) {
- switch (orderBy) {
- case "id":
- countries.sort(Comparator.comparing(Country::getId));
- break;
- case "name":
- countries.sort(Comparator.comparing(Country::getName));
- break;
- case "code":
- countries.sort(Comparator.comparing(Country::getCode));
- break;
- case "status":
- countries.sort(Comparator.comparing(Country::getStatus));
- break;
- default:
- countries.sort(Comparator.comparing(Country::getId));
- break;
- }
- return countrySerializer.toDto(countries);
- }
- }
- //controller/delivery
- @RestController
- @RequestMapping(value = "/v1/master/country",
- consumes = MediaType.APPLICATION_JSON_VALUE,
- produces = MediaType.APPLICATION_JSON_VALUE)
- public class CountryController {
- @Autowired
- private CountryServiceImpl countryService;
- @GetMapping
- public Object find(
- @RequestParam(value = "orderBy", defaultValue = "name") String orderBy,
- @RequestParam(value = "page", defaultValue = "1") Integer page,
- @RequestParam(value = "limit", defaultValue = "10") Integer limit,
- @RequestParam(value = "name", required = false) String name,
- @RequestParam(value = "code", required = false) String code,
- @RequestParam(value = "show", required = false) String show,
- @RequestParam(value = "id", required = false) String id
- ) {
- if (code != null) return countryService.findByCode(code, orderBy, page, limit);
- if (name != null) return countryService.findByName(name, orderBy, page, limit);
- if (show != null && show.equals("all")) return countryService.findAll();
- if (id !=null) return countryService.findById(UUID.fromString(id));
- return countryService.findAllPaging(orderBy, page, limit);
- }
- @PostMapping
- public Object save(@RequestBody CountryDto countryDto) {
- return countryService.save(countryDto);
- }
- @PutMapping
- public Object edit(@RequestBody CountryDto countryDto) {
- return countryService.edit(countryDto);
- }
- @DeleteMapping(value = "/{id}")
- public Object del(@PathVariable("id") String id) {
- return countryService.del(UUID.fromString(id));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement