Пример #1
0
    /// <summary>
    /// Creates a roleplay with the given parameters.
    /// </summary>
    /// <param name="owner">The user that owns the roleplay.</param>
    /// <param name="server">The server that the roleplay is associated with.</param>
    /// <param name="roleplayName">The name of the roleplay.</param>
    /// <param name="roleplaySummary">The summary of the roleplay.</param>
    /// <param name="isNSFW">Whether or not the roleplay is NSFW.</param>
    /// <param name="isPublic">Whether or not the roleplay is public.</param>
    /// <param name="ct">The cancellation token in use.</param>
    /// <returns>A creation result which may or may not have been successful.</returns>
    public async Task <Result <Roleplay> > CreateRoleplayAsync
    (
        User owner,
        Server server,
        string roleplayName,
        string roleplaySummary,
        bool isNSFW,
        bool isPublic,
        CancellationToken ct = default
    )
    {
        owner  = _database.NormalizeReference(owner);
        server = _database.NormalizeReference(server);

        // Use a dummy name, since we'll be setting it using the service.
        var roleplay = _database.CreateProxy <Roleplay>(server, owner, string.Empty, string.Empty);

        _database.Roleplays.Update(roleplay);

        var ownerParticipant = _database.CreateProxy <RoleplayParticipant>(roleplay, owner);

        _database.Update(ownerParticipant);

        ownerParticipant.Status = ParticipantStatus.Joined;
        roleplay.ParticipatingUsers.Add(ownerParticipant);

        var setNameResult = await SetRoleplayNameAsync(roleplay, roleplayName, ct);

        if (!setNameResult.IsSuccess)
        {
            return(Result <Roleplay> .FromError(setNameResult));
        }

        var setSummaryResult = await SetRoleplaySummaryAsync(roleplay, roleplaySummary, ct);

        if (!setSummaryResult.IsSuccess)
        {
            return(Result <Roleplay> .FromError(setSummaryResult));
        }

        roleplay.IsNSFW   = isNSFW;
        roleplay.IsPublic = isPublic;

        await _database.SaveChangesAsync(ct);

        return(roleplay);
    }