Skip to content

Commit

Permalink
Adding Send Email functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
efonsecab committed Apr 17, 2024
1 parent 1884567 commit 93d7675
Show file tree
Hide file tree
Showing 12 changed files with 434 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ public FairPlayCombinedDbContext(DbContextOptions<FairPlayCombinedDbContext> opt

public virtual DbSet<UserMessage> UserMessage { get; set; }

public virtual DbSet<UserMessage1> UserMessage1 { get; set; }

public virtual DbSet<UserProfile> UserProfile { get; set; }

public virtual DbSet<VideoCaptions> VideoCaptions { get; set; }
Expand Down Expand Up @@ -405,6 +407,17 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.HasConstraintName("FK_ToApplicationUserId_ApplicationUser");
});

modelBuilder.Entity<UserMessage1>(entity =>
{
entity.HasOne(d => d.FromApplicationUser).WithMany(p => p.UserMessage1FromApplicationUser)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_FromApplicationUserId_AspNetUsers");
entity.HasOne(d => d.ToApplicationUser).WithMany(p => p.UserMessage1ToApplicationUser)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_ToApplicationUserId_AspNetUsers");
});

modelBuilder.Entity<UserProfile>(entity =>
{
entity.HasOne(d => d.ApplicationUser).WithOne(p => p.UserProfile)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// <auto-generated> This file has been auto generated by EF Core Power Tools. </auto-generated>
#nullable disable
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using FairPlayCombined.DataAccess.Models.dboSchema;
using FairPlayCombined.DataAccess.Models.FairPlayBudgetSchema;
using FairPlayCombined.DataAccess.Models.FairPlayDatingSchema;
using FairPlayCombined.DataAccess.Models.FairPlayShopSchema;
using FairPlayCombined.DataAccess.Models.FairPlaySocialSchema;
using FairPlayCombined.DataAccess.Models.FairPlayTubeSchema;


namespace FairPlayCombined.DataAccess.Models.FairPlayTubeSchema;

[Table("UserMessage", Schema = "FairPlayTube")]
public partial class UserMessage1
{
[Key]
public long UserMessageId { get; set; }

[Required]
[StringLength(450)]
public string FromApplicationUserId { get; set; }

[Required]
[StringLength(450)]
public string ToApplicationUserId { get; set; }

[Required]
public string Message { get; set; }

[Required]
[StringLength(250)]
public string SourceApplication { get; set; }

[Required]
[StringLength(100)]
public string OriginatorIpaddress { get; set; }

public DateTimeOffset RowCreationDateTime { get; set; }

[Required]
[StringLength(256)]
public string RowCreationUser { get; set; }

public bool ReadByDestinatary { get; set; }

[ForeignKey("FromApplicationUserId")]
[InverseProperty("UserMessage1FromApplicationUser")]
public virtual AspNetUsers FromApplicationUser { get; set; }

[ForeignKey("ToApplicationUserId")]
[InverseProperty("UserMessage1ToApplicationUser")]
public virtual AspNetUsers ToApplicationUser { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ public partial class AspNetUsers
[InverseProperty("ApplicationUser")]
public virtual ICollection<UserActivity> UserActivity { get; set; } = new List<UserActivity>();

[InverseProperty("FromApplicationUser")]
public virtual ICollection<UserMessage1> UserMessage1FromApplicationUser { get; set; } = new List<UserMessage1>();

[InverseProperty("ToApplicationUser")]
public virtual ICollection<UserMessage1> UserMessage1ToApplicationUser { get; set; } = new List<UserMessage1>();

[InverseProperty("FromApplicationUser")]
public virtual ICollection<UserMessage> UserMessageFromApplicationUser { get; set; } = new List<UserMessage>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,10 @@
"Name": "[FairPlaySocial].[PostVisibility]",
"ObjectType": 0
},
{
"Name": "[FairPlayTube].[UserMessage]",
"ObjectType": 0
},
{
"Name": "[FairPlayTube].[VideoCaptions]",
"ObjectType": 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FairPlayCombined.Models.FairPlayTube.Conversation
{
/// <summary>
/// Holds the data regarding an ApplicationUser
/// </summary>
public class ConversationsUserModel
{
[Required]
[StringLength(450)]
/// <summary>
/// Id of an application user
/// </summary>
public string? ApplicationUserId { get; set; }
/// <summary>
/// User's Full Name
/// </summary>
[Required]
[StringLength(150)]
public string? FullName { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FairPlayCombined.Models.FairPlayTube.UserMessage
{

/// <summary>
/// Represents the User Message entry
/// </summary>
public class UserMessageModel
{
/// <summary>
/// ApplicationUserId of the user to whom the message is sent
/// </summary>
public string? ToApplicationUserId { get; set; }
/// <summary>
/// Full Name of the To Application User
/// </summary>
public string? ToApplicationUserFullName { get; set; }
/// <summary>
/// Full Name of the From Application User
/// </summary>
public string? FromApplicationUserFullName { get; set; }
/// <summary>
/// Message to be sent
/// </summary>
[Required]
public string? Message { get; set; }
/// <summary>
/// UTC DateTime the message was created
/// </summary>
public DateTimeOffset? RowCreationDateTime { get; set; }
/// <summary>
/// Indicated if the message has been read by the destinatary
/// </summary>
public bool ReadByDestinatary { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using FairPlayCombined.DataAccess.Data;
using FairPlayCombined.Interfaces;
using FairPlayCombined.Models.FairPlayTube.Conversation;
using FairPlayCombined.Models.FairPlayTube.UserMessage;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FairPlayCombined.Services.FairPlayTube
{
public partial class UserMessageService(
IDbContextFactory<FairPlayCombinedDbContext> dbContextFactory,
IUserProviderService userProviderService) : BaseService
{
public async Task<ConversationsUserModel[]?> GetMyConversationsUsersAsync(
CancellationToken cancellationToken)
{
var dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
var currentUser = await
dbContext.AspNetUsers
.SingleAsync(p => p.Id ==
userProviderService.GetCurrentUserId(), cancellationToken: cancellationToken);
var receivedMessagesUsers = await dbContext.UserMessage1
.Include(p => p.FromApplicationUser)
.Where(p => p.ToApplicationUserId == currentUser.Id)
.Select(p => p.FromApplicationUser)
.Distinct().ToListAsync();
var sentMessagesUsers = await dbContext.UserMessage1
.Include(p => p.ToApplicationUser)
.Where(p => p.FromApplicationUserId == currentUser.Id)
.Select(p => p.ToApplicationUser)
.Distinct().ToListAsync();
var result = receivedMessagesUsers
.Union(sentMessagesUsers)
.Distinct()
.Select(p=>new ConversationsUserModel()
{
ApplicationUserId = p.Id,
FullName = p.UserName
})
.ToArray();
return result;
}

public async Task<UserMessageModel[]?> GetMyConversationsWithUserAsync(string? userId, CancellationToken cancellationToken)
{
var dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
var currentUser = await
dbContext.AspNetUsers
.SingleAsync(p => p.Id ==
userProviderService.GetCurrentUserId(), cancellationToken: cancellationToken);
return await dbContext.UserMessage1
.Include(p => p.ToApplicationUser)
.Include(p => p.FromApplicationUser)
.Where(p =>
(p.FromApplicationUserId == currentUser.Id &&
p.ToApplicationUserId == userId)
||
(p.FromApplicationUserId == userId &&
p.ToApplicationUserId == currentUser.Id)
)
.OrderByDescending(p => p.RowCreationDateTime)
.Select(p=>new UserMessageModel()
{
FromApplicationUserFullName = p.FromApplicationUser.UserName,
Message = p.Message,
ReadByDestinatary = p.ReadByDestinatary,
RowCreationDateTime = p.RowCreationDateTime,
ToApplicationUserFullName = p.ToApplicationUser.UserName,
ToApplicationUserId = p.ToApplicationUserId
}).ToArrayAsync(cancellationToken);
}

public async Task SendMessageAsync(UserMessageModel? userMessageModel,
CancellationToken cancellationToken)
{
var dbContext = await dbContextFactory.CreateDbContextAsync(cancellationToken);
await dbContext.UserMessage1.AddAsync(
new DataAccess.Models.FairPlayTubeSchema.UserMessage1()
{
FromApplicationUserId = userProviderService.GetCurrentUserId(),
ToApplicationUserId = userMessageModel!.ToApplicationUserId,
Message = userMessageModel.Message,
}, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
<Build Include="FairPlayTube\Tables\VideoPlanThumbnail.sql" />
<Build Include="FairPlayTube\Tables\VideoWatchTime.sql" />
<Build Include="FairPlayTube\Tables\VideoIndexerSupportedLanguage.sql" />
<Build Include="FairPlayTube\Tables\UserMessage.sql" />
</ItemGroup>
<ItemGroup>
<PostDeploy Include="Scripts\1-Script.PostDeployment1.sql" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE [FairPlayTube].[UserMessage]
(
[UserMessageId] BIGINT NOT NULL CONSTRAINT PK_UserMessage PRIMARY KEY IDENTITY,
[FromApplicationUserId] NVARCHAR(450) NOT NULL CONSTRAINT FK_FromApplicationUserId_AspNetUsers FOREIGN KEY REFERENCES [dbo].[AspNetUsers]([Id]),
[ToApplicationUserId] NVARCHAR(450) NOT NULL CONSTRAINT FK_ToApplicationUserId_AspNetUsers FOREIGN KEY REFERENCES [dbo].[AspNetUsers]([Id]),
[Message] NVARCHAR(MAX) NOT NULL,
[SourceApplication] NVARCHAR(250) NOT NULL,
[OriginatorIpaddress] NVARCHAR(100) NOT NULL,
[RowCreationDateTime] DATETIMEOFFSET NOT NULL,
[RowCreationUser] NVARCHAR(256) NOT NULL,
[ReadByDestinatary] BIT NOT NULL DEFAULT 0
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,26 @@

<div class="@ThemeConfiguration.Grids.GridContainerCss">
<FluentDataGrid ItemsProvider="ItemsProvider" Pagination="this.paginationState">
<PropertyColumn Property="@( p=> p.Username)" Class="multiline-text"></PropertyColumn>
<TemplateColumn Class="multiline-text">
@if (String.IsNullOrWhiteSpace(context.Username))
{
<FluentLabel>Anonymous</FluentLabel>
}
else
{
<FluentLabel>@context.Username</FluentLabel>
}
</TemplateColumn>
<TemplateColumn Title="Actions">
@if (!String.IsNullOrWhiteSpace(context.Username))
{
<FluentAnchor data-enhance-nav="false"
data-bs-toggle="tooltip" data-bs-placement="top" title="Send Email"
Href="@($"mailto://{context.Username}")" Target="_blank">
<FluentIcon Value="@(new Icons.Regular.Size20.MailAdd())"></FluentIcon>
</FluentAnchor>
}
</TemplateColumn>
<TemplateColumn Title="@nameof(VideoViewerModel.TotalTimeWatched)">
@TimeSpan.FromSeconds(context.TotalTimeWatched)
</TemplateColumn>
Expand Down Expand Up @@ -52,7 +71,7 @@
};
var items = await videoViewerService
.GetPaginatedVideoViewerInfoForUserIdAsync(paginationRequest,
videoId:this.VideoId!,
videoId: this.VideoId!,
userId: this.userProviderService!.GetCurrentUserId()!,
this.cancellationTokenSource.Token);
StateHasChanged();
Expand Down
Loading

0 comments on commit 93d7675

Please sign in to comment.