Advertisement
dawciobiel

rscp-usage-service

Aug 22nd, 2022
1,936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.19 KB | Source Code | 0 0
  1. @Service
  2. @Primary
  3. class ReviewServiceImpl implements ReviewService {
  4.  
  5.  
  6.     /* Example: Find wallets by name pattern -> wallets list */
  7.     public Page<RscpDTO> getWalletsByNameForUser__example_1(long userId, String patternName) {
  8.  
  9.         if (!userService.checkUserExists(userId)) {
  10.             logger.error("User not found", userId);
  11.             throw new UserNotFoundException(userId);
  12.             /* Nie znaleziono użtkownika, więc stworzono wyjątek. Dalsze wykonywanie tej
  13.             metody jest przerywane, a wyjątek jest zwracany do kontrolera.
  14.             W kontrolerze musi być blok try-catch, poprawna idetyfikacja wyjąku i jego konkretna obsługa.
  15.              */
  16.         }
  17.  
  18.         List<Wallet> list = walletRepository.findWalletsByName(patternName);
  19.  
  20.         // Create Response Service to Controller Protocol DTO (RscpDTO))
  21.         RscpStatus      status      = new RscpStatus.OK;
  22.         String          message     = String.format("Wallets list containing %s in name", patternName);
  23.         List<Wallet>    body        = list;
  24.  
  25.         return new RscpDTO<Wallet>(status, message, body);
  26.         /* Zwracany jest obiekt DTO który zawiera status, opis rezultatu oraz listę obiektów typu wallet */
  27.     }
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.     /* Example: Find wallets by name pattern -> empty list */
  36.     public Page<RscpDTO> getWalletsByNameForUser__example_1(long userId, String patternName) {
  37.  
  38.         if (!userService.checkUserExists(userId)) {
  39.             // ...
  40.         }
  41.  
  42.         List<Wallet> list = walletRepository.findWalletsByName(patternName);
  43.         if (list.size() == 0) {
  44.             // Create Response Service to Controller Protocol DTO (RscpDTO))
  45.             RscpStatus status = new RscpStatus.NO_CONTENT; // NO_CONTENT(204, Series.SUCCESSFUL, "No Content"),
  46.             String message = String.format("Wallets list containing no wallets by %s name patten",
  47.                     patternName);
  48.             List<Wallet> body = list;
  49.  
  50.             return new RscpDTO<Wallet>(status, message, body);
  51.             /* Zwracany jest obiekt DTO który zawiera status, opis rezultatu oraz listę obiektów typu wallet */
  52.         } else{
  53.             // ...
  54.         }
  55.     }
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.     /* Example: User by Id not found */
  67.     public Page<RscpDTO> getWalletsByNameForUser__example_2(long userId, String patternName) {
  68.  
  69.         if (!userService.checkUserExists(userId)) {
  70.             logger.error("User not found", userId)
  71.  
  72.             // Create Response Service to Controller Protocol DTO
  73.             RscpStatus      status      = new RscpStatus.UNAUTHORIZED;  // UNAUTHORIZED(401, Series.CLIENT_ERROR, "Unauthorized")
  74.             String          message     = String.format("User unauthorized %s", userId);
  75.             List<Wallet>    body        = Collections.emptyList();
  76.  
  77.             return new RscpDTO<T>(status, message, body);
  78.             /* Zaleta jest taka, że nie przerzucamy między warstwami aplikacji wyjątku
  79.             tylko zwykły obiekt DTO który zawiera informacje o błędzie.
  80.              W kontrolerze w ogóle bloku try-catch nie ma, bo wyjątki UserNotFoundException nie istnieją. */
  81.  
  82.         }
  83.  
  84.         // ...
  85.  
  86.         return dto;
  87.     }
  88.  
  89.  
  90.  
  91.  
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement