public bool AddUser(User user)
        {
            var elements = this.document
                .Root
                .Elements(TableName);

            int maxId = (!elements.Any()) ? 0 : elements.Max(t => (int)t.Attribute(FId));

            XElement userElement = new XElement(
                TableName,
                new XAttribute(FId, ++maxId),
                new XElement(FName, user.Name),
                new XElement(FBirthDay, user.BirthDay));

            this.document.Root.Add(userElement);
            this.document.Save(this.pathUserXml);

            return true;
        }
        public bool DeleteUser(User user)
        {
            IEnumerable<XElement> elements = this.document
                .Root
                .Elements(TableName)
                .Where(el => (int)el.Attribute(FId) == user.Id);

            if (!elements.Any())
            {
                return false;
            }

            elements.First().Remove();
            this.document.Save(this.pathUserXml);

            // Remove dependences in awards
            this.RemoveUserIdElements(XDocument.Load(this.pathAwardXml), user.Id);

            return true;
        }
        public bool AddUser(User user)
        {
            if (user.Id < 0)
            {
                return false;
            }

            var elements = this.document
                .Root
                .Elements(TableName);

            if (user.Id == 0)
            {
                int maxId = (!elements.Any()) ? 0 : elements.Max(t => (int)t.Attribute(FId));
                user.Id = maxId + 1;
                XElement userElement = new XElement(
                    TableName,
                    new XAttribute(FId, user.Id),
                    new XElement(FName, user.Name),
                    new XElement(FBirthDay, user.BirthDay));

                this.document.Root.Add(userElement);
            }
            else
            {
                elements = elements.Where(el => (int)el.Attribute(FId) == user.Id);

                if (!elements.Any())
                {
                    return false;
                }

                elements.First().SetElementValue(FName, user.Name);
                elements.First().SetElementValue(FBirthDay, user.BirthDay);
            }

            this.document.Save(this.pathUserXml);

            return true;
        }
Exemple #4
0
        private static void AddUser(IUserLogic logic)
        {
            try
            {
                // Name
                Console.Write("Enter Name: ");
                string name = Console.ReadLine();

                // BirthDay
                DateTime date = new DateTime();
                Console.Write($"Enter BirthDay ({DateFormat}): ");
                string dateStr = Console.ReadLine();
                if (!DateTime.TryParseExact(
                    dateStr,
                    DateFormat,
                    CultureInfo.CurrentCulture,
                    DateTimeStyles.None,
                    out date))
                {
                    throw new ArgumentException($"Incorrect input, use this template {DateFormat}");
                }

                var user = new User(name, date);

                logic.AddUser(user);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            Console.WriteLine();
        }
        private void CheckUsersValues(User user)
        {
            if (user == null)
            {
                throw new ArgumentException("The User mustn't be null");
            }

            if (string.IsNullOrWhiteSpace(user.Name))
            {
                throw new ArgumentException($"The Name mustn't be empty.");
            }

            if (user.Name.Length > this.maxNameLength)
            {
                throw new ArgumentException($"The Name mustn't be longer than {this.maxNameLength}");
            }

            var matches = this.regUserName.Matches(user.Name);

            if (matches.Count != 0)
            {
                StringBuilder strMatches = new StringBuilder(this.maxNameLength);

                foreach (Match match in matches)
                {
                    strMatches.Append(match.Value);
                }

                throw new ArgumentException($"Characters '{strMatches.ToString()}' aren't correct for name.");
            }

            if (user.BirthDay > DateTime.Now)
            {
                throw new ArgumentException($"The BirthDay mustn't be in the future.");
            }
        }
        public bool AddUser(User user)
        {
            this.CheckUsersValues(user);

            return Stores.UserStore.AddUser(user);
        }
        private User ElementToUser(XElement element)
        {
            int id = (int)element.Attribute(FId);
            string name = (string)element.Element(FName);
            DateTime birthDay = Convert.ToDateTime((string)element.Element(FBirthDay));

            User user = new User(id, name, birthDay);

            return user;
        }