diff --git a/CouchLog/Components/Pages/GlobalList.razor b/CouchLog/Components/Pages/GlobalList.razor index d01230a..8d16ac8 100644 --- a/CouchLog/Components/Pages/GlobalList.razor +++ b/CouchLog/Components/Pages/GlobalList.razor @@ -9,6 +9,7 @@ @inject ApplicationDbContext CouchLogDB @inject UserManager UserManager @inject AuthenticationStateProvider AuthenticationStateProvider +@inject NavigationManager NavigationManager @attribute [Authorize] @@ -17,29 +18,44 @@

Global List

- +
+ +
-
- @foreach(MediaType Type in MediaTypes) { }
- - +
+ + + @if (!string.IsNullOrEmpty(ImagePreviewUrl)) + { + + } +
+
+ + @foreach(Genre Genre in Genres) + { + + } +
+
@@ -48,6 +64,29 @@ {
+
+ +
+ + + +
@@ -57,10 +96,10 @@

@Entity.Title

- - +
@@ -71,18 +110,33 @@ @code { private List MediaTypes = new List(); - public List GlobalEntities = new List(); + private List Genres = new List(); + private List GlobalEntities = new List(); + private List GenreIds = new List(); private HashSet UserPrivateEntityIds = new(); + private string? ImagePreviewUrl; + private IBrowserFile? Picture; + System.Security.Claims.ClaimsPrincipal User = new(); + ApplicationUser? AppUser = new(); + private GlobalEntity GlobalEntity = new(); + private bool isCollapseNewGlobalEntityOpen = false; + private int SelectedMediaTypeId; + /// + /// + /// + /// + /// protected override async Task OnInitializedAsync() { GlobalEntities = await CouchLogDB.GlobalEntities.OrderByDescending(Entity => Entity.Id).ToListAsync(); - MediaTypes = await CouchLogDB.MediaType.OrderByDescending(Type => Type.Id).ToListAsync(); + MediaTypes = await CouchLogDB.MediaType.OrderBy(Type => Type.Id).ToListAsync(); + Genres = await CouchLogDB.Genres.OrderBy(Genre => Genre.Id).ToListAsync(); var AuthState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); - var User = AuthState.User; + User = AuthState.User; - var AppUser = await UserManager.GetUserAsync(User); + AppUser = await UserManager.GetUserAsync(User); if (AppUser == null) { @@ -97,41 +151,102 @@ UserPrivateEntityIds = TempUserPrivateEntityIds.ToHashSet(); } + /// + /// + /// + /// + /// + private async Task LoadFiles(InputFileChangeEventArgs eventArgs) + { + Picture = eventArgs.File; + //Byte[] Buffer = new byte[Picture.Size]; + //await Picture.OpenReadStream().ReadAsync(Buffer); + //await Picture.OpenReadStream(maxAllowedSize: 10_000_000).ReadAsync(Buffer); - private bool isCollapseNewGlobalEntityOpen = false; + //string Base64 = Convert.ToBase64String(Buffer); + //ImagePreviewUrl = $"data:{Picture.ContentType};base64,{Base64}"; + } + /// + /// + /// private void ToogleCollapseNewGlobalEntity() { isCollapseNewGlobalEntityOpen = !isCollapseNewGlobalEntityOpen; + StateHasChanged(); } - private GlobalEntity GlobalEntity = new(); - - - private void CreateNewGlobalEntity() + /// + /// + /// + /// + private async Task CreateNewGlobalEntity() { - + if (Picture is null) + { + throw new InvalidOperationException("No Picture selected."); + } + + if (AppUser is null) + { + throw new InvalidOperationException("User not loaded or not logged in."); + } + + //Save Picture and Name it + string NewFileName = $"{GlobalEntity.Title.Replace(" ", "_")}_{Guid.NewGuid()}{Path.GetExtension(Picture.Name)}"; + string PicturePath = Path.Combine("wwwroot", "Pictures", NewFileName); + using FileStream FileStream = File.Create(PicturePath); + await Picture.OpenReadStream().CopyToAsync(FileStream); + //await Picture.OpenReadStream(maxAllowedSize: 10_000_000).CopyToAsync(FileStream); + + GlobalEntity.PicturePath = $"Pictures/{NewFileName}"; + GlobalEntity.CreatorId = AppUser.Id; + GlobalEntity.TypeId = SelectedMediaTypeId; + GlobalEntity.CreationTime = DateTime.Now; + + CouchLogDB.Add(GlobalEntity); + await CouchLogDB.SaveChangesAsync(); + + foreach(int GenreId in GenreIds) + { + LinkTableGlobalGenre LinkTableGlobalGenre = new() { GenreId = GenreId, GlobalEntityId = GlobalEntity.Id }; + CouchLogDB.Add(LinkTableGlobalGenre); + } + + await CouchLogDB.SaveChangesAsync(); + NavigationManager.NavigateTo(NavigationManager.Uri, true); } + /// + /// + /// + /// + /// private bool IsInPrivateList(int GolbalEntityId) { return UserPrivateEntityIds.Contains(GolbalEntityId); } + /// + /// + /// + /// + /// private async Task AddToPrivateList(GlobalEntity GlobalEntity) { - var AuthState = await AuthenticationStateProvider.GetAuthenticationStateAsync(); - var User = AuthState.User; - if(User.Identity?.IsAuthenticated == true) { - //var AppUser = await UserManager.GetUserAsync(User); if(!IsInPrivateList(GlobalEntity.Id)) { + if (AppUser is null) + { + throw new InvalidOperationException("User not loaded or not logged in."); + } + PrivateEntity PrivateEntity = new() { - UserId = (await UserManager.GetUserAsync(User))!.Id, + UserId = AppUser.Id, CreationTime = DateTime.Now, GlobalEntityId = GlobalEntity.Id, }; @@ -144,4 +259,12 @@ } } } + + private void AddGenreToList(int GenreId) + { + if (!GenreIds.Contains(GenreId)) + { + GenreIds.Add(GenreId); + } + } } \ No newline at end of file diff --git a/CouchLog/Components/Pages/GlobalList.razor.css b/CouchLog/Components/Pages/GlobalList.razor.css index acadcd3..344f6d3 100644 --- a/CouchLog/Components/Pages/GlobalList.razor.css +++ b/CouchLog/Components/Pages/GlobalList.razor.css @@ -7,9 +7,9 @@ color:aliceblue } -.Entity-Container:hover { - color: rgba(0,255,255,0.5); -} + .Entity-Container:hover { + color: rgba(0,255,255,0.5); + } .Entity-Container-Card { position: relative; @@ -74,4 +74,15 @@ .CreateNewGlobalEntity-Container { background: #111; border-radius: 15px; +} + +.Entity-Container-Menu-Button { + position: absolute; + top: 2px; + right: 7.5px; + background: transparent; + border: none; + color: #0dcaf0; + font-size: 2.5rem; + cursor: pointer; } \ No newline at end of file diff --git a/CouchLog/CouchLog.csproj b/CouchLog/CouchLog.csproj index 22ebf31..c077a66 100644 --- a/CouchLog/CouchLog.csproj +++ b/CouchLog/CouchLog.csproj @@ -4,6 +4,9 @@ net10.0 enable enable + Exe + True + True diff --git a/CouchLog/Data/CouchLog.db b/CouchLog/Data/CouchLog.db index 2c6304e..0cd1ecb 100644 Binary files a/CouchLog/Data/CouchLog.db and b/CouchLog/Data/CouchLog.db differ diff --git a/CouchLog/Data/DatabaseModels/Global/GlobalEntity.cs b/CouchLog/Data/DatabaseModels/Global/GlobalEntity.cs index ca7dc9c..82dd69c 100644 --- a/CouchLog/Data/DatabaseModels/Global/GlobalEntity.cs +++ b/CouchLog/Data/DatabaseModels/Global/GlobalEntity.cs @@ -10,7 +10,7 @@ namespace CouchLog.Data [Key] public int Id { get; set; } - [Required] + [Required (ErrorMessage = "Title is required")] [MaxLength(200)] public required string Title { get; set; } diff --git a/CouchLog/OnStartUp.cs b/CouchLog/OnStartUp.cs index 8de3ce3..82863e6 100644 --- a/CouchLog/OnStartUp.cs +++ b/CouchLog/OnStartUp.cs @@ -11,6 +11,38 @@ namespace CouchLog this.CouchLogDB = CouchLogDB; } + public void AddBasicGenresToDatabase() + { + List ExistingGenres = CouchLogDB.Genres.OrderByDescending(Genre => Genre.Id).ToList(); + + List Genres = new List + { + new() { Name = "Action", CreationTime = DateTime.Now }, + new() { Name = "Animation", CreationTime = DateTime.Now }, + new() { Name = "Comedy", CreationTime = DateTime.Now }, + new() { Name = "Crime", CreationTime = DateTime.Now }, + new() { Name = "Drama", CreationTime = DateTime.Now }, + new() { Name = "Fantasy", CreationTime = DateTime.Now }, + new() { Name = "History", CreationTime = DateTime.Now }, + new() { Name = "Horror", CreationTime = DateTime.Now }, + new() { Name = "Musical", CreationTime = DateTime.Now }, + new() { Name = "Miniseries", CreationTime = DateTime.Now }, + new() { Name = "Mystery", CreationTime = DateTime.Now }, + new() { Name = "Romance", CreationTime = DateTime.Now }, + new() { Name = "Science Fiction", CreationTime = DateTime.Now }, + new() { Name = "Thriller", CreationTime = DateTime.Now }, + new() { Name = "War", CreationTime = DateTime.Now }, + new() { Name = "Western", CreationTime = DateTime.Now }, + }; + + foreach (Genre Genre in Genres) + { + CouchLogDB.Add(Genre); + } + + CouchLogDB.SaveChangesAsync(); + } + public void AddBasicDatabaseEntries() { //################## diff --git a/CouchLog/Program.cs b/CouchLog/Program.cs index 28c78a0..d889d38 100644 --- a/CouchLog/Program.cs +++ b/CouchLog/Program.cs @@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore; using CouchLog.Components; using CouchLog.Components.Account; using CouchLog.Data; +using CouchLog; var builder = WebApplication.CreateBuilder(args); @@ -145,8 +146,9 @@ using (var scope = app.Services.CreateScope()) await CouchLogDB.SaveChangesAsync(); + //OnStartUp onStartUp = new(CouchLogDB); + //onStartUp.AddBasicGenresToDatabase(); + } -Console.WriteLine("ALL UP"); - app.Run(); \ No newline at end of file diff --git a/CouchLog/wwwroot/Pictures/James Bond Casino Royale_2b7bafde-0d3a-44ca-816f-54da65c2a482.jpg b/CouchLog/wwwroot/Pictures/James Bond Casino Royale_2b7bafde-0d3a-44ca-816f-54da65c2a482.jpg new file mode 100644 index 0000000..c29322f Binary files /dev/null and b/CouchLog/wwwroot/Pictures/James Bond Casino Royale_2b7bafde-0d3a-44ca-816f-54da65c2a482.jpg differ diff --git a/CouchLog/wwwroot/Pictures/James_Bond_Casino_Royale_2df9da94-dccd-42aa-a4fd-bd4f354f8655.jpg b/CouchLog/wwwroot/Pictures/James_Bond_Casino_Royale_2df9da94-dccd-42aa-a4fd-bd4f354f8655.jpg new file mode 100644 index 0000000..c29322f Binary files /dev/null and b/CouchLog/wwwroot/Pictures/James_Bond_Casino_Royale_2df9da94-dccd-42aa-a4fd-bd4f354f8655.jpg differ diff --git a/CouchLog/wwwroot/Pictures/James_Bond_Casino_Royale_6272acfb-d5b5-4e1c-a28c-38d2cc41393e.jpg b/CouchLog/wwwroot/Pictures/James_Bond_Casino_Royale_6272acfb-d5b5-4e1c-a28c-38d2cc41393e.jpg new file mode 100644 index 0000000..c29322f Binary files /dev/null and b/CouchLog/wwwroot/Pictures/James_Bond_Casino_Royale_6272acfb-d5b5-4e1c-a28c-38d2cc41393e.jpg differ diff --git a/CouchLog/wwwroot/Pictures/James_Bond_Casino_Royale_6c6e842a-409a-47e1-b0c7-8c9335438c65.jpg b/CouchLog/wwwroot/Pictures/James_Bond_Casino_Royale_6c6e842a-409a-47e1-b0c7-8c9335438c65.jpg new file mode 100644 index 0000000..c29322f Binary files /dev/null and b/CouchLog/wwwroot/Pictures/James_Bond_Casino_Royale_6c6e842a-409a-47e1-b0c7-8c9335438c65.jpg differ