package de.qaware.edu.cc.bookservice.server;

import de.qaware.edu.cc.bookservice.server.domain.Book;
import de.qaware.edu.cc.bookservice.server.domain.BookAlreadyExistsException;
import de.qaware.edu.cc.bookservice.server.domain.BookNotFoundException;
import de.qaware.edu.cc.bookservice.server.domain.BookRepository;
import de.qaware.edu.cc.generated.BookOuterClass;
import de.qaware.edu.cc.generated.BookServiceGrpc;
import io.grpc.Status;
import io.grpc.stub.StreamObserver;

/**
 * gRPC server implementation.
 */
class BookServiceImpl extends BookServiceGrpc.BookServiceImplBase {
    private final BookRepository bookRepository;

    BookServiceImpl(BookRepository bookRepository) {
        this.bookRepository = bookRepository;
    }

    @Override
    public void listBooks(BookOuterClass.ListBooksRequest request, StreamObserver<BookOuterClass.Book> responseObserver) {
        for (Book book : bookRepository.listAll()) {
            responseObserver.onNext(book.toProtobuf());
        }

        responseObserver.onCompleted();
    }

    @Override
    public void addBook(BookOuterClass.Book request, StreamObserver<BookOuterClass.Book> responseObserver) {
        Book book = Book.fromProtobuf(request);
        try {
            bookRepository.add(book);
            responseObserver.onNext(request);
        } catch (BookAlreadyExistsException e) {
            responseObserver.onError(Status.ALREADY_EXISTS.asException());
        }

        responseObserver.onCompleted();
    }

    @Override
    public void deleteBook(BookOuterClass.Isbn request, StreamObserver<BookOuterClass.Void> responseObserver) {
        try {
            bookRepository.delete(request.getValue());
            responseObserver.onNext(BookOuterClass.Void.getDefaultInstance());
        } catch (BookNotFoundException e) {
            responseObserver.onError(Status.NOT_FOUND.asException());
        }

        responseObserver.onCompleted();
    }

    @Override
    public void updateBook(BookOuterClass.UpdateBookRequest request, StreamObserver<BookOuterClass.Book> responseObserver) {
        Book newBook = Book.fromProtobuf(request.getNewBook());

        try {
            bookRepository.update(request.getIsbn(), newBook);
            responseObserver.onNext(request.getNewBook());
        } catch (BookNotFoundException e) {
            responseObserver.onError(Status.NOT_FOUND.asException());
        } catch (BookAlreadyExistsException e) {
            responseObserver.onError(Status.ALREADY_EXISTS.asException());
        }

        responseObserver.onCompleted();
    }
}

@@ -1301,32 +1301,76 @@
 nNext(request);%0A
+            responseObserver.onCompleted();%0A
         %7D catch 
@@ -1394,32 +1394,32 @@
 sException e) %7B%0A
-
             resp
@@ -1490,49 +1490,8 @@
    %7D
-%0A%0A        responseObserver.onCompleted();
 %0A   
@@ -1764,24 +1764,68 @@
 nstance());%0A
+            responseObserver.onCompleted();%0A
         %7D ca
@@ -1939,49 +1939,8 @@
    %7D
-%0A%0A        responseObserver.onCompleted();
 %0A   
@@ -2279,24 +2279,68 @@
 NewBook());%0A
+            responseObserver.onCompleted();%0A
         %7D ca
@@ -2557,32 +2557,32 @@
 asException());%0A
+
         %7D%0A%0A     
@@ -2578,49 +2578,8 @@
    %7D
-%0A%0A        responseObserver.onCompleted();
 %0A   

Fix onCompleted() and onError() calls