Skip to content

Commit b31e010

Browse files
committed
Some code cleanup
1 parent a2c1f2f commit b31e010

File tree

10 files changed

+39
-54
lines changed

10 files changed

+39
-54
lines changed

SimpleWebAppMVC/Controllers/TasksApiController.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public IActionResult Get(string id)
3434
if (string.IsNullOrWhiteSpace(id))
3535
return NotFound();
3636

37-
var task = this.dbContext.Tasks.SingleOrDefault(t => t.ID == id);
37+
var task = this.dbContext.Tasks.SingleOrDefault(t => t.Id == id);
3838

3939
if (task == null)
4040
return NotFound();
@@ -52,7 +52,7 @@ public IActionResult Post([FromBody] Models.Task taskModel)
5252
this.dbContext.Add(taskModel);
5353
this.dbContext.SaveChanges();
5454

55-
return CreatedAtRoute("GetTask", new { id = taskModel.ID }, taskModel);
55+
return CreatedAtRoute("GetTask", new { id = taskModel.Id }, taskModel);
5656
}
5757

5858
// PUT api/Tasks/<id>
@@ -62,7 +62,7 @@ public IActionResult Put(string id, [FromBody] Models.Task taskModel)
6262
if (string.IsNullOrWhiteSpace(id))
6363
return NotFound();
6464

65-
var task = this.dbContext.Tasks.SingleOrDefault(t => t.ID == id);
65+
var task = this.dbContext.Tasks.SingleOrDefault(t => t.Id == id);
6666

6767
if (task == null)
6868
return NotFound();
@@ -84,7 +84,7 @@ public IActionResult Delete(string id)
8484
if (string.IsNullOrWhiteSpace(id))
8585
return NotFound();
8686

87-
var task = this.dbContext.Tasks.SingleOrDefault(t => t.ID == id);
87+
var task = this.dbContext.Tasks.SingleOrDefault(t => t.Id == id);
8888

8989
if (task == null)
9090
return NotFound();

SimpleWebAppMVC/Controllers/TasksController.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public IActionResult Create()
3737
* @param taskModel Task model
3838
*/
3939
[HttpPost, ValidateAntiForgeryToken]
40-
public async Task<IActionResult> Create([Bind("ID,Title,Description,Date,Status")] Models.Task taskModel)
40+
public async Task<IActionResult> Create([Bind("Id,Title,Description,Date,Status")] Models.Task taskModel)
4141
{
4242
if (ModelState.IsValid)
4343
{
@@ -52,15 +52,15 @@ public async Task<IActionResult> Create([Bind("ID,Title,Description,Date,Status"
5252

5353
/**
5454
* GET: /Tasks/Details/<id>
55-
* @param id Task ID
55+
* @param id Task Id
5656
*/
5757
[HttpGet]
5858
public async Task<IActionResult> Details(string id)
5959
{
6060
if (string.IsNullOrWhiteSpace(id))
6161
return NotFound();
6262

63-
var taskModel = await this.dbContext.Tasks.SingleOrDefaultAsync(task => task.ID == id);
63+
var taskModel = await this.dbContext.Tasks.SingleOrDefaultAsync(task => task.Id == id);
6464

6565
if (taskModel == null)
6666
return NotFound();
@@ -70,15 +70,15 @@ public async Task<IActionResult> Details(string id)
7070

7171
/**
7272
* GET: /Tasks/Delete/<id>
73-
* @param id Task ID
73+
* @param id Task Id
7474
*/
7575
[HttpGet]
7676
public async Task<IActionResult> Delete(string id)
7777
{
7878
if (string.IsNullOrWhiteSpace(id))
7979
return NotFound();
8080

81-
var taskModel = await this.dbContext.Tasks.SingleOrDefaultAsync(task => task.ID == id);
81+
var taskModel = await this.dbContext.Tasks.SingleOrDefaultAsync(task => task.Id == id);
8282

8383
if (taskModel == null)
8484
return NotFound();
@@ -88,12 +88,12 @@ public async Task<IActionResult> Delete(string id)
8888

8989
/**
9090
* POST: /Tasks/Delete/<id>
91-
* @param id Task ID
91+
* @param id Task Id
9292
*/
9393
[HttpPost, ActionName("Delete"), ValidateAntiForgeryToken]
9494
public async Task<IActionResult> DeleteConfirmed(string id)
9595
{
96-
var taskModel = await this.dbContext.Tasks.SingleOrDefaultAsync(task => task.ID == id);
96+
var taskModel = await this.dbContext.Tasks.SingleOrDefaultAsync(task => task.Id == id);
9797

9898
this.dbContext.Tasks.Remove(taskModel);
9999
await this.dbContext.SaveChangesAsync();
@@ -103,15 +103,15 @@ public async Task<IActionResult> DeleteConfirmed(string id)
103103

104104
/**
105105
* GET: /Tasks/Edit/<id>
106-
* @param id Task ID
106+
* @param id Task Id
107107
*/
108108
[HttpGet]
109109
public async Task<IActionResult> Edit(string id)
110110
{
111111
if (string.IsNullOrWhiteSpace(id))
112112
return NotFound();
113113

114-
var taskModel = await this.dbContext.Tasks.SingleOrDefaultAsync(task => task.ID == id);
114+
var taskModel = await this.dbContext.Tasks.SingleOrDefaultAsync(task => task.Id == id);
115115

116116
if (taskModel == null)
117117
return NotFound();
@@ -122,16 +122,16 @@ public async Task<IActionResult> Edit(string id)
122122
/**
123123
* POST: /Tasks/Edit/<id>
124124
* http://go.microsoft.com/fwlink/?LinkId=317598
125-
* @param id Task ID
125+
* @param id Task Id
126126
* @param taskModel Task model
127127
*/
128128
[HttpPost, ValidateAntiForgeryToken]
129-
public async Task<IActionResult> Edit(string id, [Bind("ID,Title,Description,Date,Status")] Models.Task taskModel)
129+
public async Task<IActionResult> Edit(string id, [Bind("Id,Title,Description,Date,Status")] Models.Task taskModel)
130130
{
131-
if (id != taskModel.ID)
131+
if (id != taskModel.Id)
132132
return NotFound();
133133

134-
if (!this.dbContext.Tasks.Any(t => t.ID == id))
134+
if (!this.dbContext.Tasks.Any(t => t.Id == id))
135135
return NotFound();
136136

137137
if (ModelState.IsValid)

SimpleWebAppMVC/Models/Task.cs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ namespace SimpleWebAppMVC.Models
1111
*/
1212
public class Task
1313
{
14-
public string ID { get; set; }
14+
[Key]
15+
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
16+
public string Id { get; set; }
1517

1618
[StringLength(50, MinimumLength = 3), Required]
1719
public string Title { get; set; }
@@ -26,11 +28,9 @@ public class Task
2628
public string Status { get; set; }
2729

2830
[NotMapped]
29-
public SelectList StatusCodes { get; } = new SelectList(
30-
new List<string> {
31-
"N/A", "Not Started", "Started", "In Progress", "Almost Done", "Completed"
32-
}
33-
);
31+
public SelectList StatusCodes { get; } = new SelectList(new List<string> {
32+
"N/A", "Not Started", "Started", "In Progress", "Almost Done", "Completed"
33+
});
3434

3535
public void Update(Task task)
3636
{
@@ -39,15 +39,5 @@ public void Update(Task task)
3939
this.Date = task.Date;
4040
this.Status = task.Status;
4141
}
42-
43-
/*public List<SelectListItem> StatusCodes { get; } = new List<SelectListItem>
44-
{
45-
new SelectListItem { Value = "N/A", Text = "N/A" },
46-
new SelectListItem { Value = "Not Started", Text = "Not Started" },
47-
new SelectListItem { Value = "Started", Text = "Started" },
48-
new SelectListItem { Value = "In Progress", Text = "In Progress" },
49-
new SelectListItem { Value = "Almost Done", Text = "Almost Done" },
50-
new SelectListItem { Value = "Completed", Text = "Completed" }
51-
};*/
5242
}
5343
}

SimpleWebAppMVC/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ public static void Main(string[] args)
2727
// Create the database (if it does not already exist)
2828
try {
2929
var context = services.GetRequiredService<AppDbContext>();
30+
3031
context.Database.EnsureCreated();
3132
} catch (InvalidOperationException ex) {
3233
var logger = services.GetRequiredService<ILogger<Program>>();
34+
3335
logger.LogError(ex, "An error occurred creating the DB.");
3436
}
3537
}

SimpleWebAppMVC/Startup.cs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,10 @@ public Startup(IConfiguration config)
3030
*/
3131
public void ConfigureServices(IServiceCollection services)
3232
{
33-
services.AddDbContext<AppDbContext>(
34-
options => options.UseSqlServer(this.Configuration.GetConnectionString("DbConnection"))
35-
);
33+
string connectionString = this.Configuration.GetConnectionString("DbConnection");
3634

37-
services.AddMvc(
38-
options => options.EnableEndpointRouting = false
39-
);
35+
services.AddDbContext<AppDbContext>(options => options.UseSqlServer(connectionString));
36+
services.AddMvc(options => options.EnableEndpointRouting = false);
4037
}
4138

4239
/**
@@ -47,9 +44,11 @@ public void ConfigureServices(IServiceCollection services)
4744
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
4845
{
4946
// ASPNETCORE_ENVIRONMENT = [ "Development" | "Production" ]
47+
string jsonFile = (env.IsProduction() ? "appsettings.json" : $"appsettings.{env.EnvironmentName}.json");
48+
5049
var builder = new ConfigurationBuilder()
5150
.SetBasePath(env.ContentRootPath)
52-
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
51+
.AddJsonFile(jsonFile, optional: true, reloadOnChange: true)
5352
.AddEnvironmentVariables();
5453

5554
this.Configuration = builder.Build();
@@ -61,13 +60,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
6160
app.UseExceptionHandler("/Home/Error");
6261
}
6362

64-
// Allow the web server to use access static file paths in wwwroot folder
63+
// Allow the web server to access static file paths in wwwroot folder
6564
app.UseStaticFiles();
6665

6766
// Register routes
68-
app.UseMvc(
69-
routes => routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}")
70-
);
67+
app.UseMvc(routes => routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}"));
7168
}
7269
}
7370
}

SimpleWebAppMVC/Views/Tasks/Delete.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</dl>
2323

2424
<form asp-action="Delete">
25-
<input type="hidden" asp-for="ID" />
25+
<input type="hidden" asp-for="Id" />
2626

2727
<div class="form-group">
2828
<input type="submit" value="Delete" class="btn btn-default" />

SimpleWebAppMVC/Views/Tasks/Details.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
</div>
2323

2424
<div>
25-
<a asp-action="Edit" asp-route-id="@Model.ID">Edit</a> |
25+
<a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
2626
<a asp-action="Index">Back to List</a>
2727
</div>
2828
</div>

SimpleWebAppMVC/Views/Tasks/Edit.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<div class="col-md-4">
1212
<form asp-action="Edit">
1313
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
14-
<input type="hidden" asp-for="ID" />
14+
<input type="hidden" asp-for="Id" />
1515
<div class="form-group">
1616
<label asp-for="Title" class="control-label"></label>
1717
<input asp-for="Title" class="form-control" />

SimpleWebAppMVC/Views/Tasks/Index.cshtml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
<td>@Html.DisplayFor(modelItem => item.Date)</td>
2929
<td>@Html.DisplayFor(modelItem => item.Status)</td>
3030
<td>
31-
<a asp-action="Edit" asp-route-id="@item.ID">Edit</a> |
32-
<a asp-action="Details" asp-route-id="@item.ID">Details</a> |
33-
<a asp-action="Delete" asp-route-id="@item.ID">Delete</a>
31+
<a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
32+
<a asp-action="Details" asp-route-id="@item.Id">Details</a> |
33+
<a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
3434
</td>
3535
</tr>
3636
}

SimpleWebAppMVC/appsettings.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
{
22
"ConnectionStrings": {
33
"DbConnection": ""
4-
//"DbConnection": "Server=(localdb)\\mssqllocaldb;Database=simple-web-app-mvc-dotnet-db;Trusted_Connection=True;"
54
},
65
"Logging": {
76
"IncludeScopes": false,
87
"LogLevel": {
98
"Default": "Warning"
10-
//"Default": "Debug",
11-
//"System": "Information",
12-
//"Microsoft": "Information"
139
}
1410
}
1511
}

0 commit comments

Comments
 (0)