-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathUpdate.cs
142 lines (119 loc) · 4.39 KB
/
Update.cs
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
using AutoMapper;
using CleanArchitectureCosmosDB.Core.Exceptions;
using CleanArchitectureCosmosDB.Core.Interfaces;
using CleanArchitectureCosmosDB.Core.Specifications;
using FluentValidation;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace CleanArchitectureCosmosDB.WebAPI.Models.ToDoItem
{
/// <summary>
/// Update related commands, validators, and handlers
/// </summary>
public class Update
{
/// <summary>
/// Model to Update an entity
/// </summary>
public class UpdateCommand : IRequest<CommandResponse>
{
/// <summary>
/// Id
/// </summary>
public string Id { get; set; }
/// <summary>
/// Category
/// </summary>
public string Category { get; set; }
/// <summary>
/// Title
/// </summary>
public string Title { get; set; }
}
/// <summary>
/// Command Response
/// </summary>
public class CommandResponse
{
}
/// <summary>
/// Register Validation
/// </summary>
public class UpdateToDoItemCommandValidator : AbstractValidator<UpdateCommand>
{
private readonly IToDoItemRepository _repo;
/// <summary>
/// Validator ctor
/// </summary>
public UpdateToDoItemCommandValidator(IToDoItemRepository repo)
{
this._repo = repo ?? throw new ArgumentNullException(nameof(repo));
RuleFor(x => x.Id)
.NotEmpty();
RuleFor(x => x.Category)
.NotEmpty();
RuleFor(x => x.Title)
.NotEmpty();
}
/// <summary>
/// Check uniqueness
/// </summary>
/// <param name="command"></param>
/// <param name="title"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<bool> HasUniqueName(UpdateCommand command, string title, CancellationToken cancellationToken)
{
ToDoItemSearchSpecification specification = new ToDoItemSearchSpecification(title,
exactSearch: true);
IEnumerable<Core.Entities.ToDoItem> entities = await _repo.GetItemsAsync(specification);
return entities == null ||
entities.Count() == 0 ||
// self
entities.All(x => x.Id == command.Id);
}
}
/// <summary>
/// Handler
/// </summary>
public class CommandHandler : IRequestHandler<UpdateCommand, CommandResponse>
{
private readonly IToDoItemRepository _repo;
private readonly IMapper _mapper;
/// <summary>
/// Ctor
/// </summary>
/// <param name="repo"></param>
/// <param name="mapper"></param>
public CommandHandler(IToDoItemRepository repo,
IMapper mapper)
{
this._repo = repo ?? throw new ArgumentNullException(nameof(repo));
this._mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
/// <summary>
/// Handle
/// </summary>
/// <param name="command"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<CommandResponse> Handle(UpdateCommand command, CancellationToken cancellationToken)
{
CommandResponse response = new CommandResponse();
Core.Entities.ToDoItem entity = await _repo.GetItemAsync(command.Id);
if (entity == null)
{
throw new EntityNotFoundException(nameof(ToDoItem), command.Id);
}
entity.Category = command.Category;
entity.Title = command.Title;
await _repo.UpdateItemAsync(command.Id, entity);
return response;
}
}
}
}