From 3bade8859bcd938b85c39ab16eaa0dcf8e01535f Mon Sep 17 00:00:00 2001 From: Sergey Chebotar Date: Sat, 3 Jun 2023 07:41:46 +0300 Subject: [PATCH] Mass refactoring --- Controllers/BundleController.cs | 147 ---------------- Controllers/FigureController.cs | 166 ++++++++++-------- Controllers/ProductsController.cs | 133 ++++++++++++++ ....cs => 20230603040054_Initial.Designer.cs} | 34 ++-- ...1621_Init.cs => 20230603040054_Initial.cs} | 30 ++-- Migrations/DataContextModelSnapshot.cs | 30 ++-- Models/DataContext.cs | 8 +- Models/Figure.cs | 19 +- Models/LoginModel.cs | 2 +- Models/Product.cs | 15 ++ Models/UnderwearBundle.cs | 11 -- Pages/Figure.cshtml | 124 +++++++++++++ Pages/Index.cshtml | 2 +- Pages/Shared/_Navigation.cshtml | 2 +- Pages/Shared/_Products.cshtml | 44 +++++ Pages/Shared/_Projects.cshtml | 39 ---- Views/Account/Create.cshtml | 8 +- Views/Bundle/Details.cshtml | 53 ------ Views/Bundle/Index.cshtml | 49 ------ Views/Figure/Details.cshtml | 45 +++-- Views/{Bundle => Products}/Create.cshtml | 20 ++- Views/Products/Details.cshtml | 60 +++++++ Views/Products/Index.cshtml | 43 +++++ 23 files changed, 614 insertions(+), 470 deletions(-) delete mode 100644 Controllers/BundleController.cs create mode 100644 Controllers/ProductsController.cs rename Migrations/{20230303041621_Init.Designer.cs => 20230603040054_Initial.Designer.cs} (65%) rename Migrations/{20230303041621_Init.cs => 20230603040054_Initial.cs} (58%) create mode 100644 Models/Product.cs delete mode 100644 Models/UnderwearBundle.cs create mode 100644 Pages/Figure.cshtml create mode 100644 Pages/Shared/_Products.cshtml delete mode 100644 Pages/Shared/_Projects.cshtml delete mode 100644 Views/Bundle/Details.cshtml delete mode 100644 Views/Bundle/Index.cshtml rename Views/{Bundle => Products}/Create.cshtml (50%) create mode 100644 Views/Products/Details.cshtml create mode 100644 Views/Products/Index.cshtml diff --git a/Controllers/BundleController.cs b/Controllers/BundleController.cs deleted file mode 100644 index b1d61fd..0000000 --- a/Controllers/BundleController.cs +++ /dev/null @@ -1,147 +0,0 @@ -using System.Data; -using Microsoft.AspNetCore.Authorization; -using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; -using MyDarling.Models; - -namespace MyDarling.Controllers -{ - [Authorize] - public class BundleController : Controller - { - private DataContext context; - private IWebHostEnvironment environment; - - public BundleController(DataContext context, IWebHostEnvironment environment) - { - this.environment = environment; - this.context = context; - } - - public ActionResult Index() - { - return View(context.UnderwearBundles.Include(b => b.Figures)); - } - - public ActionResult Create() - { - return View(); - } - - [HttpPost] - public async Task Create([Bind] UnderwearBundle bundle) - { - try - { - if (ModelState.IsValid) - { - await context.UnderwearBundles.AddAsync(bundle); - context.SaveChanges(); - return RedirectToAction(nameof(Index)); - } - } - catch (DataException) - { - ModelState.AddModelError("", "Unable to save changes"); - } - return View(bundle); - } - - public async Task Details(int id) - { - return View(await context.UnderwearBundles.Include(b => b.Figures).Where(b => b.Id == id).FirstOrDefaultAsync()); - } - - public async Task Edit(int id) - { - return View(nameof(Details), await context.UnderwearBundles.FindAsync(id)); - } - - [HttpPost] - public async Task Edit(int? id) - { - - if (id == null) - { - return NotFound(); - } - - var bundle = await context.UnderwearBundles.FindAsync(id); - if (bundle == null) - { - return NotFound(); - } - - var file = Request.Form.Files.FirstOrDefault(); - - if (await TryUpdateModelAsync( - bundle, - "", - b => b.Name, b => b.Description, b => b.Figures, b => b.Price)) - { - if (file != null) - { - var newFigure = new Figure(); - bundle.Figures.Add(newFigure); - newFigure.FilePath = $"/Content/{bundle.Id}/{Guid.NewGuid()}{Path.GetExtension(file.FileName)}"; - var savePath = environment.WebRootPath + "/Content/" + bundle.Id + "/"; - if (!Directory.Exists(savePath)) - { - Directory.CreateDirectory(savePath); - } - using var fileStream = new FileStream(environment.WebRootPath + newFigure.FilePath, FileMode.Create); - await file.CopyToAsync(fileStream); - } - - try - { - await context.SaveChangesAsync(); - return RedirectToAction(nameof(Index)); - } - - catch (System.Exception) - { - ModelState.AddModelError("", "Unable to save changes"); - } - - - } - return View(bundle); - } - - [HttpPost] - public async Task Delete(int id) - { - var bundleToDelete = await context.UnderwearBundles.FindAsync(id); - if (bundleToDelete == null) - { - return NotFound(); - } - - try - { - var bundleDirPath = String.Concat(environment.WebRootPath, - "/Content/", - bundleToDelete.Id); - - if (Directory.Exists(bundleDirPath)) - { - Directory.Delete(bundleDirPath, true); - } - // foreach (var figure in bundleToDelete.Figures) - // { - // using FigureController controller = new(context, environment); - // await controller.Delete(figure.Id); - // } - - context.UnderwearBundles.Remove(bundleToDelete); - await context.SaveChangesAsync(); - return RedirectToAction(nameof(Index)); - } - catch (DbUpdateException) - { - return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true }); - } - } - } -} \ No newline at end of file diff --git a/Controllers/FigureController.cs b/Controllers/FigureController.cs index 9f6cf0c..bad8791 100644 --- a/Controllers/FigureController.cs +++ b/Controllers/FigureController.cs @@ -6,84 +6,104 @@ using MyDarling.Models; namespace MyDarling.Controllers { - [Authorize] - public class FigureController : Controller - { - private DataContext context; - private IWebHostEnvironment environment; - public FigureController(DataContext context, IWebHostEnvironment environment) - { - this.context = context; - this.environment = environment; - } + [Authorize] + public class FigureController : Controller + { + private DataContext context; + private IWebHostEnvironment environment; + public FigureController(DataContext context, IWebHostEnvironment environment) + { + this.context = context; + this.environment = environment; + } - public async Task Details(int id) - { - return View(await context.Figures.Where(f => f.Id == id).FirstOrDefaultAsync()); - } + public async Task Details(string id) + { + var figure = await context.Figures + .Where(f => f.Id.Equals(id)) + .FirstOrDefaultAsync(); + if (figure == null) + { + return NotFound(); + } + var product = await context.Products + .Where(b => b.Figures.Contains(figure)) + .FirstOrDefaultAsync(); + if (product == null) + { + return NotFound(); + } + return View(figure); + } - [HttpPost] - public async Task Edit(int? id) - { - if (id == null) - { - return NotFound(); - } + [HttpPost] + public async Task Edit(string id) + { + if (id == null) + { + return NotFound(); + } - var figure = await context.Figures.FindAsync(id); + var figure = await context.Figures + .Where(f => f.Id.Equals(id)) + .FirstOrDefaultAsync(); - if (figure == null) - { - return NotFound(); - } + if (figure == null) + { + return NotFound(); + } - var bundle = await context.UnderwearBundles - .Where(b => b.Figures.Contains(figure)) - .FirstOrDefaultAsync(); + var product = await context.Products + .Where(b => b.Figures.Contains(figure)) + .FirstOrDefaultAsync(); - if (await TryUpdateModelAsync
( - figure, - "", - f => f.Description)) - { - try - { - await context.SaveChangesAsync(); - return RedirectToAction("Details", "Bundle", new { Id = bundle?.Id}); - } - catch (SystemException) - { - ModelState.AddModelError("", "Unable to save changes"); - } - } - return View(figure); - } - - [HttpPost] - public async Task Delete(int id) - { - var figureToDelete = await context.Figures.FindAsync(id); - if (figureToDelete == null) - { - return NotFound(); - } + if (await TryUpdateModelAsync
( + figure, + "", + f => f.Description)) + { + try + { + await context.SaveChangesAsync(); + return RedirectToAction("Details", "Products", new { Id = product?.Id }); + } + catch (SystemException) + { + ModelState.AddModelError("", "Unable to save changes"); + } + } + return View(figure); + } - try - { - FileInfo figureFile = new FileInfo(environment.WebRootPath + figureToDelete.FilePath); - if (figureFile.Exists) - { - figureFile.Delete(); - } - - context.Figures.Remove(figureToDelete); - await context.SaveChangesAsync(); - return RedirectToAction(nameof(Index), "Bundle"); - } - catch (DbUpdateException) - { - return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true }); - } - } - } + [HttpPost] + public async Task Delete(string id) + { + var figure = await context.Figures.FindAsync(id); + if (figure == null) + { + return NotFound(); + } + var product = await context.Products + .Where(b => b.Figures.Contains(figure)) + .FirstAsync(); + + try + { + string filePath = $"/Content/{product.Id}/{figure.Id}.jpg"; + FileInfo figureFile = new FileInfo(environment.WebRootPath + filePath); + if (figureFile.Exists) + { + figureFile.Delete(); + } + + context.Figures.Remove(figure); + await context.SaveChangesAsync(); + return RedirectToAction("Details", "Products", new { Id = product?.Id }); + } + catch (DbUpdateException) + { + return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true }); + } + } + } } \ No newline at end of file diff --git a/Controllers/ProductsController.cs b/Controllers/ProductsController.cs new file mode 100644 index 0000000..14a4b27 --- /dev/null +++ b/Controllers/ProductsController.cs @@ -0,0 +1,133 @@ +using System.Data; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using MyDarling.Models; + +namespace MyDarling.Controllers +{ + [Authorize] + public class ProductsController : Controller + { + private DataContext context; + private IWebHostEnvironment environment; + + public ProductsController(DataContext context, IWebHostEnvironment environment) + { + this.environment = environment; + this.context = context; + } + + public IActionResult Index() + { + return View(context.Products.Include(b => b.Figures)); + } + + public IActionResult Create() + { + return View(); + } + + [HttpPost] + public async Task Create([Bind] Product bundle) + { + try + { + if (ModelState.IsValid) + { + await context.Products.AddAsync(bundle); + context.SaveChanges(); + return RedirectToAction(nameof(Index)); + } + } + catch (DataException) + { + ModelState.AddModelError("", "Unable to save changes"); + } + return View(bundle); + } + + public async Task Details(string id) + { + return View(await context.Products.Include(b => b.Figures).Where(b => b.Id.Equals(id)).FirstOrDefaultAsync()); + } + + [HttpPost] + public async Task Edit(string id) + { + if (string.IsNullOrEmpty(id)) + { + return NotFound(); + } + + var product = await context.Products.FindAsync(id); + if (product == null) + { + return NotFound(); + } + + var file = Request.Form.Files.FirstOrDefault(); + + if (await TryUpdateModelAsync( + product, + "", + b => b.Name, b => b.Description, b => b.Figures, b => b.Price)) + { + if (file != null) + { + var newFigure = new Figure(string.Empty, product.Id); + product.Figures.Add(newFigure); + string filePath = $"/Content/{product.Id}/{newFigure.Id}.jpg"; + var savePath = environment.WebRootPath + "/Content/" + product.Id + "/"; + if (!Directory.Exists(savePath)) + { + Directory.CreateDirectory(savePath); + } + using var fileStream = new FileStream(environment.WebRootPath + filePath, FileMode.Create); + await file.CopyToAsync(fileStream); + } + + try + { + await context.SaveChangesAsync(); + return RedirectToAction("Details", "Products", new { Id = product?.Id}); + } + + catch (System.Exception) + { + ModelState.AddModelError("", "Unable to save changes"); + } + } + return View(product); + } + + [HttpPost] + public async Task Delete(string id) + { + var productToDelete = await context.Products.FindAsync(id); + if (productToDelete == null) + { + return NotFound(); + } + + try + { + var bundleDirPath = String.Concat(environment.WebRootPath, + "/Content/", + productToDelete.Id); + + if (Directory.Exists(bundleDirPath)) + { + Directory.Delete(bundleDirPath, true); + } + context.Products.Remove(productToDelete); + await context.SaveChangesAsync(); + return RedirectToAction(nameof(Index)); + } + catch (DbUpdateException) + { + return RedirectToAction(nameof(Delete), new { id = id, saveChangesError = true }); + } + } + } +} \ No newline at end of file diff --git a/Migrations/20230303041621_Init.Designer.cs b/Migrations/20230603040054_Initial.Designer.cs similarity index 65% rename from Migrations/20230303041621_Init.Designer.cs rename to Migrations/20230603040054_Initial.Designer.cs index 833e097..cf1a728 100644 --- a/Migrations/20230303041621_Init.Designer.cs +++ b/Migrations/20230603040054_Initial.Designer.cs @@ -1,5 +1,4 @@ // -using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; @@ -11,8 +10,8 @@ using MyDarling.Models; namespace MyDarling.Migrations { [DbContext(typeof(DataContext))] - [Migration("20230303041621_Init")] - partial class Init + [Migration("20230603040054_Initial")] + partial class Initial { /// protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -22,33 +21,28 @@ namespace MyDarling.Migrations modelBuilder.Entity("MyDarling.Models.Figure", b => { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); + b.Property("Id") + .HasColumnType("TEXT"); b.Property("Description") .IsRequired() .HasColumnType("TEXT"); - b.Property("FilePath") + b.Property("ProductId") .IsRequired() .HasColumnType("TEXT"); - b.Property("UnderwearBundleId") - .HasColumnType("INTEGER"); - b.HasKey("Id"); - b.HasIndex("UnderwearBundleId"); + b.HasIndex("ProductId"); b.ToTable("Figures"); }); - modelBuilder.Entity("MyDarling.Models.UnderwearBundle", b => + modelBuilder.Entity("MyDarling.Models.Product", b => { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); + b.Property("Id") + .HasColumnType("TEXT"); b.Property("Description") .IsRequired() @@ -63,17 +57,19 @@ namespace MyDarling.Migrations b.HasKey("Id"); - b.ToTable("UnderwearBundles"); + b.ToTable("Products"); }); modelBuilder.Entity("MyDarling.Models.Figure", b => { - b.HasOne("MyDarling.Models.UnderwearBundle", null) + b.HasOne("MyDarling.Models.Product", null) .WithMany("Figures") - .HasForeignKey("UnderwearBundleId"); + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); }); - modelBuilder.Entity("MyDarling.Models.UnderwearBundle", b => + modelBuilder.Entity("MyDarling.Models.Product", b => { b.Navigation("Figures"); }); diff --git a/Migrations/20230303041621_Init.cs b/Migrations/20230603040054_Initial.cs similarity index 58% rename from Migrations/20230303041621_Init.cs rename to Migrations/20230603040054_Initial.cs index 788d447..80b5601 100644 --- a/Migrations/20230303041621_Init.cs +++ b/Migrations/20230603040054_Initial.cs @@ -5,50 +5,48 @@ namespace MyDarling.Migrations { /// - public partial class Init : Migration + public partial class Initial : Migration { /// protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.CreateTable( - name: "UnderwearBundles", + name: "Products", columns: table => new { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), + Id = table.Column(type: "TEXT", nullable: false), Name = table.Column(type: "TEXT", nullable: false), Description = table.Column(type: "TEXT", nullable: false), Price = table.Column(type: "TEXT", nullable: false) }, constraints: table => { - table.PrimaryKey("PK_UnderwearBundles", x => x.Id); + table.PrimaryKey("PK_Products", x => x.Id); }); migrationBuilder.CreateTable( name: "Figures", columns: table => new { - Id = table.Column(type: "INTEGER", nullable: false) - .Annotation("Sqlite:Autoincrement", true), + Id = table.Column(type: "TEXT", nullable: false), Description = table.Column(type: "TEXT", nullable: false), - FilePath = table.Column(type: "TEXT", nullable: false), - UnderwearBundleId = table.Column(type: "INTEGER", nullable: true) + ProductId = table.Column(type: "TEXT", nullable: false) }, constraints: table => { table.PrimaryKey("PK_Figures", x => x.Id); table.ForeignKey( - name: "FK_Figures_UnderwearBundles_UnderwearBundleId", - column: x => x.UnderwearBundleId, - principalTable: "UnderwearBundles", - principalColumn: "Id"); + name: "FK_Figures_Products_ProductId", + column: x => x.ProductId, + principalTable: "Products", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); }); migrationBuilder.CreateIndex( - name: "IX_Figures_UnderwearBundleId", + name: "IX_Figures_ProductId", table: "Figures", - column: "UnderwearBundleId"); + column: "ProductId"); } /// @@ -58,7 +56,7 @@ namespace MyDarling.Migrations name: "Figures"); migrationBuilder.DropTable( - name: "UnderwearBundles"); + name: "Products"); } } } diff --git a/Migrations/DataContextModelSnapshot.cs b/Migrations/DataContextModelSnapshot.cs index b2177d7..6a8e605 100644 --- a/Migrations/DataContextModelSnapshot.cs +++ b/Migrations/DataContextModelSnapshot.cs @@ -1,5 +1,4 @@ // -using System; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Storage.ValueConversion; @@ -19,33 +18,28 @@ namespace MyDarling.Migrations modelBuilder.Entity("MyDarling.Models.Figure", b => { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); + b.Property("Id") + .HasColumnType("TEXT"); b.Property("Description") .IsRequired() .HasColumnType("TEXT"); - b.Property("FilePath") + b.Property("ProductId") .IsRequired() .HasColumnType("TEXT"); - b.Property("UnderwearBundleId") - .HasColumnType("INTEGER"); - b.HasKey("Id"); - b.HasIndex("UnderwearBundleId"); + b.HasIndex("ProductId"); b.ToTable("Figures"); }); - modelBuilder.Entity("MyDarling.Models.UnderwearBundle", b => + modelBuilder.Entity("MyDarling.Models.Product", b => { - b.Property("Id") - .ValueGeneratedOnAdd() - .HasColumnType("INTEGER"); + b.Property("Id") + .HasColumnType("TEXT"); b.Property("Description") .IsRequired() @@ -60,17 +54,19 @@ namespace MyDarling.Migrations b.HasKey("Id"); - b.ToTable("UnderwearBundles"); + b.ToTable("Products"); }); modelBuilder.Entity("MyDarling.Models.Figure", b => { - b.HasOne("MyDarling.Models.UnderwearBundle", null) + b.HasOne("MyDarling.Models.Product", null) .WithMany("Figures") - .HasForeignKey("UnderwearBundleId"); + .HasForeignKey("ProductId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); }); - modelBuilder.Entity("MyDarling.Models.UnderwearBundle", b => + modelBuilder.Entity("MyDarling.Models.Product", b => { b.Navigation("Figures"); }); diff --git a/Models/DataContext.cs b/Models/DataContext.cs index 23e80ab..9fd10e0 100644 --- a/Models/DataContext.cs +++ b/Models/DataContext.cs @@ -14,13 +14,7 @@ namespace MyDarling.Models { opts.UseSqlite(configuration.GetConnectionString("MyDarlingDb")); } - - // protected override void OnModelCreating(ModelBuilder builder) - // { - // builder.Entity().HasMany(b => b.Figures).WithOne(); - // } - - public DbSet UnderwearBundles => Set(); + public DbSet Products => Set(); public DbSet
Figures => Set
(); } } \ No newline at end of file diff --git a/Models/Figure.cs b/Models/Figure.cs index f63d842..8bbc591 100644 --- a/Models/Figure.cs +++ b/Models/Figure.cs @@ -1,9 +1,16 @@ namespace MyDarling.Models { - public class Figure - { - public int Id { get; set; } - public string Description { get; set; } = string.Empty; - public string FilePath { get; set; } = string.Empty; - } + public class Figure + { + public string Id { get; set; } + public string Description { get; set; } = string.Empty; + public string ProductId { get; set; } + + public Figure(string description, string productId) + { + Id = Guid.NewGuid().ToString(); + Description = description; + ProductId = productId; + } + } } \ No newline at end of file diff --git a/Models/LoginModel.cs b/Models/LoginModel.cs index 93485c8..6592135 100644 --- a/Models/LoginModel.cs +++ b/Models/LoginModel.cs @@ -9,5 +9,5 @@ public class LoginModel [Required] public string? Password { get; set; } - public string ReturnUrl { get; set; } = "/Bundle"; + public string ReturnUrl { get; set; } = "/Products"; } \ No newline at end of file diff --git a/Models/Product.cs b/Models/Product.cs new file mode 100644 index 0000000..efb691c --- /dev/null +++ b/Models/Product.cs @@ -0,0 +1,15 @@ +namespace MyDarling.Models +{ + public class Product + { + public string Id { get; set; } + public string Name { get; set; } = string.Empty; + public List
Figures { get; set; } = new List
(); + public string Description { get; set; } = string.Empty; + public decimal Price { get; set; } = 1000M; + public Product() + { + Id = Guid.NewGuid().ToString(); + } + } +} \ No newline at end of file diff --git a/Models/UnderwearBundle.cs b/Models/UnderwearBundle.cs deleted file mode 100644 index 1f451e6..0000000 --- a/Models/UnderwearBundle.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace MyDarling.Models -{ - public class UnderwearBundle - { - public int Id { get; set; } - public string Name { get; set; } = "My Darling Bundle"; - public List
Figures { get; set; } = new List
(); - public string Description { get; set; } = string.Empty; - public decimal Price { get; set; } - } -} \ No newline at end of file diff --git a/Pages/Figure.cshtml b/Pages/Figure.cshtml new file mode 100644 index 0000000..76c2216 --- /dev/null +++ b/Pages/Figure.cshtml @@ -0,0 +1,124 @@ +@* @page "{id}" +@model FigureModel; +@using MyDarling.Models; +@using Microsoft.EntityFrameworkCore; +@using Microsoft.AspNetCore.Mvc.RazorPages + + + + + + Редактирование фотографии + + + + + +
+
+
+
+ + @Model.Figure?.Description + +
+
+ @Html.AntiForgeryToken() +
+ + +
+ +
+
+ +
+
+
+
+ + + + +@functions +{ + public class FigureModel : PageModel + { + private DataContext context; + private IWebHostEnvironment environment; + public string? FilePath { get; set; } + public Figure? Figure { get; set; } + public UnderwearBundle? Bundle { get; set; } + public FigureModel(DataContext context, IWebHostEnvironment environment) + { + this.context = context; + this.environment = environment; + } + + public async Task OnGetAsync(string id) + { + Figure = await context.Figures + .Where(f => f.Id.Equals(id)) + .FirstOrDefaultAsync(); + if (Figure == null) + { + return NotFound(); + } + Bundle = await context.UnderwearBundles + .Where(b => b.Figures.Contains(Figure)) + .FirstOrDefaultAsync(); + if (Bundle == null) + { + return NotFound(); + } + FilePath = $"/Content/{Bundle.Id}/{Figure.Id}.jpg"; + + return Page(); + } + public async Task OnPostAsync(string id, string description) + { + Figure = await context.Figures + .Where(f => f.Id.Equals(id)) + .FirstOrDefaultAsync(); + if (Figure != null) + { + Figure.Description = description ?? string.Empty; + } + await context.SaveChangesAsync(); + return RedirectToPage(); + } + + public async Task OnPostDeleteAsync(string id) + { + Figure = await context.Figures.FindAsync(id); + if (Figure == null) + { + return NotFound(); + } + var parentBundle = await context.UnderwearBundles + .Where(b => b.Figures.Contains(Figure)) + .FirstAsync(); + + try + { + string filePath = $"/Content/{parentBundle.Id}/{Figure}.jpg"; + FileInfo figureFile = new FileInfo(environment.WebRootPath + filePath); + if (figureFile.Exists) + { + figureFile.Delete(); + } + + context.Figures.Remove(Figure); + await context.SaveChangesAsync(); + return RedirectToPage($"/Bundle/{Bundle.Id}"); + } + catch (DbUpdateException) + { + return RedirectToPage($"/Bundle/{Bundle.Id}"); + } + } + } +} *@ \ No newline at end of file diff --git a/Pages/Index.cshtml b/Pages/Index.cshtml index a3ec3a5..9a887a6 100644 --- a/Pages/Index.cshtml +++ b/Pages/Index.cshtml @@ -9,5 +9,5 @@ - + \ No newline at end of file diff --git a/Pages/Shared/_Navigation.cshtml b/Pages/Shared/_Navigation.cshtml index 7c29a6b..736fd61 100644 --- a/Pages/Shared/_Navigation.cshtml +++ b/Pages/Shared/_Navigation.cshtml @@ -10,7 +10,7 @@ diff --git a/Pages/Shared/_Products.cshtml b/Pages/Shared/_Products.cshtml new file mode 100644 index 0000000..a82ac1c --- /dev/null +++ b/Pages/Shared/_Products.cshtml @@ -0,0 +1,44 @@ +@using System.Globalization +@using Microsoft.EntityFrameworkCore +@using MyDarling.Models +@inject DataContext context + +@{ + var products = context.Products + .Include(b => b.Figures) + .Where(b => b.Price != 0 && b.Figures.Count > 0) + .OrderByDescending(b => b.Id); +} + +
+
+
+ @foreach (var product in products) + { +
+
+ @{ + var figure = product.Figures.First(); + var filePath = $"/Content/{product.Id}/{figure.Id}.jpg"; + } + @product.Name + @for (int i = 1; i < product.Figures.Count(); i++) + { + filePath = $"/Content/{product.Id}/{product.Figures[i].Id}.jpg"; + + } +
+
+
@product.Name
+ @String.Format(new CultureInfo("ru-RU"), "{0:C0}", product.Price) +
+
+
+
+ } +
+
+
\ No newline at end of file diff --git a/Pages/Shared/_Projects.cshtml b/Pages/Shared/_Projects.cshtml deleted file mode 100644 index 4b9a62e..0000000 --- a/Pages/Shared/_Projects.cshtml +++ /dev/null @@ -1,39 +0,0 @@ -@using System.Globalization -@using Microsoft.EntityFrameworkCore -@using MyDarling.Models -@inject DataContext context - -@{ - var bundles = context.UnderwearBundles - .Include(b => b.Figures) - .Where(b => b.Price != 0 && b.Figures.Count > 0) - .OrderByDescending(b => b.Id); -} - -
-
-
- @foreach (var bundle in bundles) - { -
-
- @bundle.Name - @for (int i = 1; i < @bundle.Figures.Count(); i++) - { - - } -
-
-
@bundle.Name
- @String.Format(new CultureInfo("ru-RU"), "{0:C0}", @bundle.Price) -
-
-
-
- } -
-
-
\ No newline at end of file diff --git a/Views/Account/Create.cshtml b/Views/Account/Create.cshtml index 2fff64a..f871673 100644 --- a/Views/Account/Create.cshtml +++ b/Views/Account/Create.cshtml @@ -5,7 +5,7 @@ - User Acccounts + User Acсounts @@ -14,7 +14,7 @@
- +
@@ -22,11 +22,11 @@
- +
- + Back
diff --git a/Views/Bundle/Details.cshtml b/Views/Bundle/Details.cshtml deleted file mode 100644 index ac13624..0000000 --- a/Views/Bundle/Details.cshtml +++ /dev/null @@ -1,53 +0,0 @@ -@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers -@model MyDarling.Models.UnderwearBundle - - - - - - Bundles list - - - - - -
-
-
- - -
-
- - @Html.TextAreaFor(model => model.Description, new { @class="form-control", @rows = 2 }) -
-
- -
- @foreach (var figure in @Model.Figures) - { -
-
- - @figure.Description - -
-
- } -
-
-
- -
-
- - -
- - -
-
- - - \ No newline at end of file diff --git a/Views/Bundle/Index.cshtml b/Views/Bundle/Index.cshtml deleted file mode 100644 index 9e1f293..0000000 --- a/Views/Bundle/Index.cshtml +++ /dev/null @@ -1,49 +0,0 @@ -@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers -@model IQueryable - - - - - - Bundles list - - - - -
-
-
- Underwear bundle list -
-
- Log Out -
-
-
- - - - - - - - - - - - @foreach (var bundle in Model) - { - - - - - - - } - -
#NameDescritionPrice
@bundle.Id@bundle.Name@bundle.Description@bundle.Price
- Add bundle -
- - - \ No newline at end of file diff --git a/Views/Figure/Details.cshtml b/Views/Figure/Details.cshtml index ace2a9a..3bb43ea 100644 --- a/Views/Figure/Details.cshtml +++ b/Views/Figure/Details.cshtml @@ -5,30 +5,39 @@ - Figure + Редактирование фотографии -
-
-
- - @Model.Description - + +
+
+
+
+ @{ + var filePath = $"/Content/{Model.ProductId}/{Model.Id}.jpg"; + } + + @Model.Description + +
+
+
+
+ + @Html.TextAreaFor(model => model.Description, new { @class="form-control", @rows = 2 }) +
+ + +
-
-
-
- - @Html.TextAreaFor(model => model.Description, new { @class="form-control", @rows = 2 }) -
- - -
- - + \ No newline at end of file diff --git a/Views/Bundle/Create.cshtml b/Views/Products/Create.cshtml similarity index 50% rename from Views/Bundle/Create.cshtml rename to Views/Products/Create.cshtml index 5197909..4a6732c 100644 --- a/Views/Bundle/Create.cshtml +++ b/Views/Products/Create.cshtml @@ -1,5 +1,5 @@ @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers -@model MyDarling.Models.UnderwearBundle +@model MyDarling.Models.Product @@ -10,24 +10,28 @@ - -
+ +
+
- +
- + @Html.TextAreaFor(model => model.Description, new { @class="form-control", @rows = 2 })
- +
- + - +
\ No newline at end of file diff --git a/Views/Products/Details.cshtml b/Views/Products/Details.cshtml new file mode 100644 index 0000000..3e45748 --- /dev/null +++ b/Views/Products/Details.cshtml @@ -0,0 +1,60 @@ +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers +@model MyDarling.Models.Product + + + + + + Комплект: @Model.Name + + + + + +
+
+
+
+ + +
+
+ + @Html.TextAreaFor(model => model.Description, new { @class="form-control", @rows = 2 }) +
+
+ + +
+
+ +
+ @foreach (var figure in @Model.Figures) + { + + } +
+ +
+
+
+ + +
+
+ + + \ No newline at end of file diff --git a/Views/Products/Index.cshtml b/Views/Products/Index.cshtml new file mode 100644 index 0000000..0725bb6 --- /dev/null +++ b/Views/Products/Index.cshtml @@ -0,0 +1,43 @@ +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers +@model IQueryable + + + + + + My Darling Underwear + + + + + +
+
+ + + + + + + + + + @foreach (var bundle in Model) + { + + + + + + } + +
НазваниеОписаниеЦена
@bundle.Name@bundle.Description@bundle.Price
+ +
+
+ + + \ No newline at end of file