bulletboards/ImageBoardServerApp/Shared/Components/Forms/CommentForm.razor

172 lines
5.1 KiB
Text
Raw Normal View History

2023-01-25 22:45:36 +00:00
@using System.ComponentModel.DataAnnotations
@using ImageBoardServerApp.Data.Repository
@inject NavigationManager navigationManager
2023-01-25 22:45:36 +00:00
<div>
<span>[</span>
<a class="toggleOpened" onclick="@ToggleOpened">@toggleText</a>
<span>]</span>
</div>
@if (opened)
{
<div class="pd centered">
2023-01-26 08:07:25 +00:00
<span>Comment on @post.Title in /@post.Board/</span>
<FormInfo/>
2023-01-25 22:45:36 +00:00
<div class="centered formContent">
<div>
<div class="pd centered marg">
@if (image != null)
{
<img class="formImage" src="@($"data:image/png;base64,{Convert.ToBase64String(image)}")" alt="No Image"/>
}
</div>
<InputFile OnChange="@SingleUpload" type="file" accept="image/*"/>
</div>
<div>
<div class="pd centered marg">
<RadzenTextBox Placeholder="Username (Anonymous)" MaxLength="15" Change=@(args => OnChange(args, "username")) Class="w-100"/>
</div>
<div class="pd centered marg">
<RadzenTextArea Placeholder="Comment..." @bind-Value="@postContent" Cols="30" Rows="6" Change=@(args => OnChange(args, "Content")) Class="w-100"/>
</div>
</div>
2023-01-25 22:45:36 +00:00
</div>
<div class="pd centered marg">
<FormInfo/>
2023-01-25 22:45:36 +00:00
<RadzenButton class="pd" Click="@onPostClick" Text="Comment!"></RadzenButton>
</div>
</div>
}
@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}");
}
2023-01-26 08:07:25 +00:00
private async Task SingleUpload(InputFileChangeEventArgs e)
{
MemoryStream ms = new MemoryStream();
await e.File.OpenReadStream().CopyToAsync(ms);
var bytes = ms.ToArray();
image = bytes;
Console.WriteLine("File has been selected!");
ms.Close();
}
private Byte[] image = null;
2023-01-25 22:45:36 +00:00
private async Task onPostClick()
{
var userToCreate = new UserData
{
Ipv4Address = "192.168.178.101",
Banned = false,
lastActionTimeStamp = DateTime.Now.Millisecond
};
int userID;
UserData foundusr = await UsersRepository.getUserByIPAsync(userToCreate.Ipv4Address);
if (foundusr == null)
{
userID = await UsersRepository.createUserAsync(userToCreate);
}
else
{
userID = foundusr.UserID;
if(foundusr.Banned)
return;
foundusr.lastActionTimeStamp = DateTime.Now.Millisecond;
await UsersRepository.updateUserAsync(foundusr);
}
2023-01-26 08:07:25 +00:00
bool hasImage = image != null;
2023-01-26 08:07:25 +00:00
CommentData commentToCreate;
if (hasImage)
2023-01-25 22:45:36 +00:00
{
2023-01-26 08:07:25 +00:00
var imageToUpload = new ImageData
{
Board = post.Board,
Image = image
};
int imageID = await ImagesRepository.createImageAsync(imageToUpload);
commentToCreate = new CommentData()
{
PostID = post.PostID,
UserID = userID,
ImageID = imageID,
Content = postContent,
Username = postUsername,
Board = post.Board,
CreatedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds()
};
}
else
{
commentToCreate = new CommentData()
{
PostID = post.PostID,
UserID = userID,
Content = postContent,
Username = postUsername,
Board = post.Board,
CreatedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds()
};
}
2023-01-25 22:45:36 +00:00
int commentId = await CommentsRepository.createCommentAsync(commentToCreate);
if (commentId == -1)
{
//Open comment unsucessfull
navigationManager.NavigateTo("/UnSuccessfulPost");
2023-01-25 22:45:36 +00:00
Console.WriteLine("Shit sucks and did not work.");
return;
}
//comment successfull
Console.WriteLine("Post created");
navigationManager.NavigateTo($"/{post.Board}/thread/{post.PostID}", true, true);
opened = false;
2023-01-25 22:45:36 +00:00
}
}