diff --git a/EGUI/lab2/lab2/.vscode/launch.json b/EGUI/lab2/lab2/.vscode/launch.json new file mode 100644 index 00000000..87cb9c17 --- /dev/null +++ b/EGUI/lab2/lab2/.vscode/launch.json @@ -0,0 +1,35 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "name": ".NET Core Launch (web)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/bin/Debug/net6.0/lab2.dll", + "args": [], + "cwd": "${workspaceFolder}", + "stopAtEntry": false, + // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser + "serverReadyAction": { + "action": "openExternally", + "pattern": "\\bNow listening on:\\s+(https?://\\S+)" + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "sourceFileMap": { + "/Views": "${workspaceFolder}/Views" + } + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/EGUI/lab2/lab2/.vscode/tasks.json b/EGUI/lab2/lab2/.vscode/tasks.json new file mode 100644 index 00000000..cc8e8bef --- /dev/null +++ b/EGUI/lab2/lab2/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/lab2.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/lab2.csproj", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/lab2.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/EGUI/lab2/lab2/Controllers/BlogView.cs b/EGUI/lab2/lab2/Controllers/BlogView.cs new file mode 100644 index 00000000..04e949eb --- /dev/null +++ b/EGUI/lab2/lab2/Controllers/BlogView.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Mvc; +using System.Text.Encodings.Web; + +namespace lab2.Controllers +{ + public class BlogViewController : Controller + { + // + // GET: /Register/ + + public IActionResult Index() + { + return View(); + } + + // + // GET: /Register/RegisterUser/?userid=Rick&email=4&password=test&blogid=blogtest + + public IActionResult RegisterUser(string userid, string email, string password, string blogid) + { + ViewData["userid"] = "userid: " + userid; + ViewData["email"] = "email: " + email; + ViewData["password"] = "password: " + password; + ViewData["blogid"] = "blogid: " + blogid; + return View(); + } + } +} \ No newline at end of file diff --git a/EGUI/lab2/lab2/Controllers/Login.cs b/EGUI/lab2/lab2/Controllers/Login.cs new file mode 100644 index 00000000..04d5f8ac --- /dev/null +++ b/EGUI/lab2/lab2/Controllers/Login.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Mvc; +using System.Text.Encodings.Web; + +namespace lab2.Controllers +{ + public class LoginController : Controller + { + // + // GET: /Login/ + + public IActionResult Index() + { + return View(); + } + + // + // GET: /Login/LoginUser/ + // https://localhost:{PORT}/Login/LoginUser/?userid=Rick&password=4 + + public IActionResult LoginUser(string userid, string password) + { + ViewData["userid"] = "userid: " + userid; + ViewData["password"] = "password: " + password; + + return View(); + } + } +} \ No newline at end of file diff --git a/EGUI/lab2/lab2/Controllers/Register.cs b/EGUI/lab2/lab2/Controllers/Register.cs new file mode 100644 index 00000000..91b7018a --- /dev/null +++ b/EGUI/lab2/lab2/Controllers/Register.cs @@ -0,0 +1,28 @@ +using Microsoft.AspNetCore.Mvc; +using System.Text.Encodings.Web; + +namespace lab2.Controllers +{ + public class RegisterController : Controller + { + // + // GET: /Register/ + + public IActionResult Index() + { + return View(); + } + + // + // GET: /Register/RegisterUser/?userid=Rick&email=4&password=test&blogid=blogtest + + public IActionResult RegisterUser(string userid, string email, string password, string blogid) + { + ViewData["userid"] = "userid: " + userid; + ViewData["email"] = "email: " + email; + ViewData["password"] = "password: " + password; + ViewData["blogid"] = "blogid: " + blogid; + return View(); + } + } +} \ No newline at end of file diff --git a/EGUI/lab2/lab2/Controllers/UserController.cs b/EGUI/lab2/lab2/Controllers/UserController.cs new file mode 100644 index 00000000..a27217a9 --- /dev/null +++ b/EGUI/lab2/lab2/Controllers/UserController.cs @@ -0,0 +1,153 @@ +#nullable disable +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Rendering; +using Microsoft.EntityFrameworkCore; +using lab2.Models; + +namespace lab2.Controllers +{ + public class UserController : Controller + { + private readonly lab2UserContext _context; + + public UserController(lab2UserContext context) + { + _context = context; + } + + // GET: User + public async Task Index() + { + return View(await _context.User.ToListAsync()); + } + + // GET: User/Details/5 + public async Task Details(string id) + { + if (id == null) + { + return NotFound(); + } + + var user = await _context.User + .FirstOrDefaultAsync(m => m.Id == id); + if (user == null) + { + return NotFound(); + } + + return View(user); + } + + // GET: User/Create + public IActionResult Create() + { + return View(); + } + + // POST: User/Create + // To protect from overposting attacks, enable the specific properties you want to bind to. + // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public async Task Create([Bind("Id,email,password")] User user) + { + if (ModelState.IsValid) + { + _context.Add(user); + await _context.SaveChangesAsync(); + return RedirectToAction(nameof(Index)); + } + return View(user); + } + + // GET: User/Edit/5 + public async Task Edit(string id) + { + if (id == null) + { + return NotFound(); + } + + var user = await _context.User.FindAsync(id); + if (user == null) + { + return NotFound(); + } + return View(user); + } + + // POST: User/Edit/5 + // To protect from overposting attacks, enable the specific properties you want to bind to. + // For more details, see http://go.microsoft.com/fwlink/?LinkId=317598. + [HttpPost] + [ValidateAntiForgeryToken] + public async Task Edit(string id, [Bind("Id,email,password")] User user) + { + if (id != user.Id) + { + return NotFound(); + } + + if (ModelState.IsValid) + { + try + { + _context.Update(user); + await _context.SaveChangesAsync(); + } + catch (DbUpdateConcurrencyException) + { + if (!UserExists(user.Id)) + { + return NotFound(); + } + else + { + throw; + } + } + return RedirectToAction(nameof(Index)); + } + return View(user); + } + + // GET: User/Delete/5 + public async Task Delete(string id) + { + if (id == null) + { + return NotFound(); + } + + var user = await _context.User + .FirstOrDefaultAsync(m => m.Id == id); + if (user == null) + { + return NotFound(); + } + + return View(user); + } + + // POST: User/Delete/5 + [HttpPost, ActionName("Delete")] + [ValidateAntiForgeryToken] + public async Task DeleteConfirmed(string id) + { + var user = await _context.User.FindAsync(id); + _context.User.Remove(user); + await _context.SaveChangesAsync(); + return RedirectToAction(nameof(Index)); + } + + private bool UserExists(string id) + { + return _context.User.Any(e => e.Id == id); + } + } +} diff --git a/EGUI/lab2/lab2/Data/lab2UserContext.cs b/EGUI/lab2/lab2/Data/lab2UserContext.cs new file mode 100644 index 00000000..ea085e46 --- /dev/null +++ b/EGUI/lab2/lab2/Data/lab2UserContext.cs @@ -0,0 +1,17 @@ +#nullable disable +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using lab2.Models; + + public class lab2UserContext : DbContext + { + public lab2UserContext (DbContextOptions options) + : base(options) + { + } + + public DbSet User { get; set; } + } diff --git a/EGUI/lab2/lab2/Models/Blog b/EGUI/lab2/lab2/Models/Blog new file mode 100644 index 00000000..6b56881a --- /dev/null +++ b/EGUI/lab2/lab2/Models/Blog @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; + +namespace lab2.Models +{ + public class Blog + { + public string? Id { get; set; } + public string? Title { get; set; } + + public string? OwnerId { get; set; } + + public BlogEntry[]? Entries {get; set; } + + + } +} \ No newline at end of file diff --git a/EGUI/lab2/lab2/Models/BlogEntry b/EGUI/lab2/lab2/Models/BlogEntry new file mode 100644 index 00000000..56cb2bbb --- /dev/null +++ b/EGUI/lab2/lab2/Models/BlogEntry @@ -0,0 +1,12 @@ +using System.ComponentModel.DataAnnotations; + +namespace lab2.Models +{ + public class BlogEntry + { + public string? Title { get; set; } + [DataType(DataType.Date)] + public DateTime datetime { get; set; } + public string? content {get; set; } + } +} \ No newline at end of file diff --git a/EGUI/lab2/lab2/Models/User.cs b/EGUI/lab2/lab2/Models/User.cs new file mode 100644 index 00000000..ba05ca6e --- /dev/null +++ b/EGUI/lab2/lab2/Models/User.cs @@ -0,0 +1,11 @@ +using System.ComponentModel.DataAnnotations; + +namespace lab2.Models +{ + public class User + { + public string? Id { get; set; } + public string? email { get; set; } + public string? password { get; set; } + } +} \ No newline at end of file diff --git a/EGUI/lab2/lab2/Program.cs b/EGUI/lab2/lab2/Program.cs index 4ac679a0..0cd9f8e2 100644 --- a/EGUI/lab2/lab2/Program.cs +++ b/EGUI/lab2/lab2/Program.cs @@ -1,4 +1,9 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.DependencyInjection; var builder = WebApplication.CreateBuilder(args); + +builder.Services.AddDbContext(options => + options.UseSqlite(builder.Configuration.GetConnectionString("lab2UserContext") ?? throw new InvalidOperationException("Connection string 'lab2UserContext' not found."))); // Add services to the container. builder.Services.AddControllersWithViews(); diff --git a/EGUI/lab2/lab2/Views/BlogView/Index.cshtml b/EGUI/lab2/lab2/Views/BlogView/Index.cshtml new file mode 100644 index 00000000..46d297a8 --- /dev/null +++ b/EGUI/lab2/lab2/Views/BlogView/Index.cshtml @@ -0,0 +1,5 @@ +@{ + ViewData["Title"] = "Blog View"; +} + +

Blog View

diff --git a/EGUI/lab2/lab2/Views/Login/Index.cshtml b/EGUI/lab2/lab2/Views/Login/Index.cshtml new file mode 100644 index 00000000..7ce8a949 --- /dev/null +++ b/EGUI/lab2/lab2/Views/Login/Index.cshtml @@ -0,0 +1,7 @@ +@{ + ViewData["Title"] = "Login"; +} + +

Login

+ +

Hello from our Login Template!

diff --git a/EGUI/lab2/lab2/Views/Login/LoginUser.cshtml b/EGUI/lab2/lab2/Views/Login/LoginUser.cshtml new file mode 100644 index 00000000..9f624df5 --- /dev/null +++ b/EGUI/lab2/lab2/Views/Login/LoginUser.cshtml @@ -0,0 +1,10 @@ +@{ + ViewData["Title"] = "Logged User"; +} + +

Welcome

+ +
    +
  • @ViewData["userid"]
  • +
  • @ViewData["password"]
  • +
\ No newline at end of file diff --git a/EGUI/lab2/lab2/Views/Register/Index.cshtml b/EGUI/lab2/lab2/Views/Register/Index.cshtml new file mode 100644 index 00000000..468789d2 --- /dev/null +++ b/EGUI/lab2/lab2/Views/Register/Index.cshtml @@ -0,0 +1,7 @@ +@{ + ViewData["Title"] = "Register"; +} + +

Register

+ +

Hello from our Register Template!

diff --git a/EGUI/lab2/lab2/Views/Register/RegisterUser.cshtml b/EGUI/lab2/lab2/Views/Register/RegisterUser.cshtml new file mode 100644 index 00000000..f4c6c628 --- /dev/null +++ b/EGUI/lab2/lab2/Views/Register/RegisterUser.cshtml @@ -0,0 +1,12 @@ +@{ + ViewData["Title"] = "Register User"; +} + +

Welcome

+ +
    +
  • @ViewData["userid"]
  • +
  • @ViewData["email"]
  • +
  • @ViewData["password"]
  • +
  • @ViewData["blogid"]
  • +
\ No newline at end of file diff --git a/EGUI/lab2/lab2/Views/Shared/_Layout.cshtml b/EGUI/lab2/lab2/Views/Shared/_Layout.cshtml index 248f49ed..988dd8fe 100644 --- a/EGUI/lab2/lab2/Views/Shared/_Layout.cshtml +++ b/EGUI/lab2/lab2/Views/Shared/_Layout.cshtml @@ -3,7 +3,7 @@ - @ViewData["Title"] - lab2 + @ViewData["Title"] - Blog App @@ -12,7 +12,7 @@