예제 #1
0
        private List <Contact> ContactsSearch(string filter)
        {
            filter.Trim(',', ' ');

            string             db         = ConfigurationManager.AppSettings["DBConnectionString"];
            ContactsDBOperator dBOperator = new ContactsDBOperator(db);

            string[] parameters;
            if (filter.Contains(" "))
            {
                //имя и фамилия должны частично соответствовать поиску
                Logger.Log.Info($"Search by two params, search condition AND");
                parameters = filter.Split(' ');
                return(dBOperator.GetByNameParts(parameters, false));
            }
            else if (filter.Contains(","))
            {
                //имя ИЛИ фамилия должны частично соответствовать поиску
                Logger.Log.Info($"Search by two params, search condition OR");
                parameters = filter.Split(',');
                return(dBOperator.GetByNameParts(parameters, true));
            }
            else
            {
                //поиск по одной строке
                Logger.Log.Info($"Search by one param");
                return(dBOperator.GetByNameParts(filter));
            }
        }
예제 #2
0
        public void DeleteContact(string id)
        {
            Logger.Log.Info($"DeleteContact got params: id = {id}");
            string             db         = ConfigurationManager.AppSettings["DBConnectionString"];
            ContactsDBOperator dBOperator = new ContactsDBOperator(db);

            if (dBOperator.RowExists(id))
            {
                try
                {
                    dBOperator.DeleteContact(id);
                }
                catch (DBConnectionException)
                {
                    throw new FaultException("Ошибка подключения к базе данных");
                }
                catch (SQLCommandException)
                {
                    throw new FaultException("Ошибка выполнения запроса к базе данных");
                }
            }
            else
            {
                Logger.Log.Info($"Contact doesn't exist: id = {id}");
                throw new FaultException <string>(id, "Контакт не существует");
            }
        }
예제 #3
0
        public MemoryStream SaveToXML(string filter)
        {
            string             db         = ConfigurationManager.AppSettings["DBConnectionString"];
            ContactsDBOperator dBOperator = new ContactsDBOperator(db);
            List <Contact>     contacts   = (filter == "")?dBOperator.GetAll():ContactsSearch(filter);

            //string file = ConfigurationManager.AppSettings["tempfilePath"];

            //string filepath = AppDomain.CurrentDomain.BaseDirectory + file;

            using (ExcelPackage package = new ExcelPackage())
            {
                var newworksheet = package.Workbook.Worksheets.Add("test");
                newworksheet.Cells["A1"].LoadFromCollection(contacts, true);

                var stream = new MemoryStream(package.GetAsByteArray());
                return(stream);

                //string filepath = "C:\\Users\\anotfullina\\Documents\\GitHub\\learning\\FinalService\\temp\\contacts1.xlsx";
                //FileInfo fi = new FileInfo(filepath);
                //package.SaveAs(fi);
            }



            //return "temp\\contacts.xls";
        }
예제 #4
0
        public string InsertContact(string Name, string Surname, string MiddleName,
                                    string Gender, string BirthDate, string Phone, string TaxNumber,
                                    string Position, string JobID)
        {
            Logger.Log.Info($"InsertContact got params: name = {Name}, surname = {Surname}, " +
                            $"middlename = {MiddleName},gender = { Gender}, birthday = {BirthDate}, phone = {Phone}," +
                            $" taxnumber = {TaxNumber}, position = {Position}, jobid = {JobID}");
            ContactsDataConverter converter = new ContactsDataConverter();
            Contact newContact = converter.CreateInstance(Name, Surname, MiddleName,
                                                          Gender, BirthDate, Phone, TaxNumber, Position, JobID);

            string             db         = ConfigurationManager.AppSettings["DBConnectionString"];
            ContactsDBOperator dBOperator = new ContactsDBOperator(db);

            try
            {
                int    id     = dBOperator.InsertContact(newContact);
                string result = JsonConvert.SerializeObject(id);
                return(result);
            }
            catch (DBConnectionException)
            {
                throw new FaultException("Ошибка подключения к базе данных");
            }
            catch (SQLCommandException)
            {
                throw new FaultException("Ошибка выполнения запроса к базе данных");
            }
        }
예제 #5
0
        public string GetContact(string id)
        {
            Logger.Log.Info($"GetContact got params: id = {id}");
            string             db         = ConfigurationManager.AppSettings["DBConnectionString"];
            ContactsDBOperator dBOperator = new ContactsDBOperator(db);

            try
            {
                if (dBOperator.RowExists(id))
                {
                    Contact contact = dBOperator.GetById(id);

                    JsonSerializerSettings settings = new JsonSerializerSettings();
                    settings.DateFormatString = "yyyy-MM-dd";
                    string responce = JsonConvert.SerializeObject(contact, settings);
                    return(responce);
                }
                else
                {
                    Logger.Log.Info($"Contact doesn't exist: id = {id}");
                    throw new FaultException <string>(id, "Контакт не существует");
                }
            }
            catch (DBConnectionException)
            {
                throw new FaultException("Ошибка подключения к базе данных");
            }
            catch (SQLCommandException)
            {
                throw new FaultException("Ошибка выполнения запроса к базе данных");
            }
        }
예제 #6
0
        public string GetAllContacts()
        {
            Logger.Log.Info($"GetAllContacts called");
            string             db         = ConfigurationManager.AppSettings["DBConnectionString"];
            ContactsDBOperator dBOperator = new ContactsDBOperator(db);
            List <Contact>     contacts   = dBOperator.GetAll();

            if (contacts == null)
            {
                Logger.Log.Info($"Contacts list is empty");
                throw new FaultException <int>(0, $"Список контактов пуст");
            }
            else
            {
                JsonSerializerSettings settings = new JsonSerializerSettings();
                settings.DateFormatString = "d MMMM, yyyy";
                string responce = JsonConvert.SerializeObject(contacts, settings);
                Logger.Log.Info($"GetAllContacts return {contacts?.Count} contacts");
                return(responce);
            }
        }
예제 #7
0
        public void UpdateContact(string ID, string Name, string Surname, string MiddleName,
                                  string Gender, string BirthDate, string Phone, string TaxNumber,
                                  string Position, string JobID)
        {
            Logger.Log.Info($"UpdateContact got params: id = {ID}, name = {Name}, surname = {Surname}, " +
                            $"middlename = {MiddleName},gender = { Gender}, birthday = {BirthDate}, phone = {Phone}," +
                            $" taxnumber = {TaxNumber}, position = {Position}, jobid = {JobID}");

            string             db         = ConfigurationManager.AppSettings["DBConnectionString"];
            ContactsDBOperator dBOperator = new ContactsDBOperator(db);

            if (dBOperator.RowExists(ID))
            {
                ContactsDataConverter converter = new ContactsDataConverter();
                Contact newContact = converter.CreateInstance(Name, Surname, MiddleName,
                                                              Gender, BirthDate, Phone, TaxNumber, Position, JobID, ID);
                try
                {
                    dBOperator.UpdateContact(newContact);
                }
                catch (DBConnectionException)
                {
                    throw new FaultException("Ошибка подключения к базе данных");
                }
                catch (SQLCommandException)
                {
                    throw new FaultException("Ошибка выполнения запроса к базе данных");
                }
            }
            else
            {
                Logger.Log.Info($"Contact doesn't exist: id = {ID}");

                throw new FaultException <string>(ID, "Контакт не существует");
            }
        }