Skip to content

Commit a0cfbec

Browse files
committed
Delete
1 parent 34bb823 commit a0cfbec

File tree

7 files changed

+113
-69
lines changed

7 files changed

+113
-69
lines changed

ContosoUniversity/Features/Instructors/CreateEdit.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ public async Task<Command> Handle(Query message)
9999
else
100100
{
101101
model = await _db.Instructors
102+
.Include(m => m.CourseAssignments)
103+
.ThenInclude(ca => ca.Course)
104+
.Include(m => m.OfficeAssignment)
102105
.Where(i => i.Id == message.Id)
103106
.Map()
104107
.SingleOrDefaultAsync<Command>();

ContosoUniversity/Features/Instructors/CreateEdit.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<hr />
1111
<div class="row">
1212
<div class="col-md-4">
13-
<form>
13+
<form method="post">
1414
@Html.ValidationDiv()
1515
@Html.FormBlock(m => m.LastName)
1616
@Html.FormBlock(m => m.FirstMidName)
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using AutoMapper.QueryableExtensions;
6+
using ContosoUniversity.Data;
7+
using ContosoUniversity.Models;
8+
using FluentValidation;
9+
using MediatR;
10+
using Microsoft.EntityFrameworkCore;
11+
12+
namespace ContosoUniversity.Features.Instructors
13+
{
14+
public class Delete
15+
{
16+
public class Query : IRequest<Command>
17+
{
18+
public int? Id { get; set; }
19+
}
20+
21+
public class Validator : AbstractValidator<Query>
22+
{
23+
public Validator()
24+
{
25+
RuleFor(m => m.Id).NotNull();
26+
}
27+
}
28+
29+
public class Command : IRequest
30+
{
31+
public int? ID { get; set; }
32+
33+
public string LastName { get; set; }
34+
[Display(Name = "First Name")]
35+
public string FirstMidName { get; set; }
36+
37+
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}")]
38+
public DateTime? HireDate { get; set; }
39+
40+
[Display(Name = "Location")]
41+
public string OfficeAssignmentLocation { get; set; }
42+
}
43+
44+
public class QueryHandler : IAsyncRequestHandler<Query, Command>
45+
{
46+
private readonly SchoolContext _db;
47+
48+
public QueryHandler(SchoolContext db) => _db = db;
49+
50+
public Task<Command> Handle(Query message) => _db
51+
.Instructors
52+
.Where(i => i.Id == message.Id)
53+
.ProjectTo<Command>()
54+
.SingleOrDefaultAsync();
55+
}
56+
57+
public class CommandHandler : IAsyncRequestHandler<Command>
58+
{
59+
private readonly SchoolContext _db;
60+
61+
public CommandHandler(SchoolContext db) => _db = db;
62+
63+
public async Task Handle(Command message)
64+
{
65+
Instructor instructor = await _db.Instructors
66+
.Include(i => i.OfficeAssignment)
67+
.Where(i => i.Id == message.ID)
68+
.SingleAsync();
69+
70+
instructor.Handle(message);
71+
72+
_db.Instructors.Remove(instructor);
73+
74+
var department = await _db.Departments
75+
.Where(d => d.InstructorID == message.ID)
76+
.SingleOrDefaultAsync();
77+
if (department != null)
78+
{
79+
department.InstructorID = null;
80+
}
81+
82+
}
83+
}
84+
}
85+
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@model ContosoUniversity.Models.Instructor
1+
@model ContosoUniversity.Features.Instructors.Delete.Command
22

33
@{
44
ViewData["Title"] = "Delete";
@@ -12,28 +12,28 @@
1212
<hr />
1313
<dl class="dl-horizontal">
1414
<dt>
15-
@Html.DisplayNameFor(model => model.LastName)
15+
<display-label-tag for="LastName"/>
1616
</dt>
1717
<dd>
18-
@Html.DisplayFor(model => model.LastName)
18+
<display-tag for="LastName"/>
1919
</dd>
2020
<dt>
21-
@Html.DisplayNameFor(model => model.FirstMidName)
21+
<display-label-tag for="FirstMidName" />
2222
</dt>
2323
<dd>
24-
@Html.DisplayFor(model => model.FirstMidName)
24+
<display-tag for="FirstMidName" />
2525
</dd>
2626
<dt>
27-
@Html.DisplayNameFor(model => model.HireDate)
27+
<display-label-tag for="HireDate" />
2828
</dt>
2929
<dd>
30-
@Html.DisplayFor(model => model.HireDate)
30+
<display-tag for="HireDate" />
3131
</dd>
3232
</dl>
3333

3434
<form asp-action="Delete">
35-
<input type="hidden" asp-for="ID" />
36-
<input type="submit" value="Delete" class="btn btn-default" /> |
35+
<input-tag for="ID"/>
36+
<input type="submit" value="Delete" class="btn btn-default"/> |
3737
<a asp-action="Index">Back to List</a>
3838
</form>
3939
</div>

ContosoUniversity/Features/Instructors/InstructorsController.cs

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,9 @@ namespace ContosoUniversity.Features.Instructors
1313
{
1414
public class InstructorsController : Controller
1515
{
16-
private readonly SchoolContext _context;
1716
private readonly IMediator _mediator;
1817

19-
public InstructorsController(SchoolContext context, IMediator mediator)
20-
{
21-
_context = context;
22-
_mediator = mediator;
23-
}
18+
public InstructorsController(IMediator mediator) => _mediator = mediator;
2419

2520
public async Task<IActionResult> Index(Index.Query query)
2621
=> View(await _mediator.Send(query));
@@ -29,12 +24,8 @@ public async Task<IActionResult> Index(Index.Query query)
2924
public async Task<IActionResult> Details(Details.Query query)
3025
=> View(await _mediator.Send(query));
3126

32-
public async Task<IActionResult> Create()
33-
{
34-
var model = await _mediator.Send(new CreateEdit.Query());
35-
36-
return View(nameof(CreateEdit), model);
37-
}
27+
public async Task<IActionResult> Create()
28+
=> View(nameof(CreateEdit), await _mediator.Send(new CreateEdit.Query()));
3829

3930
[HttpPost]
4031
[ValidateAntiForgeryToken]
@@ -45,12 +36,8 @@ public async Task<IActionResult> Create(CreateEdit.Command command)
4536
return this.RedirectToActionJson(nameof(Index));
4637
}
4738

48-
public async Task<IActionResult> Edit(CreateEdit.Query query)
49-
{
50-
var model = await _mediator.Send(query);
51-
52-
return View(nameof(CreateEdit), model);
53-
}
39+
public async Task<IActionResult> Edit(CreateEdit.Query query)
40+
=> View(nameof(CreateEdit), await _mediator.Send(query));
5441

5542
[HttpPost]
5643
[ValidateAntiForgeryToken]
@@ -60,47 +47,16 @@ public async Task<IActionResult> Edit(CreateEdit.Command command)
6047

6148
return this.RedirectToActionJson(nameof(Index));
6249
}
63-
// GET: Instructors/Delete/5
64-
public async Task<IActionResult> Delete(int? id)
65-
{
66-
if (id == null)
67-
{
68-
return NotFound();
69-
}
70-
71-
var instructor = await _context.Instructors
72-
.SingleOrDefaultAsync(m => m.Id == id);
73-
if (instructor == null)
74-
{
75-
return NotFound();
76-
}
77-
78-
return View(instructor);
79-
}
50+
public async Task<IActionResult> Delete(Delete.Query query)
51+
=> View(await _mediator.Send(query));
8052

81-
// POST: Instructors/Delete/5
82-
[HttpPost, ActionName("Delete")]
53+
[HttpPost]
8354
[ValidateAntiForgeryToken]
84-
public async Task<IActionResult> DeleteConfirmed(int id)
55+
public async Task<ActionResult> Delete(Delete.Command command)
8556
{
86-
Instructor instructor = await _context.Instructors
87-
.Include(i => i.CourseAssignments)
88-
.SingleAsync(i => i.Id == id);
89-
90-
var departments = await _context.Departments
91-
.Where(d => d.Id == id)
92-
.ToListAsync();
93-
departments.ForEach(d => d.InstructorID = null);
94-
95-
_context.Instructors.Remove(instructor);
96-
97-
await _context.SaveChangesAsync();
98-
return RedirectToAction("Index");
99-
}
57+
await _mediator.Send(command);
10058

101-
private bool InstructorExists(int id)
102-
{
103-
return _context.Instructors.Any(e => e.Id == id);
59+
return this.RedirectToActionJson(nameof(Index));
10460
}
10561
}
10662
}

ContosoUniversity/Features/Instructors/MappingProfile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public MappingProfile()
1616
CreateMap<CourseAssignment, CreateEdit.Command.CourseAssignment>();
1717

1818
CreateMap<Instructor, Details.Model>();
19-
//CreateMap<Instructor, Delete.Command>();
19+
CreateMap<Instructor, Delete.Command>();
2020
}
2121
}
2222
}

ContosoUniversity/Models/Instructor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class Instructor : Person
1414
[Display(Name = "Hire Date")]
1515
public DateTime HireDate { get; set; }
1616

17-
public ICollection<CourseAssignment> CourseAssignments { get; set; }
18-
public OfficeAssignment OfficeAssignment { get; set; }
17+
public ICollection<CourseAssignment> CourseAssignments { get; private set; } = new List<CourseAssignment>();
18+
public OfficeAssignment OfficeAssignment { get; private set; }
1919

2020
public void Handle(CreateEdit.Command message,
2121
IEnumerable<Course> courses)
@@ -25,7 +25,7 @@ public void Handle(CreateEdit.Command message,
2525
UpdateInstructorCourses(message.SelectedCourses, courses);
2626
}
2727

28-
// public void Handle(Delete.Command message) => OfficeAssignment = null;
28+
public void Handle(Delete.Command message) => OfficeAssignment = null;
2929

3030
private void UpdateDetails(CreateEdit.Command message)
3131
{

0 commit comments

Comments
 (0)