public void returnBook(int userId, int bookId)
        {
            UserBookDTO userBookDto = new UserBookDTO(userId, bookId);

            sendRequest(new Request(RequestType.RETURN_BOOK, userBookDto));
            Response response = readResponse();

            if (response.Type == ResponseType.ERROR)
            {
                string err = (string)response.Data;
                throw new LibraryException(err);
            }
        }
Example #2
0
        public void returnBook(int userId, int bookId)
        {
            UserBookDTO userBookDto = new UserBookDTO(userId, bookId);

            sendRequest(new ReturnBookRequest(userBookDto));
            Response response = readResponse();

            if (response is ErrorResponse)
            {
                ErrorResponse err = (ErrorResponse)response;
                throw new LibraryException(err.Message);
            }
        }
Example #3
0
        private Response handleRequest(Request request)
        {
            Response response = null;

            if (request.Type == RequestType.LOGIN)
            {
                Console.WriteLine("Login request ...");
                UserDTO userDto = (UserDTO)request.Data;
                try
                {
                    User user = null;
                    lock (server)
                    {
                        user = server.login(userDto.UserName, userDto.Password, this);
                    }
                    return(new Response(ResponseType.LOGIN_SUCCESSFULLY, user));
                }
                catch (LibraryException e)
                {
                    connected = false;
                    return(new Response(ResponseType.ERROR, e.Message));
                }
            }

            if (request.Type == RequestType.LOGOUT)
            {
                Console.WriteLine("Logout request");
                int userId = (int)request.Data;
                try
                {
                    lock (server)
                    {
                        server.logout(userId, this);
                    }
                    connected = false;
                    return(new Response(ResponseType.LOGOUT_SUCCESSFULLY, null));
                }
                catch (LibraryException e)
                {
                    return(new Response(ResponseType.ERROR, e.Message));
                }
            }

            if (request.Type == RequestType.GET_AVAILABLE_BOOKS)
            {
                Console.WriteLine("SendMessageRequest ...");
                try
                {
                    List <Book> availableBooks = null;
                    lock (server)
                    {
                        availableBooks = server.getAvailableBooks();
                    }
                    return(new Response(ResponseType.GET_AVAILABLE_BOOKS, availableBooks));
                }
                catch (LibraryException e)
                {
                    return(new Response(ResponseType.ERROR, e.Message));
                }
            }

            if (request.Type == RequestType.GET_USER_BOOKS)
            {
                Console.WriteLine("GetLoggedFriends Request ...");
                int userId = (int)request.Data;
                try
                {
                    List <Book> userBooks = null;
                    lock (server)
                    {
                        userBooks = server.getUserBooks(userId);
                    }
                    return(new Response(ResponseType.GET_USER_BOOKS, userBooks));
                }
                catch (LibraryException e)
                {
                    return(new Response(ResponseType.ERROR, e.Message));
                }
            }

            if (request.Type == RequestType.SEARCH_BOOKS)
            {
                Console.WriteLine("GetLoggedFriends Request ...");
                String searchKey = (String)request.Data;
                try
                {
                    List <Book> foundBooks = null;
                    lock (server)
                    {
                        foundBooks = server.searchBooks(searchKey);
                    }
                    return(new Response(ResponseType.SEARCH_BOOKS, foundBooks));
                }
                catch (LibraryException e)
                {
                    return(new Response(ResponseType.ERROR, e.Message));
                }
            }

            if (request.Type == RequestType.BORROW_BOOK)
            {
                Console.WriteLine("GetLoggedFriends Request ...");
                UserBookDTO userBookDto = (UserBookDTO)request.Data;
                try
                {
                    lock (server)
                    {
                        server.borrowBook(userBookDto.UserId, userBookDto.BookId);
                    }
                    return(new Response(ResponseType.OK, null));
                }
                catch (LibraryException e)
                {
                    return(new Response(ResponseType.ERROR, e.Message));
                }
            }

            if (request.Type == RequestType.RETURN_BOOK)
            {
                Console.WriteLine("GetLoggedFriends Request ...");
                UserBookDTO userBookDto = (UserBookDTO)request.Data;
                try
                {
                    lock (server)
                    {
                        server.returnBook(userBookDto.UserId, userBookDto.BookId);
                    }
                    return(new Response(ResponseType.OK, null));
                }
                catch (LibraryException e)
                {
                    return(new Response(ResponseType.ERROR, e.Message));
                }
            }

            return(response);
        }
Example #4
0
 public BorrowBookRequest(UserBookDTO userBookDto)
 {
     this.userBookDto = userBookDto;
 }
Example #5
0
 public ReturnBookRequest(UserBookDTO userBookDto)
 {
     this.userBookDto = userBookDto;
 }
Example #6
0
        private Response handleRequest(Request request)
        {
            Response response = null;

            if (request is LoginRequest)
            {
                Console.WriteLine("Login request ...");
                LoginRequest loginRequest = (LoginRequest)request;
                UserDTO      userDto      = loginRequest.UserDto;
                try
                {
                    User user = null;
                    lock (server)
                    {
                        user = server.login(userDto.UserName, userDto.Password, this);
                    }
                    return(new UserLoggedInResponse(user));
                }
                catch (LibraryException e)
                {
                    connected = false;
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is LogoutRequest)
            {
                Console.WriteLine("Logout request");
                LogoutRequest logoutRequest = (LogoutRequest)request;
                int           userId        = logoutRequest.UserId;
                try
                {
                    lock (server)
                    {
                        server.logout(userId, this);
                    }
                    connected = false;
                    return(new OkResponse());
                }
                catch (LibraryException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetAvailableBooksRequest)
            {
                Console.WriteLine("SendMessageRequest ...");
                try
                {
                    List <Book> availableBooks = null;
                    lock (server)
                    {
                        availableBooks = server.getAvailableBooks();
                    }
                    return(new GetAvailableBooksResponse(availableBooks));
                }
                catch (LibraryException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetUserBooksRequest)
            {
                Console.WriteLine("GetLoggedFriends Request ...");
                GetUserBooksRequest getUserBooksRequest = (GetUserBooksRequest)request;
                int userId = getUserBooksRequest.UserId;
                try
                {
                    List <Book> userBooks = null;
                    lock (server)
                    {
                        userBooks = server.getUserBooks(userId);
                    }
                    return(new GetUserBooksResponse(userBooks));
                }
                catch (LibraryException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is SearchBooksRequest)
            {
                Console.WriteLine("GetLoggedFriends Request ...");
                SearchBooksRequest searchBooksRequest = (SearchBooksRequest)request;
                String             searchKey          = searchBooksRequest.SearchKey;
                try
                {
                    List <Book> foundBooks = null;
                    lock (server)
                    {
                        foundBooks = server.searchBooks(searchKey);
                    }
                    return(new SearchBooksResponse(foundBooks));
                }
                catch (LibraryException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is BorrowBookRequest)
            {
                Console.WriteLine("GetLoggedFriends Request ...");
                BorrowBookRequest borrowBookRequest = (BorrowBookRequest)request;
                UserBookDTO       userBookDto       = borrowBookRequest.UserBookDto;
                try
                {
                    lock (server)
                    {
                        server.borrowBook(userBookDto.UserId, userBookDto.BookId);
                    }
                    return(new OkResponse());
                }
                catch (LibraryException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is ReturnBookRequest)
            {
                Console.WriteLine("GetLoggedFriends Request ...");
                ReturnBookRequest returnBookRequest = (ReturnBookRequest)request;
                UserBookDTO       userBookDto       = returnBookRequest.UserBookDto;
                try
                {
                    lock (server)
                    {
                        server.returnBook(userBookDto.UserId, userBookDto.BookId);
                    }
                    return(new OkResponse());
                }
                catch (LibraryException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            return(response);
        }