From 050f0bc935937b8b80500a59bc6ac5513fd7af93 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Mon, 24 Nov 2025 14:00:13 -0500 Subject: [PATCH] feat: initial commit --- .dockerignore | 30 ++ .gitignore | 326 ++++++++++++++++++ SharpbinV3.slnx | 3 + SharpbinV3/Controllers/UserController.cs | 50 +++ SharpbinV3/DTOs/CreateUserDto.cs | 20 ++ SharpbinV3/Data/AppDbContext.cs | 11 + SharpbinV3/Data/Entities/Paste.cs | 31 ++ SharpbinV3/Data/Entities/User.cs | 23 ++ SharpbinV3/Dockerfile | 30 ++ .../20251124021817_InitialCreate.Designer.cs | 122 +++++++ .../20251124021817_InitialCreate.cs | 77 +++++ ...20251124181150_AddPasteIndexes.Designer.cs | 143 ++++++++ .../20251124181150_AddPasteIndexes.cs | 95 +++++ .../20251124184531_AddVisibility.Designer.cs | 154 +++++++++ .../20251124184531_AddVisibility.cs | 60 ++++ .../Migrations/AppDbContextModelSnapshot.cs | 151 ++++++++ SharpbinV3/Program.cs | 48 +++ SharpbinV3/Properties/launchSettings.json | 31 ++ SharpbinV3/Services/IUserService.cs | 13 + SharpbinV3/Services/UserService.cs | 33 ++ SharpbinV3/SharpbinV3.csproj | 35 ++ SharpbinV3/SharpbinV3.http | 6 + SharpbinV3/appsettings.Development.json | 8 + SharpbinV3/appsettings.json | 12 + 24 files changed, 1512 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 SharpbinV3.slnx create mode 100644 SharpbinV3/Controllers/UserController.cs create mode 100644 SharpbinV3/DTOs/CreateUserDto.cs create mode 100644 SharpbinV3/Data/AppDbContext.cs create mode 100644 SharpbinV3/Data/Entities/Paste.cs create mode 100644 SharpbinV3/Data/Entities/User.cs create mode 100644 SharpbinV3/Dockerfile create mode 100644 SharpbinV3/Migrations/20251124021817_InitialCreate.Designer.cs create mode 100644 SharpbinV3/Migrations/20251124021817_InitialCreate.cs create mode 100644 SharpbinV3/Migrations/20251124181150_AddPasteIndexes.Designer.cs create mode 100644 SharpbinV3/Migrations/20251124181150_AddPasteIndexes.cs create mode 100644 SharpbinV3/Migrations/20251124184531_AddVisibility.Designer.cs create mode 100644 SharpbinV3/Migrations/20251124184531_AddVisibility.cs create mode 100644 SharpbinV3/Migrations/AppDbContextModelSnapshot.cs create mode 100644 SharpbinV3/Program.cs create mode 100644 SharpbinV3/Properties/launchSettings.json create mode 100644 SharpbinV3/Services/IUserService.cs create mode 100644 SharpbinV3/Services/UserService.cs create mode 100644 SharpbinV3/SharpbinV3.csproj create mode 100644 SharpbinV3/SharpbinV3.http create mode 100644 SharpbinV3/appsettings.Development.json create mode 100644 SharpbinV3/appsettings.json diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..fe1152b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,30 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/azds.yaml +**/bin +**/charts +**/docker-compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md +!**/.gitignore +!.git/HEAD +!.git/config +!.git/packed-refs +!.git/refs/heads/** \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5820705 --- /dev/null +++ b/.gitignore @@ -0,0 +1,326 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Oo]ut/ +[Ll]og/ +[Ll]ogs/ + +p/ +privp/ +raw/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop6 +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessar6 +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment i6 +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# Database +**/*.db +**/*.wal +**/*.db-wal +**/*.db-shm +**/*.sqlite \ No newline at end of file diff --git a/SharpbinV3.slnx b/SharpbinV3.slnx new file mode 100644 index 0000000..58d2f19 --- /dev/null +++ b/SharpbinV3.slnx @@ -0,0 +1,3 @@ + + + diff --git a/SharpbinV3/Controllers/UserController.cs b/SharpbinV3/Controllers/UserController.cs new file mode 100644 index 0000000..508f903 --- /dev/null +++ b/SharpbinV3/Controllers/UserController.cs @@ -0,0 +1,50 @@ +using Microsoft.AspNetCore.Mvc; +using SharpbinV3.DTOs; +using SharpbinV3.Services; + +namespace SharpbinV3.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class UserController(IUserService userService) : ControllerBase + { + private readonly IUserService _userService = userService; + + [HttpPost] + public async Task Create([FromBody] CreateUserDto dto) + { + if (await _userService.GetByUsername(dto.Username) != null) + return Conflict("Username already exists."); + + if (!string.IsNullOrEmpty(dto.Email) && await _userService.GetByEmail(dto.Email) != null) + return Conflict("Email already exists."); + + var user = await _userService.CreateUser(dto.Username, dto.Password, dto.Email, dto.DisplayName); + + return CreatedAtAction(nameof(GetByUid), new { uid = user.UID }, new { user.UID, user.Username, user.UUID }); + } + + [HttpGet("{uid:int}")] + public async Task GetByUid(int uid) + { + var user = await _userService.GetByUID(uid); + if (user == null) return NotFound(); + + return Ok(new { user.UID, user.Username, user.UUID, user.DisplayName }); + } + [HttpGet("username/{username:string}")] + public async Task GetByUsername(string username) + { + var user = await _userService.GetByUsername(username); + if (user == null) return NotFound(); + return Ok(new { user.UID, user.Username, user.UUID, user.DisplayName }); + } + [HttpGet("uuid/{uuid:guid}")] + public async Task GetByUuid(Guid uuid) + { + var user = await _userService.GetByUUID(uuid); + if (user == null) return NotFound(); + return Ok(new { user.UID, user.Username, user.UUID, user.DisplayName }); + } + } +} diff --git a/SharpbinV3/DTOs/CreateUserDto.cs b/SharpbinV3/DTOs/CreateUserDto.cs new file mode 100644 index 0000000..1865ad6 --- /dev/null +++ b/SharpbinV3/DTOs/CreateUserDto.cs @@ -0,0 +1,20 @@ +using System.ComponentModel.DataAnnotations; + +namespace SharpbinV3.DTOs +{ + public class CreateUserDto + { + [Required] + [StringLength(50, MinimumLength = 3)] + public required string Username { get; set; } + + [Required] + [StringLength(128, MinimumLength = 6)] + public required string Password { get; set; } + + [EmailAddress] + public string? Email { get; set; } + + public string? DisplayName { get; set; } + } +} diff --git a/SharpbinV3/Data/AppDbContext.cs b/SharpbinV3/Data/AppDbContext.cs new file mode 100644 index 0000000..a6d14c4 --- /dev/null +++ b/SharpbinV3/Data/AppDbContext.cs @@ -0,0 +1,11 @@ +using Microsoft.EntityFrameworkCore; +using SharpbinV3.Data.Entities; + +namespace SharpbinV3.Data +{ + public sealed class AppDbContext(DbContextOptions options) : DbContext(options) + { + public DbSet Users => Set(); + public DbSet Pastes => Set(); + } +} diff --git a/SharpbinV3/Data/Entities/Paste.cs b/SharpbinV3/Data/Entities/Paste.cs new file mode 100644 index 0000000..032bd5e --- /dev/null +++ b/SharpbinV3/Data/Entities/Paste.cs @@ -0,0 +1,31 @@ +using System.ComponentModel.DataAnnotations; +using Microsoft.EntityFrameworkCore; + +namespace SharpbinV3.Data.Entities +{ + [Index(nameof(PID), IsUnique = true)] + [Index(nameof(UUID), IsUnique = true)] + [Index(nameof(ID), IsUnique = true)] + [Index(nameof(AuthorUUID))] + [Index(nameof(ExpiresAt))] + [Index(nameof(Views))] + [Index(nameof(Visiblity))] + public sealed class Paste + { + [Key] + public int PID { get; set; } + public required string UUID { get; set; } // uuid v7 includes timestamp, this counts as created at + public required string ID { get; set; } + public string? Title { get; set; } + public required string AuthorUUID { get; set; } // uuid of the user who created the paste, or "ANON" for anonymous pastes + public User? User { get; set; } + public required string FilePath { get; set; } + public long? EditedAt { get; set; } + public int Size { get; set; } // stored size (may be compressed) + public int TrueSize { get; set; } // uncompressed size + public int Views { get; set; } + public string? Syntax { get; set; } + public int Visiblity { get; set; } = 0; // 0 = public, 1 = unlisted, 2 = private + public long ExpiresAt { get; set; } // timestamp of when the paste expires, or 0 for never + } +} diff --git a/SharpbinV3/Data/Entities/User.cs b/SharpbinV3/Data/Entities/User.cs new file mode 100644 index 0000000..3383b15 --- /dev/null +++ b/SharpbinV3/Data/Entities/User.cs @@ -0,0 +1,23 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.ValueGeneration.Internal; +using System.ComponentModel.DataAnnotations; + +namespace SharpbinV3.Data.Entities +{ + [Index(nameof(UID), IsUnique = true)] + [Index(nameof(UUID), IsUnique = true)] + public sealed class User + { + [Key] + public int UID { get; set; } + public required Guid UUID { get; set; } = Guid.CreateVersion7(); // uuid v7 includes timestamp + public required string Username { get; set; } + public required string PasswordHash { get; set; } + public string? Email { get; set; } + public string? DisplayName { get; set; } + public long? LastLogin { get; set; } + public int Type { get; set; } = 0; // 0 = regular user, 1 = moderator, 255 = admin + public int Visiblity { get; set; } = 0; // 0 = public, 1 = unlisted, 2 = private + public List Pastes { get; set; } = []; + } +} diff --git a/SharpbinV3/Dockerfile b/SharpbinV3/Dockerfile new file mode 100644 index 0000000..092939d --- /dev/null +++ b/SharpbinV3/Dockerfile @@ -0,0 +1,30 @@ +# See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging. + +# This stage is used when running from VS in fast mode (Default for Debug configuration) +FROM mcr.microsoft.com/dotnet/aspnet:10.0 AS base +USER $APP_UID +WORKDIR /app +EXPOSE 8080 +EXPOSE 8081 + + +# This stage is used to build the service project +FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build +ARG BUILD_CONFIGURATION=Release +WORKDIR /src +COPY ["SharpbinV3/SharpbinV3.csproj", "SharpbinV3/"] +RUN dotnet restore "./SharpbinV3/SharpbinV3.csproj" +COPY . . +WORKDIR "/src/SharpbinV3" +RUN dotnet build "./SharpbinV3.csproj" -c $BUILD_CONFIGURATION -o /app/build + +# This stage is used to publish the service project to be copied to the final stage +FROM build AS publish +ARG BUILD_CONFIGURATION=Release +RUN dotnet publish "./SharpbinV3.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false + +# This stage is used in production or when running from VS in regular mode (Default when not using the Debug configuration) +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "SharpbinV3.dll"] \ No newline at end of file diff --git a/SharpbinV3/Migrations/20251124021817_InitialCreate.Designer.cs b/SharpbinV3/Migrations/20251124021817_InitialCreate.Designer.cs new file mode 100644 index 0000000..ce0df12 --- /dev/null +++ b/SharpbinV3/Migrations/20251124021817_InitialCreate.Designer.cs @@ -0,0 +1,122 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SharpbinV3.Data; + +#nullable disable + +namespace SharpbinV3.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20251124021817_InitialCreate")] + partial class InitialCreate + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.0"); + + modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b => + { + b.Property("PID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AuthorUUID") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("EditedAt") + .HasColumnType("INTEGER"); + + b.Property("ExpiresAt") + .HasColumnType("INTEGER"); + + b.Property("FilePath") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ID") + .HasColumnType("TEXT"); + + b.Property("Size") + .HasColumnType("INTEGER"); + + b.Property("Syntax") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.Property("TrueSize") + .HasColumnType("INTEGER"); + + b.Property("UUID") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UserUID") + .HasColumnType("INTEGER"); + + b.Property("Views") + .HasColumnType("INTEGER"); + + b.HasKey("PID"); + + b.HasIndex("UserUID"); + + b.ToTable("Pastes"); + }); + + modelBuilder.Entity("SharpbinV3.Data.Entities.User", b => + { + b.Property("UID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("DisplayName") + .HasColumnType("TEXT"); + + b.Property("Email") + .HasColumnType("TEXT"); + + b.Property("LastLogin") + .HasColumnType("INTEGER"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UUID") + .HasColumnType("TEXT"); + + b.Property("Username") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("UID"); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b => + { + b.HasOne("SharpbinV3.Data.Entities.User", "User") + .WithMany("Pastes") + .HasForeignKey("UserUID"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("SharpbinV3.Data.Entities.User", b => + { + b.Navigation("Pastes"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SharpbinV3/Migrations/20251124021817_InitialCreate.cs b/SharpbinV3/Migrations/20251124021817_InitialCreate.cs new file mode 100644 index 0000000..f09aba0 --- /dev/null +++ b/SharpbinV3/Migrations/20251124021817_InitialCreate.cs @@ -0,0 +1,77 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SharpbinV3.Migrations +{ + /// + public partial class InitialCreate : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + UID = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + UUID = table.Column(type: "TEXT", nullable: false), + Username = table.Column(type: "TEXT", nullable: false), + PasswordHash = table.Column(type: "TEXT", nullable: false), + Email = table.Column(type: "TEXT", nullable: true), + DisplayName = table.Column(type: "TEXT", nullable: true), + LastLogin = table.Column(type: "INTEGER", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.UID); + }); + + migrationBuilder.CreateTable( + name: "Pastes", + columns: table => new + { + PID = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + UUID = table.Column(type: "TEXT", nullable: false), + ID = table.Column(type: "TEXT", nullable: true), + Title = table.Column(type: "TEXT", nullable: true), + AuthorUUID = table.Column(type: "TEXT", nullable: false), + UserUID = table.Column(type: "INTEGER", nullable: true), + FilePath = table.Column(type: "TEXT", nullable: false), + EditedAt = table.Column(type: "INTEGER", nullable: true), + Size = table.Column(type: "INTEGER", nullable: false), + TrueSize = table.Column(type: "INTEGER", nullable: false), + Views = table.Column(type: "INTEGER", nullable: false), + Syntax = table.Column(type: "TEXT", nullable: true), + ExpiresAt = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Pastes", x => x.PID); + table.ForeignKey( + name: "FK_Pastes_Users_UserUID", + column: x => x.UserUID, + principalTable: "Users", + principalColumn: "UID"); + }); + + migrationBuilder.CreateIndex( + name: "IX_Pastes_UserUID", + table: "Pastes", + column: "UserUID"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Pastes"); + + migrationBuilder.DropTable( + name: "Users"); + } + } +} diff --git a/SharpbinV3/Migrations/20251124181150_AddPasteIndexes.Designer.cs b/SharpbinV3/Migrations/20251124181150_AddPasteIndexes.Designer.cs new file mode 100644 index 0000000..69c22da --- /dev/null +++ b/SharpbinV3/Migrations/20251124181150_AddPasteIndexes.Designer.cs @@ -0,0 +1,143 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SharpbinV3.Data; + +#nullable disable + +namespace SharpbinV3.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20251124181150_AddPasteIndexes")] + partial class AddPasteIndexes + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.0"); + + modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b => + { + b.Property("PID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AuthorUUID") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("EditedAt") + .HasColumnType("INTEGER"); + + b.Property("ExpiresAt") + .HasColumnType("INTEGER"); + + b.Property("FilePath") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ID") + .HasColumnType("TEXT"); + + b.Property("Size") + .HasColumnType("INTEGER"); + + b.Property("Syntax") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.Property("TrueSize") + .HasColumnType("INTEGER"); + + b.Property("UUID") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UserUID") + .HasColumnType("INTEGER"); + + b.Property("Views") + .HasColumnType("INTEGER"); + + b.HasKey("PID"); + + b.HasIndex("AuthorUUID"); + + b.HasIndex("ExpiresAt"); + + b.HasIndex("ID") + .IsUnique(); + + b.HasIndex("PID") + .IsUnique(); + + b.HasIndex("UUID") + .IsUnique(); + + b.HasIndex("UserUID"); + + b.HasIndex("Views"); + + b.ToTable("Pastes"); + }); + + modelBuilder.Entity("SharpbinV3.Data.Entities.User", b => + { + b.Property("UID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("DisplayName") + .HasColumnType("TEXT"); + + b.Property("Email") + .HasColumnType("TEXT"); + + b.Property("LastLogin") + .HasColumnType("INTEGER"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UUID") + .HasColumnType("TEXT"); + + b.Property("Username") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("UID"); + + b.HasIndex("UID") + .IsUnique(); + + b.HasIndex("UUID") + .IsUnique(); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b => + { + b.HasOne("SharpbinV3.Data.Entities.User", "User") + .WithMany("Pastes") + .HasForeignKey("UserUID"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("SharpbinV3.Data.Entities.User", b => + { + b.Navigation("Pastes"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SharpbinV3/Migrations/20251124181150_AddPasteIndexes.cs b/SharpbinV3/Migrations/20251124181150_AddPasteIndexes.cs new file mode 100644 index 0000000..f820753 --- /dev/null +++ b/SharpbinV3/Migrations/20251124181150_AddPasteIndexes.cs @@ -0,0 +1,95 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SharpbinV3.Migrations +{ + /// + public partial class AddPasteIndexes : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateIndex( + name: "IX_Users_UID", + table: "Users", + column: "UID", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Users_UUID", + table: "Users", + column: "UUID", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Pastes_AuthorUUID", + table: "Pastes", + column: "AuthorUUID"); + + migrationBuilder.CreateIndex( + name: "IX_Pastes_ExpiresAt", + table: "Pastes", + column: "ExpiresAt"); + + migrationBuilder.CreateIndex( + name: "IX_Pastes_ID", + table: "Pastes", + column: "ID", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Pastes_PID", + table: "Pastes", + column: "PID", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Pastes_UUID", + table: "Pastes", + column: "UUID", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Pastes_Views", + table: "Pastes", + column: "Views"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropIndex( + name: "IX_Users_UID", + table: "Users"); + + migrationBuilder.DropIndex( + name: "IX_Users_UUID", + table: "Users"); + + migrationBuilder.DropIndex( + name: "IX_Pastes_AuthorUUID", + table: "Pastes"); + + migrationBuilder.DropIndex( + name: "IX_Pastes_ExpiresAt", + table: "Pastes"); + + migrationBuilder.DropIndex( + name: "IX_Pastes_ID", + table: "Pastes"); + + migrationBuilder.DropIndex( + name: "IX_Pastes_PID", + table: "Pastes"); + + migrationBuilder.DropIndex( + name: "IX_Pastes_UUID", + table: "Pastes"); + + migrationBuilder.DropIndex( + name: "IX_Pastes_Views", + table: "Pastes"); + } + } +} diff --git a/SharpbinV3/Migrations/20251124184531_AddVisibility.Designer.cs b/SharpbinV3/Migrations/20251124184531_AddVisibility.Designer.cs new file mode 100644 index 0000000..6484092 --- /dev/null +++ b/SharpbinV3/Migrations/20251124184531_AddVisibility.Designer.cs @@ -0,0 +1,154 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SharpbinV3.Data; + +#nullable disable + +namespace SharpbinV3.Migrations +{ + [DbContext(typeof(AppDbContext))] + [Migration("20251124184531_AddVisibility")] + partial class AddVisibility + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.0"); + + modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b => + { + b.Property("PID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AuthorUUID") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("EditedAt") + .HasColumnType("INTEGER"); + + b.Property("ExpiresAt") + .HasColumnType("INTEGER"); + + b.Property("FilePath") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ID") + .HasColumnType("TEXT"); + + b.Property("Size") + .HasColumnType("INTEGER"); + + b.Property("Syntax") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.Property("TrueSize") + .HasColumnType("INTEGER"); + + b.Property("UUID") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UserUID") + .HasColumnType("INTEGER"); + + b.Property("Views") + .HasColumnType("INTEGER"); + + b.Property("Visiblity") + .HasColumnType("INTEGER"); + + b.HasKey("PID"); + + b.HasIndex("AuthorUUID"); + + b.HasIndex("ExpiresAt"); + + b.HasIndex("ID") + .IsUnique(); + + b.HasIndex("PID") + .IsUnique(); + + b.HasIndex("UUID") + .IsUnique(); + + b.HasIndex("UserUID"); + + b.HasIndex("Views"); + + b.HasIndex("Visiblity"); + + b.ToTable("Pastes"); + }); + + modelBuilder.Entity("SharpbinV3.Data.Entities.User", b => + { + b.Property("UID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("DisplayName") + .HasColumnType("TEXT"); + + b.Property("Email") + .HasColumnType("TEXT"); + + b.Property("LastLogin") + .HasColumnType("INTEGER"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Type") + .HasColumnType("INTEGER"); + + b.Property("UUID") + .HasColumnType("TEXT"); + + b.Property("Username") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Visiblity") + .HasColumnType("INTEGER"); + + b.HasKey("UID"); + + b.HasIndex("UID") + .IsUnique(); + + b.HasIndex("UUID") + .IsUnique(); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b => + { + b.HasOne("SharpbinV3.Data.Entities.User", "User") + .WithMany("Pastes") + .HasForeignKey("UserUID"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("SharpbinV3.Data.Entities.User", b => + { + b.Navigation("Pastes"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SharpbinV3/Migrations/20251124184531_AddVisibility.cs b/SharpbinV3/Migrations/20251124184531_AddVisibility.cs new file mode 100644 index 0000000..65d4fe3 --- /dev/null +++ b/SharpbinV3/Migrations/20251124184531_AddVisibility.cs @@ -0,0 +1,60 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace SharpbinV3.Migrations +{ + /// + public partial class AddVisibility : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "Type", + table: "Users", + type: "INTEGER", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "Visiblity", + table: "Users", + type: "INTEGER", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "Visiblity", + table: "Pastes", + type: "INTEGER", + nullable: false, + defaultValue: 0); + + migrationBuilder.CreateIndex( + name: "IX_Pastes_Visiblity", + table: "Pastes", + column: "Visiblity"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropIndex( + name: "IX_Pastes_Visiblity", + table: "Pastes"); + + migrationBuilder.DropColumn( + name: "Type", + table: "Users"); + + migrationBuilder.DropColumn( + name: "Visiblity", + table: "Users"); + + migrationBuilder.DropColumn( + name: "Visiblity", + table: "Pastes"); + } + } +} diff --git a/SharpbinV3/Migrations/AppDbContextModelSnapshot.cs b/SharpbinV3/Migrations/AppDbContextModelSnapshot.cs new file mode 100644 index 0000000..961d283 --- /dev/null +++ b/SharpbinV3/Migrations/AppDbContextModelSnapshot.cs @@ -0,0 +1,151 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using SharpbinV3.Data; + +#nullable disable + +namespace SharpbinV3.Migrations +{ + [DbContext(typeof(AppDbContext))] + partial class AppDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.0"); + + modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b => + { + b.Property("PID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("AuthorUUID") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("EditedAt") + .HasColumnType("INTEGER"); + + b.Property("ExpiresAt") + .HasColumnType("INTEGER"); + + b.Property("FilePath") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("ID") + .HasColumnType("TEXT"); + + b.Property("Size") + .HasColumnType("INTEGER"); + + b.Property("Syntax") + .HasColumnType("TEXT"); + + b.Property("Title") + .HasColumnType("TEXT"); + + b.Property("TrueSize") + .HasColumnType("INTEGER"); + + b.Property("UUID") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UserUID") + .HasColumnType("INTEGER"); + + b.Property("Views") + .HasColumnType("INTEGER"); + + b.Property("Visiblity") + .HasColumnType("INTEGER"); + + b.HasKey("PID"); + + b.HasIndex("AuthorUUID"); + + b.HasIndex("ExpiresAt"); + + b.HasIndex("ID") + .IsUnique(); + + b.HasIndex("PID") + .IsUnique(); + + b.HasIndex("UUID") + .IsUnique(); + + b.HasIndex("UserUID"); + + b.HasIndex("Views"); + + b.HasIndex("Visiblity"); + + b.ToTable("Pastes"); + }); + + modelBuilder.Entity("SharpbinV3.Data.Entities.User", b => + { + b.Property("UID") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("DisplayName") + .HasColumnType("TEXT"); + + b.Property("Email") + .HasColumnType("TEXT"); + + b.Property("LastLogin") + .HasColumnType("INTEGER"); + + b.Property("PasswordHash") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Type") + .HasColumnType("INTEGER"); + + b.Property("UUID") + .HasColumnType("TEXT"); + + b.Property("Username") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Visiblity") + .HasColumnType("INTEGER"); + + b.HasKey("UID"); + + b.HasIndex("UID") + .IsUnique(); + + b.HasIndex("UUID") + .IsUnique(); + + b.ToTable("Users"); + }); + + modelBuilder.Entity("SharpbinV3.Data.Entities.Paste", b => + { + b.HasOne("SharpbinV3.Data.Entities.User", "User") + .WithMany("Pastes") + .HasForeignKey("UserUID"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("SharpbinV3.Data.Entities.User", b => + { + b.Navigation("Pastes"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/SharpbinV3/Program.cs b/SharpbinV3/Program.cs new file mode 100644 index 0000000..e2c5df2 --- /dev/null +++ b/SharpbinV3/Program.cs @@ -0,0 +1,48 @@ +using Microsoft.EntityFrameworkCore; +using SharpbinV3.Data; +using SharpbinV3.Services; + +namespace SharpbinV3 +{ + public class Program + { + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + // Add services to the container. + + builder.Services.AddControllers(); + // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi + builder.Services.AddOpenApi(); + builder.Services.AddDbContext(opt => + { + opt.UseSqlite(builder.Configuration.GetConnectionString("DefaultConnection")); + }); + builder.Services.AddScoped(); + + var app = builder.Build(); + + // Configure the HTTP request pipeline. + if (app.Environment.IsDevelopment()) + { + app.MapOpenApi(); + } + + app.UseHttpsRedirection(); + + app.UseAuthorization(); + + + app.MapControllers(); + + using (var scope = app.Services.CreateScope()) + { + var db = scope.ServiceProvider.GetRequiredService(); + db.Database.Migrate(); + } + + app.Run(); + } + } +} diff --git a/SharpbinV3/Properties/launchSettings.json b/SharpbinV3/Properties/launchSettings.json new file mode 100644 index 0000000..074a096 --- /dev/null +++ b/SharpbinV3/Properties/launchSettings.json @@ -0,0 +1,31 @@ +{ + "profiles": { + "http": { + "commandName": "Project", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "http://localhost:5050" + }, + "https": { + "commandName": "Project", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "dotnetRunMessages": true, + "applicationUrl": "https://localhost:7149;http://localhost:5050" + }, + "Container (Dockerfile)": { + "commandName": "Docker", + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", + "environmentVariables": { + "ASPNETCORE_HTTPS_PORTS": "8081", + "ASPNETCORE_HTTP_PORTS": "8080" + }, + "publishAllPorts": true, + "useSSL": true + } + }, + "$schema": "https://json.schemastore.org/launchsettings.json" +} \ No newline at end of file diff --git a/SharpbinV3/Services/IUserService.cs b/SharpbinV3/Services/IUserService.cs new file mode 100644 index 0000000..3cfd5d4 --- /dev/null +++ b/SharpbinV3/Services/IUserService.cs @@ -0,0 +1,13 @@ +using SharpbinV3.Data.Entities; + +namespace SharpbinV3.Services +{ + public interface IUserService + { + Task GetByUID(int uid); + Task GetByUsername(string username); + Task GetByUUID(Guid uuid); + Task GetByEmail(string email); + Task CreateUser(string username, string password, string? email, string? displayName); + } +} diff --git a/SharpbinV3/Services/UserService.cs b/SharpbinV3/Services/UserService.cs new file mode 100644 index 0000000..0c12dad --- /dev/null +++ b/SharpbinV3/Services/UserService.cs @@ -0,0 +1,33 @@ +using Microsoft.EntityFrameworkCore; +using SharpbinV3.Data; +using SharpbinV3.Data.Entities; +using Bcrypt = BCrypt.Net.BCrypt; + +namespace SharpbinV3.Services +{ + public sealed class UserService(AppDbContext db) : IUserService + { + private readonly AppDbContext _db = db; + + public Task GetByUID(int uid) => _db.Users.FirstOrDefaultAsync(u => u.UID == uid); + public Task GetByUUID(Guid uuid) => _db.Users.FirstOrDefaultAsync(u => u.UUID == uuid); + public Task GetByUsername(string username) => _db.Users.FirstOrDefaultAsync(u => u.Username == username); + public Task GetByEmail(string email) => _db.Users.FirstOrDefaultAsync(u => u.Email == email); + + public async Task CreateUser(string username, string password, string? email, string? displayName) + { + var user = new User + { + Username = username, + PasswordHash = Bcrypt.HashPassword(password), + Email = email, + DisplayName = displayName, + UUID = Guid.CreateVersion7() + }; + + _db.Users.Add(user); + await _db.SaveChangesAsync(); + return user; + } + } +} diff --git a/SharpbinV3/SharpbinV3.csproj b/SharpbinV3/SharpbinV3.csproj new file mode 100644 index 0000000..2b04157 --- /dev/null +++ b/SharpbinV3/SharpbinV3.csproj @@ -0,0 +1,35 @@ + + + + net10.0 + enable + enable + a95b1aac-5cce-4fa9-8056-51f0d045cf2f + Linux + + + + + + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + diff --git a/SharpbinV3/SharpbinV3.http b/SharpbinV3/SharpbinV3.http new file mode 100644 index 0000000..399d430 --- /dev/null +++ b/SharpbinV3/SharpbinV3.http @@ -0,0 +1,6 @@ +@SharpbinV3_HostAddress = http://localhost:5050 + +GET {{SharpbinV3_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/SharpbinV3/appsettings.Development.json b/SharpbinV3/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/SharpbinV3/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/SharpbinV3/appsettings.json b/SharpbinV3/appsettings.json new file mode 100644 index 0000000..d51abd6 --- /dev/null +++ b/SharpbinV3/appsettings.json @@ -0,0 +1,12 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "ConnectionStrings": { + "DefaultConnection": "Data Source=sharpbin.db" + }, + "AllowedHosts": "*" +}