Subversion Repository Public Repository

ChrisCompleteCodeTrunk

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
@inherits Microsoft.VisualStudio.Web.CodeGeneration.Templating.RazorTemplateBase
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@{
    foreach (var namespaceName in Model.RequiredNamespaces)
    {
@:using @namespaceName;
    }
}

namespace @Model.ControllerNamespace
{
@{
    string routePrefix = "api/" + Model.ControllerRootName;
    var entitySetName = Model.ModelMetadata.EntitySetName;
    var primaryKeyName = Model.ModelMetadata.PrimaryKeys[0].PropertyName;
    var primaryKeyShortTypeName = Model.ModelMetadata.PrimaryKeys[0].ShortTypeName;
    var primaryKeyType = Model.ModelMetadata.PrimaryKeys[0].TypeName;
    var primaryKeyIsAutoGenerated = Model.ModelMetadata.PrimaryKeys[0].IsAutoGenerated;
}
    [Route("api/[controller]")]
    [ApiController]
    public class @Model.ControllerName : ControllerBase
    {
        private readonly @Model.ContextTypeName _context;

        public @(Model.ControllerName)(@Model.ContextTypeName context)
        {
            _context = context;
        }

        // GET: @routePrefix
        [HttpGet]
        public IEnumerable<@Model.ModelTypeName> Get@(entitySetName)()
        {
            return _context.@(entitySetName);
        }

        // GET: @routePrefix/5
        [HttpGet("{id}")]
        public async Task<IActionResult> Get@(Model.ModelTypeName)([FromRoute] @primaryKeyShortTypeName id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var @Model.ModelVariable = await _context.@(entitySetName).FindAsync(id);

            if (@Model.ModelVariable == null)
            {
                return NotFound();
            }

            return Ok(@Model.ModelVariable);
        }

        // PUT: @routePrefix/5
        [HttpPut("{id}")]
        public async Task<IActionResult> Put@(Model.ModelTypeName)([FromRoute] @primaryKeyShortTypeName id, [FromBody] @Model.ModelTypeName @Model.ModelVariable)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != @(Model.ModelVariable).@primaryKeyName)
            {
                return BadRequest();
            }

            _context.Entry(@Model.ModelVariable).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!@(Model.ModelTypeName)Exists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: @routePrefix
        [HttpPost]
        public async Task<IActionResult> Post@(Model.ModelTypeName)([FromBody] @Model.ModelTypeName @Model.ModelVariable)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _context.@(entitySetName).Add(@Model.ModelVariable);
@{
    if (primaryKeyIsAutoGenerated)
    {
            @:await _context.SaveChangesAsync();
    }
    else
    {
            @:try
            @:{
                @:await _context.SaveChangesAsync();
            @:}
            @:catch (DbUpdateException)
            @:{
                @:if (@(Model.ModelTypeName)Exists(@(Model.ModelVariable).@primaryKeyName))
                @:{
                    @:return new StatusCodeResult(StatusCodes.Status409Conflict);
                @:}
                @:else
                @:{
                    @:throw;
                @:}
            @:}
    }
}

            return CreatedAtAction("Get@(Model.ModelTypeName)", new { id = @(Model.ModelVariable).@primaryKeyName }, @Model.ModelVariable);
        }

        // DELETE: @routePrefix/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete@(Model.ModelTypeName)([FromRoute] @primaryKeyShortTypeName id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var @Model.ModelVariable = await _context.@(entitySetName).FindAsync(id);
            if (@Model.ModelVariable == null)
            {
                return NotFound();
            }

            _context.@(entitySetName).Remove(@Model.ModelVariable);
            await _context.SaveChangesAsync();

            return Ok(@Model.ModelVariable);
        }

        private bool @(Model.ModelTypeName)Exists(@primaryKeyShortTypeName id)
        {
            return _context.@(entitySetName).Any(e => e.@primaryKeyName == id);
        }
    }
}

Commits for ChrisCompleteCodeTrunk/ActionTireCo/packages/Microsoft.VisualStudio.Web.CodeGenerators.Mvc.2.1.0/Templates/ControllerGenerator/ApiControllerWithContext.cshtml

Diff revisions: vs.
Revision Author Commited Message
1 BBDSCHRIS picture BBDSCHRIS Wed 22 Aug, 2018 20:08:03 +0000