0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【保険商品管理システムの開発】生命保険のコントローラーを画面表示するための修正

Posted at

コード解説

Controllers/LifeInsuranceMvcController.cs
using InsuranceProductManager.Data;
using InsuranceProductManager.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Threading.Tasks;

• using
必要なライブラリを読み込んでいます。

• InsuranceProductManager.Data … AppDbContext を使うため
• InsuranceProductManager.Models … Customer, InsurancePolicy モデルを使うため
• Microsoft.AspNetCore.Mvc … MVC コントローラとビューを使うため
• Microsoft.EntityFrameworkCore … 非同期で DB にアクセスするため
• System.Linq … Where, Sum など LINQ を使うため
• System.Threading.Tasks … async/await を使うため

Controllers/LifeInsuranceMvcController.cs
namespace InsuranceApp.Controllers
{
    public class LifeInsuranceMvcController : Controller

• namespace
コントローラが所属する名前空間(整理用)。

• LifeInsuranceMvcController : Controller
ControllerBase ではなく Controller を継承しているので、ビュー (cshtml) を返せる MVC コントローラ になります。

Controllers/LifeInsuranceMvcController.cs
private readonly AppDbContext _context;

public LifeInsuranceMvcController(AppDbContext context)
{
    _context = context;
}

• 依存性注入 (DI) によって AppDbContext(DB接続クラス)を受け取り、コントローラ内部で _context として使えるようにしています。
これにより DB から顧客や保険契約を取得できるようになります。

顧客一覧を表示するアクション

Controllers/LifeInsuranceMvcController.cs
public async Task<IActionResult> Customers()
{
    var customers = await _context.Customers.ToListAsync();
    return View(customers);
}

• _context.Customers → Customer テーブルから全データを取得
• ToListAsync() → 非同期でリストに変換
• View(customers) → 取得したデータをビュー (Views/LifeInsuranceMvc/Customers.cshtml) に渡して表示する

👉 ブラウザで /LifeInsuranceMvc/Customers にアクセスすると、顧客一覧が表示されます。

特定顧客の契約一覧

Controllers/LifeInsuranceMvcController.cs
public async Task<IActionResult> ActivePolicies(string customerId)
{
    var policies = await _context.InsurancePolicies
        .Where(p => p.CustomerId == customerId && p.IsActive)
        .ToListAsync();

    policies = policies.Where(p => !p.IsExpired()).ToList();

    ViewBag.CustomerId = customerId;
    return View(policies);
}

• InsurancePolicies テーブルから「特定顧客ID」かつ「アクティブな契約」を取得
• DB から持ってきた後に C# 側で IsExpired() を呼んで期限切れを除外
• ViewBag.CustomerId → ビューに顧客IDを渡す(タイトルなどに使える)
• View(policies) → 契約一覧をビューに渡す

👉 /LifeInsuranceMvc/ActivePolicies?customerId=xxx にアクセスすると、特定顧客の有効な契約一覧が表示されます。

保険料合計を表示するアクション

Controllers/LifeInsuranceMvcController.cs
public async Task<IActionResult> TotalPremium(string customerId)
{
    var policies = await _context.InsurancePolicies
        .Where(p => p.CustomerId == customerId && p.IsActive)
        .ToListAsync();

    var totalPremium = policies
        .Where(p => !p.IsExpired())
        .Sum(p => p.Premium);

    ViewBag.CustomerId = customerId;
    return View(totalPremium);
}

• 指定した顧客のアクティブ契約を取得
• 有効期限内 (!p.IsExpired()) の契約に絞り込み
• Sum(p => p.Premium) で保険料を合計
• View(totalPremium) でビューに渡す

👉 /LifeInsuranceMvc/TotalPremium?customerId=xxx にアクセスすると、顧客の保険料合計が表示されます。

全体の流れ

• LifeInsuranceMvcController = ユーザーがブラウザから見る画面用コントローラ
• DB (AppDbContext) から顧客や契約情報を取得
• Razor View (.cshtml) にデータを渡して HTML で表示

👉 まとめると、このコントローラは

• Customers → 顧客一覧を表示
• ActivePolicies → 特定顧客の有効契約一覧を表示
• TotalPremium → 特定顧客の保険料合計を表示

という 3つの画面用エンドポイント を提供しています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?