Advertisement
andersonalmada2

Untitled

Nov 29th, 2022
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. package server;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5.  
  6. import io.grpc.Status;
  7. import io.grpc.stub.StreamObserver;
  8. import proto.UserOuterClass.CreateUserRequest;
  9. import proto.UserOuterClass.CreateUserResponse;
  10. import proto.UserOuterClass.DeleteUserRequest;
  11. import proto.UserOuterClass.DeleteUserResponse;
  12. import proto.UserOuterClass.GetUserRequest;
  13. import proto.UserOuterClass.GetUserResponse;
  14. import proto.UserOuterClass.ListUserRequest;
  15. import proto.UserOuterClass.ListUserResponse;
  16. import proto.UserOuterClass.User;
  17. import proto.UserServiceGrpc;
  18.  
  19. public class Service extends UserServiceGrpc.UserServiceImplBase {
  20.  
  21.     private Map<Integer, User> userMap = new HashMap<Integer, User>();
  22.     private int i = 1;
  23.  
  24.     @Override
  25.     public void createUser(CreateUserRequest request, StreamObserver<CreateUserResponse> responseObserver) {
  26.         System.out.println("Creating User");
  27.         User user = request.getUser();
  28.  
  29.         user = user.toBuilder().setId(i).setName(request.getUser().getName()).setEmail(request.getUser().getEmail())
  30.                 .build();
  31.  
  32.         CreateUserResponse response = CreateUserResponse.newBuilder().setUser(user).build();
  33.         responseObserver.onNext(response);
  34.         responseObserver.onCompleted();
  35.  
  36.         userMap.put(i, user);
  37.         i++;
  38.     }
  39.  
  40.     @Override
  41.     public void deleteUser(DeleteUserRequest request, StreamObserver<DeleteUserResponse> responseObserver) {
  42.         if (userMap.containsKey(request.getUserId())) {
  43.             userMap.remove(request.getUserId());
  44.             System.out.println("User was deleted");
  45.             responseObserver.onNext(DeleteUserResponse.newBuilder().setUserId(request.getUserId()).build());
  46.             responseObserver.onCompleted();
  47.         } else {
  48.             System.out.println("User not found");
  49.             responseObserver.onError(Status.NOT_FOUND.asRuntimeException());
  50.         }
  51.     }
  52.  
  53.     @Override
  54.     public void getUser(GetUserRequest request, StreamObserver<GetUserResponse> responseObserver) {
  55.         System.out.println("Get user");
  56.         if (userMap.containsKey(request.getUserId())) {
  57.             User user = userMap.get(request.getUserId());
  58.             GetUserResponse response = GetUserResponse.newBuilder().setUser(user).build();
  59.             responseObserver.onNext(response);
  60.             responseObserver.onCompleted();
  61.         } else {
  62.             System.out.println("User not found");
  63.             responseObserver.onError(Status.NOT_FOUND.asRuntimeException());
  64.         }
  65.  
  66.     }
  67.  
  68.     @Override
  69.     public void listUser(ListUserRequest request, StreamObserver<ListUserResponse> responseObserver) {
  70.         System.out.println("List users");
  71.         ListUserResponse response = ListUserResponse.newBuilder().addAllUser(userMap.values()).build();
  72.         responseObserver.onNext(response);
  73.         responseObserver.onCompleted();
  74.     }
  75. }
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement