From 143f825448d6f1e7582bec7c440492031315edeb Mon Sep 17 00:00:00 2001 From: Sakurai Date: Sun, 9 Feb 2025 10:43:17 +0900 Subject: [PATCH] Add good list --- .../{GoodController.cs => GoodsController.cs} | 6 +-- Uriagekun/Controllers/RecordController.cs | 46 +++++++++++++++++++ Uriagekun/Lib/IO/MyRepository.cs | 14 ++++++ Uriagekun/Models/AddUriageModel.cs | 12 +++++ Uriagekun/Views/{Good => Goods}/Index.cshtml | 2 +- Uriagekun/Views/Record/Register.cshtml | 29 ++++++++++++ Uriagekun/Views/Shared/_Layout.cshtml | 4 +- 7 files changed, 107 insertions(+), 6 deletions(-) rename Uriagekun/Controllers/{GoodController.cs => GoodsController.cs} (87%) create mode 100644 Uriagekun/Controllers/RecordController.cs create mode 100644 Uriagekun/Models/AddUriageModel.cs rename Uriagekun/Views/{Good => Goods}/Index.cshtml (99%) create mode 100644 Uriagekun/Views/Record/Register.cshtml diff --git a/Uriagekun/Controllers/GoodController.cs b/Uriagekun/Controllers/GoodsController.cs similarity index 87% rename from Uriagekun/Controllers/GoodController.cs rename to Uriagekun/Controllers/GoodsController.cs index ae1c023..df429fc 100644 --- a/Uriagekun/Controllers/GoodController.cs +++ b/Uriagekun/Controllers/GoodsController.cs @@ -4,11 +4,11 @@ using Uriagekun.Models; namespace Uriagekun.Controllers; -public class GoodController : Controller +public class GoodsController : Controller { private readonly MyRepository repository; - public GoodController(MyRepository repository) + public GoodsController(MyRepository repository) { this.repository = repository; } @@ -35,7 +35,7 @@ public class GoodController : Controller } await this.repository.AddGoods(model.Label, model.Barcode, model.Price).ConfigureAwait(false); - return Ok(); + return RedirectToAction("Index"); } public async Task List() diff --git a/Uriagekun/Controllers/RecordController.cs b/Uriagekun/Controllers/RecordController.cs new file mode 100644 index 0000000..d21586a --- /dev/null +++ b/Uriagekun/Controllers/RecordController.cs @@ -0,0 +1,46 @@ +using Microsoft.AspNetCore.Mvc; +using Uriagekun.Lib.IO; +using Uriagekun.Models; + +namespace Uriagekun.Controllers; + +public class RecordController : Controller +{ + private readonly MyRepository repository; + + public RecordController(MyRepository repository) + { + this.repository = repository; + } + + public IActionResult Register() + { + return View(); + } + + public async Task Add(AddUriageModel model) + { + if (!ModelState.IsValid) + { + return BadRequest(); + } + + if ( + string.IsNullOrEmpty(model.Barcode) || model.Barcode.Length > 16 + || model.Count < 0 + ) + { + return BadRequest(); + } + + var good = await this.repository.GetGoodByBarcode(model.Barcode).ConfigureAwait(false); + if (good is null) + { + return NotFound(); + } + + await this.repository.AddRegister(good.Id, model.Count, DateTime.Now).ConfigureAwait(false); + + return RedirectToAction("Register"); + } +} diff --git a/Uriagekun/Lib/IO/MyRepository.cs b/Uriagekun/Lib/IO/MyRepository.cs index 366239b..4c5d73c 100644 --- a/Uriagekun/Lib/IO/MyRepository.cs +++ b/Uriagekun/Lib/IO/MyRepository.cs @@ -53,6 +53,20 @@ public class MyRepository return ret; } + /// + /// 指定されたバーコードの商品を取得します。 + /// + /// + /// + public async ValueTask GetGoodByBarcode(string barcode) + { + var ret = await this.database.FetchAsync( + $"SELECT * FROM goods WHERE barcode={barcode}" + ).ConfigureAwait(false); + + return ret.FirstOrDefault(); + } + /// /// 指定商品の売上個数を登録します。 /// diff --git a/Uriagekun/Models/AddUriageModel.cs b/Uriagekun/Models/AddUriageModel.cs new file mode 100644 index 0000000..db7dec6 --- /dev/null +++ b/Uriagekun/Models/AddUriageModel.cs @@ -0,0 +1,12 @@ +using System.ComponentModel.DataAnnotations; + +namespace Uriagekun.Models; + +public class AddUriageModel +{ + [Required] + public string Barcode { get; set; } + + [Required] + public int Count { get; set; } +} diff --git a/Uriagekun/Views/Good/Index.cshtml b/Uriagekun/Views/Goods/Index.cshtml similarity index 99% rename from Uriagekun/Views/Good/Index.cshtml rename to Uriagekun/Views/Goods/Index.cshtml index 7db7ea1..6225d7b 100644 --- a/Uriagekun/Views/Good/Index.cshtml +++ b/Uriagekun/Views/Goods/Index.cshtml @@ -5,7 +5,7 @@

商品登録

-
+
diff --git a/Uriagekun/Views/Record/Register.cshtml b/Uriagekun/Views/Record/Register.cshtml new file mode 100644 index 0000000..f2721ad --- /dev/null +++ b/Uriagekun/Views/Record/Register.cshtml @@ -0,0 +1,29 @@ +@{ + ViewData["Title"] = "売上登録"; +} + +
+ +
+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+ +
+
+ +
diff --git a/Uriagekun/Views/Shared/_Layout.cshtml b/Uriagekun/Views/Shared/_Layout.cshtml index 07924a9..36728cf 100644 --- a/Uriagekun/Views/Shared/_Layout.cshtml +++ b/Uriagekun/Views/Shared/_Layout.cshtml @@ -23,10 +23,10 @@ ホーム