add good state

This commit is contained in:
Sakurai Ryota 2025-02-09 10:20:26 +09:00
parent e44d3cdb55
commit 216944ae66
6 changed files with 144 additions and 6 deletions

View File

@ -0,0 +1,40 @@
using Microsoft.AspNetCore.Mvc;
using Uriagekun.Lib.IO;
using Uriagekun.Models;
namespace Uriagekun.Controllers;
public class GoodController : Controller
{
private readonly MyRepository repository;
public GoodController(MyRepository repository)
{
this.repository = repository;
}
public IActionResult Index()
{
return View();
}
public async Task<IActionResult> Add(AddGoodModel model)
{
if (ModelState.IsValid)
{
return BadRequest();
}
if (
string.IsNullOrEmpty(model.Barcode) || model.Barcode.Length > 16
|| string.IsNullOrEmpty(model.Label) || model.Label.Length > 40
|| model.Price < 0
)
{
return BadRequest();
}
await this.repository.AddGoods(model.Label, model.Barcode, model.Price);
return Ok();
}
}

View File

@ -1,4 +1,5 @@
using PetaPoco;
using Uriagekun.Lib.Data;
namespace Uriagekun.Lib.IO;
@ -10,4 +11,43 @@ public class MyRepository
{
this.database = database;
}
/// <summary>
/// 商品を登録します。
/// </summary>
/// <param name="label"></param>
/// <param name="barcode"></param>
/// <param name="price"></param>
/// <returns>正常終了時、商品IDを返します。</returns>
public async ValueTask<int> AddGoods(string label, string barcode, int price)
{
GoodData data = new GoodData()
{
Label = label,
Barcode = barcode,
Price = price,
};
var ret = await this.database.InsertAsync(data).ConfigureAwait(false);
return (int) ret;
}
/// <summary>
/// 指定商品の売上個数を登録します。
/// </summary>
/// <param name="goodId"></param>
/// <param name="count"></param>
/// <param name="date"></param>
/// <returns></returns>
public async ValueTask AddRegister(int goodId, int count, DateTime date)
{
RegisterData data = new RegisterData()
{
GoodId = goodId,
Date = date,
Count = count,
};
await this.database.InsertAsync(data).ConfigureAwait(false);
}
}

View File

@ -0,0 +1,15 @@
using System.ComponentModel.DataAnnotations;
namespace Uriagekun.Models;
public class AddGoodModel
{
[Required]
public string Barcode { get; set; }
[Required]
public string Label { get; set; }
[Required]
public int Price { get; set; }
}

View File

@ -0,0 +1,40 @@
@{
ViewData["Title"] = "商品管理";
}
<div class="container-fluid row">
<div class="col-md-6">
<h2>商品登録</h2>
<form method="post" asp-action="Add" asp-controller="Good">
<div class="form-group row m-2">
<div class="col-md-3">
<label class="form-label" for="goodLabel">商品名</label>
</div>
<div class="col-md-9">
<input type="text" minlength="0" maxlength="40" required name="Label" class="form-control" id="goodLabel" />
</div>
</div>
<div class="form-group row m-2">
<div class="col-md-3">
<label class="form-label" for="goodBarcode">バーコード</label>
</div>
<div class="col-md-9">
<input type="text" minlength="0" maxlength="16" required name="Barcode" class="form-control" id="goodBarcode" />
</div>
</div>
<div class="form-group row m-2">
<div class="col-md-3">
<label class="form-label" for="goodPrice">金額</label>
</div>
<div class="col-md-9">
<input type="number" min="0" required name="Price" class="form-control" id="goodPrice" />
</div>
</div>
<div class="form-group row m-2">
<div class="col-md-9 offset-md-3">
<button type="submit" class="btn btn-block btn-outline-primary">登録</button>
</div>
</div>
</form>
</div>
</div>

View File

@ -1,5 +1,5 @@
@{
ViewData["Title"] = "Home Page";
ViewData["Title"] = "ホーム";
}
<div class="text-center">

View File

@ -1,9 +1,9 @@
<!DOCTYPE html>
<html lang="en">
<html lang="ja">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - Uriagekun</title>
<title>@ViewData["Title"] - 売上管理</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" asp-append-version="true" />
<link rel="stylesheet" href="~/Uriagekun.styles.css" asp-append-version="true" />
@ -12,7 +12,7 @@
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container-fluid">
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">Uriagekun</a>
<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">売上管理</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
@ -20,7 +20,10 @@
<div class="navbar-collapse collapse d-sm-inline-flex justify-content-between">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">ホーム</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Good" asp-action="Index">商品管理</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
@ -38,7 +41,7 @@
<footer class="border-top footer text-muted">
<div class="container">
&copy; 2025 - Uriagekun - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
&copy; 2025 - 売上管理 - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>