initial commit
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Profile.PersonalInformation.Cqrs;
|
||||
|
||||
public class GetProfileByIdResponse
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
public string? FirstName { get; set; }
|
||||
public string? LastName { get; set; }
|
||||
public string? ShortBio { get; set; }
|
||||
public string? JobTitle { get; set; }
|
||||
public DateTime? DateOfBirth { get; set; }
|
||||
public string? Email { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? StreetAddress { get; set; }
|
||||
public string? City { get; set; }
|
||||
public string? Country { get; set; }
|
||||
public string? ZipCode { get; set; }
|
||||
public string? SocialMediaLinkedIn { get; set; }
|
||||
public string? SocialMediaInstagram { get; set; }
|
||||
public string? SocialMediaX { get; set; }
|
||||
}
|
||||
|
||||
public record GetProfileByIdQuery(string Id) : IRequest<GetProfileByIdResponse?>;
|
||||
|
||||
public class GetProfileByIdHandler : IRequestHandler<GetProfileByIdQuery, GetProfileByIdResponse?>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public GetProfileByIdHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<GetProfileByIdResponse?> Handle(GetProfileByIdQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
return await _context.Users
|
||||
.AsNoTracking()
|
||||
.Where(x => x.Id == request.Id)
|
||||
.Select(x => new GetProfileByIdResponse
|
||||
{
|
||||
Id = x.Id,
|
||||
FirstName = x.FirstName,
|
||||
LastName = x.LastName,
|
||||
ShortBio = x.ShortBio,
|
||||
JobTitle = x.JobTitle,
|
||||
DateOfBirth = x.DateOfBirth,
|
||||
Email = x.Email,
|
||||
PhoneNumber = x.PhoneNumber,
|
||||
StreetAddress = x.StreetAddress,
|
||||
City = x.City,
|
||||
Country = x.Country,
|
||||
ZipCode = x.ZipCode,
|
||||
SocialMediaLinkedIn = x.SocialMediaLinkedIn,
|
||||
SocialMediaInstagram = x.SocialMediaInstagram,
|
||||
SocialMediaX = x.SocialMediaX
|
||||
})
|
||||
.FirstOrDefaultAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using Indotalent.Infrastructure.Database;
|
||||
using MediatR;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Indotalent.Features.Profile.PersonalInformation.Cqrs;
|
||||
|
||||
public class UpdateProfileRequest
|
||||
{
|
||||
public string Id { get; set; } = string.Empty;
|
||||
public string? FirstName { get; set; }
|
||||
public string? LastName { get; set; }
|
||||
public string? ShortBio { get; set; }
|
||||
public string? JobTitle { get; set; }
|
||||
public DateTime? DateOfBirth { get; set; }
|
||||
public string? PhoneNumber { get; set; }
|
||||
public string? StreetAddress { get; set; }
|
||||
public string? City { get; set; }
|
||||
public string? Country { get; set; }
|
||||
public string? ZipCode { get; set; }
|
||||
public string? SocialMediaLinkedIn { get; set; }
|
||||
public string? SocialMediaInstagram { get; set; }
|
||||
public string? SocialMediaX { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateProfileResponse
|
||||
{
|
||||
public bool Success { get; set; }
|
||||
public string Message { get; set; } = string.Empty;
|
||||
}
|
||||
|
||||
public record UpdateProfileCommand(UpdateProfileRequest Data) : IRequest<UpdateProfileResponse>;
|
||||
|
||||
public class UpdateProfileHandler : IRequestHandler<UpdateProfileCommand, UpdateProfileResponse>
|
||||
{
|
||||
private readonly AppDbContext _context;
|
||||
|
||||
public UpdateProfileHandler(AppDbContext context) => _context = context;
|
||||
|
||||
public async Task<UpdateProfileResponse> Handle(UpdateProfileCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var user = await _context.Users
|
||||
.FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);
|
||||
|
||||
if (user == null)
|
||||
return new UpdateProfileResponse { Success = false, Message = "User not found" };
|
||||
|
||||
user.FullName = $"{request.Data.FirstName} {request.Data.LastName}";
|
||||
user.FirstName = request.Data.FirstName;
|
||||
user.LastName = request.Data.LastName;
|
||||
user.ShortBio = request.Data.ShortBio;
|
||||
user.JobTitle = request.Data.JobTitle;
|
||||
user.DateOfBirth = request.Data.DateOfBirth ?? user.DateOfBirth;
|
||||
user.PhoneNumber = request.Data.PhoneNumber;
|
||||
user.StreetAddress = request.Data.StreetAddress;
|
||||
user.City = request.Data.City;
|
||||
user.Country = request.Data.Country;
|
||||
user.ZipCode = request.Data.ZipCode;
|
||||
user.SocialMediaLinkedIn = request.Data.SocialMediaLinkedIn ?? string.Empty;
|
||||
user.SocialMediaInstagram = request.Data.SocialMediaInstagram ?? string.Empty;
|
||||
user.SocialMediaX = request.Data.SocialMediaX ?? string.Empty;
|
||||
user.UpdatedAt = DateTime.Now;
|
||||
|
||||
await _context.SaveChangesAsync(cancellationToken);
|
||||
|
||||
return new UpdateProfileResponse { Success = true };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using FluentValidation;
|
||||
using Indotalent.Shared.Consts;
|
||||
|
||||
namespace Indotalent.Features.Profile.PersonalInformation.Cqrs;
|
||||
|
||||
public class UpdateProfileValidator : AbstractValidator<UpdateProfileRequest>
|
||||
{
|
||||
public UpdateProfileValidator()
|
||||
{
|
||||
RuleFor(x => x.Id).NotEmpty();
|
||||
|
||||
RuleFor(x => x.FirstName)
|
||||
.NotEmpty().WithMessage("First name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort).WithMessage($"First name cannot exceed {GlobalConsts.StringLengthShort} characters");
|
||||
|
||||
RuleFor(x => x.LastName)
|
||||
.NotEmpty().WithMessage("Last name is required")
|
||||
.MaximumLength(GlobalConsts.StringLengthShort).WithMessage($"Last name cannot exceed {GlobalConsts.StringLengthShort} characters");
|
||||
|
||||
RuleFor(x => x.PhoneNumber)
|
||||
.MaximumLength(GlobalConsts.StringLengthShort).WithMessage($"Phone cannot exceed {GlobalConsts.StringLengthShort} characters");
|
||||
|
||||
RuleFor(x => x.ShortBio)
|
||||
.MaximumLength(GlobalConsts.StringLengthShort).WithMessage($"Bio cannot exceed {GlobalConsts.StringLengthShort} characters");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user