import from other repo
This commit is contained in:
commit
d8a81fdfc0
53 changed files with 2243 additions and 0 deletions
18
ImageBoardServerApp/Data/AccountData.cs
Normal file
18
ImageBoardServerApp/Data/AccountData.cs
Normal file
|
@ -0,0 +1,18 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace ImageBoardServerApp.Data;
|
||||
|
||||
public class AccountData
|
||||
{
|
||||
[Required]
|
||||
[DatabaseGenerated((DatabaseGeneratedOption.Identity))]
|
||||
[Key]
|
||||
public int AccountID { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Email { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Password { get; set; }
|
||||
}
|
36
ImageBoardServerApp/Data/AppDBContext.cs
Normal file
36
ImageBoardServerApp/Data/AppDBContext.cs
Normal file
|
@ -0,0 +1,36 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ImageBoardServerApp.Data;
|
||||
|
||||
internal sealed class AppDBContext : DbContext
|
||||
{
|
||||
public DbSet<UserData> Users { get; set; }
|
||||
public DbSet<PostData> Posts { get; set; }
|
||||
public DbSet<ImageData> Images { get; set; }
|
||||
public DbSet<CommentData> Comments { get; set; }
|
||||
public DbSet<AccountData> Accounts { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
=> optionsBuilder.UseSqlite("Data Source=./Data/Nils.db");
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder mb)
|
||||
{
|
||||
mb.Entity<PostData>()
|
||||
.HasMany(post => post.Comments)
|
||||
.WithOne(comment => comment.Post);
|
||||
mb.Entity<PostData>()
|
||||
.HasOne(post => post.Image)
|
||||
.WithOne(image => image.Post);
|
||||
|
||||
mb.Entity<CommentData>()
|
||||
.HasOne(comment => comment.Image)
|
||||
.WithOne(image => image.Comment);
|
||||
|
||||
mb.Entity<UserData>()
|
||||
.HasMany(user => user.Posts)
|
||||
.WithOne(post => post.User);
|
||||
mb.Entity<UserData>()
|
||||
.HasMany(user => user.Comments)
|
||||
.WithOne(comment => comment.User);
|
||||
}
|
||||
}
|
16
ImageBoardServerApp/Data/BoardData.cs
Normal file
16
ImageBoardServerApp/Data/BoardData.cs
Normal file
|
@ -0,0 +1,16 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace ImageBoardServerApp.Data;
|
||||
|
||||
public class BoardData
|
||||
{
|
||||
public int BoardID { get; set; }
|
||||
|
||||
public string Tag { get; set; }
|
||||
|
||||
public string Topic { get; set; }
|
||||
|
||||
public int maxThreads { get; set; }
|
||||
|
||||
|
||||
}
|
40
ImageBoardServerApp/Data/CommentData.cs
Normal file
40
ImageBoardServerApp/Data/CommentData.cs
Normal file
|
@ -0,0 +1,40 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using ImageBoardServerApp.Shared.Components;
|
||||
|
||||
namespace ImageBoardServerApp.Data;
|
||||
|
||||
public class CommentData
|
||||
{
|
||||
[Required]
|
||||
[DatabaseGenerated((DatabaseGeneratedOption.Identity))]
|
||||
[Key]
|
||||
public int CommentID { get; set; }
|
||||
|
||||
[Required]
|
||||
public int PostID { get; set; }
|
||||
|
||||
//[ForeignKey("PostID")]
|
||||
public virtual PostData Post { get; set; }
|
||||
|
||||
[Required]
|
||||
public int UserID { get; set; }
|
||||
|
||||
//[ForeignKey("UserID")]
|
||||
public UserData User { get; set; }
|
||||
|
||||
//[ForeignKey("ImageID")]
|
||||
public virtual ImageData Image { get; set; }
|
||||
|
||||
public int ImageID { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Content { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Username { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Board { get; set; }
|
||||
|
||||
}
|
23
ImageBoardServerApp/Data/ImageData.cs
Normal file
23
ImageBoardServerApp/Data/ImageData.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace ImageBoardServerApp.Data;
|
||||
|
||||
public class ImageData
|
||||
{
|
||||
[Required]
|
||||
[DatabaseGenerated((DatabaseGeneratedOption.Identity))]
|
||||
[Key]
|
||||
public int ImageID { get; set; }
|
||||
|
||||
|
||||
[Required]
|
||||
public string Board { get; set; }
|
||||
|
||||
[Required]
|
||||
public Byte[] Image { get; set; }
|
||||
|
||||
public PostData Post { get; set; }
|
||||
|
||||
public CommentData Comment { get; set; }
|
||||
}
|
43
ImageBoardServerApp/Data/PostData.cs
Normal file
43
ImageBoardServerApp/Data/PostData.cs
Normal file
|
@ -0,0 +1,43 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace ImageBoardServerApp.Data;
|
||||
|
||||
public class PostData
|
||||
{
|
||||
[Required]
|
||||
[DatabaseGenerated((DatabaseGeneratedOption.Identity))]
|
||||
[Key]
|
||||
public int PostID { get; set; }
|
||||
|
||||
[Required]
|
||||
public int UserID { get; set; }
|
||||
|
||||
//[ForeignKey("UserID")]
|
||||
public UserData User { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ImageID { get; set; }
|
||||
|
||||
//[ForeignKey("ImageID")]
|
||||
public ImageData Image { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Username { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Title { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Content { get; set; }
|
||||
|
||||
public string Interactions { get; set; }
|
||||
|
||||
[Required]
|
||||
public long CreatedAt { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Board { get; set; }
|
||||
|
||||
public List<CommentData> Comments { get; set; }
|
||||
}
|
54
ImageBoardServerApp/Data/Repository/CommentsRepository.cs
Normal file
54
ImageBoardServerApp/Data/Repository/CommentsRepository.cs
Normal file
|
@ -0,0 +1,54 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ImageBoardServerApp.Data.Repository;
|
||||
|
||||
public static class CommentsRepository
|
||||
{
|
||||
public static async Task<List<CommentData>> getCommentsAsync()
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
return await db.Comments.ToListAsync();
|
||||
}
|
||||
|
||||
public static async Task<List<CommentData>> getCommentsByBoardAsync(string board)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
return await db.Comments
|
||||
.Where(comment => comment.Board.Equals(board))
|
||||
.Include(comment => comment.Image)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public static async Task<CommentData> getCommentByIdAsync(int postId)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
return await db.Comments.FirstOrDefaultAsync(comment => comment.PostID == postId);
|
||||
}
|
||||
|
||||
public static async Task<int> createCommentAsync(CommentData commentData)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
await db.Comments.AddAsync(commentData);
|
||||
if (await db.SaveChangesAsync() >= 1)
|
||||
{
|
||||
Console.WriteLine($"Created comment with ID: {commentData.PostID}");
|
||||
return commentData.PostID;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static async Task<bool> updateCommentAsync(CommentData commentToUpdate)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
db.Comments.Update(commentToUpdate);
|
||||
return await db.SaveChangesAsync() >= 1;
|
||||
}
|
||||
|
||||
public static async Task<bool> deleteCommentAsync(int postId)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
CommentData commentToDelete = await getCommentByIdAsync(postId);
|
||||
db.Remove(commentToDelete);
|
||||
return await db.SaveChangesAsync() >= 1;
|
||||
}
|
||||
}
|
46
ImageBoardServerApp/Data/Repository/ImagesRepository.cs
Normal file
46
ImageBoardServerApp/Data/Repository/ImagesRepository.cs
Normal file
|
@ -0,0 +1,46 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ImageBoardServerApp.Data.Repository;
|
||||
|
||||
public static class ImagesRepository
|
||||
{
|
||||
public static async Task<List<ImageData>> getImagesAsync()
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
return await db.Images.ToListAsync();
|
||||
}
|
||||
|
||||
public static async Task<ImageData> getImageByIdAsync(int imageId)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
return await db.Images.FirstOrDefaultAsync(image => image.ImageID == imageId);
|
||||
}
|
||||
|
||||
public static async Task<int> createImageAsync(ImageData imageToCreate)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
await db.Images.AddAsync(imageToCreate);
|
||||
if (await db.SaveChangesAsync() >= 1)
|
||||
{
|
||||
Console.WriteLine($"Created image with ID: {imageToCreate.ImageID}");
|
||||
return imageToCreate.ImageID;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static async Task<bool> updateImageAsync(ImageData imageToCreate)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
db.Images.Update(imageToCreate);
|
||||
return await db.SaveChangesAsync() >= 1;
|
||||
}
|
||||
|
||||
public static async Task<bool> deleteImageAsync(int imageId)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
ImageData imageToDelete = await getImageByIdAsync(imageId);
|
||||
db.Remove(imageToDelete);
|
||||
return await db.SaveChangesAsync() >= 1;
|
||||
}
|
||||
}
|
54
ImageBoardServerApp/Data/Repository/PostsRepository.cs
Normal file
54
ImageBoardServerApp/Data/Repository/PostsRepository.cs
Normal file
|
@ -0,0 +1,54 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ImageBoardServerApp.Data.Repository;
|
||||
|
||||
public static class PostsRepository
|
||||
{
|
||||
public static async Task<List<PostData>> getPostsAsync()
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
return await db.Posts.ToListAsync();
|
||||
}
|
||||
|
||||
public static async Task<List<PostData>> getPostsByBoardAsync(string board)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
return await db.Posts
|
||||
.Where(post => post.Board.Equals(board))
|
||||
.Include(post => post.Image)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public static async Task<PostData> getPostByIdAsync(int postId)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
return await db.Posts.FirstOrDefaultAsync(post => post.PostID == postId);
|
||||
}
|
||||
|
||||
public static async Task<int> createPostAsync(PostData postToCreate)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
await db.Posts.AddAsync(postToCreate);
|
||||
if (await db.SaveChangesAsync() >= 1)
|
||||
{
|
||||
Console.WriteLine($"Created post with ID: {postToCreate.PostID}");
|
||||
return postToCreate.PostID;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static async Task<bool> updatePostAsync(PostData postToUpdate)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
db.Posts.Update(postToUpdate);
|
||||
return await db.SaveChangesAsync() >= 1;
|
||||
}
|
||||
|
||||
public static async Task<bool> deletePostAsync(int postId)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
PostData postToDelete = await getPostByIdAsync(postId);
|
||||
db.Remove(postToDelete);
|
||||
return await db.SaveChangesAsync() >= 1;
|
||||
}
|
||||
}
|
46
ImageBoardServerApp/Data/Repository/UsersRepository.cs
Normal file
46
ImageBoardServerApp/Data/Repository/UsersRepository.cs
Normal file
|
@ -0,0 +1,46 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ImageBoardServerApp.Data.Repository;
|
||||
|
||||
public static class UsersRepository
|
||||
{
|
||||
public static async Task<List<UserData>> getUsersAsync()
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
return await db.Users.ToListAsync();
|
||||
}
|
||||
|
||||
public static async Task<UserData> getUserByIdAsync(int userId)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
return await db.Users.FirstOrDefaultAsync(user => user.UserID == userId);
|
||||
}
|
||||
|
||||
public static async Task<int> createUserAsync(UserData userToCreate)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
await db.Users.AddAsync(userToCreate);
|
||||
if (await db.SaveChangesAsync() >= 1)
|
||||
{
|
||||
Console.WriteLine($"Created user with ID: {userToCreate.UserID}");
|
||||
return userToCreate.UserID;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static async Task<bool> updateUserAsync(UserData userToUpdate)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
db.Users.Update(userToUpdate);
|
||||
return await db.SaveChangesAsync() >= 1;
|
||||
}
|
||||
|
||||
public static async Task<bool> deleteUserAsync(int userId)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
UserData userToDelete = await getUserByIdAsync(userId);
|
||||
db.Remove(userToDelete);
|
||||
return await db.SaveChangesAsync() >= 1;
|
||||
}
|
||||
}
|
27
ImageBoardServerApp/Data/UserData.cs
Normal file
27
ImageBoardServerApp/Data/UserData.cs
Normal file
|
@ -0,0 +1,27 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.Net;
|
||||
|
||||
namespace ImageBoardServerApp.Data;
|
||||
|
||||
public class UserData
|
||||
{
|
||||
|
||||
[Required]
|
||||
[DatabaseGenerated((DatabaseGeneratedOption.Identity))]
|
||||
[Key]
|
||||
public int UserID { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Ipv4Address { get; set; }
|
||||
|
||||
[Required]
|
||||
public bool Banned { get; set; }
|
||||
|
||||
[Required]
|
||||
public long lastActionTimeStamp { get; set; }
|
||||
|
||||
public List<PostData> Posts { get; set; }
|
||||
|
||||
public List<CommentData> Comments { get; set; }
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue