-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathUpdatePersonalInformationCommandHandler.cs
More file actions
78 lines (66 loc) · 2.85 KB
/
UpdatePersonalInformationCommandHandler.cs
File metadata and controls
78 lines (66 loc) · 2.85 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
using MangoAPI.Application.Interfaces;
using MangoAPI.BusinessLogic.Models;
using System.Threading;
using System.Threading.Tasks;
using MangoAPI.BusinessLogic.Responses;
using MangoAPI.Domain.Constants;
using MangoAPI.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Globalization;
namespace MangoAPI.BusinessLogic.ApiCommands.Users;
public class UpdatePersonalInformationCommandHandler
: IRequestHandler<UpdatePersonalInformationCommand, Result<UpdatePersonalInformationResponse>>
{
private readonly MangoDbContext dbContext;
private readonly ResponseFactory<UpdatePersonalInformationResponse> responseFactory;
private readonly IBlobServiceSettings blobServiceSettings;
public UpdatePersonalInformationCommandHandler(
MangoDbContext dbContext,
ResponseFactory<UpdatePersonalInformationResponse> responseFactory,
IBlobServiceSettings blobServiceSettings)
{
this.dbContext = dbContext;
this.responseFactory = responseFactory;
this.blobServiceSettings = blobServiceSettings;
}
public async Task<Result<UpdatePersonalInformationResponse>> Handle(
UpdatePersonalInformationCommand request,
CancellationToken cancellationToken)
{
var user = await dbContext.Users
.Include(x => x.PersonalInformation)
.FirstOrDefaultAsync(x => x.Id == request.UserId, cancellationToken);
if (user == null)
{
const string errorMessage = ResponseMessageCodes.UserNotFound;
var details = ResponseMessageCodes.ErrorDictionary[errorMessage];
return responseFactory.ConflictResponse(errorMessage, details);
}
user.PersonalInformation.UpdateSocialInformation(
request.Facebook,
request.Twitter,
request.Instagram,
request.LinkedIn);
dbContext.PersonalInformation.Update(user.PersonalInformation);
await dbContext.SaveChangesAsync(cancellationToken);
var userDto = new User
{
UserId = user.Id,
DisplayName = user.DisplayName,
DisplayNameColour = user.DisplayNameColour,
Address = user.Address,
Birthday = user.Birthday?.ToString("yyyy-MM-dd", CultureInfo.CurrentCulture),
Website = user.Website,
Facebook = user.PersonalInformation.Facebook,
Twitter = user.PersonalInformation.Twitter,
Instagram = user.PersonalInformation.Instagram,
LinkedIn = user.PersonalInformation.LinkedIn,
Username = user.Username,
Bio = user.Bio,
PictureUrl = $"{blobServiceSettings.MangoBlobAccess}/{user.ImageFileName}",
};
var response = UpdatePersonalInformationResponse.FromSuccess(userDto);
return responseFactory.SuccessResponse(response);
}
}