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

167 lines
No EOL
4.9 KiB
Text

@using Radzen
@using System.ComponentModel.DataAnnotations
@using System.IO.Pipelines
@using System.Net.Mime
@using System.Reflection
@using System.Runtime.CompilerServices
@using ImageBoardServerApp.Data
@using ImageBoardServerApp.Data.Repository
@inject NavigationManager NavigationManager
<div>
<span>[</span>
<a class="toggleOpened" onclick="@ToggleOpened">@toggleText</a>
<span>]</span>
</div>
@if (opened)
{
<div class="pd centered">
<span>Post to /@board.Tag/ - @board.Topic</span>
<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">
<RadzenTextBox Placeholder="Title" MaxLength="20" Change=@(args => OnChange(args, "title")) Class="w-100"/>
</div>
<div class="pd centered marg">
<RadzenTextArea Placeholder="Content..." @bind-Value="@postContent" Cols="30" Rows="6" Change=@(args => OnChange(args, "Content")) Class="w-100"/>
</div>
</div>
</div>
<div class="pd centered marg">
<FormInfo/>
<RadzenButton class="pd" Click="@onPostClick" Text="Post!"></RadzenButton>
</div>
</div>
}
@code {
private bool opened = false;
private string toggleText = "Open Post Formula";
private void ToggleOpened()
{
opened = !opened;
toggleText = opened ? "Close Post Formula" : "Open Post Formula";
}
[Parameter]
[Required]
public BoardData board { get; set; } = new BoardData();
string postUsername = "Anonymous";
string postTitle = "";
string postContent = "";
void OnChange(string value, string name)
{
switch (name)
{
case "title":
postTitle = value;
break;
case "username":
postUsername = value;
break;
case "content":
postContent = value;
break;
default:
Console.WriteLine("not found.");
break;
}
Console.WriteLine($"Smth changed!: {value}");
}
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;
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);
}
//TODO Add check if data is image
var imageToUpload = new ImageData
{
Board = board.Tag,
Image = image
};
int imageID = await ImagesRepository.createImageAsync(imageToUpload);
var postToPost = new PostData
{
UserID = userID,
ImageID = imageID,
Username = postUsername,
Title = postTitle,
Content = postContent,
Interactions = 0,
CreatedAt = DateTimeOffset.Now.ToUnixTimeMilliseconds(),
Board = board.Tag
};
int postId = await PostsRepository.createPostAsync(postToPost);
if (postId != -1)
{
//Open post successfull
NavigationManager.NavigateTo($"/{board.Tag}/thread/{postId}", true, true);
Console.WriteLine("Post created");
}
else
{
//Open post unsucessfull
NavigationManager.NavigateTo("/UnSuccessfulPost");
Console.WriteLine("Shit sucks and did not work.");
}
}
}