Beispiel #1
0
        public Voluntar findBy(int id, string parola)
        {
            Console.WriteLine("VoluntarDB [findBy]");
            var con = DBUtils.getConnection();

            using (var comm = con.CreateCommand())
            {
                comm.CommandText = "select nume,email from voluntari where id=@id and parola=@pass";
                var paramId = comm.CreateParameter();
                paramId.ParameterName = "@id";
                paramId.Value         = id;
                comm.Parameters.Add(paramId);
                var paramPass = comm.CreateParameter();
                paramPass.ParameterName = "@pass";
                paramPass.Value         = parola;
                comm.Parameters.Add(paramPass);
                using (var dataR = comm.ExecuteReader())
                {
                    if (dataR.Read())
                    {
                        Voluntar voluntar = new Voluntar(id);
                        voluntar.Nume  = dataR.GetString(0);
                        voluntar.Email = dataR.GetString(1);
                        return(voluntar);
                    }
                }
            }

            return(null);
        }
Beispiel #2
0
        public Voluntar findOne(int id)
        {
            log.InfoFormat("Entering findOne with value {0}", id);
            IDbConnection con = DBUtils.getConnection();

            using (var comm = con.CreateCommand())
            {
                comm.CommandText = "select id,nume, email,parola from voluntari where id=@id";
                IDbDataParameter paramId = comm.CreateParameter();
                paramId.ParameterName = "@id";
                paramId.Value         = id;
                comm.Parameters.Add(paramId);

                using (var dataR = comm.ExecuteReader())
                {
                    if (dataR.Read())
                    {
                        int    idV    = dataR.GetInt32(0);
                        String nume   = dataR.GetString(1);
                        String email  = dataR.GetString(2);
                        String parola = dataR.GetString(3);

                        Voluntar vol = new Voluntar(idV, nume, email, parola);
                        log.InfoFormat("Exiting findOne with value {0}", vol);
                        return(vol);
                    }
                }
            }
            log.InfoFormat("Exiting findOne with value {0}", null);
            return(null);
        }
Beispiel #3
0
        public static VoluntarDTO getDTO(Voluntar voluntar)
        {
            int    id   = voluntar.Id;
            string pass = voluntar.Parola;

            return(new VoluntarDTO(id, pass));
        }
Beispiel #4
0
        public IEnumerable <Voluntar> findAll()
        {
            log.Info("Entering findAll - Voluntari");
            IDbConnection    con       = DBUtils.getConnection();
            IList <Voluntar> voluntari = new List <Voluntar>();

            using (var comm = con.CreateCommand())
            {
                comm.CommandText = "select id,nume,email, parola from voluntari";

                using (var dataR = comm.ExecuteReader())
                {
                    while (dataR.Read())
                    {
                        int    id     = dataR.GetInt32(0);
                        String nume   = dataR.GetString(1);
                        String email  = dataR.GetString(2);
                        String parola = dataR.GetString(3);

                        Voluntar voluntar = new Voluntar(id, nume, email, parola);
                        voluntari.Add(voluntar);
                    }
                }
            }
            log.Info("Exiting findAll - Voluntari");
            return(voluntari);
        }
Beispiel #5
0
        public void login(String userId, String pass)
        {
            Voluntar user = new Voluntar(Int32.Parse(userId), pass);

            server.login(user, this);
            Console.WriteLine("Login succeeded ....");
            currentUser = user;
            Console.WriteLine("Current user {0}", user);
        }
Beispiel #6
0
 public static Voluntar[] getFromDTO(VoluntarDTO[] voluntari)
 {
     Voluntar[] allVol = new Voluntar[voluntari.Length];
     for (int i = 0; i < voluntari.Length; i++)
     {
         allVol[i] = getFromDTO(voluntari[i]);
     }
     return(allVol);
 }
        public void logout(Voluntar voluntar, IObserver client)
        {
            IObserver localClient = loggedClients[voluntar.Id];

            if (localClient == null)
            {
                throw new MyException("Voluntar " + voluntar.Id + " is not logged in.");
            }
            loggedClients.Remove(voluntar.Id);
        }
Beispiel #8
0
        public virtual void logout(Voluntar voluntar, IObserver client)
        {
            VoluntarDTO udto = DTOUtils.getDTO(voluntar);

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

            closeConnection();
            if (response is ErrorResponse)
            {
                ErrorResponse err = (ErrorResponse)response;
                throw new MyException(err.Message);
            }
        }
        public void login(Voluntar voluntar, IObserver client)
        {
            Voluntar voluntarOk = voluntarRepo.findBy(voluntar.Id, voluntar.Parola);

            if (voluntarOk != null)
            {
                if (loggedClients.ContainsKey(voluntar.Id))
                {
                    throw new MyException("Voluntar already logged in.");
                }
                loggedClients[voluntar.Id] = client;
            }
            else
            {
                throw new MyException("Authentication failed.");
            }
        }
Beispiel #10
0
        public virtual void login(Voluntar voluntar, IObserver client)
        {
            initializeConnection();
            VoluntarDTO vdto = DTOUtils.getDTO(voluntar);

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

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

                return;
            }
            if (response is ErrorResponse)
            {
                ErrorResponse err = (ErrorResponse)response;
                closeConnection();
                throw new MyException(err.Message);
            }
        }
Beispiel #11
0
        public void save(Voluntar entity)
        {
            log.InfoFormat("Entering save with entity {0}", entity);
            var con = DBUtils.getConnection();

            using (var comm = con.CreateCommand())
            {
                comm.CommandText = "insert into voluntari values (@id, @nume, @email, @parola)";
                var paramId = comm.CreateParameter();
                paramId.ParameterName = "@id";
                paramId.Value         = entity.Id;
                comm.Parameters.Add(paramId);

                var paramNume = comm.CreateParameter();
                paramNume.ParameterName = "@nume";
                paramNume.Value         = entity.Nume;
                comm.Parameters.Add(paramNume);

                var paramEmail = comm.CreateParameter();
                paramEmail.ParameterName = "@email";
                paramEmail.Value         = entity.Email;
                comm.Parameters.Add(paramEmail);

                var paramParola = comm.CreateParameter();
                paramParola.ParameterName = "@parola";
                paramParola.Value         = entity.Parola;
                comm.Parameters.Add(paramParola);

                var result = comm.ExecuteNonQuery();
                if (result == 0)
                {
                    throw new RepositoryException("No voluntar added !");
                }
            }
            log.Info("Exiting save - Voluntar");
        }
Beispiel #12
0
 public void update(Voluntar old, Voluntar entity)
 {
     throw new NotImplementedException();
 }
Beispiel #13
0
        private Response handleRequest(Request request)
        {
            Response response = null;

            if (request is GetCazuriRequest)
            {
                Console.WriteLine("GetCazuriRequest.....");
                GetCazuriRequest getReq = (GetCazuriRequest)request;
                try
                {
                    CazCaritabil[] allCazuri;
                    lock (server)
                    {
                        allCazuri = server.getCazuri();
                    }

                    CazCaritabilDTO[] frDTO = DTOUtils.getDTO(allCazuri);
                    return(new GetCazuriResponse(frDTO));
                }
                catch (MyException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is GetDonatiiRequest)
            {
                Console.WriteLine("GetDonatiiRequest.....");
                GetDonatiiRequest getReq = (GetDonatiiRequest)request;
                try
                {
                    Donatie[] allDonatii;
                    lock (server)
                    {
                        allDonatii = server.getDonatii();
                    }

                    DonatieDTO[] frDTO = DTOUtils.getDTO(allDonatii);
                    return(new GetDonatiiResponse(frDTO));
                }
                catch (MyException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }


            if (request is GetDonatoriRequest)
            {
                Console.WriteLine("GetDonatoriRequest.....");
                GetDonatoriRequest getReq = (GetDonatoriRequest)request;
                try
                {
                    Donator[] allDonatori;
                    lock (server)
                    {
                        allDonatori = server.getDonatori();
                    }

                    DonatorDTO[] frDTO = DTOUtils.getDTO(allDonatori);
                    return(new GetDonatoriResponse(frDTO));
                }
                catch (MyException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is LoginRequest)
            {
                Console.WriteLine("Login request ...");
                LoginRequest logReq = (LoginRequest)request;
                VoluntarDTO  udto   = logReq.User;
                Voluntar     user   = DTOUtils.getFromDTO(udto);
                try
                {
                    lock (server)
                    {
                        server.login(user, this);
                    }
                    return(new OkResponse());
                }
                catch (MyException e)
                {
                    connected = false;
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is LogoutRequest)
            {
                Console.WriteLine("Logout request");
                LogoutRequest logReq = (LogoutRequest)request;
                VoluntarDTO   udto   = logReq.User;
                Voluntar      user   = DTOUtils.getFromDTO(udto);
                try
                {
                    lock (server)
                    {
                        server.logout(user, this);
                    }
                    connected = false;
                    return(new OkResponse());
                }
                catch (MyException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }

            if (request is SaveDonatorRequest)
            {
                Console.WriteLine("SaveDonatorRequest ...");
                SaveDonatorRequest senReq  = (SaveDonatorRequest)request;
                DonatorDTO         mdto    = senReq.Add;
                Donator            message = DTOUtils.getFromDTO(mdto);
                try
                {
                    lock (server)
                    {
                        server.addDonator(message);
                    }
                    return(new OkResponse());
                }
                catch (MyException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is SaveDonatieRequest)
            {
                Console.WriteLine("SaveDonatieRequest ...");
                SaveDonatieRequest senReq  = (SaveDonatieRequest)request;
                DonatieDTO         mdto    = senReq.Add;
                Donatie            message = DTOUtils.getFromDTO(mdto);
                try
                {
                    lock (server)
                    {
                        server.addDonatie(message);
                    }
                    return(new OkResponse());
                }
                catch (MyException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }
            if (request is UpdateCazRequest)
            {
                Console.WriteLine("UpdateCazRequest ...");
                UpdateCazRequest upReq  = (UpdateCazRequest)request;
                CazCaritabilDTO  cazDTO = upReq.Caz;
                CazCaritabil     caz    = DTOUtils.getFromDTO(cazDTO);
                try
                {
                    lock (server)
                    {
                        server.updateCazCaritabil(caz);
                    }
                    return(new OkResponse());
                }
                catch (MyException e)
                {
                    return(new ErrorResponse(e.Message));
                }
            }
            return(response);
        }
Beispiel #14
0
 public void logout()
 {
     Console.WriteLine("Ctr logout");
     server.logout(currentUser, this);
     currentUser = null;
 }
Beispiel #15
0
 public ClientController(IServices server)
 {
     this.server = server;
     currentUser = null;
 }