Exemple #1
0
        public async Task Create(Comment comment, CancellationToken cancellationToken = default)
        {
            if (comment == null)
            {
                throw new ArgumentNullException(nameof(comment));
            }

            try
            {
                DATABASE.Comment dbComment = ConvertComment(comment);

                await _context.Comments.AddAsync(dbComment, cancellationToken);

                await _context.SaveChangesAsync(cancellationToken);
            }
            catch (Exception ex)
            {
                throw new ServiceException($"Failed to create comment.", ex);
            }
        }
Exemple #2
0
        public async Task Create(ApplicationUser user, CancellationToken cancellationToken = default)
        {
            try
            {
                #region Validation

                if (user == null)
                {
                    throw new ArgumentNullException(nameof(user));
                }

                if (user.UserName == default)
                {
                    throw new ArgumentException("Invalid user name.");
                }

                if (user.Email == null)
                {
                    throw new ArgumentException("Invalid user email..");
                }

                if (user.PasswordHash == null)
                {
                    throw new ArgumentException("Invalid user password hash.");
                }

                #endregion

                DATABASE.ApplicationUser dbUser = ConvertUser(user);

                #region Ensure: First user Administrator

                bool hasUser = await _context.Users.AnyAsync(cancellationToken);

                if (!hasUser)
                {
                    dbUser.Role = DATABASE.ApplicationRole.Administrator;
                }

                #endregion

                await _context.Users.AddAsync(dbUser, cancellationToken);

                await _context.SaveChangesAsync(cancellationToken);
            }
            catch (Exception ex)
            {
                throw new ServiceException("Failed to create user.", ex);
            }
        }