-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathUpdateConfig.cs
More file actions
79 lines (66 loc) · 2.04 KB
/
UpdateConfig.cs
File metadata and controls
79 lines (66 loc) · 2.04 KB
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
using Application.Common.Repositories;
using AutoMapper;
using Domain.Entities;
using FluentValidation;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Application.Features.ConfigManager;
public class UpdateConfigRequest : IRequest<UpdateConfigResult>
{
public string? Id { get; init; }
public string? Name { get; init; }
public string? Value { get; init; }
public string? UpdatedById { get; init; }
}
public class UpdateConfigResult
{
public Config? Data { get; set; }
}
public class UpdateConfigValidator : AbstractValidator<UpdateConfigRequest>
{
public UpdateConfigValidator()
{
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Name).NotEmpty();
RuleFor(x => x.Value).NotEmpty();
}
}
public class UpdateConfigHandler : IRequestHandler<UpdateConfigRequest, UpdateConfigResult>
{
private readonly ICommandRepository<Config> _repository;
private readonly IUnitOfWork _unitOfWork;
public UpdateConfigHandler(
ICommandRepository<Config> repository,
IUnitOfWork unitOfWork
)
{
_repository = repository;
_unitOfWork = unitOfWork;
}
public async Task<UpdateConfigResult> Handle(UpdateConfigRequest request, CancellationToken cancellationToken)
{
Console.WriteLine(request.Id);
Console.WriteLine(request.Name);
Console.WriteLine(request.Value);
var entity = await _repository.GetAsync(request.Id ?? string.Empty, cancellationToken);
if (entity == null)
{
throw new Exception($"Entity not found: {request.Id}");
}
entity.Name = request.Name;
entity.Value = request.Value;
entity.UpdatedById = request.UpdatedById;
entity.UpdatedAtUtc = DateTime.UtcNow;
_repository.Update(entity);
await _unitOfWork.SaveAsync(cancellationToken);
return new UpdateConfigResult
{
Data = entity
};
}
}