Esempio n. 1
0
        public void addRezervare(Rezervare r)
        {
            RezervareDTO        rezervareDto = DTOUtils.getDTO(r);
            AddRezervareRequest addRequest   = new AddRezervareRequest(rezervareDto);

            sendRequest(addRequest);
            IResponse response = readResponse();

            if (response is ErrorResponse)
            {
                ErrorResponse resp = (ErrorResponse)response;
                throw  new ServicesException(resp.Message);
            }
        }
        public virtual void addResult(Result result)
        {
            ResultDTO rdto = DTOUtils.getDTO(result);

            sendRequest(new AddResultRequest(rdto));

            Response response = readResponse();

            if (response is ErrorResponse)
            {
                ErrorResponse err = (ErrorResponse)response;
                throw new TriatlonException(err.Message);
            }
        }
        public virtual void resultAdded(Result result)
        {
            ResultDTO resultDto = DTOUtils.getDTO(result);

            Console.WriteLine("Result added  " + result);
            try
            {
                sendResponse(new NewResultResponse(resultDto));
            }
            catch (Exception e)
            {
                throw new TriatlonException("Sending error: " + e);
            }
        }
        public virtual void logout(User user, IObserver client)
        {
            UserDTO udto = DTOUtils.getDTO(user);

            sendRequest(new LogoutRequest(udto));

            Response response = readResponse();

            closeConnection();

            if (response is ErrorResponse)
            {
                ErrorResponse err = (ErrorResponse)response;
                throw new TriatlonException(err.Message);
            }
        }
 private void handleUpdate(UpdateResponse update)
 {
     if (update is NewResultResponse)
     {
         NewResultResponse resultRes = (NewResultResponse)update;
         Result            result    = DTOUtils.getFromDTO(resultRes.Result);
         try
         {
             client.resultAdded(result);
         }
         catch (TriatlonException e)
         {
             Console.WriteLine(e.StackTrace);
         }
     }
 }
Esempio n. 6
0
        public Loc[] GetLocuri(int idCursa)
        {
            CreateConnection();
            SendRequest(new GetLocuriRequest(idCursa));
            var response = ReadResponse();

            if (response is ErrorResponse)
            {
                var err = (ErrorResponse)response;
                throw new AppException(err.ErrorMessage);
            }

            var resp       = (GetLocuriResponse)response;
            var locuriDTO  = resp.Locuri;
            var dataToSend = DTOUtils.GetFromDTO(locuriDTO);

            return(dataToSend);
        }
Esempio n. 7
0
        public Destinatie[] GetAllDestinatii()
        {
            CreateConnection();
            SendRequest(new GetDestinatiiRequest());
            var response = ReadResponse();

            if (response is ErrorResponse)
            {
                var err = (ErrorResponse)response;
                throw new AppException(err.ErrorMessage);
            }

            var resp          = (GetDestinatiiResponse)response;
            var destinatiiDTO = resp.Destinatii;
            var destToSend    = DTOUtils.GetFromDTO(destinatiiDTO);

            return(destToSend);
        }
Esempio n. 8
0
        public Cursa[] GetAllCurse(Personal personal)
        {
            CreateConnection();
            SendRequest(new GetCurseRequest(DTOUtils.GetFromDTO(personal)));
            var response = ReadResponse();

            if (response is ErrorResponse)
            {
                var err = (ErrorResponse)response;
                throw new AppException(err.ErrorMessage);
            }

            var resp        = (GetCurseResponse)response;
            var curseDTO    = resp.Curse;
            var curseToSend = DTOUtils.GetFromDTO(curseDTO);

            return(curseToSend);
        }
Esempio n. 9
0
        public IEnumerable <Excursie> findAll()
        {
            GetAllExcursiiRequest getRequest = new GetAllExcursiiRequest();

            sendRequest(getRequest);
            IResponse response = readResponse();

            if (response is ErrorResponse)
            {
                ErrorResponse resp = (ErrorResponse)response;
                throw new ServicesException(resp.Message);
            }

            GetAllExcursiiResponse getResponse = (GetAllExcursiiResponse)response;
            IEnumerable <Excursie> excursii    = DTOUtils.getFromDTO(getResponse.Excursii).ToList();

            return(excursii);
        }
Esempio n. 10
0
        public Cursa FindCursa(DateTime data, Destinatie destinatie)
        {
            CreateConnection();
            SendRequest(new FindCursaRequest(data, DTOUtils.GetFromDTO(destinatie)));
            var response = ReadResponse();

            if (response is ErrorResponse)
            {
                var err = (ErrorResponse)response;
                throw new AppException(err.ErrorMessage);
            }

            var resp       = (FindCursaResponse)response;
            var cursa      = resp.Cursa;
            var dataToSend = DTOUtils.GetFromDTO(cursa);

            return(dataToSend);
        }
Esempio n. 11
0
        public IEnumerable <Excursie> findByDate(string destinatie, string begin, string end)
        {
            FilterDTO        fdto       = new FilterDTO(begin, end, destinatie);
            GetByDateRequest getRequest = new GetByDateRequest(fdto);

            sendRequest(getRequest);
            IResponse response = readResponse();

            if (response is ErrorResponse)
            {
                ErrorResponse resp = (ErrorResponse)response;
                throw new ServicesException(resp.Message);
            }

            GetByDateResponse      getResponse = (GetByDateResponse)response;
            IEnumerable <Excursie> excursii    = DTOUtils.getFromDTO(getResponse.Excursii).ToList();

            return(excursii);
        }
        public User findUserByUsername(User user)
        {
            UserDTO udto = DTOUtils.getDTO(user);

            //sendRequest(new LoginRequest(udto));

            sendRequest(new FindUserByUsernameRequest(udto));

            Response response = readResponse();

            if (response is ErrorResponse)
            {
                ErrorResponse err = (ErrorResponse)response;
                throw new TriatlonException(err.Message);
            }

            FindUserByUsernameResponse resp = (FindUserByUsernameResponse)response;
            User user1 = resp.User;

            return(user1);
        }
Esempio n. 13
0
        public virtual void Login(Personal personal, IObserver client)
        {
            CreateConnection();
            var personalDto = DTOUtils.GetFromDTO(personal);

            SendRequest(new LoginRequest(personalDto));
            var response = ReadResponse();


            if (response is OkResponse)
            {
                this._client = client;
                return;
            }


            if (response is ErrorResponse)
            {
                var error = (ErrorResponse)response;
                throw new AppException(error.ErrorMessage);
            }
        }
        public virtual User login(User user, IObserver client)
        {
            initializeConnection();
            UserDTO udto = DTOUtils.getDTO(user);

            sendRequest(new LoginRequest(udto));
            Response response = readResponse();

            if (response is OkResponse)
            {
                this.client = client;
                OkResponse resp  = (OkResponse)response;
                User       user1 = DTOUtils.getFromDTO(resp.User);
                return(user1);
            }
            if (response is ErrorResponse)
            {
                ErrorResponse err = (ErrorResponse)response;
                closeConnection();
                throw new TriatlonException(err.Message);
            }

            return(null);
        }
Esempio n. 15
0
        private IResponse HandleRequest(IRequest request)
        {
            IResponse response = null;

            if (request is LoginRequest)
            {
                Console.WriteLine("Login request...");
                var loginRequest = (LoginRequest)request;
                var personalDTO  = loginRequest.PersonalDto;
                var personal     = DTOUtils.GetFromDTO(personalDTO);

                try
                {
                    lock (_server)
                    {
                        _server.Login(personal, this);
                    }
                    return(new OkResponse());
                }
                catch (AppException e)
                {
                    _connected = false;
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetCurseRequest)
            {
                Console.WriteLine("Get curse request...");
                var req  = (GetCurseRequest)request;
                var pers = req.PersonalDto;
                try
                {
                    Cursa[] curse;
                    lock (_server)
                    {
                        curse = _server.GetAllCurse(DTOUtils.GetFromDTO(pers));
                    }

                    var curseDTO = DTOUtils.GetFromDTO(curse);
                    return(new GetCurseResponse(curseDTO));
                }
                catch (AppException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetDestinatiiRequest)
            {
                Console.WriteLine("Get destinatii request...");
                var req = (GetDestinatiiRequest)request;
                try
                {
                    Destinatie[] destinatii;
                    lock (_server)
                    {
                        destinatii = _server.GetAllDestinatii();
                    }

                    var destinatieDTO = DTOUtils.GetFromDTO(destinatii);
                    return(new GetDestinatiiResponse(destinatieDTO));
                }
                catch (AppException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetLocuriRequest)
            {
                Console.WriteLine("Get locuri request...");
                var req = (GetLocuriRequest)request;
                try
                {
                    Loc[] locuri;
                    lock (_server)
                    {
                        locuri = _server.GetLocuri(req.IdCursa);
                    }

                    var locuriDTO = DTOUtils.GetFromDTO(locuri);
                    return(new GetLocuriResponse(locuriDTO));
                }
                catch (AppException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is FindCursaRequest)
            {
                Console.WriteLine("Get cursa pt detalii request...");
                var req = (FindCursaRequest)request;
                try
                {
                    Cursa cursa = new Cursa();
                    lock (_server)
                    {
                        cursa = _server.FindCursa(req.Data, DTOUtils.GetFromDTO(req.Destinatie));
                    }

                    return(new FindCursaResponse(new CursaDTO(cursa.Id, cursa.DataPlecare, cursa.LocPlecare, cursa.NrLocuriDisponibile, cursa.IdDestinatie)));
                }
                catch (AppException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is AddRezervareRequest)
            {
                Console.WriteLine("adauga o rezervare request...");
                var req = (AddRezervareRequest)request;
                try
                {
                    lock (_server)
                    {
                        _server.AdaugaRezervare(req.AdaugaRezervareDto.IdCursa, req.AdaugaRezervareDto.Nume, req.AdaugaRezervareDto.Prenume, req.AdaugaRezervareDto.NrLocuri);
                    }

                    return(new OkResponse());
                }
                catch (AppException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            return(response);
        }
Esempio n. 16
0
        private IResponse HandleRequest(IRequest request)
        {
            List <Ride> rides;

            switch (request)
            {
            case LoginRequest _:
                Console.WriteLine("Login request ...");
                var loginRequest = (LoginRequest)request;
                var employeeDto  = loginRequest.EmployeeDto;
                var employee     = DTOUtils.GetFromDto(employeeDto);
                try
                {
                    bool response;
                    lock (_server)
                    {
                        response = _server.LogIn(employee, this);
                    }
                    if (response)
                    {
                        return(new OkResponse());
                    }
                    return(new ErrorResponse("Wrong Username/Password"));
                }
                catch (Exception e)
                {
                    _connected = false;
                    return(new ErrorResponse(e.Message));
                }

            case GetAllRidesRequest _:
                Console.WriteLine("Get All Trips request ...");
                try
                {
                    RideDTO[] response;
                    lock (_server)
                    {
                        response = _server.GetAllRides().Select(DTOUtils.GetRideDto).ToArray();
                    }
                    return(new GetRidesResponse(response));
                }
                catch (Exception e)
                {
                    return(new ErrorResponse(e.Message));
                }

            case GetAllBookingsRequest _:
                Console.WriteLine("Get All Bookings request ...");
                try
                {
                    BookingDTO[] response;
                    lock (_server)
                    {
                        response = _server.GetAllBookings().Select(DTOUtils.GetBookingDto).ToArray();
                    }
                    return(new GetBookingsResponse(response));
                }
                catch (Exception e)
                {
                    return(new ErrorResponse(e.Message));
                }

            case GetAllClientsRequest _:
                Console.WriteLine("Get All Bookings request ...");
                try
                {
                    ClientDTO[] response;
                    lock (_server)
                    {
                        response = _server.GetAllClients().Select(DTOUtils.GetClientDto).ToArray();
                    }
                    return(new GetClientsResponse(response));
                }
                catch (Exception e)
                {
                    return(new ErrorResponse(e.Message));
                }

            case GetCustomRidesRequest _:
                Console.WriteLine("Get custom rides...");
                var customRequest = (GetCustomRidesRequest)request;
                var customDto     = customRequest.CustomRideDto;
                try
                {
                    RideDTO[] response;
                    lock (_server)
                    {
                        RideDBRepository db = new RideDBRepository(DBUtils.GetProperties());
                        var rideService     = new RideService(db);
                        Console.WriteLine(rideService.FilterDestinationDateHour(customDto.Destination, customDto.Date.ToString("yyyy-MM-dd"),
                                                                                customDto.Hour.ToString(@"HH\:mm")));
                        response = _server.GetCustomRides(customDto.Destination, customDto.Date.ToString("yyyy-MM-dd"),
                                                          customDto.Hour.ToString(@"HH\:mm")).Select(DTOUtils.GetRideDto).ToArray();
                    }
                    return(new GetRidesResponse(response));
                }
                catch (Exception e)
                {
                    return(new ErrorResponse(e.Message));
                }

            case BookingRequest _:
                Console.WriteLine("Add booking...");
                var bookingRequest = (BookingRequest)request;
                var ride           = DTOUtils.GetFromDto(bookingRequest.RideDto);
                var booking        = DTOUtils.GetFromDto(bookingRequest.BookingDto);
                var client         = DTOUtils.GetFromDto(bookingRequest.ClientDto);
                employee = DTOUtils.GetFromDto(bookingRequest.EmployeeDto);
                try
                {
                    lock (_server)
                    {
                        rides = _server.AddBooking(ride, booking, employee, client);
                    }
                    return(new GetRidesResponse(rides.Select(DTOUtils.GetRideDto).ToArray()));
                }
                catch (Exception e)
                {
                    return(new ErrorResponse(e.Message));
                }

            case LogOutRequest _:
                Console.WriteLine("Log out ...");
                var logOutRequest = (LogOutRequest)request;
                employee = DTOUtils.GetFromDto(logOutRequest.EmployeeDto);
                try
                {
                    lock (_server)
                    {
                        _server.LogOut(employee);
                    }

                    return(new OkResponse());
                }
                catch (Exception e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }
            return(null);
        }
        private Response handleRequest(Request request)
        {
            Response response = null;

            if (request is LoginRequest)
            {
                Console.WriteLine("Login request ...");
                LoginRequest logReq = (LoginRequest)request;

                UserDTO udto = logReq.User;
                User    user = DTOUtils.getFromDTO(udto);

                User    loggedUser = null;
                UserDTO userDto    = null;
                try
                {
                    lock (server)
                    {
                        loggedUser = server.login(user, this);
                        userDto    = DTOUtils.getDTO(loggedUser);
                    }
                    return(new OkResponse(userDto));
                }
                catch (TriatlonException e)
                {
                    connected = false;
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is LogoutRequest)
            {
                Console.WriteLine("Logout request");
                LogoutRequest logReq = (LogoutRequest)request;

                UserDTO udto = logReq.User;
                User    user = DTOUtils.getFromDTO(udto);

                try
                {
                    lock (server)
                    {
                        server.logout(user, this);
                    }
                    connected = false;
                    return(new OkResponse());
                }
                catch (TriatlonException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is AddResultRequest)
            {
                Console.WriteLine("SendMessageRequest ...");
                AddResultRequest addReq = (AddResultRequest)request;

                ResultDTO resultDto = addReq.Result;
                Result    result    = DTOUtils.getFromDTO(resultDto);

                try
                {
                    lock (server)
                    {
                        server.addResult(result);
                    }
                    return(new OkResponse());
                }
                catch (TriatlonException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetParticipantsByScoreRequest)
            {
                Console.WriteLine("GetParticipantsByScore Request ...");
                GetParticipantsByScoreRequest getReq = (GetParticipantsByScoreRequest)request;

                int idStage = getReq.IdStage;

                try
                {
                    Participant[] participants;
                    lock (server)
                    {
                        participants = server.getParticipantsByScore(idStage);
                    }
                    // UserDTO[] frDTO =DTOUtils.getDTO(friends);
                    return(new GetParticipantsByScoreResponse(participants));
                }
                catch (TriatlonException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetParticipantsAndScoreRequest)
            {
                Console.WriteLine("GetParticipantsAndScore Request ...");
                GetParticipantsAndScoreRequest getReq = (GetParticipantsAndScoreRequest)request;

                try
                {
                    ParticipantDTO[] participants;
                    lock (server)
                    {
                        participants = server.getParticipantsAndScore();
                    }
                    // UserDTO[] frDTO =DTOUtils.getDTO(friends);
                    return(new GetParticipantsAndScoreResponse(participants));
                }
                catch (TriatlonException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is FindParticipantRequest)
            {
                Console.WriteLine("FindParticipant Request ...");
                FindParticipantRequest getReq = (FindParticipantRequest)request;

                int id = getReq.Id;

                try
                {
                    Participant participant;
                    lock (server)
                    {
                        participant = server.findParticipant(id);
                    }
                    return(new FindParticipantResponse(participant));
                }
                catch (TriatlonException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is FindUserRequest)
            {
                Console.WriteLine("FindUser Request ...");
                FindUserRequest getReq = (FindUserRequest)request;

                int id = getReq.Id;

                try
                {
                    User user;
                    lock (server)
                    {
                        user = server.findUser(id);
                    }
                    return(new FindUserResponse(user));
                }
                catch (TriatlonException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is FindUserByUsernameRequest)
            {
                Console.WriteLine("FindUser Request ...");
                FindUserByUsernameRequest getReq = (FindUserByUsernameRequest)request;

                UserDTO udto = getReq.User;
                User    user = DTOUtils.getFromDTO(udto);

                try
                {
                    User user1;
                    lock (server)
                    {
                        user1 = server.findUserByUsername(user);
                    }
                    return(new FindUserByUsernameResponse(user1));
                }
                catch (TriatlonException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            return(response);
        }
Esempio n. 18
0
        private IResponse handleRequest(IRequest request)
        {
            if (request is LoginRequest)
            {
                Console.WriteLine("login request;");
                LoginRequest loginRequest = (LoginRequest)request;
                AgentDTO     adto         = loginRequest.Agent;
                try
                {
                    bool check;
                    lock (server)
                    {
                        check = server.login(adto.User, adto.Password, this);
                    }
                    if (check == true)
                    {
                        return(new OkResponse());
                    }
                    else
                    {
                        return(new ErrorResponse("USER SAUY PAROLA GRESITE"));
                    }
                }
                catch (ServicesException e)
                {
                    connected = false;
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetAllExcursiiRequest)
            {
                Console.WriteLine("getAllExcursii request;");
                try
                {
                    Excursie[] excursii = null;
                    lock (server)
                    {
                        excursii = server.findAll().ToArray();
                    }

                    ExcursieDTO[] excursiiDTO = DTOUtils.getDTO(excursii);
                    return(new GetAllExcursiiResponse(excursiiDTO));
                }
                catch (ServicesException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is GetByDateRequest)
            {
                Console.WriteLine("getByDate request;");
                GetByDateRequest getRequest = (GetByDateRequest)request;
                FilterDTO        fdto       = getRequest.Filter;
                try
                {
                    Excursie[] excursii = null;
                    lock (server)
                    {
                        excursii = server.findByDate(fdto.Destination, fdto.Begin, fdto.End).ToArray();
                    }

                    ExcursieDTO[] excursiiDTO = DTOUtils.getDTO(excursii);
                    return(new GetByDateResponse(excursiiDTO));
                }
                catch (ServicesException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is AddRezervareRequest)
            {
                Console.WriteLine("addRezervare request;");
                AddRezervareRequest addRequest = (AddRezervareRequest)request;
                RezervareDTO        rdto       = addRequest.Rezervare;
                Rezervare           rezervare  = DTOUtils.getFromDTO(rdto);
                try
                {
                    lock (server)
                    {
                        server.addRezervare(rezervare);
                    }
                    return(new OkResponse());
                }
                catch (ServicesException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is LogoutRequest)
            {
                Console.WriteLine("logout request;");
                LogoutRequest logoutRequest = (LogoutRequest)request;
                AgentDTO      adto          = logoutRequest.Agent;
                try
                {
                    lock (server)
                    {
                        server.logout(adto.User, this);
                    }

                    connected = false;
                    return(new OkResponse());
                }
                catch (ServicesException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            return(null);
        }