Private/Cs/WebProj/New-CsControllerCsToString.ps1

Function New-CsControllerCsToString($nickName, $csprojName, $entity, [bool]$create = $true, [bool]$read = $true, [bool]$update = $true, [bool]$delete = $true, [bool]$list = $true, [bool]$filter = $true) {

    $entityCapital = (ConvertTo-CapitalCamelCase $entity)
    $entityLower = (ConvertTo-LowerCamelCase $entity)

    [string]$result = ""
    $result= @"

using ${nickName}Model.Model;
using ${nickName}Model.ModelWithView;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using BoxTurtleCs.Util;

namespace ${csprojName}.Controllers
{
    public class ${entityCapital}Controller : Controller
    {
        private readonly ${nickName}WithViewContext _context;

        public ${entityCapital}Controller(${nickName}WithViewContext context)
        {
            this._context = context;
        }

"@


    if($list -eq $true) {
        $result += @"
        [HttpGet("/api/[controller]")]
        public IActionResult List() => Json(_context.${entityCapital}.ToList());

"@

    }

    if($read -eq $true) {
        $result += @"

        [HttpGet("/api/[controller]/{id}")]
        public IActionResult Get(int id)
        {
            ${entityCapital} ${entityLower} = _context.${entityCapital}.Find(id);
            if (${entityLower} != null)
            {
                return Json(${entityLower});
            }
            else
            {
                return NotFound(id);
            }
        }

"@

    }

    if($create -eq $true) {
        $result += @"

        [HttpPost("/api/[controller]")]
        public IActionResult Create([FromBody] ${entityCapital} ${entityLower})
        {
            _context.${entityCapital}.Add(${entityLower});
            _context.SaveChanges();
            return Json(${entityLower});
        }

"@

    }

    if($update -eq $true) {
        $result += @"

        [HttpPut("/api/[controller]")]
        public IActionResult Update([FromBody] ${entityCapital} ${entityLower})
        {
            ${entityCapital} ${entityLower}Old = _context.${entityCapital}.Find(${entityLower}.${entityCapital}Id);
            if (${entityLower}Old != null)
            {
                ${entityLower}.CopyPropertiesTo(${entityLower}Old);
                _context.SaveChanges();
                return Json(${entityLower}Old);
            }
            else
            {
                return NotFound(${entityLower}.${entityCapital}Id);
            }
        }

"@

    }

    if($delete -eq $true) {
        $result += @"

        [HttpDelete("/api/[controller]/{id}")]
        public ActionResult Delete(int id)
        {
            ${entityCapital} ${entityLower} = _context.${entityCapital}.Find(id);
            if (${entityLower} != null)
            {
                _context.${entityCapital}.Remove(${entityLower});
                _context.SaveChanges();
                return Ok();
            }
            else
            {
                return NotFound(${entityLower}.${entityCapital}Id);
            }
        }

"@

    }

    $result += @"
    }
}

"@


    return $result
}