Private/Cs/WebProj/New-CsControllerCsToString.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
}