From 4b1f234f2a8d28e227c9ff4ed071942df0c0d2f0 Mon Sep 17 00:00:00 2001 From: limited_dev Date: Wed, 25 Jan 2023 23:45:36 +0100 Subject: [PATCH] continued to work on ThreadPage.razor --- ImageBoardServerApp/Data/CommentData.cs | 3 + .../Data/Repository/PostsRepository.cs | 7 +- ImageBoardServerApp/Pages/Thread.razor | 8 -- ImageBoardServerApp/Pages/ThreadPage.razor | 42 +++++++ .../Shared/Components/Board.razor | 2 +- .../Shared/Components/Comment.razor | 62 ++++++++++ .../Shared/Components/Comment.razor.css | 57 +++++++++ .../Shared/Components/CommentForm.razor | 110 ++++++++++++++++++ .../Shared/Components/CommentForm.razor.css | 23 ++++ .../Shared/Components/Post.razor | 30 +++-- 10 files changed, 327 insertions(+), 17 deletions(-) delete mode 100644 ImageBoardServerApp/Pages/Thread.razor create mode 100644 ImageBoardServerApp/Pages/ThreadPage.razor create mode 100644 ImageBoardServerApp/Shared/Components/Comment.razor create mode 100644 ImageBoardServerApp/Shared/Components/Comment.razor.css create mode 100644 ImageBoardServerApp/Shared/Components/CommentForm.razor create mode 100644 ImageBoardServerApp/Shared/Components/CommentForm.razor.css diff --git a/ImageBoardServerApp/Data/CommentData.cs b/ImageBoardServerApp/Data/CommentData.cs index 84dd57c..bfc5e5d 100644 --- a/ImageBoardServerApp/Data/CommentData.cs +++ b/ImageBoardServerApp/Data/CommentData.cs @@ -37,4 +37,7 @@ public class CommentData [Required] public string Board { get; set; } + [Required] + public long CreatedAt { get; set; } + } \ No newline at end of file diff --git a/ImageBoardServerApp/Data/Repository/PostsRepository.cs b/ImageBoardServerApp/Data/Repository/PostsRepository.cs index d841e89..61138f4 100644 --- a/ImageBoardServerApp/Data/Repository/PostsRepository.cs +++ b/ImageBoardServerApp/Data/Repository/PostsRepository.cs @@ -23,7 +23,12 @@ public static class PostsRepository public static async Task getPostByIdAsync(int postId) { await using var db = new AppDBContext(); - return await db.Posts.FirstOrDefaultAsync(post => post.PostID == postId); + return await db.Posts + .Where(post => post.PostID == postId) + .Include(post => post.Image) + .Include(post => post.Comments) + .FirstOrDefaultAsync(); + //return await db.Posts.FirstOrDefaultAsync(post => post.PostID == postId); } public static async Task createPostAsync(PostData postToCreate) diff --git a/ImageBoardServerApp/Pages/Thread.razor b/ImageBoardServerApp/Pages/Thread.razor deleted file mode 100644 index fdfbaba..0000000 --- a/ImageBoardServerApp/Pages/Thread.razor +++ /dev/null @@ -1,8 +0,0 @@ -@page "/{board}/thread/{threadId}" -

Thread #xx on /x/

- -@RouteData.Values["title"] - -@code { - -} \ No newline at end of file diff --git a/ImageBoardServerApp/Pages/ThreadPage.razor b/ImageBoardServerApp/Pages/ThreadPage.razor new file mode 100644 index 0000000..a2349e7 --- /dev/null +++ b/ImageBoardServerApp/Pages/ThreadPage.razor @@ -0,0 +1,42 @@ +@page "/{boardName}/thread/{threadId}" +@using System.ComponentModel.DataAnnotations +@using ImageBoardServerApp.Data.Repository +@inject NavigationManager NavigationManager + +

Thread #@threadId on /@boardName/

+ +
+ +@foreach (var comment in post.Comments) +{ + +
+} + + +@code { + [Parameter] + [Required] + public string boardName { get; set; } + + [Parameter] + [Required] + public string threadId { get; set; } + + private PostData post; + + protected override async Task OnInitializedAsync() + { + try + { + post = await PostsRepository.getPostByIdAsync(int.Parse(threadId)); + } + catch (FormatException fe) + { + NavigationManager.NavigateTo("/notfound"); + return; + } + if(post == null) + NavigationManager.NavigateTo("/notfound"); + } +} \ No newline at end of file diff --git a/ImageBoardServerApp/Shared/Components/Board.razor b/ImageBoardServerApp/Shared/Components/Board.razor index 3a66240..4ad8acc 100644 --- a/ImageBoardServerApp/Shared/Components/Board.razor +++ b/ImageBoardServerApp/Shared/Components/Board.razor @@ -23,7 +23,7 @@ { @foreach(var post in posts) { - +
} } diff --git a/ImageBoardServerApp/Shared/Components/Comment.razor b/ImageBoardServerApp/Shared/Components/Comment.razor new file mode 100644 index 0000000..2bf523c --- /dev/null +++ b/ImageBoardServerApp/Shared/Components/Comment.razor @@ -0,0 +1,62 @@ +@using System.ComponentModel.DataAnnotations +@using ImageBoardServerApp.Data + +
+ [ + @toggleText + ] + @comment.Username + @getTimeFromUnix(comment.CreatedAt) + No.@comment.CommentID +
+@if (opened) +{ +
+
+ @if (@comment.Image != null) + { + No Image found + } + else + { + [No Image] + } +
+
+ @foreach (string s in @comment.Content.Split("\n")) + { + @s + } +
+
+
+ [ + Report + ] +
+} + + + +@code { + private static DateTime getTimeFromUnix(double javaTimeStamp) + { + var dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); + dateTime = dateTime.AddMilliseconds( javaTimeStamp ).ToLocalTime(); + return dateTime; + } + + private bool opened = true; + + private string toggleText = "-"; + + private void ToggleOpened() + { + opened = !opened; + toggleText = opened ? "-" : "+"; + } + + [Parameter] + [Required] + public CommentData comment { get; set; } +} \ No newline at end of file diff --git a/ImageBoardServerApp/Shared/Components/Comment.razor.css b/ImageBoardServerApp/Shared/Components/Comment.razor.css new file mode 100644 index 0000000..da7824a --- /dev/null +++ b/ImageBoardServerApp/Shared/Components/Comment.razor.css @@ -0,0 +1,57 @@ +.toggleOpened{ + color: #0a58ca; + text-decoration: none; +} + +.toggleOpened:hover{ + color: #0a58ca; !important; + cursor: pointer; +} + +.title{ + color: #1e5aaf; +} + +.name{ + color: #339305; +} + +.threadHeader{ + text-align: left; +} + +.threadFooter{ + text-align: right; !important; + align-self: end; !important; +} + +.threadContent{ + text-align: left; + display: flex; +} + +.threadImage{ + margin: 6px; + max-width: 500px; + max-height: 500px; + padding: 5px; +} + +.threadImage img{ + max-width:150px; + width: 100%; +} + +.threadImage img:hover{ + transform: scale(3); + /*max-width:500px; + width: 100%; */ +} + +.threadText{ + display: grid; +} + +.threadTextContainer{ + margin: 0; +} \ No newline at end of file diff --git a/ImageBoardServerApp/Shared/Components/CommentForm.razor b/ImageBoardServerApp/Shared/Components/CommentForm.razor new file mode 100644 index 0000000..c654871 --- /dev/null +++ b/ImageBoardServerApp/Shared/Components/CommentForm.razor @@ -0,0 +1,110 @@ +@using System.ComponentModel.DataAnnotations +@using ImageBoardServerApp.Data.Repository + +@inject NavigationManager NavigationManager + +
+ [ + @toggleText + ] +
+@if (opened) +{ +
+ Comment on @post.Title by @post.Username in @post.Board + +
+ OnChange(args, "username")) Class="w-100"/> +
+ +
+ OnChange(args, "Content")) Class="w-100"/> +
+ +
+ +
+ +
+} + + +@code { + + private bool opened = false; + + private string toggleText = "Open Comment Formula"; + + private void ToggleOpened() + { + opened = !opened; + toggleText = opened ? "Close Comment Formula" : "Open Comment Formula"; + } + + [Parameter] + [Required] + public PostData post { get; set; } + + string postUsername = "Anonymous"; + string postContent = ""; + + void OnChange(string value, string name) + { + switch (name) + { + case "username": + postUsername = value; + if (value == "") + { + postUsername = "Anonymous"; + } + break; + case "content": + postContent = value; + break; + default: + Console.WriteLine("not found."); + break; + } + Console.WriteLine($"Smth changed!: {value}"); + } + + private async Task onPostClick() + { + var userToCreate = new UserData + { + Ipv4Address = "192.168.178.101", + Banned = false, + lastActionTimeStamp = DateTime.Now.Millisecond + }; + int userID = await UsersRepository.createUserAsync(userToCreate); + + var commentToCreate = new CommentData() + { + PostID = post.PostID, + UserID = userID, + Content = postContent, + Username = postUsername, + Board = post.Board, + CreatedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds() + /* + * UserID = userID, + Post = post, + Username = postUsername, + Content = postContent, + CreatedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds(), + Board = post.Board + */ + }; + int commentId = await CommentsRepository.createCommentAsync(commentToCreate); + if (commentId == -1) + { + //Open comment unsucessfull + NavigationManager.NavigateTo("/UnSuccessfulPost"); + Console.WriteLine("Shit sucks and did not work."); + return; + } + //comment successfull + Console.WriteLine("Post created"); + } +} \ No newline at end of file diff --git a/ImageBoardServerApp/Shared/Components/CommentForm.razor.css b/ImageBoardServerApp/Shared/Components/CommentForm.razor.css new file mode 100644 index 0000000..fc59e5e --- /dev/null +++ b/ImageBoardServerApp/Shared/Components/CommentForm.razor.css @@ -0,0 +1,23 @@ +.toggleOpened{ + color: #0a58ca; + text-decoration: none; +} + +.toggleOpened:hover{ + color: #0a58ca; !important; + cursor: pointer; +} + +.centered { + text-align: center; + justify-content: center; + align-items: center; +} + +.pd { + padding: 5px; +} + +.marg{ + margin: 2px +} \ No newline at end of file diff --git a/ImageBoardServerApp/Shared/Components/Post.razor b/ImageBoardServerApp/Shared/Components/Post.razor index a2a4d50..2c08a0a 100644 --- a/ImageBoardServerApp/Shared/Components/Post.razor +++ b/ImageBoardServerApp/Shared/Components/Post.razor @@ -2,9 +2,12 @@ @using ImageBoardServerApp.Data
- [ - @toggleText - ] + @if (showOpenThread) + { + [ + @toggleText + ] + } @post.Title @post.Username @getTimeFromUnix(post.CreatedAt) @@ -14,7 +17,6 @@ {
- @if (@post.Image != null) { No Image found @@ -36,9 +38,18 @@ [ Report ] - [ - (@post.Comments.Count) Open Thread - ] + @if (showOpenThread) + { + [ + (@post.Comments.Count) Open Thread + ] + } + else + { + [ + @post.Comments.Count Comments + ] + }
} @@ -65,4 +76,9 @@ [Parameter] [Required] public PostData post { get; set; } + + + [Parameter] + [Required] + public bool showOpenThread { get; set; } } \ No newline at end of file