From 35aed79337d5cbba5d2c0c172d97f7ed27971435 Mon Sep 17 00:00:00 2001 From: Stat Date: Tue, 27 Oct 2026 10:15:05 +0200 Subject: [PATCH] adding pages and css and .gitignore, scripts, resources, to-do.md --- .gitignore | 4 + Data/ApplicationUser.cs | 6 ++ Data/DbContext.cs | 11 +++ Data/GuestMessage.cs | 14 ++++ Data/UserMessage.cs | 15 ++++ LanguagesValues/StringValues.en.resx | 21 ++++++ LanguagesValues/StringValues.resx | 24 ++++++ Pages/Gallery.cshtml | 21 ++++++ Pages/Gallery.cshtml.cs | 11 +++ Pages/Index.cshtml | 14 ++++ Pages/Index.cshtml.cs | 13 ++++ Pages/Shared/_Layout.cshtml | 27 +++++++ Pages/_ViewImports.cshtml | 4 + Pages/_ViewStart.cshtml | 1 + Pages/_ViewStart.cshtml.cs | 10 +++ Program.cs | 109 +++++++++++++++++++++++++++ Properties/launchSettings.json | 10 +++ StatiKosev.csproj | 20 +++++ StringValues.cs | 6 ++ appsettings.json | 6 ++ db/setup_db.sh | 5 ++ to-do.md | 5 ++ wwwroot/styles/site.css | 3 + 23 files changed, 360 insertions(+) create mode 100644 .gitignore create mode 100644 Data/ApplicationUser.cs create mode 100644 Data/DbContext.cs create mode 100644 Data/GuestMessage.cs create mode 100644 Data/UserMessage.cs create mode 100644 LanguagesValues/StringValues.en.resx create mode 100644 LanguagesValues/StringValues.resx create mode 100644 Pages/Gallery.cshtml create mode 100644 Pages/Gallery.cshtml.cs create mode 100644 Pages/Index.cshtml create mode 100644 Pages/Index.cshtml.cs create mode 100644 Pages/Shared/_Layout.cshtml create mode 100644 Pages/_ViewImports.cshtml create mode 100644 Pages/_ViewStart.cshtml create mode 100644 Pages/_ViewStart.cshtml.cs create mode 100644 Program.cs create mode 100644 Properties/launchSettings.json create mode 100644 StatiKosev.csproj create mode 100644 StringValues.cs create mode 100644 appsettings.json create mode 100644 db/setup_db.sh create mode 100644 to-do.md create mode 100644 wwwroot/styles/site.css diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5e10b0b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +bin +obj +wwwroot/images +.idea diff --git a/Data/ApplicationUser.cs b/Data/ApplicationUser.cs new file mode 100644 index 0000000..f4d433f --- /dev/null +++ b/Data/ApplicationUser.cs @@ -0,0 +1,6 @@ +namespace StatiKosev.Data; +using Microsoft.AspNetCore.Identity; +public class ApplicationUser : IdentityUser +{ + //probably not gonna define any more fields but... +} \ No newline at end of file diff --git a/Data/DbContext.cs b/Data/DbContext.cs new file mode 100644 index 0000000..a0b0324 --- /dev/null +++ b/Data/DbContext.cs @@ -0,0 +1,11 @@ +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore; + +namespace StatiKosev.Data; + +public class AppDbContext(DbContextOptions options) + : IdentityDbContext(options) +{ + public DbSet GuestMessages => Set(); +} \ No newline at end of file diff --git a/Data/GuestMessage.cs b/Data/GuestMessage.cs new file mode 100644 index 0000000..839a6f4 --- /dev/null +++ b/Data/GuestMessage.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; + +namespace StatiKosev.Data; + +public class GuestMessage +{ + public int Id { get; set; } + [MaxLength(2000)] public required string Body { get; set; } + public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow; + public bool IsRead { get; set; } + +} +public record SendGuestMessageRequest( + [Required, StringLength(2000, MinimumLength = 1)] string Body); \ No newline at end of file diff --git a/Data/UserMessage.cs b/Data/UserMessage.cs new file mode 100644 index 0000000..268ad41 --- /dev/null +++ b/Data/UserMessage.cs @@ -0,0 +1,15 @@ +using System.ComponentModel.DataAnnotations; + +namespace StatiKosev.Data; + +public class UserMessage +{ + + public int Id { get; set; } + [MaxLength(2000)] public required string Body { get; set; } + public DateTimeOffset CreatedAt { get; set; } = DateTimeOffset.UtcNow; + public bool IsRead { get; set; } + +} +public record SendUserMessageRequest( + [Required, StringLength(2000, MinimumLength = 1)] string Body, string Username); diff --git a/LanguagesValues/StringValues.en.resx b/LanguagesValues/StringValues.en.resx new file mode 100644 index 0000000..d1fb785 --- /dev/null +++ b/LanguagesValues/StringValues.en.resx @@ -0,0 +1,21 @@ + + + text/microsoft-resx + 2.0 + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Welcome! + Home page heading + + + Login + + + Greetings! + + + Stati Kosev + + \ No newline at end of file diff --git a/LanguagesValues/StringValues.resx b/LanguagesValues/StringValues.resx new file mode 100644 index 0000000..437d731 --- /dev/null +++ b/LanguagesValues/StringValues.resx @@ -0,0 +1,24 @@ + + + text/microsoft-resx + 2.0 + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + Добре дошли в уебпортофолиото на Стати Косев - независим програмист, AI инженер, системен администратор. + Home page heading + + + Вход + + + Здравейте! + + + Стати Косев + + + Галерия + + \ No newline at end of file diff --git a/Pages/Gallery.cshtml b/Pages/Gallery.cshtml new file mode 100644 index 0000000..99c6891 --- /dev/null +++ b/Pages/Gallery.cshtml @@ -0,0 +1,21 @@ +@page +@model StatiKosev.Pages.Gallery + +@{ + Layout = null; +} + + + + + + + @StringValues["Gallery"] + + + +
+ +
+ + \ No newline at end of file diff --git a/Pages/Gallery.cshtml.cs b/Pages/Gallery.cshtml.cs new file mode 100644 index 0000000..ce7cb89 --- /dev/null +++ b/Pages/Gallery.cshtml.cs @@ -0,0 +1,11 @@ +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace StatiKosev.Pages; + +public class Gallery : PageModel +{ + public void OnGet() + { + + } +} \ No newline at end of file diff --git a/Pages/Index.cshtml b/Pages/Index.cshtml new file mode 100644 index 0000000..f2ac304 --- /dev/null +++ b/Pages/Index.cshtml @@ -0,0 +1,14 @@ +@page "/" +@model IndexModel +@{ + ViewData["Title"] = StringValues["Title"]; +} + +
+

@StringValues["Welcome"]

+
@StringValues["Hello", string.IsNullOrEmpty(Model.VisitorName) ? StringValues["Guest"].Value : Model.VisitorName]
+
+ @StringValues[ +
+
+
diff --git a/Pages/Index.cshtml.cs b/Pages/Index.cshtml.cs new file mode 100644 index 0000000..ab360bf --- /dev/null +++ b/Pages/Index.cshtml.cs @@ -0,0 +1,13 @@ +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace StatiKosev.Pages; + +public class IndexModel : PageModel +{ + public string VisitorName { get; private set; } = ""; + + public void OnGet() + { + VisitorName = User.Identity?.IsAuthenticated == true ? User.Identity.Name! : ""; + } +} diff --git a/Pages/Shared/_Layout.cshtml b/Pages/Shared/_Layout.cshtml new file mode 100644 index 0000000..e9f9127 --- /dev/null +++ b/Pages/Shared/_Layout.cshtml @@ -0,0 +1,27 @@ +@{ + var path = Context.Request.Path.Value ?? "/"; + string Active(string p) => path.Equals(p, StringComparison.OrdinalIgnoreCase) ? "active" : ""; +} + + + + + + + + + @StringValues["Title"] + + + Български + English + + + +
@RenderBody()
+ + \ No newline at end of file diff --git a/Pages/_ViewImports.cshtml b/Pages/_ViewImports.cshtml new file mode 100644 index 0000000..e5bf265 --- /dev/null +++ b/Pages/_ViewImports.cshtml @@ -0,0 +1,4 @@ +@namespace StatiKosev.Pages +@using Microsoft.Extensions.Localization +@inject IStringLocalizer StringValues +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/Pages/_ViewStart.cshtml b/Pages/_ViewStart.cshtml new file mode 100644 index 0000000..61236de --- /dev/null +++ b/Pages/_ViewStart.cshtml @@ -0,0 +1 @@ +@{ Layout = "_Layout"; } diff --git a/Pages/_ViewStart.cshtml.cs b/Pages/_ViewStart.cshtml.cs new file mode 100644 index 0000000..322518f --- /dev/null +++ b/Pages/_ViewStart.cshtml.cs @@ -0,0 +1,10 @@ +using Microsoft.AspNetCore.Mvc.RazorPages; +namespace StatiKosev; + +public class _ViewStart : PageModel +{ + public void OnGet() + { + Console.WriteLine("_ViewStart accessed!"); + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..0ad2d3c --- /dev/null +++ b/Program.cs @@ -0,0 +1,109 @@ +using Microsoft.AspNetCore.Antiforgery; +using Microsoft.AspNetCore.Identity; +using Npgsql; +using Microsoft.AspNetCore.Localization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.WebUtilities; +using Microsoft.EntityFrameworkCore; +using StatiKosev.Data; + +namespace StatiKosev; + +public class Program +{ + public static void Main(string[] arguments) + { + var builder = WebApplication.CreateBuilder(arguments); + var databaseConnectionString = builder.Configuration.GetConnectionString("DefaultConnection"); + builder.Services.AddLocalization(o => o.ResourcesPath = "LanguagesValues"); + builder.Services.AddRazorPages(); + builder.Services.AddNpgsqlDataSource(databaseConnectionString); + builder.Services.AddDbContext((sp, o) => + o.UseNpgsql(sp.GetRequiredService())); // EF shares the same connection pool + + builder.Services.AddDefaultIdentity(o => + { + o.SignIn.RequireConfirmedAccount = false; // no email sending configured yet + o.User.RequireUniqueEmail = true; + o.Password.RequiredLength = 8; + o.Password.RequireNonAlphanumeric = false; + }) + .AddRoles() + .AddEntityFrameworkStores(); + + builder.Services.AddAuthorizationBuilder() + .AddPolicy("AdminOnly", p => p.RequireRole("Admin")); + + Console.WriteLine($"Database connection string: {databaseConnectionString}"); + + var app = builder.Build(); + app.UseAntiforgery(); + + //accounts + //safety check if form sends us to the same domain + //if starting with / -> u, if null or else -> "/" redirects to Index + static string ForceLocalizeUrl(string? u) => + !string.IsNullOrEmpty(u) && u.StartsWith('/') && !u.StartsWith("//") && !u.StartsWith("/\\") ? u : "/"; + + Console.WriteLine("Local==" + ForceLocalizeUrl(app.Environment.ApplicationName)); + var account = app.MapGroup("/account"); + + account.MapPost("/login", async ([FromForm] string email, [FromForm] string password, + [FromForm] string? returnUrl, SignInManager signIn) => + { + var r = await signIn.PasswordSignInAsync(email, password, isPersistent: true, lockoutOnFailure: true); + var back = ForceLocalizeUrl(returnUrl); + return Results.LocalRedirect(r.Succeeded ? back : QueryHelpers.AddQueryString(back, "auth", "login-failed")); + }); + + account.MapPost("/register", async ([FromForm] string email, [FromForm] string password, + [FromForm] string? returnUrl, UserManager users, SignInManager signIn) => + { + var back = ForceLocalizeUrl(returnUrl); + var user = new IdentityUser { UserName = email, Email = email }; + var res = await users.CreateAsync(user, password); + if (!res.Succeeded) + return Results.LocalRedirect(QueryHelpers.AddQueryString(back, "auth", "register-failed")); + await users.AddToRoleAsync(user, "User"); + await signIn.SignInAsync(user, isPersistent: true); + return Results.LocalRedirect(back); + }); + + account.MapPost("/logout", async (SignInManager signIn) => + { + await signIn.SignOutAsync(); + return Results.LocalRedirect("/"); + }).WithMetadata(new RequireAntiforgeryTokenAttribute()); + //accounts end + + + app.UseRequestLocalization(o => o + .SetDefaultCulture("bg") + .AddSupportedCultures("bg", "en") + .AddSupportedUICultures("bg", "en")); + + app.UseStaticFiles(); // CSS/JS/images from wwwroot + app.MapRazorPages(); + //get version of database the software is configured to work with (appsettings.json) + app.MapGet("/db_version", async (NpgsqlDataSource db) => + { + + await using var cmd = db.CreateCommand("SELECT version()"); + var version = await cmd.ExecuteScalarAsync(); + Console.WriteLine("db_version api called and returned==" + version); + return version; + }); + app.MapGet("/set-language", (string culture, string? returnUrl, HttpContext ctx) => + { + ctx.Response.Cookies.Append( + CookieRequestCultureProvider.DefaultCookieName, + CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)), + new CookieOptions { Expires = DateTimeOffset.UtcNow.AddYears(1), IsEssential = true }); + return Results.LocalRedirect(string.IsNullOrEmpty(returnUrl) ? "/" : returnUrl); + }); + app.MapStaticAssets(); + app.MapGet("/_res", () => string.Join("\n", + typeof(StatiKosev.StringValues).Assembly.GetManifestResourceNames())); + app.Run(); + } +} \ No newline at end of file diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..f23d652 --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "StatiKosev": { + "commandName": "Project", + "launchBrowser": true, + "applicationUrl": "http://localhost:5555", + "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } + } + } +} \ No newline at end of file diff --git a/StatiKosev.csproj b/StatiKosev.csproj new file mode 100644 index 0000000..9b5ba9e --- /dev/null +++ b/StatiKosev.csproj @@ -0,0 +1,20 @@ + + + + Exe + net10.0 + enable + enable + + + + + + + + + + + + + diff --git a/StringValues.cs b/StringValues.cs new file mode 100644 index 0000000..217071a --- /dev/null +++ b/StringValues.cs @@ -0,0 +1,6 @@ +namespace StatiKosev; + +public class StringValues +{ + +} \ No newline at end of file diff --git a/appsettings.json b/appsettings.json new file mode 100644 index 0000000..a541844 --- /dev/null +++ b/appsettings.json @@ -0,0 +1,6 @@ +{ + "ConnectionStrings": { + "DefaultConnection": "Host=192.168.1.1;Database=dotnet_portfolio;Username=dotnet_portfolio_usr;Password=z50QhAN3v8P7Eqmj0ptPppr3jtC4WkwA" + }, + "Logging": { "LogLevel": { "Default": "Information" } } +} \ No newline at end of file diff --git a/db/setup_db.sh b/db/setup_db.sh new file mode 100644 index 0000000..77d99fc --- /dev/null +++ b/db/setup_db.sh @@ -0,0 +1,5 @@ +sudo -u postgres psql <<'SQL' +CREATE ROLE dotnet_portfolio_usr LOGIN PASSWORD 'z50QhAN3v8P7Eqmj0ptPppr3jtC4WkwA'; +CREATE DATABASE dotnet_portfolio OWNER dotnet_portfolio_usr; +SQL +psql "host=192.168.1.1 dbname=dotnet_portfolio user=dotnet_portfolio_usr password=z50QhAN3v8P7Eqmj0ptPppr3jtC4WkwA" -c 'select version();' \ No newline at end of file diff --git a/to-do.md b/to-do.md new file mode 100644 index 0000000..c8088fe --- /dev/null +++ b/to-do.md @@ -0,0 +1,5 @@ +Front End - Responsive design, HTML5, javascript, JSON, AJAX. Back end - MVC asp.net. Разработвате у дома и защитавате на текущия контрол по дискорд. + +Личният сайт трябва да има главна страница със снимка и описание, страница с галерия от снимки, страница за оставяне на коментари (да св вижда добавения коментар). Може и контактна форма на страница Контакти / изпращане на съобщение до адрес / + +Във втори текущ контрол - правите MVC backend на това, което направихте на първия текущ контрол, задължително трябва да работи, за да се мине, като основната изисквана функционалност за отлична оценка е да може да се добавя информация в базата данни от регистриран потребител (коментари на лична страница, или снимки, за който има такъв първи текущ контрол). \ No newline at end of file diff --git a/wwwroot/styles/site.css b/wwwroot/styles/site.css new file mode 100644 index 0000000..19c693a --- /dev/null +++ b/wwwroot/styles/site.css @@ -0,0 +1,3 @@ +html, body{ + background-color: lightgreen; +}