public OfficerData(OfficerDataIn _officer)
 {
     this.Name       = _officer.Name;
     this.SurName    = _officer.SurName;
     this.Patronymic = _officer.Patronymic;
     this.BirthDate  = _officer.BirthDate;
     this.Posts      = _officer.Posts;
 }
Beispiel #2
0
        /// <summary>
        /// Обновление информации о сотруднике
        /// </summary>
        /// <param name="id"></param>
        /// <param name="data"></param>
        /// <param name="session"></param>
        public void UpdateOfficer(int id, OfficerDataIn data, string session)
        {
            try
            {
                using (var _connection = new NpgsqlConnection(MainConnection.GetConnectionString()))
                {
                    _connection.Open();

                    var _transaction = _connection.BeginTransaction();

                    try
                    {
                        var sqlQueryOfficer = string.Format(@"UPDATE {0}officer SET firstname = @firstname, surname = @surname, patronymic = @patronymic, birthdate = @birthdate
                                                               WHERE id = @id
                                                           RETURNING ID", MainConnection.GetSQLNamespace());

                        using (var _postgreCommand = new NpgsqlCommand(sqlQueryOfficer, _connection))
                        {
                            _postgreCommand.Parameters.AddWithValue("id", id);
                            _postgreCommand.Parameters.AddWithValue("firstname", data.Name);
                            _postgreCommand.Parameters.AddWithValue("surname", Utils.NoNullValue(data.SurName));
                            _postgreCommand.Parameters.AddWithValue("patronymic", Utils.NoNullValue(data.Patronymic));
                            _postgreCommand.Parameters.AddWithValue("birthdate", data.BirthDate);

                            _postgreCommand.ExecuteNonQuery();
                        }

                        DeleteLink(id, session, _connection);

                        if (data.Posts != null && data.Posts.Count() > 0)
                        {
                            foreach (var post in data.Posts)
                            {
                                CreateOfficerPost(id, post.ID, session, _connection);
                            }
                        }

                        _transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        _transaction.Rollback();
                        throw;
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception("Ошибка обновления данных сотрудника в БД.", e);
            }

            if (_setting.Value.ChachedEmployee)
            {
                try
                {
                    _cache.EditOfficer(id, data, session);
                }
                catch (Exception e)
                {
                    // Логгирование ошибки, работа продолжится через БД
                    // Необходимо изменить состояние кэша не невалидное, чтобы избежать чтение из неверного кэша
                    //throw new Exception("Ошибка обновления данных сотрудника в кэше.", e);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Создания информации о сотруднике
        /// </summary>
        /// <param name="data"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public OfficerData CreateOfficerInfo(OfficerDataIn data, string session)
        {
            var id     = -1;
            var result = new OfficerData(data);
            var posts  = new List <PostData>();

            using (var _connection = new NpgsqlConnection(MainConnection.GetConnectionString()))
            {
                _connection.Open();

                var _transaction = _connection.BeginTransaction();

                try
                {
                    var sqlQueryOfficer = string.Format(@"INSERT INTO {0}Officer (FirstName, Surname, Patronymic, BirthDate)
                                                               VALUES (@firstname, @surname, @patronymic, @birthdate)
                                                            RETURNING ID", MainConnection.GetSQLNamespace());

                    using (var _postgreCommand = new NpgsqlCommand(sqlQueryOfficer, _connection))
                    {
                        _postgreCommand.Parameters.AddWithValue("firstname", data.Name);
                        _postgreCommand.Parameters.AddWithValue("surname", Utils.NoNullValue(data.SurName));
                        _postgreCommand.Parameters.AddWithValue("patronymic", Utils.NoNullValue(data.Patronymic));
                        _postgreCommand.Parameters.AddWithValue("birthdate", data.BirthDate);

                        id = Convert.ToInt32(_postgreCommand.ExecuteScalar());
                    }

                    foreach (var post in data.Posts)
                    {
                        CreateOfficerPost(id, post.ID, session, _connection);
                        posts.Add(GetPost(post.ID, session));
                    }

                    _transaction.Commit();

                    result.ID    = id;
                    result.Posts = posts;
                }
                catch (Exception e)
                {
                    _transaction.Rollback();
                    throw new Exception("Ошибка добавления сотрудника в БД.", e);
                }
            }

            if (_setting.Value.ChachedEmployee)
            {
                try
                {
                    _cache.AddOfficer(id, result, session);
                }
                catch (Exception e)
                {
                    // Логгирование ошибки, работа продолжится через БД
                    //throw new Exception("Ошибка добавления сотрудника в кэш.", e);
                }
            }

            return(result);
        }