Skip to content

Commit 828f576

Browse files
committed
Delete done
1 parent e0eab15 commit 828f576

File tree

8 files changed

+210
-179
lines changed

8 files changed

+210
-179
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
using AutoMapper;
4+
using ContosoUniversity.Data;
5+
using ContosoUniversity.Models;
6+
using FluentValidation;
7+
using MediatR;
8+
9+
namespace ContosoUniversity.Features.Students
10+
{
11+
public class Create
12+
{
13+
public class Command : IRequest
14+
{
15+
public string LastName { get; set; }
16+
17+
[Display(Name = "First Name")]
18+
public string FirstMidName { get; set; }
19+
20+
public DateTime? EnrollmentDate { get; set; }
21+
}
22+
23+
public class Validator : AbstractValidator<Command>
24+
{
25+
public Validator()
26+
{
27+
RuleFor(m => m.LastName).NotNull().Length(1, 50);
28+
RuleFor(m => m.FirstMidName).NotNull().Length(1, 50);
29+
RuleFor(m => m.EnrollmentDate).NotNull();
30+
}
31+
}
32+
33+
public class Handler : IRequestHandler<Command>
34+
{
35+
private readonly SchoolContext _db;
36+
37+
public Handler(SchoolContext db) => _db = db;
38+
39+
public void Handle(Command message)
40+
=> _db.Students.Add(Mapper.Map<Command, Student>(message));
41+
}
42+
}
43+
}

ContosoUniversity/Features/Students/Create.cshtml

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@model ContosoUniversity.Models.Student
1+
@model ContosoUniversity.Features.Students.Create.Command
22

33
@{
44
ViewData["Title"] = "Create";
@@ -11,22 +11,10 @@
1111
<div class="row">
1212
<div class="col-md-4">
1313
<form asp-action="Create">
14-
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
15-
<div class="form-group">
16-
<label asp-for="LastName" class="control-label"></label>
17-
<input asp-for="LastName" class="form-control" />
18-
<span asp-validation-for="LastName" class="text-danger"></span>
19-
</div>
20-
<div class="form-group">
21-
<label asp-for="FirstMidName" class="control-label"></label>
22-
<input asp-for="FirstMidName" class="form-control" />
23-
<span asp-validation-for="FirstMidName" class="text-danger"></span>
24-
</div>
25-
<div class="form-group">
26-
<label asp-for="EnrollmentDate" class="control-label"></label>
27-
<input asp-for="EnrollmentDate" class="form-control" />
28-
<span asp-validation-for="EnrollmentDate" class="text-danger"></span>
29-
</div>
14+
@Html.ValidationDiv()
15+
@Html.FormBlock(m => m.LastName)
16+
@Html.FormBlock(m => m.FirstMidName)
17+
@Html.FormBlock(m => m.EnrollmentDate)
3018
<div class="form-group">
3119
<input type="submit" value="Create" class="btn btn-default" />
3220
</div>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 MediatR;
8+
using Microsoft.EntityFrameworkCore;
9+
10+
namespace ContosoUniversity.Features.Students
11+
{
12+
public class Delete
13+
{
14+
public class Query : IRequest<Command>
15+
{
16+
public int Id { get; set; }
17+
}
18+
19+
public class Command : IRequest
20+
{
21+
public int ID { get; set; }
22+
[Display(Name = "First Name")]
23+
public string FirstMidName { get; set; }
24+
public string LastName { get; set; }
25+
public DateTime EnrollmentDate { get; set; }
26+
}
27+
28+
public class QueryHandler : IAsyncRequestHandler<Query, Command>
29+
{
30+
private readonly SchoolContext _db;
31+
32+
public QueryHandler(SchoolContext db) => _db = db;
33+
34+
public async Task<Command> Handle(Query message) => await _db
35+
.Students
36+
.Where(s => s.Id == message.Id)
37+
.ProjectTo<Command>()
38+
.SingleOrDefaultAsync();
39+
}
40+
41+
public class CommandHandler : IAsyncRequestHandler<Command>
42+
{
43+
private readonly SchoolContext _db;
44+
45+
public CommandHandler(SchoolContext db) => _db = db;
46+
47+
public async Task Handle(Command message)
48+
=> _db.Students.Remove(await _db.Students.FindAsync(message.ID));
49+
}
50+
51+
}
52+
}

ContosoUniversity/Features/Students/Delete.cshtml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@model ContosoUniversity.Models.Student
1+
@model ContosoUniversity.Features.Students.Delete.Command
22

33
@{
44
ViewData["Title"] = "Delete";
@@ -13,27 +13,27 @@
1313
<hr />
1414
<dl class="dl-horizontal">
1515
<dt>
16-
@Html.DisplayNameFor(model => model.LastName)
16+
<display-label-tag for="LastName"/>
1717
</dt>
1818
<dd>
19-
@Html.DisplayFor(model => model.LastName)
19+
<display-tag for="LastName"/>
2020
</dd>
2121
<dt>
22-
@Html.DisplayNameFor(model => model.FirstMidName)
22+
<display-label-tag for="FirstMidName" />
2323
</dt>
2424
<dd>
25-
@Html.DisplayFor(model => model.FirstMidName)
25+
<display-tag for="FirstMidName" />
2626
</dd>
2727
<dt>
28-
@Html.DisplayNameFor(model => model.EnrollmentDate)
28+
<display-label-tag for="EnrollmentDate" />
2929
</dt>
3030
<dd>
31-
@Html.DisplayFor(model => model.EnrollmentDate)
31+
<display-tag for="EnrollmentDate" />
3232
</dd>
3333
</dl>
3434

3535
<form asp-action="Delete">
36-
<input type="hidden" asp-for="ID" />
36+
<input-tag for="ID" />
3737
<input type="submit" value="Delete" class="btn btn-default" /> |
3838
<a asp-action="Index">Back to List</a>
3939
</form>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using AutoMapper;
6+
using AutoMapper.QueryableExtensions;
7+
using ContosoUniversity.Data;
8+
using FluentValidation;
9+
using MediatR;
10+
using Microsoft.EntityFrameworkCore;
11+
12+
namespace ContosoUniversity.Features.Students
13+
{
14+
public class Edit
15+
{
16+
public class Query : IRequest<Command>
17+
{
18+
public int? Id { get; set; }
19+
}
20+
21+
public class QueryValidator : AbstractValidator<Query>
22+
{
23+
public QueryValidator()
24+
{
25+
RuleFor(m => m.Id).NotNull();
26+
}
27+
}
28+
29+
public class Command : IRequest
30+
{
31+
public int ID { get; set; }
32+
public string LastName { get; set; }
33+
34+
[Display(Name = "First Name")]
35+
public string FirstMidName { get; set; }
36+
37+
public DateTime? EnrollmentDate { get; set; }
38+
}
39+
40+
public class Validator : AbstractValidator<Command>
41+
{
42+
public Validator()
43+
{
44+
RuleFor(m => m.LastName).NotNull().Length(1, 50);
45+
RuleFor(m => m.FirstMidName).NotNull().Length(1, 50);
46+
RuleFor(m => m.EnrollmentDate).NotNull();
47+
}
48+
}
49+
50+
public class QueryHandler : IAsyncRequestHandler<Query, Command>
51+
{
52+
private readonly SchoolContext _db;
53+
54+
public QueryHandler(SchoolContext db) => _db = db;
55+
56+
public async Task<Command> Handle(Query message) => await _db.Students
57+
.Where(s => s.Id == message.Id)
58+
.ProjectTo<Command>()
59+
.SingleOrDefaultAsync();
60+
}
61+
62+
public class CommandHandler : IAsyncRequestHandler<Command>
63+
{
64+
private readonly SchoolContext _db;
65+
66+
public CommandHandler(SchoolContext db) => _db = db;
67+
68+
public async Task Handle(Command message)
69+
=> Mapper.Map(message, await _db.Students.FindAsync(message.ID));
70+
}
71+
}
72+
}

ContosoUniversity/Features/Students/Edit.cshtml

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@model ContosoUniversity.Models.Student
1+
@model ContosoUniversity.Features.Students.Edit.Command
22

33
@{
44
ViewData["Title"] = "Edit";
@@ -11,23 +11,11 @@
1111
<div class="row">
1212
<div class="col-md-4">
1313
<form asp-action="Edit">
14-
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
15-
<input type="hidden" asp-for="ID" />
16-
<div class="form-group">
17-
<label asp-for="LastName" class="control-label"></label>
18-
<input asp-for="LastName" class="form-control" />
19-
<span asp-validation-for="LastName" class="text-danger"></span>
20-
</div>
21-
<div class="form-group">
22-
<label asp-for="FirstMidName" class="control-label"></label>
23-
<input asp-for="FirstMidName" class="form-control" />
24-
<span asp-validation-for="FirstMidName" class="text-danger"></span>
25-
</div>
26-
<div class="form-group">
27-
<label asp-for="EnrollmentDate" class="control-label"></label>
28-
<input asp-for="EnrollmentDate" class="form-control" />
29-
<span asp-validation-for="EnrollmentDate" class="text-danger"></span>
30-
</div>
14+
@Html.ValidationDiv()
15+
@Html.Input(m => m.ID)
16+
@Html.FormBlock(m => m.LastName)
17+
@Html.FormBlock(m => m.FirstMidName)
18+
@Html.FormBlock(m => m.EnrollmentDate)
3119
<div class="form-group">
3220
<input type="submit" value="Save" class="btn btn-default" />
3321
</div>

ContosoUniversity/Features/Students/MappingProfile.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public MappingProfile()
1010
CreateMap<Student, Index.Model>();
1111
CreateMap<Student, Details.Model>();
1212
CreateMap<Enrollment, Details.Model.Enrollment>();
13-
//CreateMap<Create.Command, Student>(MemberList.Source);
14-
//CreateMap<Student, Edit.Command>().ReverseMap();
15-
//CreateMap<Student, Delete.Command>().ReverseMap();
13+
CreateMap<Create.Command, Student>(MemberList.Source);
14+
CreateMap<Student, Edit.Command>().ReverseMap();
15+
CreateMap<Student, Delete.Command>().ReverseMap();
1616
}
1717
}
1818
}

0 commit comments

Comments
 (0)