Ejemplo n.º 1
0
        /// <summary>
        /// convert to user entity then save changes.
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <bool> UpdateUserAsync(int id, User user)
        {
            // make sure the user can be converted
            var userEntity = DbEntityConverter.ToUserEntity(user);

            var entities = _context.UserEntities;

            if (id < 1 || !entities.Any() || id > entities.Max(u => u.Id))
            {
                throw new ArgumentException($"{id} is not a valid id.");
            }

            UserEntity entity = await entities.FindAsync(id);

            // assign all the values
            {
                entity.Status    = userEntity.Status;
                entity.FirstName = userEntity.FirstName;
                if (!userEntity.Email.IsNullOrEmpty())
                {
                    entity.Email = userEntity.Email;
                }
                entity.LastName = userEntity.LastName;
            }
            // save changes.
            _context.SaveChanges();
            return(true);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// try to create a user if it goes through return a true otherwise return a flase
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <int> CreateUser(User user)
        {
            try {
                var newUser = DbEntityConverter.ToUserEntity(user); // convert
                await _context.AddAsync(newUser);

                await _context.SaveChangesAsync();

                return(newUser.Id);
            } catch {
                return(-1);
            }
        }