예제 #1
0
        public Child RegisterChild(string name, int age, string challenge1, string challenge2)
        {
            RegistrationDTO registration = new RegistrationDTO(name, age, challenge1, challenge2);

            sendRequest(new RegisterChildRequest(registration));
            ObjectResponseProtocol.Response response = readResponse();
            if (response is ObjectResponseProtocol.ErrorResponse)
            {
                ObjectResponseProtocol.ErrorResponse error = (ObjectResponseProtocol.ErrorResponse)response;
                throw new ValidationException(error.Message);
            }

            ObjectResponseProtocol.RegisteredChildResponse resp = (ObjectResponseProtocol.RegisteredChildResponse)response;
            return(resp.Child);
        }
 public RegisterChildRequest(RegistrationDTO registration)
 {
     Registration = registration;
 }
예제 #3
0
        private ObjectResponseProtocol.Response handleRequest(Request request)
        {
            ObjectResponseProtocol.Response response = null;
            if (request is LoginRequest)
            {
                LoginRequest loginRequest = (LoginRequest)request;
                Employee     employee     = loginRequest.Employee;
                try
                {
                    Employee loggedEmployee;
                    lock (server)
                    {
                        loggedEmployee = server.LoginEmployee(employee.Username, employee.Password, this);
                    }

                    return(new ObjectResponseProtocol.LoggedReponse(loggedEmployee));
                }
                catch (ValidationException e)
                {
                    connected = false;
                    return(new ObjectResponseProtocol.ErrorResponse(e.Message));
                }
            }

            if (request is LogoutRequest)
            {
                LogoutRequest logoutRequest = (LogoutRequest)request;
                Employee      employee      = logoutRequest.Employee;
                try
                {
                    lock (server)
                    {
                        server.Logout(employee, this);
                    }

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

            if (request is RegisterRequest)
            {
                RegisterRequest registerRequest = (RegisterRequest)request;
                Employee        employee        = registerRequest.Employee;

                try
                {
                    Employee registeredEmployee;
                    lock (server)
                    {
                        registeredEmployee = server.RegisterEmployee(employee.Username, employee.Password);
                    }

                    return(new ObjectResponseProtocol.RegisteredResponse(registeredEmployee));
                }
                catch (ValidationException e)
                {
                    return(new ObjectResponseProtocol.ErrorResponse(e.Message));
                }
            }

            if (request is GetAllChallengesRequest)
            {
                GetAllChallengesRequest getAllChallengesRequest = (GetAllChallengesRequest)request;

                try
                {
                    List <ChallengeDTO> challenges;
                    lock (server)
                    {
                        challenges = server.GetAllChallenges();
                    }

                    return(new ObjectResponseProtocol.GetAllChallengesResponse(challenges));
                }
                catch (ValidationException e)
                {
                    return(new ObjectResponseProtocol.ErrorResponse(e.Message));
                }
            }

            if (request is GetAllChildrenRequest)
            {
                GetAllChildrenRequest getAllChildrenRequest = (GetAllChildrenRequest)request;

                try
                {
                    List <ChildDTO> children;
                    lock (server)
                    {
                        children = server.GetAllChildren();
                    }

                    return(new ObjectResponseProtocol.GetAllChildrenResponse(children));
                }
                catch (ValidationException e)
                {
                    return(new ObjectResponseProtocol.ErrorResponse(e.Message));
                }
            }

            if (request is RegisterChildRequest)
            {
                RegisterChildRequest registerChildRequest = (RegisterChildRequest)request;
                RegistrationDTO      registration         = registerChildRequest.Registration;
                try
                {
                    Child child;
                    lock (server)
                    {
                        child = server.RegisterChild(registration.Name, registration.Age, registration.Challenge1,
                                                     registration.Challenge2);
                    }

                    return(new ObjectResponseProtocol.RegisteredChildResponse(child));
                }
                catch (ValidationException e)
                {
                    return(new ObjectResponseProtocol.ErrorResponse(e.Message));
                }
            }

            if (request is GetChallengeByPropertiesRequest)
            {
                GetChallengeByPropertiesRequest getChallengeByPropertiesRequest =
                    (GetChallengeByPropertiesRequest)request;
                Challenge challenge = getChallengeByPropertiesRequest.Challenge;

                try
                {
                    Challenge foundChallenge;
                    lock (server)
                    {
                        foundChallenge = server.GetChallengeByProperties(challenge.MinimumAge, challenge.MaximumAge,
                                                                         challenge.Name);
                    }

                    return(new ObjectResponseProtocol.GetChallengeByPropertiesResponse(foundChallenge));
                }
                catch (ValidationException e)
                {
                    return(new ObjectResponseProtocol.ErrorResponse(e.Message));
                }
            }

            if (request is GetChildrenByIdRequest)
            {
                GetChildrenByIdRequest getChildrenByIdRequest = (GetChildrenByIdRequest)request;
                long cid = getChildrenByIdRequest.Cid;
                try
                {
                    List <Child> children;
                    lock (server)
                    {
                        children = server.GetChildrenById(cid);
                    }

                    return(new ObjectResponseProtocol.GetChildrenByIdResponse(children));
                }
                catch (ValidationException e)
                {
                    return(new ObjectResponseProtocol.ErrorResponse(e.Message));
                }
            }

            return(response);
        }