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?

【保険商品管理システムの開発】生命保険のControllersの修正

Posted at

修正理由

コードに下記の様なWebAPI用の属性 (ApiController, Route) が付いていないため、HTTP経由で叩けずテストできない為です。

Controllers/LifeInsuranceController.cs
[ApiController]
[Route("api/[controller]")]
public class LifeInsuranceController : ControllerBase

修正後コード

Controllers/LifeInsuranceController.cs
using InsuranceApp.Models;
using Microsoft.AspNetCore.Mvc;

namespace InsuranceApp.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class LifeInsuranceController : ControllerBase
    {
        private readonly List<InsurancePolicy> _policies = new();
        private readonly List<Customer> _customers = new();

        /// <summary>
        /// 顧客を登録
        /// </summary>
        [HttpPost("register-customer")]
        public IActionResult RegisterCustomer([FromBody] Customer customer)
        {
            _customers.Add(customer);
            return Ok("顧客を登録しました");
        }

        /// <summary>
        /// 保険契約を追加
        /// </summary>
        [HttpPost("add-policy")]
        public IActionResult AddPolicy([FromBody] InsurancePolicy policy)
        {
            _policies.Add(policy);
            return Ok("契約を追加しました");
        }

        /// <summary>
        /// 特定顧客の有効契約一覧を取得
        /// </summary>
        [HttpGet("active-policies/{customerId}")]
        public IActionResult GetActivePoliciesByCustomer(string customerId)
        {
            var active = _policies
                .Where(p => p.CustomerId == customerId && p.IsActive && !p.IsExpired())
                .ToList();
            return Ok(active);
        }

        /// <summary>
        /// 保険料の合計を算出
        /// </summary>
        [HttpGet("total-premium/{customerId}")]
        public IActionResult CalculateTotalPremium(string customerId)
        {
            var total = _policies
                .Where(p => p.CustomerId == customerId && p.IsActive && !p.IsExpired())
                .Sum(p => p.Premium);
            return Ok(total);
        }

        /// <summary>
        /// 保険金請求処理
        /// </summary>
        [HttpPost("claim/{policyId}")]
        public IActionResult Claim(string policyId)
        {
            var policy = _policies.FirstOrDefault(p => p.PolicyId == policyId);
            if (policy == null || !policy.IsActive || policy.IsExpired())
            {
                return BadRequest("保険契約が無効です。");
            }

            policy.IsActive = false; // 支払後は失効
            return Ok(new { CoverageAmount = policy.CoverageAmount });
        }
    }
}

1. [HttpGet] の役割

• HTTPのGETリクエストに対応するアクションを指定します。
• 用途:データの取得(読み取り)

2. [HttpPost] の役割

• HTTPのPOSTリクエストに対応するアクションを指定します。
• 用途:データの送信(登録・計算依頼・保存など)

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?