Пример #1
0
        public string Delete(int id)
        {
            if (id < 0)
            {
                _Logger.LogError(LogConst.IdIsNegative);
                throw new ArgumentException(LogConst.IdIsNegative);
            }

            var person = _DBContext.Person.FirstOrDefault(p => p.Id == id);

            if (person != null)
            {
                try
                {
                    _DBContext.Person.Remove(person);
                    _DBContext.SaveChanges();
                    _Logger.LogInformation(LogConst.ElementDeleted(id, nameof(_DBContext.Person)));
                    return("Элемент успешно удален");
                }
                catch (Exception ex)
                {
                    _Logger.LogError(ex, LogConst.ElementNotDeleted(id, nameof(_DBContext.Person)));
                    return("Произошла ошибка при удалении из бд");
                }
            }
            else
            {
                _Logger.LogInformation(LogConst.ElementNotFound(id, nameof(_DBContext.Person)));
                return("Элемент не найден");
            }
        }
Пример #2
0
        public Person Select(int id)
        {
            if (id < 0)
            {
                _Logger.LogError(LogConst.IdIsNegative);
                throw new ArgumentException(LogConst.IdIsNegative);
            }
            var person = _DBContext.Person.FirstOrDefault(p => p.Id == id);

            if (person == null)
            {
                _Logger.LogInformation(LogConst.ElementNotFound(id, nameof(_DBContext.Person)));
            }
            _Logger.LogInformation(LogConst.ElementIsFound(id, nameof(_DBContext.Person)));
            return(person);
        }