Exemplo n.º 1
0
        /// <summary>
        /// Adds a Discord user to the database.
        /// </summary>
        /// <param name="discordUser">The Discord user.</param>
        /// <param name="ct">The cancellation token in use.</param>
        /// <returns>The freshly created information about the user.</returns>
        /// <exception cref="ArgumentException">Thrown if the user already exists in the database.</exception>
        public async Task <RetrieveEntityResult <User> > AddUserAsync
        (
            IUser discordUser,
            CancellationToken ct = default
        )
        {
            if (discordUser.IsBot || discordUser.IsWebhook)
            {
                return(RetrieveEntityResult <User> .FromError
                       (
                           "Users cannot be viewed or created for bots or webhooks."
                       ));
            }

            if (await IsUserKnownAsync(discordUser, ct))
            {
                return(RetrieveEntityResult <User> .FromError
                       (
                           $"A user with the ID {discordUser.Id} has already been added to the database."
                       ));
            }

            var newUser = _database.CreateProxy <User>((long)discordUser.Id);

            _database.Users.Update(newUser);
            await _database.SaveChangesAsync(ct);

            return(newUser);
        }
Exemplo n.º 2
0
    /// <summary>
    /// Grants consent to store user data for a given user.
    /// </summary>
    /// <param name="discordUser">The user that has granted consent.</param>
    /// <param name="ct">The cancellation token in use.</param>
    /// <returns>A task that must be awaited.</returns>
    public async Task <Result <UserConsent> > GrantUserConsentAsync
    (
        Snowflake discordUser,
        CancellationToken ct = default
    )
    {
        var getConsent = await GetUserConsentAsync(discordUser, ct);

        UserConsent userConsent;

        if (!getConsent.IsSuccess)
        {
            userConsent = _database.CreateProxy <UserConsent>(discordUser);
            _database.UserConsents.Update(userConsent);

            userConsent.HasConsented = true;
        }
        else
        {
            userConsent = getConsent.Entity;
            userConsent.HasConsented = true;
        }

        await _database.SaveChangesAsync(ct);

        return(userConsent);
    }
Exemplo n.º 3
0
        public async Task <RetrieveEntityResult <User> > AddUserAsync([NotNull] IUser discordUser)
        {
            if (discordUser.IsBot || discordUser.IsWebhook)
            {
                return(RetrieveEntityResult <User> .FromError
                       (
                           "Users cannot be viewed or created for bots or webhooks."
                       ));
            }

            if (await IsUserKnownAsync(discordUser))
            {
                return(RetrieveEntityResult <User> .FromError
                       (
                           $"A user with the ID {discordUser.Id} has already been added to the database."
                       ));
            }

            var newUser = new User((long)discordUser.Id);

            _database.Users.Update(newUser);

            await _database.SaveChangesAsync();

            // Requery the database
            return(await GetUserAsync(discordUser));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> PutQuestion([FromRoute] int id, [FromBody] Question question)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != question.QuestionId)
            {
                return(BadRequest());
            }

            _context.Entry(question).State = EntityState.Modified;

            try
            {
                var stat = await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (!QuestionExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    return(Forbid());
                }
            }

            return(Ok());
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Create([Bind("QuizId,QuizName")] Quiz quiz)
        {
            if (ModelState.IsValid)
            {
                _context.Add(quiz);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(quiz));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Adds a Discord server to the database.
        /// </summary>
        /// <param name="discordServer">The Discord server.</param>
        /// <returns>The freshly created information about the server.</returns>
        /// <exception cref="ArgumentException">Thrown if the server already exists in the database.</exception>
        public async Task <RetrieveEntityResult <Server> > AddServerAsync(IGuild discordServer)
        {
            if (await IsServerKnownAsync(discordServer))
            {
                return(RetrieveEntityResult <Server> .FromError
                       (
                           $"A server with the ID {discordServer.Id} has already been added to the database."
                       ));
            }

            var server = Server.CreateDefault(discordServer);

            await _database.Servers.AddAsync(server);

            await _database.SaveChangesAsync();

            return(RetrieveEntityResult <Server> .FromSuccess(server));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Grants consent to store user data for a given user.
        /// </summary>
        /// <param name="discordUser">The user that has granted consent.</param>
        /// <returns>A task that must be awaited.</returns>
        public async Task GrantUserConsentAsync(IUser discordUser)
        {
            var userConsent = await _database.UserConsents.FirstOrDefaultAsync(uc => uc.DiscordID == (long)discordUser.Id);

            if (userConsent is null)
            {
                userConsent = new UserConsent((long)discordUser.Id)
                {
                    HasConsented = true
                };

                await _database.UserConsents.AddAsync(userConsent);
            }
            else
            {
                userConsent.HasConsented = true;
            }

            await _database.SaveChangesAsync();
        }
Exemplo n.º 8
0
    /// <summary>
    /// Adds a Discord user to the database.
    /// </summary>
    /// <param name="discordUser">The Discord user.</param>
    /// <param name="ct">The cancellation token in use.</param>
    /// <returns>The freshly created information about the user.</returns>
    /// <exception cref="ArgumentException">Thrown if the user already exists in the database.</exception>
    public async Task <Result <User> > AddUserAsync
    (
        Snowflake discordUser,
        CancellationToken ct = default
    )
    {
        if (await IsUserKnownAsync(discordUser, ct))
        {
            return(new UserError
                   (
                       $"A user with the ID {discordUser} has already been added to the database."
                   ));
        }

        var newUser = _database.CreateProxy <User>(discordUser);

        _database.Users.Update(newUser);
        await _database.SaveChangesAsync(ct);

        return(newUser);
    }
Exemplo n.º 9
0
        /// <summary>
        /// Adds a Discord server to the database.
        /// </summary>
        /// <param name="discordServer">The Discord server.</param>
        /// <param name="ct">The cancellation token in use.</param>
        /// <returns>The freshly created information about the server.</returns>
        /// <exception cref="ArgumentException">Thrown if the server already exists in the database.</exception>
        public async Task <RetrieveEntityResult <Server> > AddServerAsync
        (
            IGuild discordServer,
            CancellationToken ct = default
        )
        {
            if (await IsServerKnownAsync(discordServer, ct))
            {
                return(RetrieveEntityResult <Server> .FromError
                       (
                           $"A server with the ID {discordServer.Id} has already been added to the database."
                       ));
            }

            var server = _database.CreateProxy <Server>((long)discordServer.Id);

            _database.Servers.Update(server);

            server.IsNSFW = true;

            await _database.SaveChangesAsync(ct);

            return(RetrieveEntityResult <Server> .FromSuccess(server));
        }