Ejemplo n.º 1
0
        public void addContestant(string name, int engine, string team, string username)
        {
            Contestant      c               = new Contestant(name, team, engine);
            Race            r               = new Race(engine);
            ContestantDTO   contestantDTO   = new ContestantDTO(name, team, engine);
            RaceDTO         raceDTO         = new RaceDTO(engine);
            RegistrationDTO registrationDTO = new RegistrationDTO(contestantDTO, raceDTO);

            sendRequest(new SaveRegistrationRequest(registrationDTO));
            Response response = readResponse();

            if (response is ErrorResponse)
            {
                ErrorResponse error = (ErrorResponse)response;
                throw new RaceException(error.Message);
            }
        }
        private Response handleRequest(Request request)
        {
            Console.WriteLine("Handle request pentru..." + request);
            Response response = null;

            if (request is LoginRequest)
            {
                Console.WriteLine("Login request...");
                LoginRequest    loginRequest = (LoginRequest)request;
                OfficePersonDTO userDTO      = loginRequest.OfficePersonDTO();
                OfficePers      pers         = DTOUtils.getFromDTO(userDTO);
                try
                {
                    lock (server)
                    {
                        server.login(pers, this);
                        username = pers.Username;
                    }
                    return(new OKResponse());
                }catch (RaceException ex)
                {
                    connected = false;
                    return(new ErrorResponse(ex.Message));
                }
            }

            if (request is LogoutRequest)
            {
                Console.WriteLine("Logout request...");
                LogoutRequest   logReq = (LogoutRequest)request;
                OfficePersonDTO dTO    = logReq.OfficePersonDTO();
                OfficePers      user   = DTOUtils.getFromDTO(dTO);
                try
                {
                    lock (server)
                    {
                        server.logout(user, this);
                    }
                    connected = false;
                    return(new OKResponse());
                }catch (RaceException ex)
                {
                    return(new ErrorResponse(ex.Message));
                }
            }

            if (request is GetRacesRequest)
            {
                Console.WriteLine("Get all races request");
                GetRacesRequest req = (GetRacesRequest)request;
                try
                {
                    IList <Race> races = new List <Race>();
                    lock (server)
                    {
                        races = server.getAllRaces();
                    }
                    IList <RaceDTO> raceDTOs = new List <RaceDTO>();
                    foreach (Race r in races)
                    {
                        RaceDTO raceDTO = DTOUtils.getDTO(r);
                        raceDTOs.Add(raceDTO);
                    }
                    return(new GetRacesResponse((List <RaceDTO>)raceDTOs));
                }catch (Exception ex)
                {
                    throw new RaceException(ex.Message);
                }
            }

            if (request is GetContestantsRequest)
            {
                GetContestantsRequest req = (GetContestantsRequest)request;
                try
                {
                    IList <Contestant> contestants = new List <Contestant>();
                    lock (server)
                    {
                        contestants = server.getAllContestants();
                    }
                    IList <ContestantDTO> contestantDTOs = new List <ContestantDTO>();
                    foreach (Contestant c in contestants)
                    {
                        ContestantDTO dTO = DTOUtils.getDTO(c);
                        contestantDTOs.Add(dTO);
                    }
                    return(new GetContestantsResponse((List <ContestantDTO>)contestantDTOs));
                }catch (Exception e)
                {
                    throw new RaceException(e.Message);
                }
            }

            if (request is SaveRegistrationRequest)
            {
                Console.WriteLine("Saving registration...");
                SaveRegistrationRequest req             = (SaveRegistrationRequest)request;
                RegistrationDTO         registrationDTO = req.RegistrationDTO;
                try
                {
                    lock (server)
                    {
                        server.addContestant(registrationDTO.Contestant.Name, registrationDTO.Contestant.Engine, registrationDTO.Contestant.Team, username);
                    }
                    return(new OKResponse());
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }
            }
            return(response);
        }